-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrayt111.cpp
228 lines (191 loc) · 5.16 KB
/
rayt111.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
#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;
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;
};
class Lambertian : public Material {
public:
Lambertian(const vec3& c)
: m_albedo(c) {
}
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;
return true;
};
private:
vec3 m_albedo;
};
//-------------------------------------------------------------------------
class Shape {
public:
virtual bool hit(const Ray& r, float t0, float t1, HitRec& hrec) const = 0;
};
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;
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;
return true;
}
}
return false;
}
private:
vec3 m_center;
float m_radius;
MaterialPtr m_material;
};
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::list<ShapePtr> m_list;
};
//-------------------------------------------------------------------------
class Scene {
public:
Scene(int width, int height, int samples)
: m_image(std::make_unique<Image>(width, height))
, m_backColor(0.2f)
, 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.6, 0, -1), 0.5f,
std::make_shared<Lambertian>(vec3(0.1f, 0.2f, 0.5f))));
world->add(std::make_shared<Sphere>(
vec3(-0.6, 0, -1), 0.5f,
std::make_shared<Lambertian>(vec3(0.8f, 0.0f, 0.0f))));
world->add(std::make_shared<Sphere>(
vec3(0, -100.5, -1), 100,
std::make_shared<Lambertian>(vec3(0.8f, 0.8f, 0.0f))));
m_world.reset(world);
}
vec3 color(const rayt::Ray& r, const Shape* world) const {
HitRec hrec;
if (world->hit(r, 0.001f, FLT_MAX, hrec)) {
ScatterRec srec;
if (hrec.mat->scatter(r, hrec, srec)) {
return mulPerElem(srec.albedo, color(srec.ray, world));
}
else {
return vec3(0);
}
}
return backgroundSky(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());
}
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;
}