-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrayt204.cpp
342 lines (291 loc) · 8.6 KB
/
rayt204.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
/*
* 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"
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 ScatterRec {
public:
Ray ray;
vec3 albedo;
};
class Material {
public:
virtual bool scatter(const Ray& r, const HitRec& hrec, ScatterRec& srec) const = 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 {
vec3 target = hrec.p + hrec.n + random_in_unit_sphere();
srec.ray = Ray(hrec.p, target - hrec.p);
srec.albedo = m_albedo->value(hrec.u, hrec.v, hrec.p);
return true;
};
private:
TexturePtr m_albedo;
};
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);
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 {
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 {
return m_emit->value(hrec.u, hrec.v, hrec.p);
}
private:
TexturePtr m_emit;
};
//-------------------------------------------------------------------------
class Shape {
public:
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const = 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;
}
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;
}
private:
vec3 m_center;
float m_radius;
MaterialPtr m_material;
};
//-------------------------------------------------------------------------
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() {
// Camera
vec3 w(-2.0f, -1.0f, -1.0f);
vec3 u(4.0f, 0.0f, 0.0f);
vec3 v(0.0f, 2.0f, 0.0f);
m_camera = std::make_unique<Camera>(u, v, w);
// Shapes
ShapeList* world = new ShapeList();
world->add(std::make_shared<Sphere>(
vec3(0, 0, -1), 0.5f,
std::make_shared<DiffuseLight>(
std::make_shared<ColorTexture>(vec3(1)))));
world->add(std::make_shared<Sphere>(
vec3(0, -100.5, -1), 100,
std::make_shared<Lambertian>(
std::make_shared<ColorTexture>(vec3(0.8f, 0.8f, 0.8f)))));
m_world.reset(world);
}
vec3 color(const rayt::Ray& r, const Shape* world, 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)) {
return emitted + mulPerElem(srec.albedo, color(srec.ray, world, depth + 1));
}
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 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(), 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;
vec3 m_backColor;
int m_samples;
};
} // namespace rayt
int main()
{
int nx = 200;
int ny = 100;
int ns = 100;
std::unique_ptr<rayt::Scene> scene(std::make_unique<rayt::Scene>(nx, ny, ns));
scene->render();
return 0;
}