|
164 | 164 | Here’s the top part of my vec3 class: |
165 | 165 |
|
166 | 166 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
| 167 | + #include <iostream> |
167 | 168 | #include <math.h> |
168 | 169 | #include <stdlib.h> |
169 | | - #include <iostream> |
170 | 170 |
|
171 | 171 | class vec3 { |
172 | 172 | public: |
|
232 | 232 | return vec3(v1.e[0] * v2.e[0], v1.e[1] * v2.e[1], v1.e[2] * v2.e[2]); |
233 | 233 | } |
234 | 234 |
|
235 | | - inline vec3 operator/(const vec3 &v1, const vec3 &v2) { |
236 | | - return vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]); |
| 235 | + inline vec3 operator*(float t, const vec3 &v) { |
| 236 | + return vec3(t*v.e[0], t*v.e[1], t*v.e[2]); |
237 | 237 | } |
238 | 238 |
|
239 | | - inline vec3 operator*(float t, const vec3 &v) { |
| 239 | + inline vec3 operator*(const vec3 &v, float t) { |
240 | 240 | return vec3(t*v.e[0], t*v.e[1], t*v.e[2]); |
241 | 241 | } |
242 | 242 |
|
243 | | - inline vec3 operator/(vec3 v, float t) { |
244 | | - return vec3(v.e[0]/t, v.e[1]/t, v.e[2]/t); |
| 243 | + inline vec3 operator/(const vec3 &v1, const vec3 &v2) { |
| 244 | + return vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]); |
245 | 245 | } |
246 | 246 |
|
247 | | - inline vec3 operator*(const vec3 &v, float t) { |
248 | | - return vec3(t*v.e[0], t*v.e[1], t*v.e[2]); |
| 247 | + inline vec3 operator/(vec3 v, float t) { |
| 248 | + return vec3(v.e[0]/t, v.e[1]/t, v.e[2]/t); |
249 | 249 | } |
250 | 250 |
|
251 | 251 | inline float dot(const vec3 &v1, const vec3 &v2) { |
252 | | - return v1.e[0] *v2.e[0] + v1.e[1] *v2.e[1] + v1.e[2] *v2.e[2]; |
| 252 | + return v1.e[0]*v2.e[0] |
| 253 | + + v1.e[1]*v2.e[1] |
| 254 | + + v1.e[2]*v2.e[2]; |
253 | 255 | } |
254 | 256 |
|
255 | 257 | inline vec3 cross(const vec3 &v1, const vec3 &v2) { |
|
258 | 260 | v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]); |
259 | 261 | } |
260 | 262 |
|
261 | | - inline vec3& vec3::operator+=(const vec3 &v){ |
262 | | - e[0] += v.e[0]; |
263 | | - e[1] += v.e[1]; |
264 | | - e[2] += v.e[2]; |
| 263 | + inline vec3& vec3::operator+=(const vec3 &v) { |
| 264 | + e[0] += v.e[0]; |
| 265 | + e[1] += v.e[1]; |
| 266 | + e[2] += v.e[2]; |
265 | 267 | return *this; |
266 | 268 | } |
267 | 269 |
|
268 | | - inline vec3& vec3::operator*=(const vec3 &v){ |
269 | | - e[0] *= v.e[0]; |
270 | | - e[1] *= v.e[1]; |
271 | | - e[2] *= v.e[2]; |
| 270 | + inline vec3& vec3::operator-=(const vec3& v) { |
| 271 | + e[0] -= v.e[0]; |
| 272 | + e[1] -= v.e[1]; |
| 273 | + e[2] -= v.e[2]; |
272 | 274 | return *this; |
273 | 275 | } |
274 | 276 |
|
275 | | - inline vec3& vec3::operator/=(const vec3 &v){ |
276 | | - e[0] /= v.e[0]; |
277 | | - e[1] /= v.e[1]; |
278 | | - e[2] /= v.e[2]; |
| 277 | + inline vec3& vec3::operator*=(const vec3 &v) { |
| 278 | + e[0] *= v.e[0]; |
| 279 | + e[1] *= v.e[1]; |
| 280 | + e[2] *= v.e[2]; |
279 | 281 | return *this; |
280 | 282 | } |
281 | 283 |
|
282 | | - inline vec3& vec3::operator-=(const vec3& v) { |
283 | | - e[0] -= v.e[0]; |
284 | | - e[1] -= v.e[1]; |
285 | | - e[2] -= v.e[2]; |
| 284 | + inline vec3& vec3::operator*=(const float t) { |
| 285 | + e[0] *= t; |
| 286 | + e[1] *= t; |
| 287 | + e[2] *= t; |
286 | 288 | return *this; |
287 | 289 | } |
288 | 290 |
|
289 | | - inline vec3& vec3::operator*=(const float t) { |
290 | | - e[0] *= t; |
291 | | - e[1] *= t; |
292 | | - e[2] *= t; |
| 291 | + inline vec3& vec3::operator/=(const vec3 &v) { |
| 292 | + e[0] /= v.e[0]; |
| 293 | + e[1] /= v.e[1]; |
| 294 | + e[2] /= v.e[2]; |
293 | 295 | return *this; |
294 | 296 | } |
295 | 297 |
|
296 | 298 | inline vec3& vec3::operator/=(const float t) { |
297 | 299 | float k = 1.0/t; |
298 | 300 |
|
299 | | - e[0] *= k; |
300 | | - e[1] *= k; |
301 | | - e[2] *= k; |
| 301 | + e[0] *= k; |
| 302 | + e[1] *= k; |
| 303 | + e[2] *= k; |
302 | 304 | return *this; |
303 | 305 | } |
304 | 306 |
|
|
311 | 313 | Now we can change our main to use this: |
312 | 314 |
|
313 | 315 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
314 | | - #include <iostream> |
315 | 316 | #include "vec3.h" |
316 | 317 |
|
| 318 | + #include <iostream> |
| 319 | + |
317 | 320 | int main() { |
318 | 321 | int nx = 200; |
319 | 322 | int ny = 100; |
320 | 323 | std::cout << "P3\n" << nx << " " << ny << "\n255\n"; |
| 324 | + |
321 | 325 | for (int j = ny-1; j >= 0; j--) { |
322 | 326 | for (int i = 0; i < nx; i++) { |
323 | 327 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
395 | 399 | for now because we’ll add antialiasing later): |
396 | 400 |
|
397 | 401 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
398 | | - #include <iostream> |
399 | 402 | #include "ray.h" |
400 | 403 |
|
401 | | - vec3 color(const ray& r, hittable *world, int depth) { |
| 404 | + #include <iostream> |
| 405 | + |
| 406 | + vec3 color(const ray& r) { |
402 | 407 | vec3 unit_direction = unit_vector(r.direction()); |
403 | 408 | float t = 0.5*(unit_direction.y() + 1.0); |
404 | 409 | return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0); |
|
636 | 641 |
|
637 | 642 | #include "ray.h" |
638 | 643 |
|
639 | | - struct hit_record |
640 | | - { |
| 644 | + struct hit_record { |
641 | 645 | float t; |
642 | 646 | vec3 p; |
643 | 647 | vec3 normal; |
|
711 | 715 |
|
712 | 716 | #include "hittable.h" |
713 | 717 |
|
714 | | - class hittable_list: public hittable { |
| 718 | + class hittable_list: public hittable { |
715 | 719 | public: |
716 | 720 | hittable_list() {} |
717 | 721 | hittable_list(hittable **l, int n) {list = l; list_size = n; } |
|
721 | 725 | int list_size; |
722 | 726 | }; |
723 | 727 |
|
724 | | - bool hittable_list::hit( |
725 | | - const ray& r, float t_min, float t_max, hit_record& rec) const { |
| 728 | + bool hittable_list::hit(const ray& r, float t_min, float t_max, |
| 729 | + hit_record& rec) const { |
726 | 730 |
|
727 | 731 | hit_record temp_rec; |
728 | 732 | bool hit_anything = false; |
|
745 | 749 | And the new main: |
746 | 750 |
|
747 | 751 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
748 | | - #include <iostream> |
749 | | - #include "sphere.h" |
750 | | - #include "hittable_list.h" |
751 | 752 | #include "float.h" |
| 753 | + #include "hittable_list.h" |
| 754 | + #include "sphere.h" |
| 755 | + |
| 756 | + #include <iostream> |
752 | 757 |
|
753 | 758 |
|
754 | 759 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
937 | 942 | for (int i = 0; i < nx; i++) { |
938 | 943 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
939 | 944 | vec3 col(0, 0, 0); |
940 | | - for (int s=0; s < ns; s++) { |
| 945 | + for (int s = 0; s < ns; s++) { |
941 | 946 | float u = float(i + random_double()) / float(nx); |
942 | 947 | float v = float(j + random_double()) / float(ny); |
943 | 948 | ray r = cam.get_ray(u, v); |
|
1022 | 1027 | Then update the `color()` function to use the new random direction generator: |
1023 | 1028 |
|
1024 | 1029 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1025 | | - vec3 color(const ray& r, hittable *world, int depth) { |
| 1030 | + vec3 color(const ray& r, hittable *world) { |
1026 | 1031 | hit_record rec; |
1027 | 1032 | if (world->hit(r, 0.0, MAXFLOAT, rec)) { |
1028 | 1033 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
1125 | 1130 |
|
1126 | 1131 | class material; |
1127 | 1132 |
|
1128 | | - struct hit_record |
1129 | | - { |
| 1133 | + struct hit_record { |
1130 | 1134 | float t; |
1131 | 1135 | vec3 p; |
1132 | 1136 | vec3 normal; |
|
1205 | 1209 | public: |
1206 | 1210 | lambertian(const vec3& a) : albedo(a) {} |
1207 | 1211 | virtual bool scatter(const ray& r_in, const hit_record& rec, |
1208 | | - vec3& attenuation, ray& scattered) const |
1209 | | - { |
1210 | | - vec3 target = rec.p + rec.normal + random_in_unit_sphere(); |
1211 | | - scattered = ray(rec.p, target-rec.p); |
1212 | | - attenuation = albedo; |
1213 | | - return true; |
| 1212 | + vec3& attenuation, ray& scattered) const { |
| 1213 | + vec3 target = rec.p + rec.normal + random_in_unit_sphere(); |
| 1214 | + scattered = ray(rec.p, target-rec.p); |
| 1215 | + attenuation = albedo; |
| 1216 | + return true; |
1214 | 1217 | } |
1215 | 1218 |
|
1216 | 1219 | vec3 albedo; |
|
1249 | 1252 | public: |
1250 | 1253 | metal(const vec3& a) : albedo(a) {} |
1251 | 1254 | virtual bool scatter(const ray& r_in, const hit_record& rec, |
1252 | | - vec3& attenuation, ray& scattered) const |
1253 | | - { |
| 1255 | + vec3& attenuation, ray& scattered) const { |
1254 | 1256 | vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal); |
1255 | 1257 | scattered = ray(rec.p, reflected); |
1256 | 1258 | attenuation = albedo; |
|
1302 | 1304 |
|
1303 | 1305 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1304 | 1306 | hittable *list[4]; |
1305 | | - list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5))); |
| 1307 | + list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.8, 0.3, 0.3))); |
1306 | 1308 | list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8, 0.8, 0.0))); |
1307 | | - list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 0.0)); |
| 1309 | + list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2))); |
1308 | 1310 | list[3] = new sphere(vec3(-1,0,-1), 0.5, new metal(vec3(0.8, 0.8, 0.8))); |
1309 | 1311 | hittable *world = new hittable_list(list,4); |
1310 | 1312 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
|
1313 | 1315 | for (int j = ny-1; j >= 0; j--) { |
1314 | 1316 | for (int i = 0; i < nx; i++) { |
1315 | 1317 | vec3 col(0, 0, 0); |
1316 | | - for (int s=0; s < ns; s++) { |
| 1318 | + for (int s = 0; s < ns; s++) { |
1317 | 1319 | float u = float(i + random_double()) / float(nx); |
1318 | 1320 | float v = float(j + random_double()) / float(ny); |
1319 | 1321 | ray r = cam.get_ray(u, v); |
1320 | | - col += color(r, world,0); |
| 1322 | + col += color(r, world, 0); |
1321 | 1323 | } |
1322 | 1324 | col /= float(ns); |
1323 | 1325 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
1366 | 1368 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1367 | 1369 |
|
1368 | 1370 | virtual bool scatter(const ray& r_in, const hit_record& rec, |
1369 | | - vec3& attenuation, ray& scattered) const |
1370 | | - { |
| 1371 | + vec3& attenuation, ray& scattered) const { |
1371 | 1372 | vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal); |
1372 | 1373 | scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere()); |
1373 | 1374 | attenuation = albedo; |
|
1452 | 1453 | class dielectric : public material { |
1453 | 1454 | public: |
1454 | 1455 | dielectric(float ri) : ref_idx(ri) {} |
1455 | | - virtual bool scatter( |
1456 | | - const ray& r_in, |
1457 | | - const hit_record& rec, |
1458 | | - vec3& attenuation, |
1459 | | - ray& scattered) const |
1460 | | - { |
| 1456 | + virtual bool scatter(const ray& r_in, const hit_record& rec, |
| 1457 | + vec3& attenuation, ray& scattered) const { |
1461 | 1458 | vec3 outward_normal; |
1462 | 1459 | vec3 reflected = reflect(r_in.direction(), rec.normal); |
1463 | 1460 | float ni_over_nt; |
|
1499 | 1496 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1500 | 1497 | list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5))); |
1501 | 1498 | list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8, 0.8, 0.0))); |
1502 | | - list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 0.0)); |
| 1499 | + list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2))); |
1503 | 1500 | list[3] = new sphere(vec3(-1,0,-1), 0.5, new dielectric(1.5)); |
1504 | 1501 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1505 | 1502 |
|
|
1536 | 1533 | class dielectric : public material { |
1537 | 1534 | public: |
1538 | 1535 | dielectric(float ri) : ref_idx(ri) {} |
1539 | | - virtual bool scatter( |
1540 | | - const ray& r_in, |
1541 | | - const hit_record& rec, |
1542 | | - vec3& attenuation, |
1543 | | - ray& scattered) const |
1544 | | - { |
| 1536 | + virtual bool scatter(const ray& r_in, const hit_record& rec, |
| 1537 | + vec3& attenuation, ray& scattered) const { |
1545 | 1538 | vec3 outward_normal; |
1546 | 1539 | vec3 reflected = reflect(r_in.direction(), rec.normal); |
1547 | 1540 | float ni_over_nt; |
|
1726 | 1719 | float half_height = tan(theta/2); |
1727 | 1720 | float half_width = aspect * half_height; |
1728 | 1721 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1729 | | - origin = lookatfrom; |
| 1722 | + origin = lookfrom; |
1730 | 1723 | w = unit_vector(lookfrom - lookat); |
1731 | 1724 | u = unit_vector(cross(vup, w)); |
1732 | 1725 | v = cross(w, u); |
|
1821 | 1814 | public: |
1822 | 1815 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1823 | 1816 | camera(vec3 lookfrom, vec3 lookat, vec3 vup, float vfov, float aspect, |
1824 | | - float aperture, float focus_dist) |
1825 | | - { |
| 1817 | + float aperture, float focus_dist) { |
1826 | 1818 | lens_radius = aperture / 2; |
1827 | 1819 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1828 | 1820 | float theta = vfov*M_PI/180; |
|
0 commit comments