0%

【RayTracer】(十七)新特性最终场景

现在我们新建一个非常复杂的场景来测试到目前为止光线追踪器的全部特性,生成我们的第二张光线追踪“大片”。

1 创建新场景

创建一个包含所有特性的场景:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 新特性最终场景
hittable_list final_scene() {
// 高低起伏的盒子组成地面
hittable_list boxes1;
auto ground = make_shared<lambertian>(color(0.48, 0.83, 0.53));

const int boxes_per_side = 20;
for (int i = 0; i < boxes_per_side; i++) {
for (int j = 0; j < boxes_per_side; j++) {
auto w = 100.0;
auto x0 = -1000.0 + i * w;
auto z0 = -1000.0 + j * w;
auto y0 = 0.0;
auto x1 = x0 + w;
auto y1 = random_double(1, 101);
auto z1 = z0 + w;

boxes1.add(make_shared<box>(point3(x0, y0, z0), point3(x1, y1, z1), ground));
}
}

hittable_list objects;
// 构建地面的BVH树
objects.add(make_shared<bvh_node>(boxes1, 0, 1));
// 光源
auto light = make_shared<diffuse_light>(color(7, 7, 7));
objects.add(make_shared<xz_rect>(123, 423, 147, 412, 554, light));
// 移动的球体
auto center1 = point3(400, 400, 200);
auto center2 = center1 + vec3(30, 0, 0);
auto moving_sphere_material = make_shared<lambertian>(color(0.7, 0.3, 0.1));
objects.add(make_shared<sphere>(center1, center2, 0, 1, 50, moving_sphere_material));
// 透明球体
objects.add(make_shared<sphere>(
point3(260, 150, 45), point3(260, 150, 45), 0, 1, 50, make_shared<dielectric>(1.5)));
// 金属球体
objects.add(make_shared<sphere>(
point3(0, 150, 145), point3(0, 150, 145), 0, 1, 50, make_shared<metal>(color(0.8, 0.8, 0.9), 1.0)
));

// 参与介质
auto boundary = make_shared<sphere>(
point3(360, 150, 145), point3(360, 150, 145), 0, 1, 70, make_shared<dielectric>(1.5));
objects.add(boundary);
objects.add(make_shared<constant_medium>(boundary, 0.01, color(0.2, 0.4, 0.9)));
boundary = make_shared<sphere>(
point3(0, 0, 0), point3(0, 0, 0), 0, 1, 5000, make_shared<dielectric>(1.5));
objects.add(make_shared<constant_medium>(boundary, .0001, color(1, 1, 1)));

// 地球
auto emat = make_shared<lambertian>(make_shared<image_texture>(
"D:\\TechStack\\ComputerGraphics\\Ray Tracing in One Weekend Series\\Project\\Textures\\earthmap.jpg"));
objects.add(make_shared<sphere>(
point3(400, 200, 400), point3(400, 200, 400), 0, 1, 100, emat));
// 噪声纹理
auto pertext = make_shared<noise_texture>(4);
objects.add(make_shared<sphere>(
point3(220, 280, 300), point3(220, 280, 300), 0, 1, 80, make_shared<lambertian>(pertext)));

// 一堆小球
hittable_list boxes2;
auto white = make_shared<lambertian>(color(.73, .73, .73));
int ns = 1000;
for (int j = 0; j < ns; j++) {
auto center = random_vec(0, 165);
boxes2.add(make_shared<sphere>(
center, center, 0, 1, 10, white));
}
// 旋转平移一堆小球
objects.add(make_shared<translate>(
make_shared<rotate_y>(
make_shared<bvh_node>(boxes2, 0.0, 1.0), 15),
vec3(-100, 270, 395)
)
);

return objects;
}

2 渲染效果

修改主函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() {
...
switch (sence) {
...
default:
case 8:
world = final_scene();
aspect_ratio = 1.0;
image_width = 800;
samples_per_pixel = 10000;
min_bounce = 95;
background = color(0,0,0);
lookfrom = point3(478, 278, -600);
lookat = point3(278, 278, 0);
vfov = 40.0;
break;
...
}

渲染效果如下:

FinalSence

---- 本文结束 知识又增加了亿点点!----

文章版权声明 1、博客名称:LycTechStack
2、博客网址:https://lz328.github.io/LycTechStack.github.io/
3、本博客的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系博主进行删除处理。
4、本博客所有文章版权归博主所有,如需转载请标明出处。