-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
312 lines (264 loc) · 9.74 KB
/
main.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
#define STB_IMAGE_IMPLEMENTATION
#include "stdafx.h"
#include "Ray.h"
#include "Hittable/IHittable.h"
#include "Hittable/HittableList.h"
#include "Hittable/Sphere.h"
#include "Camera.h"
#include "Material/Material.h"
#include "Material/Lambertian.h"
#include "Material/Metal.h"
#include "Material/Dielectric.h"
#include "Hittable/MovingSphere.h"
#include "Textures/Texture.h"
#include "Textures/NoiseTexture.h"
#include "Textures/ImageTexture.h"
#include "Hittable/AARect.h"
#include "Material/DiffuseLight.h"
#include "Hittable/Box.h"
#include "Hittable/Translate.h"
#include "Hittable/BVHNode.h"
using Array2D = std::vector<std::vector<glm::ivec3>>;
void writeColor(std::ostream& out, Color const & element)
{
out << element.x << ' '
<< element.y << ' '
<< element.z << '\n';
}
void writeColor(glm::ivec3& out, vec3 pixelColor, int samplesPerPixel)
{
auto r = pixelColor.r;
auto g = pixelColor.g;
auto b = pixelColor.b;
// Divde the color by the number of samples
auto scale = 1.0 / samplesPerPixel;
r = sqrt(scale * r);
g = sqrt(scale * g);
b = sqrt(scale * b);
out.x = static_cast<int>( 256 * std::clamp(r, 0.0, 0.999));
out.y = static_cast<int>( 256 * std::clamp(g, 0.0, 0.999));
out.z = static_cast<int>( 256 * std::clamp(b, 0.0, 0.999));
/* out << static_cast<int>( 256 * std::clamp(r, 0.0, 0.999)) << ' '
<< static_cast<int>( 256 * std::clamp(g, 0.0, 0.999)) << ' '
<< static_cast<int>( 256 * std::clamp(b, 0.0, 0.999)) << '\n';*/
}
Color RayColor(Ray const & r, Color const & background, const IHittable& world, int depth)
{
// Exceed depth, no more light gathered
if(depth <= 0)
return Color(0);
// If the ray hits nothing, return background color
HitRecord rec;
if(!world.Hit(r, 0.001, infinity, rec))
return background;
Ray scattered;
Color attenuation;
Color emitted = rec.matPtr->Emitted(rec.u, rec.v, rec.p);
if(!rec.matPtr->Scatter(r, rec, attenuation, scattered))
{
return emitted;
}
return emitted + attenuation * RayColor(scattered, background, world, depth - 1);
}
HittableList Earth()
{
auto earthTexture = make_shared<ImageTexture>("assets/earthmap.jpg");
auto earthSurface = make_shared<Lambertian>(earthTexture);
auto globe = make_shared<Sphere>(vec3(0,0,0), 2, earthSurface);
return HittableList(globe);
}
HittableList randomScene()
{
HittableList world;
auto checker = make_shared<CheckerTexture>(Color(0.2, 0.3, 0.1), Color(0.9, 0.9, 0.9));
world.Add(make_shared<Sphere>(vec3(0,-1000,0), 1000, make_shared<Lambertian>(checker)));
for(int i = 0; i < 11; ++i)
{
for(int j = 0; j < 11; ++j)
{
double chooseMatRand = randomDouble();
vec3 center(i + 0.9 * randomDouble(), 0.2, j + 0.9 * randomDouble());
if(glm::distance(vec3(4,0.2,0), center) > 0.9)
{
shared_ptr<Material> sphereMat;
if(chooseMatRand < 0.8)
{
// Diffuse
auto albedo = randomVec3() * randomVec3();
sphereMat = make_shared<Lambertian>(albedo);
auto center2 = center + vec3(0, randomDouble(0,.5), 0);
world.Add(make_shared<MovingSphere>(
center, center2, 0.0, 1.0, 0.2, sphereMat));
}
else if(chooseMatRand < 0.95)
{
// Metal
auto albedo = randomVec3(0.5,1);
auto fuzz = randomDouble(0, 0.5);
sphereMat = make_shared<Metal>(albedo, fuzz);
world.Add(make_shared<Sphere>(center, 0.2, sphereMat));
}
else
{
// Glass
sphereMat = make_shared<Dielectric>(1.5);
world.Add(make_shared<Sphere>(center, 0.2, sphereMat));
}
}
}
}
auto material1 = make_shared<Dielectric>(1.5);
world.Add(make_shared<Sphere>(vec3(0, 1, 0), 1.0, material1));
auto material2 = make_shared<Lambertian>(vec3(0.4, 0.2, 0.1));
world.Add(make_shared<Sphere>(vec3(-4, 1, 0), 1.0, material2));
auto material3 = make_shared<Metal>(vec3(0.7, 0.6, 0.5), 0.0);
world.Add(make_shared<Sphere>(vec3(4, 1, 0), 1.0, material3));
return HittableList(make_shared<BVHNode>(world, 0.0, 1.0));
}
HittableList TwoPerlinSpheres()
{
HittableList objects;
auto checker = make_shared<NoiseTexture>(4);
objects.Add(make_shared<Sphere>(vec3(0, -1000, 0), 1000, make_shared<Lambertian>(checker)));
objects.Add(make_shared<Sphere>(vec3(0, 2, 0), 2, make_shared<Lambertian>(checker)));
return objects;
}
HittableList TwoSpheres()
{
HittableList objects;
auto checker = make_shared<CheckerTexture>();
objects.Add(make_shared<Sphere>(vec3(0, -10, 0), 10, make_shared<Lambertian>(checker)));
objects.Add(make_shared<Sphere>(vec3(0, 10, 0), 10, make_shared<Lambertian>(checker)));
return objects;
}
HittableList SimpleLight()
{
HittableList objects;
auto pertext = make_shared<NoiseTexture>(4);
objects.Add(make_shared<Sphere>(vec3(0, -1000, 0), 1000, make_shared<Lambertian>(pertext)));
objects.Add(make_shared<Sphere>(vec3(0, 2, 0), 2, make_shared<Lambertian>(pertext)));
auto diffLight = make_shared<DiffuseLight>(Color(4,4,4));
objects.Add(make_shared<XY_Rect>(3,5,1,3,-2, diffLight));
return objects;
}
HittableList Cornell_Box()
{
HittableList objects;
auto red = make_shared<Lambertian>(Color(0.65, 0.05, 0.05));
auto white = make_shared<Lambertian>(Color(0.73,0.73,0.73));
auto green = make_shared<Lambertian>(Color(.12, .45,0.15));
auto light = make_shared<DiffuseLight>(Color(15, 15, 15));
objects.Add(make_shared<YZ_Rect>(0, 555, 0, 555, 555, green));
objects.Add(make_shared<YZ_Rect>(0, 555, 0, 555, 0, red));
objects.Add(make_shared<XZ_Rect>(213, 343, 227, 332, 554, light));
objects.Add(make_shared<XZ_Rect>(0, 555, 0, 555, 0, white));
objects.Add(make_shared<XZ_Rect>(0, 555, 0, 555, 555, white));
objects.Add(make_shared<XY_Rect>(0, 555, 0, 555, 555, white));
shared_ptr<IHittable> box1 = make_shared<Box>(vec3(0,0,0), vec3(165, 330, 165), white);
box1 = make_shared<Rotate_Y>(box1, 15);
box1 = make_shared<Translate>(box1, vec3(265, 0, 295));
objects.Add(box1);
shared_ptr<IHittable> box2 = make_shared<Box>(vec3(0,0,0), vec3(165, 165, 165), white);
box2 = make_shared<Rotate_Y>(box2, -18);
box2 = make_shared<Translate>(box2, vec3(130, 0, 65));
objects.Add(box2);
// objects.Add(make_shared<Box>(vec3(130, 0, 65), vec3(295, 165, 230), white));
// objects.Add(make_shared<Box>(vec3(265, 0, 295),vec3(430, 330, 460), white));
return objects;
}
int main(void) {
// Image
float aspectRatio = 16.0 / 9.0;
int imageWidth = 400;
int samplesPerPixel = 100;
int maxDepth = 50;
Color background (0,0,0);
// World
HittableList world;
// Camera
vec3 lookFrom(13,2,3);
vec3 lookAt(0,0,0);
vec3 up(0, 1,0);
auto distanceToFocus = 10.0;
auto aperture = 0.1;
auto fov = 20.0;
switch (6) {
case 1:
world = randomScene();
lookFrom = vec3(13,2,3);
lookAt = vec3(0,0,0);
fov = 20.0;
aperture = 0.1;
background = Color(0.70, 0.80, 1.00);
break;
case 2:
world = TwoSpheres();
lookFrom = vec3(13,2,3);
lookAt = vec3(0,0,0);
fov = 20.0;
background = Color(0.70, 0.80, 1.00);
break;
case 3:
world = TwoPerlinSpheres();
lookFrom = vec3(13,2,3);
lookAt = vec3(0,0,0);
background = Color(0.70, 0.80, 1.00);
fov = 20.0;
break;
case 4:
world = Earth();
lookFrom = vec3(13,2,3);
lookAt = vec3(0,0,0);
fov = 20.0;
background = Color(0.70, 0.80, 1.00);
break;
case 5:
world = SimpleLight();
samplesPerPixel = 400;
lookFrom = vec3(26,3,6);
lookAt = vec3(0,2,0);
fov = 20.0;
background = Color(0,0,0);
break;
case 6:
world = Cornell_Box();
aspectRatio = 1.0;
imageWidth = 600;
samplesPerPixel = 200;
lookFrom = vec3(278, 278, -800);
lookAt = vec3(278, 278, 0);
fov = 40.0;
background = Color(0,0,0);
break;
default:
break;
}
int imageHeight = static_cast<int>(imageWidth / aspectRatio);
Camera cam(lookFrom, lookAt, up, aspectRatio, fov, aperture, distanceToFocus, 0.0, 1.0);
Array2D bufferArray (imageWidth, std::vector<glm::ivec3>(imageHeight, {0,0,0}));
// Render
std::cout << "P3\n" << imageWidth << " " << imageHeight << "\n255\n";
#pragma omp parallel for
for (int j = imageHeight - 1; j >= 0; j--)
{
// std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
for(int i = 0; i < imageWidth; ++i) {
Color color(0);
for(int s = 0; s < samplesPerPixel; ++s) {
auto u = (i + randomDouble()) / (imageWidth - 1);
auto v = (j + randomDouble()) / (imageHeight - 1);
Ray r = cam.getRay(u,v);
color += RayColor(r, background, world, maxDepth);
}
writeColor(bufferArray[i][j], color, samplesPerPixel);
}
}
for (int j = imageHeight - 1; j >= 0; j--)
{
for(int i = 0; i < imageWidth; ++i)
{
writeColor(std::cout, bufferArray[i][j]);
}
}
return 0;
}