-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrayt.cpp
793 lines (686 loc) · 21 KB
/
rayt.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/*
* Copyright (c) 2017 mebiusbox software. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "rayt.h"
#include <ctime>
namespace rayt {
class Shape;
class Material;
typedef std::shared_ptr<Shape> ShapePtr;
typedef std::shared_ptr<Material> MaterialPtr;
//-------------------------------------------------------------------------
class HitRec {
public:
float t;
float u;
float v;
vec3 p;
vec3 n;
MaterialPtr mat;
};
//-------------------------------------------------------------------------
class Pdf {
public:
virtual float value(const HitRec& hrec, const vec3& direction) const = 0;
virtual vec3 generate(const HitRec& hrec) const = 0;
};
class CosinePdf : public Pdf {
public:
CosinePdf() { }
virtual float value(const HitRec& hrec, const vec3& direction) const override {
float cosine = dot(normalize(direction), hrec.n);
if (cosine > 0) {
return cosine / PI;
}
else {
return 0;
}
}
virtual vec3 generate(const HitRec& hrec) const override {
ONB uvw; uvw.build_from_w(hrec.n);
vec3 v = uvw.local(random_cosine_direction());
return v;
}
};
class MixturePdf : public Pdf {
public:
MixturePdf(const Pdf* p0, const Pdf* p1) { m_pdfs[0] = p0; m_pdfs[1] = p1; }
virtual float value(const HitRec& hrec, const vec3& direction) const override {
float pdf0_value = m_pdfs[0]->value(hrec, direction);
float pdf1_value = m_pdfs[1]->value(hrec, direction);
return 0.5f*pdf0_value + 0.5f*pdf1_value;
}
virtual vec3 generate(const HitRec& hrec) const override {
if (drand48()<0.5f) {
return m_pdfs[0]->generate(hrec);
}
else {
return m_pdfs[1]->generate(hrec);
}
}
private:
const Pdf* m_pdfs[2];
};
//-------------------------------------------------------------------------
class ScatterRec {
public:
Ray ray;
vec3 albedo;
const Pdf* pdf;
bool is_specular;
};
class Material {
public:
virtual bool scatter(const Ray& r, const HitRec& hrec, ScatterRec& srec) const = 0;
virtual float scattering_pdf(const Ray& r, const HitRec& hrec) const { return 0; }
virtual vec3 emitted(const Ray& r, const HitRec& hrec) const { return vec3(0); }
};
class Lambertian : public Material {
public:
Lambertian(const TexturePtr& a)
: m_albedo(a) {
}
virtual bool scatter(const Ray& r, const HitRec& hrec, ScatterRec& srec) const override {
srec.albedo = m_albedo->value(hrec.u, hrec.v, hrec.p);
srec.pdf = &m_pdf;
srec.is_specular = false;
return true;
};
virtual float scattering_pdf(const Ray& r, const HitRec& hrec) const override {
return std::max(dot(hrec.n, normalize(r.direction())), 0.0f) / PI;
}
private:
TexturePtr m_albedo;
CosinePdf m_pdf;
};
class Metal : public Material {
public:
Metal(const TexturePtr& a, float fuzz)
: m_albedo(a)
, m_fuzz(fuzz) {
}
virtual bool scatter(const Ray& r, const HitRec& hrec, ScatterRec& srec) const override {
vec3 reflected = reflect(normalize(r.direction()), hrec.n);
reflected += m_fuzz*random_in_unit_sphere();
srec.ray = Ray(hrec.p, reflected);
srec.albedo = m_albedo->value(hrec.u, hrec.v, hrec.p);
srec.pdf = nullptr;
srec.is_specular = true;
return dot(srec.ray.direction(), hrec.n) > 0;
}
private:
TexturePtr m_albedo;
float m_fuzz;
};
class Dielectric : public Material {
public:
Dielectric(float ri)
: m_ri(ri) {
}
virtual bool scatter(const Ray& r, const HitRec& hrec, ScatterRec& srec) const override {
srec.is_specular = true;
vec3 outward_normal;
vec3 reflected = reflect(r.direction(), hrec.n);
float ni_over_nt;
float reflect_prob;
float cosine;
if (dot(r.direction(), hrec.n) > 0) {
outward_normal = -hrec.n;
ni_over_nt = m_ri;
cosine = m_ri * dot(r.direction(), hrec.n) / length(r.direction());
}
else {
outward_normal = hrec.n;
ni_over_nt = recip(m_ri);
cosine = -dot(r.direction(), hrec.n) / length(r.direction());
}
srec.albedo = vec3(1);
vec3 refracted;
if (refract(-r.direction(), outward_normal, ni_over_nt, refracted)) {
reflect_prob = schlick(cosine, m_ri);
}
else {
reflect_prob = 1;
}
if (drand48() < reflect_prob) {
srec.ray = Ray(hrec.p, reflected);
}
else {
srec.ray = Ray(hrec.p, refracted);
}
return true;
}
private:
float m_ri;
};
class DiffuseLight : public Material {
public:
DiffuseLight(const TexturePtr& emit)
: m_emit(emit) {
}
virtual bool scatter(const Ray& r, const HitRec& hrec, ScatterRec& srec) const override {
return false;
}
virtual vec3 emitted(const Ray& r, const HitRec& hrec) const override {
if (dot(hrec.n, r.direction()) < 0) {
return m_emit->value(hrec.u, hrec.v, hrec.p);
}
else {
return vec3(0);
}
}
private:
TexturePtr m_emit;
};
//-------------------------------------------------------------------------
class Shape {
public:
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const = 0;
virtual float pdf_value(const vec3& o, const vec3& v) const { return 0; }
virtual vec3 random(const vec3& o) const { return vec3(1, 0, 0); }
};
class ShapeList : public Shape {
public:
ShapeList() {}
void add(const ShapePtr& shape) {
m_list.push_back(shape);
}
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const override {
HitRec temp_rec;
bool hit_anything = false;
float closest_so_far = t1;
for (auto& p : m_list) {
if (p->hit(r, t0, closest_so_far, temp_rec)) {
hit_anything = true;
closest_so_far = temp_rec.t;
hrec = temp_rec;
}
}
return hit_anything;
}
virtual float pdf_value(const vec3& o, const vec3& v) const override {
float weight = 1.0f / m_list.size();
float sum = 0;
for (auto& p : m_list) {
sum += weight * p->pdf_value(o, v);
}
return sum;
}
virtual vec3 random(const vec3& o) const override {
size_t n = m_list.size();
size_t index = size_t(drand48() * n);
if (n > 0 && index >= n) {
index = n - 1;
}
return m_list[index]->random(o);
}
private:
std::vector<ShapePtr> m_list;
};
class Sphere : public Shape {
public:
Sphere() {}
Sphere(const vec3& c, float r, const MaterialPtr& mat)
: m_center(c)
, m_radius(r)
, m_material(mat) {
}
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const override {
vec3 oc = r.origin() - m_center;
float a = dot(r.direction(), r.direction());
float b = 2.0f*dot(oc, r.direction());
float c = dot(oc, oc) - pow2(m_radius);
float D = b*b - 4 * a*c;
if (D > 0) {
float root = sqrtf(D);
float temp = (-b - root) / (2.0f*a);
if (temp < t1 && temp > t0) {
hrec.t = temp;
hrec.p = r.at(hrec.t);
hrec.n = (hrec.p - m_center) / m_radius;
hrec.mat = m_material;
get_sphere_uv(hrec.n, hrec.u, hrec.v);
return true;
}
temp = (-b + root) / (2.0f*a);
if (temp < t1 && temp > t0) {
hrec.t = temp;
hrec.p = r.at(hrec.t);
hrec.n = (hrec.p - m_center) / m_radius;
hrec.mat = m_material;
get_sphere_uv(hrec.n, hrec.u, hrec.v);
return true;
}
}
return false;
}
virtual float pdf_value(const vec3& o, const vec3& v) const override {
HitRec hrec;
if (this->hit(Ray(o, v), 0.001f, FLT_MAX, hrec)) {
float dd = lengthSqr(m_center - o);
float rr = std::min(pow2(m_radius), dd);
float cos_theta_max_sq = 1.0f - rr*recip(dd);
float cos_theta_max = sqrtf(cos_theta_max_sq);
float solid_angle = PI2*(1.0f - cos_theta_max);
return recip(solid_angle);
}
else {
return 0;
}
}
virtual vec3 random(const vec3& o) const override {
vec3 direction = m_center - o;
float distance_squared = lengthSqr(direction);
ONB uvw; uvw.build_from_w(direction);
vec3 v = uvw.local(random_to_sphere(m_radius, distance_squared));
return v;
}
private:
vec3 m_center;
float m_radius;
MaterialPtr m_material;
};
class Rect : public Shape {
public:
enum AxisType {
kXY = 0,
kXZ,
kYZ
};
Rect() {}
Rect(float x0, float x1, float y0, float y1, float k, AxisType axis, const MaterialPtr& m)
: m_x0(x0)
, m_x1(x1)
, m_y0(y0)
, m_y1(y1)
, m_k(k)
, m_axis(axis)
, m_material(m) {
}
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const override {
int xi, yi, zi;
vec3 axis;
switch (m_axis) {
case kXY: xi = 0; yi = 1; zi = 2; axis = vec3::zAxis(); break;
case kXZ: xi = 0; yi = 2; zi = 1; axis = vec3::yAxis(); break;
case kYZ: xi = 1; yi = 2; zi = 0; axis = vec3::xAxis(); break;
}
float t = (m_k - r.origin()[zi]) / r.direction()[zi];
if (t < t0 || t > t1) {
return false;
}
float x = r.origin()[xi] + t*r.direction()[xi];
float y = r.origin()[yi] + t*r.direction()[yi];
if (x < m_x0 || x > m_x1 || y < m_y0 || y > m_y1) {
return false;
}
hrec.u = (x - m_x0) / (m_x1 - m_x0);
hrec.v = (y - m_y0) / (m_y1 - m_y0);
hrec.t = t;
hrec.mat = m_material;
hrec.p = r.at(t);
hrec.n = axis;
return true;
}
virtual float pdf_value(const vec3& o, const vec3& v) const override {
if (m_axis != kXZ) return 0;
HitRec hrec;
if (this->hit(Ray(o, v), 0.001f, FLT_MAX, hrec)) {
float area = (m_x1 - m_x0) * (m_y1 - m_y0);
float distance_squared = hrec.t * hrec.t * lengthSqr(v);
float cosine = fabs(dot(v, hrec.n)) / length(v);
return distance_squared / (cosine * area);
}
else {
return 0;
}
}
virtual vec3 random(const vec3& o) const override {
if (m_axis != kXZ) return vec3(1, 0, 0);
float x = m_x0 + drand48()*(m_x1 - m_x0);
float y = m_y0 + drand48()*(m_y1 - m_y0);
vec3 random_point;
switch (m_axis) {
case kXY:
random_point = vec3(x, y, m_k);
break;
case kXZ:
random_point = vec3(x, m_k, y);
break;
case kYZ:
random_point = vec3(m_k, x, y);
break;
}
vec3 v = random_point - o;
return v;
}
private:
float m_x0, m_x1, m_y0, m_y1, m_k;
AxisType m_axis;
MaterialPtr m_material;
};
class FlipNormals : public Shape {
public:
FlipNormals(const ShapePtr& shape)
: m_shape(shape) {
}
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const override {
if (m_shape->hit(r, t0, t1, hrec)) {
hrec.n = -hrec.n;
return true;
}
else {
return false;
}
}
private:
ShapePtr m_shape;
};
class Translate : public Shape {
public:
Translate(const ShapePtr& sp, const vec3& displacement)
: m_shape(sp)
, m_offset(displacement) {
}
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const override {
Ray move_r(r.origin() - m_offset, r.direction());
if (m_shape->hit(move_r, t0, t1, hrec)) {
hrec.p += m_offset;
return true;
}
else {
return false;
}
}
private:
ShapePtr m_shape;
vec3 m_offset;
};
class Rotate : public Shape {
public:
Rotate(const ShapePtr& sp, const vec3& axis, float angle)
: m_shape(sp)
, m_quat(Quat::rotation(radians(angle), axis)) {
}
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const override {
Quat revq = conj(m_quat);
vec3 origin = rotate(revq, r.origin());
vec3 direction = rotate(revq, r.direction());
Ray rot_r(origin, direction);
if (m_shape->hit(rot_r, t0, t1, hrec)) {
hrec.p = rotate(m_quat, hrec.p);
hrec.n = rotate(m_quat, hrec.n);
return true;
}
else {
return false;
}
}
private:
ShapePtr m_shape;
Quat m_quat;
};
class Box : public Shape {
public:
Box() {}
Box(const vec3& p0, const vec3& p1, const MaterialPtr& m)
: m_p0(p0)
, m_p1(p1)
, m_list(std::make_unique<ShapeList>()) {
ShapeList* l = new ShapeList();
l->add(std::make_shared<Rect>(
p0.getX(), p1.getX(), p0.getY(), p1.getY(), p1.getZ(), Rect::kXY, m));
l->add(std::make_shared<FlipNormals>(std::make_shared<Rect>(
p0.getX(), p1.getX(), p0.getY(), p1.getY(), p0.getZ(), Rect::kXY, m)));
l->add(std::make_shared<Rect>(
p0.getX(), p1.getX(), p0.getZ(), p1.getZ(), p1.getY(), Rect::kXZ, m));
l->add(std::make_shared<FlipNormals>(std::make_shared<Rect>(
p0.getX(), p1.getX(), p0.getZ(), p1.getZ(), p0.getY(), Rect::kXZ, m)));
l->add(std::make_shared<Rect>(
p0.getY(), p1.getY(), p0.getZ(), p1.getZ(), p1.getX(), Rect::kYZ, m));
l->add(std::make_shared<FlipNormals>(std::make_shared<Rect>(
p0.getY(), p1.getY(), p0.getZ(), p1.getZ(), p0.getX(), Rect::kYZ, m)));
m_list.reset(l);
}
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const override {
return m_list->hit(r, t0, t1, hrec);
}
private:
vec3 m_p0, m_p1;
std::unique_ptr<ShapeList> m_list;
};
//-------------------------------------------------------------------------
class ShapePdf : public Pdf {
public:
ShapePdf(const Shape* p, const vec3& origin) : m_ptr(p), m_origin(origin) {
}
virtual float value(const HitRec& hrec, const vec3& direction) const override {
return m_ptr->pdf_value(m_origin, direction);
}
virtual vec3 generate(const HitRec& hrec) const override {
return m_ptr->random(m_origin);
}
private:
const Shape* m_ptr;
vec3 m_origin;
};
class ShapeBuilder {
public:
ShapeBuilder() {}
ShapeBuilder(const ShapePtr& sp)
: m_ptr(sp) {
}
ShapeBuilder& reset(const ShapePtr& sp) {
m_ptr = sp;
return *this;
}
ShapeBuilder& sphere(const vec3& c, float r, const MaterialPtr& m) {
m_ptr = std::make_shared<Sphere>(c, r, m);
return *this;
}
ShapeBuilder& rect(float x0, float x1, float y0, float y1, float k, Rect::AxisType axis, const MaterialPtr& m) {
m_ptr = std::make_shared<Rect>(x0, x1, y0, y1, k, axis, m);
return *this;
}
ShapeBuilder& rectXY(float x0, float x1, float y0, float y1, float k, const MaterialPtr& m) {
m_ptr = std::make_shared<Rect>(x0, x1, y0, y1, k, Rect::kXY, m);
return *this;
}
ShapeBuilder& rectXZ(float x0, float x1, float y0, float y1, float k, const MaterialPtr& m) {
m_ptr = std::make_shared<Rect>(x0, x1, y0, y1, k, Rect::kXZ, m);
return *this;
}
ShapeBuilder& rectYZ(float x0, float x1, float y0, float y1, float k, const MaterialPtr& m) {
m_ptr = std::make_shared<Rect>(x0, x1, y0, y1, k, Rect::kYZ, m);
return *this;
}
ShapeBuilder& rect(const vec3& p0, const vec3& p1, float k, Rect::AxisType axis, const MaterialPtr& m) {
switch (axis) {
case Rect::kXY:
m_ptr = std::make_shared<Rect>(
p0.getX(), p1.getX(), p0.getY(), p1.getY(), k, axis, m);
break;
case Rect::kXZ:
m_ptr = std::make_shared<Rect>(
p0.getX(), p1.getX(), p0.getZ(), p1.getZ(), k, axis, m);
break;
case Rect::kYZ:
m_ptr = std::make_shared<Rect>(
p0.getY(), p1.getY(), p0.getZ(), p1.getZ(), k, axis, m);
break;
}
return *this;
}
ShapeBuilder& rectXY(const vec3& p0, const vec3& p1, float k, const MaterialPtr& m) {
return rect(p0, p1, k, Rect::kXY, m);
}
ShapeBuilder& rectXZ(const vec3& p0, const vec3& p1, float k, const MaterialPtr& m) {
return rect(p0, p1, k, Rect::kXZ, m);
}
ShapeBuilder& rectYZ(const vec3& p0, const vec3& p1, float k, const MaterialPtr& m) {
return rect(p0, p1, k, Rect::kYZ, m);
}
ShapeBuilder& box(const vec3& p0, const vec3& p1, const MaterialPtr& m) {
m_ptr = std::make_shared<Box>(p0, p1, m);
return *this;
}
ShapeBuilder& flip() {
m_ptr = std::make_shared<FlipNormals>(m_ptr);
return *this;
}
ShapeBuilder& translate(const vec3& t) {
m_ptr = std::make_shared<Translate>(m_ptr, t);
return *this;
}
ShapeBuilder& rotate(const vec3& axis, float angle) {
m_ptr = std::make_shared<Rotate>(m_ptr, axis, angle);
return *this;
}
const ShapePtr& get() const { return m_ptr; }
private:
ShapePtr m_ptr;
};
//-------------------------------------------------------------------------
class Scene {
public:
Scene(int width, int height, int samples)
: m_image(std::make_unique<Image>(width, height))
, m_backColor(0.1f)
, m_samples(samples) {
}
void build() {
m_backColor = vec3(0);
// Camera
vec3 lookfrom(278, 278, -800);
vec3 lookat(278, 278, 0);
vec3 vup(0, 1, 0);
float aspect = float(m_image->width()) / float(m_image->height());
m_camera = std::make_unique<Camera>(lookfrom, lookat, vup, 40, aspect);
// Shapes
MaterialPtr red = std::make_shared<Lambertian>(
std::make_shared<ColorTexture>(vec3(0.65f, 0.05f, 0.05f)));
MaterialPtr white = std::make_shared<Lambertian>(
std::make_shared<ColorTexture>(vec3(0.73f)));
MaterialPtr green = std::make_shared<Lambertian>(
std::make_shared<ColorTexture>(vec3(0.12f, 0.45f, 0.15f)));
MaterialPtr light = std::make_shared<DiffuseLight>(
std::make_shared<ColorTexture>(vec3(15.0f)));
MaterialPtr metal = std::make_shared<Dielectric>(1.5f);
ShapeList* world = new ShapeList();
ShapeBuilder builder;
world->add(builder.rectYZ(0, 555, 0, 555, 555, green).flip().get());
world->add(builder.rectYZ(0, 555, 0, 555, 0, red).get());
world->add(builder.rectXZ(213, 343, 227, 332, 554, light).flip().get());
world->add(builder.rectXZ(0, 555, 0, 555, 555, white).flip().get());
world->add(builder.rectXZ(0, 555, 0, 555, 0, white).get());
world->add(builder.rectXY(0, 555, 0, 555, 555, white).flip().get());
world->add(builder.sphere(vec3(190, 90, 190), 90, metal).get());
world->add(builder.box(vec3(0), vec3(165, 330, 165), white)
.rotate(vec3::yAxis(), 15)
.translate(vec3(265, 0, 295))
.get());
m_world.reset(world);
ShapeList* l = new ShapeList();
l->add(builder.rectXZ(213, 343, 227, 332, 554, MaterialPtr()).get());
l->add(builder.sphere(vec3(190, 90, 190), 90, MaterialPtr()).get());
m_light.reset(l);
}
vec3 color(const rayt::Ray& r, const Shape* world, const Shape* light, int depth) {
HitRec hrec;
if (world->hit(r, 0.001f, FLT_MAX, hrec)) {
vec3 emitted = hrec.mat->emitted(r, hrec);
ScatterRec srec;
if (depth < MAX_DEPTH && hrec.mat->scatter(r, hrec, srec)) {
if (srec.is_specular) {
return emitted + mulPerElem(srec.albedo, color(srec.ray, world, light, depth + 1));
}
else {
ShapePdf shapePdf(light, hrec.p);
MixturePdf mixPdf(&shapePdf, srec.pdf);
srec.ray = Ray(hrec.p, mixPdf.generate(hrec));
float pdf_value = mixPdf.value(hrec, srec.ray.direction());
if (pdf_value > 0) {
float spdf_value = hrec.mat->scattering_pdf(srec.ray, hrec);
vec3 albedo = srec.albedo * spdf_value;
return emitted + mulPerElem(albedo, color(srec.ray, world, light, depth + 1)) / pdf_value;
}
else {
return emitted;
}
}
}
else {
return emitted;
}
}
return background(r.direction());
}
vec3 background(const vec3& d) const {
return m_backColor;
}
vec3 backgroundSky(const vec3& d) const {
vec3 v = normalize(d);
float t = 0.5f * (v.getY() + 1.0f);
return lerp(t, vec3(1), vec3(0.5f, 0.7f, 1.0f));
}
void render() {
build();
int nx = m_image->width();
int ny = m_image->height();
#pragma omp parallel for schedule(dynamic, 1) num_threads(NUM_THREAD)
for (int j = 0; j<ny; j++) {
std::cerr << "Rendering (y = " << j << ") " << (100.0 * j / (ny - 1)) << "%" << std::endl;
for (int i = 0; i<nx; ++i) {
vec3 c(0);
for (int s = 0; s<m_samples; ++s) {
float u = (float(i) + drand48()) / float(nx);
float v = (float(j) + drand48()) / float(ny);
Ray r = m_camera->getRay(u, v);
c += color(r, m_world.get(), m_light.get(), 0);
}
c /= m_samples;
m_image->write(i, (ny - j - 1), c.getX(), c.getY(), c.getZ());
}
}
stbi_write_bmp("render.bmp", nx, ny, sizeof(Image::rgb), m_image->pixels());
}
private:
std::unique_ptr<Camera> m_camera;
std::unique_ptr<Image> m_image;
std::unique_ptr<Shape> m_world;
std::unique_ptr<Shape> m_light;
vec3 m_backColor;
int m_samples;
};
} // namespace rayt
int main()
{
srand((unsigned)time(NULL));
int nx = 200;
int ny = 200;
int ns = 1000;
std::unique_ptr<rayt::Scene> scene(std::make_unique<rayt::Scene>(nx, ny, ns));
scene->render();
return 0;
}