-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.cpp
372 lines (308 loc) · 8.28 KB
/
Scene.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
#include "precomp.h"
Scene::Scene(Surface* screen)
{
// create camera
this->screen = screen;
this->camera = new Camera();
this->topBVHExists = false;
}
void Scene::render(int row)
{
for (int x = 0; x < SCRWIDTH; x++)
{
// generate and trace ray
Ray* ray = this->camera->generateRay(x, row);
vec4 color = this->trace(ray, 0);
// plot pixel with color
this->screen->Plot(x, row, this->convertColorToPixel(color));
// clear garbages
delete ray;
}
}
vec4 Scene::trace(Ray* ray, int depth)
{
depth += 1;
if (depth > 5) return BGCOLOR;
this->intersectPrimitives(ray);
if (ray->intersectedObjectId == -1) // no primitive intersected
{
this->intersectLightSources(ray);
if (ray->intersectedObjectId == -1)
{
return BGCOLOR;
}
else
{
// direct light
return this->lightSources[ray->intersectedObjectId]->color;
}
}
// primitive intersected
Material* material = this->primitives[ray->intersectedObjectId]->material;
vec4 color = (material->color * 0.2);
if (material->type == diffuse)
{
return color + material->color * illuminate(ray);
}
if (material->type == mirror)
{
Ray* reflectionRay = computeReflectionRay(ray);
vec4 reflectionColor = this->trace(reflectionRay, depth);
delete reflectionRay;
return color + reflectionColor * 0.8;
}
if (material->type == dielectric)
{
Ray* refractionRay = this->computeRefractionRay(ray);
if (refractionRay->intersectedObjectId == -2)
{
delete refractionRay;
return color;
}
else
{
color += this->trace(refractionRay, depth);
}
Ray* reflectionRay = this->computeReflectionRay(ray);
vec4 reflectionColor = this->trace(reflectionRay, depth);
delete refractionRay;
delete reflectionRay;
return material->reflection * reflectionColor + material->color.w * color;
}
return BGCOLOR;
}
vec4 Scene::illuminate(Ray* ray)
{
vec4 color = vec4(0, 0, 0, 0);
for (int i = 0; i < this->lightSources.size(); i++)
{
vec3 origin = ray->origin + ray->t * ray->direction;
vec3 direction = normalize(this->lightSources[i]->position - origin);
Ray* shadowRay = new Ray(origin, direction);
vec3 normal = this->primitives[ray->intersectedObjectId]->getNormal(shadowRay->origin);
float dotDirectionNormal = dot(shadowRay->direction, normal);
if (dotDirectionNormal < 0)
{
delete shadowRay;
continue;
}
vec3 hitEpsilon = shadowRay->origin + shadowRay->direction * 0.01;
shadowRay->origin = hitEpsilon;
shadowRay->t = (shadowRay->origin - this->lightSources[i]->position).length();
this->intersectPrimitives(shadowRay, true);
if (shadowRay->intersectedObjectId != -1)
{
delete shadowRay;
continue;
}
float attenuation = this->lightSources[i]->intensity / (this->lightSources[i]->position - shadowRay->origin).sqrLentgh();
color += dotDirectionNormal * attenuation * this->lightSources[i]->color;
delete shadowRay;
}
return color;
}
Ray* Scene::computeReflectionRay(Ray* ray)
{
vec3 hitPoint = ray->origin + ray->t * ray->direction;
vec3 N = this->primitives[ray->intersectedObjectId]->getNormal(hitPoint);
vec3 direction = ray->direction - 2 * (ray->direction * N) * N;
vec3 origin = hitPoint + direction * EPSILON;
return new Ray(origin, direction);
}
Ray* Scene::computeRefractionRay(Ray* ray)
{
// source: https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
vec3 hitPoint = ray->origin + ray->t * ray->direction;
vec3 N = this->primitives[ray->intersectedObjectId]->getNormal(hitPoint);
float incommingAngle = dot(N, ray->direction);
float cosi = CLAMP(-1, 1, incommingAngle);
float etai = 1, etat = this->primitives[ray->intersectedObjectId]->material->refraction;
vec3 n = N;
if (cosi < 0) { cosi = -cosi; }
else { swap(etai, etat); n = -N; }
float eta = etai / etat;
float k = 1 - eta * eta * (1 - cosi * cosi);
if (k < 0)
{
Ray* refractionRay = new Ray(vec3(0), vec3(0));
refractionRay->intersectedObjectId = -2;
return refractionRay;
}
else
{
vec3 direction = eta * ray->direction + (eta * cosi - sqrtf(k)) * n;
vec3 origin = hitPoint + direction * EPSILON;
return new Ray(origin, direction);
}
}
void Scene::intersectPrimitives(Ray* ray, bool isShadowRay)
{
if (BVH_ENABLED)
{
this->topBHV->traverse(this->topBHV->root, ray, isShadowRay);
}
else
{
for (int i = 0; i < this->primitives.size(); i++)
{
this->primitives[i]->intersect(ray);
}
}
}
void Scene::intersectLightSources(Ray* ray)
{
for (int i = 0; i < this->lightSources.size(); i++)
{
this->lightSources[i]->intersect(ray);
}
}
Pixel Scene::convertColorToPixel(vec4 color)
{
color *= 255.0f;
int r = min((int)color.x, 255);
int g = min((int)color.y, 255);
int b = min((int)color.z, 255);
return (r << 16) + (g << 8) + b;
}
void Scene::buildTopBVH()
{
if (this->topBVHExists)
delete this->topBHV;
this->topBHV = new TopBVH(this->primitives, this->BVHs);
this->topBVHExists = true;
}
int Scene::buildBVH(int startIndex, int endIndex)
{
int id = this->BVHs.size();
BVH* tree = new BVH(this->primitives);
tree->build(id, startIndex, endIndex);
this->BVHs.push_back(tree);
this->buildTopBVH();
return id;
}
int Scene::addPrimitive(Primitive* primitive)
{
primitive->id = this->primitives.size();
this->primitives.push_back(primitive);
int modelId = this->buildBVH(primitive->id, primitive->id);
this->models.push_back(
new Model(modelId, primitive->id, primitive->id)
);
return modelId;
}
void Scene::addLightSource(LightSource* lightSource)
{
lightSource->id = this->lightSources.size();
this->lightSources.push_back(lightSource);
}
void Scene::clear()
{
for (int i = 0; i < this->primitives.size(); i++)
{
delete this->primitives[i]->boundingBox;
delete this->primitives[i];
}
this->primitives.clear();
for (int i = 0; i < this->lightSources.size(); i++)
{
delete this->lightSources[i];
}
this->lightSources.clear();
for (int i = 0; i < this->BVHs.size(); i++)
{
this->BVHs[i]->root->destroy();
delete this->BVHs[i]->objectIndices;
delete this->BVHs[i];
}
this->BVHs.clear();
this->models.clear();
}
int Scene::getPrimitivesCount()
{
return this->primitives.size();
}
int Scene::loadModel(const char *filename, Material* material, vec3 translationVector)
{
// obj file content
std::vector<vec3> vertices;
std::vector<int> faceIndexes;
std::vector<vec3> meshVertices;
std::ifstream stream(filename, std::ios::in);
if (!stream)
{
printf("Cannot load %s file!", filename);
}
std::string line;
while (std::getline(stream, line))
{
if (line.substr(0, 2) == "v ")
{
std::istringstream v(line.substr(2));
float x, y, z;
v >> x; v >> y; v >> z;
vertices.push_back(vec3(x, y, z));
}
else if (line.substr(0, 2) == "vt")
{
// TODO: implement textures support
}
else if (line.substr(0, 2) == "f ")
{
std::istringstream v(line.substr(2));
int a, b, c;
v >> a; v >> b; v >> c;
faceIndexes.push_back(--a);
faceIndexes.push_back(--b);
faceIndexes.push_back(--c);
}
}
// calculate mesh vertices
for (unsigned int i = 0; i < faceIndexes.size(); i++)
{
meshVertices.push_back(
vec3(vertices[faceIndexes[i]].x, vertices[faceIndexes[i]].y, vertices[faceIndexes[i]].z) + translationVector
);
}
// add triangles to the scene
int primitivesCount = meshVertices.size() / 3;
int startIndex = this->primitives.size();
for (int i = 0; i < primitivesCount; i++)
{
vec3 a = meshVertices[i * 3];
vec3 b = meshVertices[i * 3 + 1];
vec3 c = meshVertices[i * 3 + 2];
Triangle* triangle = new Triangle(material, a, b, c);
triangle->id = this->primitives.size();
this->primitives.push_back(triangle);
}
int endIndex = this->primitives.size() - 1;
int modelId = this->buildBVH(startIndex, endIndex);
this->models.push_back(
new Model(modelId, startIndex, endIndex)
);
return modelId;
}
void Scene::translateModel(int id, vec3 vector)
{
// find model by id
Model* model;
for (int i = 0; i < this->models.size(); i++)
{
if (this->models[i]->id == id) model = this->models[i];
}
// find BVH by id
BVH* bvh;
for (int i = 0; i < this->BVHs.size(); i++)
{
if (this->BVHs[i]->id == id) bvh = this->BVHs[i];
}
if (model == NULL || bvh == NULL) return;
// translate model
for (int i = model->startIndex; i <= model->endIndex; i++)
{
this->primitives[i]->translate(vector);
}
// translate bvh
bvh->root->translate(vector);
this->buildTopBVH();
}