14
14
#include " hittable_list.h"
15
15
#include " material.h"
16
16
#include " sphere.h"
17
-
18
17
#include < iostream>
19
18
20
19
21
- vec3 ray_color (const ray& r, hittable * world, int depth) {
20
+ vec3 ray_color (const ray& r, hittable& world, int depth) {
22
21
hit_record rec;
23
- if (world->hit (r, 0.001 , infinity, rec)) {
24
- if (depth <= 0 )
25
- return vec3 (0 ,0 ,0 );
22
+
23
+ // If we've exceeded the ray bounce limit, no more light is gathered.
24
+ if (depth <= 0 )
25
+ return vec3 (0 ,0 ,0 );
26
+
27
+ if (world.hit (r, 0.001 , infinity, rec)) {
26
28
ray scattered;
27
29
vec3 attenuation;
28
30
if (rec.mat_ptr ->scatter (r, rec, attenuation, scattered))
@@ -36,10 +38,11 @@ vec3 ray_color(const ray& r, hittable *world, int depth) {
36
38
}
37
39
38
40
39
- hittable *random_scene () {
40
- size_t n = 500 ;
41
- hittable **list = new hittable*[n+1 ];
42
- list[0 ] = new sphere (vec3 (0 ,-1000 ,0 ), 1000 , new lambertian (vec3 (0.5 , 0.5 , 0.5 )));
41
+ hittable_list random_scene () {
42
+ hittable_list objects;
43
+
44
+ objects.add (new sphere (vec3 (0 ,-1000 ,0 ), 1000 , new lambertian (vec3 (0.5 , 0.5 , 0.5 ))));
45
+
43
46
int i = 1 ;
44
47
for (int a = -11 ; a < 11 ; a++) {
45
48
for (int b = -11 ; b < 11 ; b++) {
@@ -49,25 +52,25 @@ hittable *random_scene() {
49
52
if (choose_mat < 0.8 ) {
50
53
// diffuse
51
54
auto albedo = vec3::random () * vec3::random ();
52
- list[i++] = new sphere (center, 0.2 , new lambertian (albedo));
55
+ objects. add ( new sphere (center, 0.2 , new lambertian (albedo) ));
53
56
} else if (choose_mat < 0.95 ) {
54
57
// metal
55
58
auto albedo = vec3::random (.5 , 1 );
56
59
auto fuzz = random_double (0 , .5 );
57
- list[i++] = new sphere (center, 0.2 , new metal (albedo, fuzz));
60
+ objects. add ( new sphere (center, 0.2 , new metal (albedo, fuzz) ));
58
61
} else {
59
62
// glass
60
- list[i++] = new sphere (center, 0.2 , new dielectric (1.5 ));
63
+ objects. add ( new sphere (center, 0.2 , new dielectric (1.5 ) ));
61
64
}
62
65
}
63
66
}
64
67
}
65
68
66
- list[i++] = new sphere (vec3 (0 , 1 , 0 ), 1.0 , new dielectric (1.5 ));
67
- list[i++] = new sphere (vec3 (-4 , 1 , 0 ), 1.0 , new lambertian (vec3 (0.4 , 0.2 , 0.1 )));
68
- list[i++] = new sphere (vec3 (4 , 1 , 0 ), 1.0 , new metal (vec3 (0.7 , 0.6 , 0.5 ), 0.0 ));
69
+ objects. add ( new sphere (vec3 (0 , 1 , 0 ), 1.0 , new dielectric (1.5 ) ));
70
+ objects. add ( new sphere (vec3 (-4 , 1 , 0 ), 1.0 , new lambertian (vec3 (0.4 , 0.2 , 0.1 ) )));
71
+ objects. add ( new sphere (vec3 (4 , 1 , 0 ), 1.0 , new metal (vec3 (0.7 , 0.6 , 0.5 ), 0.0 ) ));
69
72
70
- return new hittable_list (list,i) ;
73
+ return objects ;
71
74
}
72
75
73
76
@@ -79,7 +82,7 @@ int main() {
79
82
80
83
std::cout << " P3\n " << nx << ' ' << ny << " \n 255\n " ;
81
84
82
- hittable * world = random_scene ();
85
+ auto world = random_scene ();
83
86
84
87
vec3 lookfrom (13 ,2 ,3 );
85
88
vec3 lookat (0 ,0 ,0 );
0 commit comments