在第1~11章的学习完毕之后,小经编写代码便可以形成以下【渲染结果】
【代码下载】-使用VS2015编译
链接:https://pan.baidu.com/s/1iZwhGSO9U5KmdyyfwR478w 提取码:0cu0
【实现】
代码使用了随机数生成了500个球,渲染也耗费了大约10分钟。
hitable *random_scene() { int n = 500; hitable**list = new hitable*[n + 1]; list[0] = new sphere(vec3(0, -1000, 0), 1000, new lambertian(vec3(0.5, 0.5, 0.5))); int i = 1; for (int a0 = -11; a0 < 11; a0++) { for(int b0 = -11; b0 < 11; b0++) { float choose_mat = drand48(); vec3 center(a0 + 0.9*drand48(), 0.2, b0 + 0.9*drand48()); if ((center - vec3(4.0f, 0.2f, 0.0f)).length() > 0.9) { if (choose_mat < 0.8)//diffuse { list[i++] = new sphere(center, 0.2f, new lambertian(vec3(drand48()*drand48(), drand48()*drand48(), drand48()*drand48()))); } else if (choose_mat < 0.95)//metal { list[i++] = new sphere(center, 0.2f, new metal(vec3(0.5f*(1 + drand48()), 0.5f*(1 + drand48()), 0.5f*(1 + drand48())), 0.5f + drand48())); } else //glass { list[i++] = new sphere(center, 0.2f, new dielectric(1.5f)); } } } } list[i++] = new sphere(vec3(0, 1, 0), 1.0f, new dielectric(1.5f)); list[i++] = new sphere(vec3(-4, 1, 0), 1.0f, new lambertian(vec3(0.4, 0.2, 0.1))); list[i++] = new sphere(vec3(4, 1, 0), 1.0, new metal(vec3(0.7, 0.6, 0.5), 0.0)); return new hitable_list(list, i); }