@@ -40,127 +40,162 @@ struct hit_record {
40
40
}
41
41
};
42
42
43
+
43
44
class hittable {
44
45
public:
45
46
virtual bool hit (const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
46
47
virtual bool bounding_box (double t0, double t1, aabb& output_box) const = 0;
47
- virtual double pdf_value (const vec3& o, const vec3& v) const { return 0.0 ; }
48
- virtual vec3 random (const vec3& o) const { return vec3 (1 ,0 ,0 ); }
48
+
49
+ virtual double pdf_value (const vec3& o, const vec3& v) const {
50
+ return 0.0 ;
51
+ }
52
+
53
+ virtual vec3 random (const vec3& o) const {
54
+ return vec3 (1 ,0 ,0 );
55
+ }
49
56
};
50
57
58
+
51
59
class flip_face : public hittable {
52
60
public:
53
61
flip_face (shared_ptr<hittable> p) : ptr(p) {}
62
+
54
63
virtual bool hit (const ray& r, double t_min, double t_max, hit_record& rec) const {
55
- if (ptr->hit (r, t_min, t_max, rec)) {
56
- rec.front_face = !rec.front_face ;
57
- return true ;
58
- }
59
- else
64
+ if (!ptr->hit (r, t_min, t_max, rec))
60
65
return false ;
66
+
67
+ rec.front_face = !rec.front_face ;
68
+ return true ;
61
69
}
70
+
62
71
virtual bool bounding_box (double t0, double t1, aabb& output_box) const {
63
72
return ptr->bounding_box (t0, t1, output_box);
64
73
}
74
+
75
+ public:
65
76
shared_ptr<hittable> ptr;
66
77
};
67
78
79
+
68
80
class translate : public hittable {
69
81
public:
70
82
translate (shared_ptr<hittable> p, const vec3& displacement)
71
83
: ptr(p), offset(displacement) {}
84
+
72
85
virtual bool hit (const ray& r, double t_min, double t_max, hit_record& rec) const ;
73
86
virtual bool bounding_box (double t0, double t1, aabb& output_box) const ;
87
+
88
+ public:
74
89
shared_ptr<hittable> ptr;
75
90
vec3 offset;
76
91
};
77
92
93
+
78
94
bool translate::hit (const ray& r, double t_min, double t_max, hit_record& rec) const {
79
95
ray moved_r (r.origin () - offset, r.direction (), r.time ());
80
- if (ptr->hit (moved_r, t_min, t_max, rec)) {
81
- rec.p += offset;
82
- rec.set_face_normal (moved_r, rec.normal );
83
- return true ;
84
- }
85
- else
96
+ if (!ptr->hit (moved_r, t_min, t_max, rec))
86
97
return false ;
98
+
99
+ rec.p += offset;
100
+ rec.set_face_normal (moved_r, rec.normal );
101
+
102
+ return true ;
87
103
}
88
104
105
+
89
106
bool translate::bounding_box (double t0, double t1, aabb& output_box) const {
90
- if (ptr->bounding_box (t0, t1, output_box)) {
91
- output_box = aabb (
92
- output_box.min () + offset,
93
- output_box.max () + offset);
94
- return true ;
95
- }
96
- else
107
+ if (!ptr->bounding_box (t0, t1, output_box))
97
108
return false ;
109
+
110
+ output_box = aabb (
111
+ output_box.min () + offset,
112
+ output_box.max () + offset);
113
+
114
+ return true ;
98
115
}
99
116
117
+
100
118
class rotate_y : public hittable {
101
119
public:
102
120
rotate_y (shared_ptr<hittable> p, double angle);
121
+
103
122
virtual bool hit (const ray& r, double t_min, double t_max, hit_record& rec) const ;
104
123
virtual bool bounding_box (double t0, double t1, aabb& output_box) const {
105
124
output_box = bbox;
106
125
return hasbox;
107
126
}
127
+
128
+ public:
108
129
shared_ptr<hittable> ptr;
109
130
double sin_theta;
110
131
double cos_theta;
111
132
bool hasbox;
112
133
aabb bbox;
113
134
};
114
135
136
+
115
137
rotate_y::rotate_y (shared_ptr<hittable> p, double angle) : ptr(p) {
116
138
auto radians = degrees_to_radians (angle);
117
139
sin_theta = sin (radians);
118
140
cos_theta = cos (radians);
119
141
hasbox = ptr->bounding_box (0 , 1 , bbox);
120
- vec3 min (infinity, infinity, infinity);
142
+
143
+ vec3 min ( infinity, infinity, infinity);
121
144
vec3 max (-infinity, -infinity, -infinity);
145
+
122
146
for (int i = 0 ; i < 2 ; i++) {
123
147
for (int j = 0 ; j < 2 ; j++) {
124
148
for (int k = 0 ; k < 2 ; k++) {
125
149
auto x = i*bbox.max ().x () + (1 -i)*bbox.min ().x ();
126
150
auto y = j*bbox.max ().y () + (1 -j)*bbox.min ().y ();
127
151
auto z = k*bbox.max ().z () + (1 -k)*bbox.min ().z ();
128
- auto newx = cos_theta*x + sin_theta*z;
152
+
153
+ auto newx = cos_theta*x + sin_theta*z;
129
154
auto newz = -sin_theta*x + cos_theta*z;
155
+
130
156
vec3 tester (newx, y, newz);
157
+
131
158
for (int c = 0 ; c < 3 ; c++) {
132
- if (tester[c] > max[c])
133
- max[c] = tester[c];
134
- if (tester[c] < min[c])
135
- min[c] = tester[c];
159
+ min[c] = ffmin (min[c], tester[c]);
160
+ max[c] = ffmax (max[c], tester[c]);
136
161
}
137
162
}
138
163
}
139
164
}
165
+
140
166
bbox = aabb (min, max);
141
167
}
142
168
169
+
143
170
bool rotate_y::hit (const ray& r, double t_min, double t_max, hit_record& rec) const {
144
171
vec3 origin = r.origin ();
145
172
vec3 direction = r.direction ();
173
+
146
174
origin[0 ] = cos_theta*r.origin ()[0 ] - sin_theta*r.origin ()[2 ];
147
- origin[2 ] = sin_theta*r.origin ()[0 ] + cos_theta*r.origin ()[2 ];
175
+ origin[2 ] = sin_theta*r.origin ()[0 ] + cos_theta*r.origin ()[2 ];
176
+
148
177
direction[0 ] = cos_theta*r.direction ()[0 ] - sin_theta*r.direction ()[2 ];
149
178
direction[2 ] = sin_theta*r.direction ()[0 ] + cos_theta*r.direction ()[2 ];
179
+
150
180
ray rotated_r (origin, direction, r.time ());
151
- if (ptr->hit (rotated_r, t_min, t_max, rec)) {
152
- vec3 p = rec.p ;
153
- vec3 normal = rec.normal ;
154
- p[0 ] = cos_theta*rec.p [0 ] + sin_theta*rec.p [2 ];
155
- p[2 ] = -sin_theta*rec.p [0 ] + cos_theta*rec.p [2 ];
156
- normal[0 ] = cos_theta*rec.normal [0 ] + sin_theta*rec.normal [2 ];
157
- normal[2 ] = -sin_theta*rec.normal [0 ] + cos_theta*rec.normal [2 ];
158
- rec.p = p;
159
- rec.set_face_normal (rotated_r, normal);
160
- return true ;
161
- }
162
- else
181
+
182
+ if (!ptr->hit (rotated_r, t_min, t_max, rec))
163
183
return false ;
184
+
185
+ vec3 p = rec.p ;
186
+ vec3 normal = rec.normal ;
187
+
188
+ p[0 ] = cos_theta*rec.p [0 ] + sin_theta*rec.p [2 ];
189
+ p[2 ] = -sin_theta*rec.p [0 ] + cos_theta*rec.p [2 ];
190
+
191
+ normal[0 ] = cos_theta*rec.normal [0 ] + sin_theta*rec.normal [2 ];
192
+ normal[2 ] = -sin_theta*rec.normal [0 ] + cos_theta*rec.normal [2 ];
193
+
194
+ rec.p = p;
195
+ rec.set_face_normal (rotated_r, normal);
196
+
197
+ return true ;
164
198
}
165
199
200
+
166
201
#endif
0 commit comments