-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrayt108.cpp
205 lines (174 loc) · 5.35 KB
/
rayt108.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
/*
* 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;
typedef std::shared_ptr<Shape> ShapePtr;
//-------------------------------------------------------------------------
class HitRec {
public:
float t;
vec3 p;
vec3 n;
};
//-------------------------------------------------------------------------
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)
: m_center(c)
, m_radius(r) {
}
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;
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;
return true;
}
}
return false;
}
private:
vec3 m_center;
float m_radius;
};
//-------------------------------------------------------------------------
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, 0, -1), 0.5f));
world->add(std::make_shared<Sphere>(
vec3(0, -100.5, -1), 100));
m_world.reset(world);
}
vec3 color(const rayt::Ray& r, const Shape* world) const {
HitRec hrec;
if (world->hit(r, 0, FLT_MAX, hrec)) {
vec3 target = hrec.p + hrec.n + random_in_unit_sphere();
return 0.5f * color(Ray(hrec.p, target - hrec.p), world);
}
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;
}