-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRayTracer.cpp
More file actions
430 lines (353 loc) · 11.2 KB
/
RayTracer.cpp
File metadata and controls
430 lines (353 loc) · 11.2 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "RayTracer.h"
#include "Camera.h"
#include "Ray.h"
#include "Hit.h"
#include "Group.h"
#include "Material.h"
#include "Light.h"
#define EPSILON 0.001
//IMPLEMENT THESE FUNCTIONS
//These function definitions are mere suggestions. Change them as you like.
Vector3f mirrorDirection( const Vector3f& normal, const Vector3f& incoming)
{
//Week 7 - Slide 14
Vector3f Reflected = incoming - 2*Vector3f::dot(normal, incoming)*normal;
return Reflected.normalized();
// Vector3f reflection = incoming - (2 * (Vector3f::dot(incoming,normal)) * normal);
// return reflection.normalized();
}
bool transmittedDirection( const Vector3f& normal, const Vector3f& incoming,
float index_n, float index_nt,
Vector3f& transmitted)
{
//Equation Given in Assignment PDF
float cal1 = Vector3f::dot(incoming,normal);
float cal2 = cal1 * cal1;
float cal3 = ((index_n*index_n)*(1 - cal2))/(index_nt*index_nt);
float cal4 = 1 - cal3;
if(cal4 < 0)
return false;
Vector3f cal5 = normal * sqrt(cal4);
Vector3f cal6 = cal1 * normal;
Vector3f cal7 = incoming - cal6;
Vector3f cal8 = (index_n * cal7)/index_nt;
transmitted = (cal8 - cal5).normalized();
return true;
}
RayTracer::RayTracer( SceneParser * scene, ArgParser* arg
//more arguments if you need...
) :
m_scene(scene)
{
g=scene->getGroup();
m_maxBounces = arg->bounces;
args = arg;
}
RayTracer::~RayTracer()
{
}
Matrix3f RayTracer::traceRay( Ray& currentRay, float tmin, int bounces,
float refr_index, Hit& hit ) const
{
//cout << "HERE2\t";
//Check ray intersection with Group of Objects
Vector3f pixelColor(0.0f), depthColor(0.0f), normalColor(0.0f);
bool toReturn = false;
if(m_scene->getGroup()->intersect(currentRay, hit, tmin))
{
//cout << "Intersection Found!\n";
float T = hit.getT();
Vector3f ambientLight = m_scene->getAmbientLight();
Vector3f diffShading = Vector3f(0.0f);
Vector3f Ka = hit.getMaterial()->getDiffuseColor();
for(register int light = 0 ; light < m_scene->getNumLights() ; ++light)
{
//cout << "Light: " << light << endl;
Vector3f dirToLight;
Vector3f lightColor;
float distanceToLight;
Vector3f intersectionPoint = currentRay.pointAtParameter(T);
m_scene->getLight(light)->getIllumination(intersectionPoint, dirToLight,
lightColor, distanceToLight);
if(args->shadows == 1)
{
Ray ray2(intersectionPoint, dirToLight);
Hit hit2(distanceToLight, NULL, NULL);
Light* curLight = m_scene->getLight(light);
if (curLight->type == 'p')
{
if (!g->intersectShadowRay(ray2, hit2, EPSILON))
diffShading += hit.getMaterial()->Shade(currentRay, hit, dirToLight, lightColor);
}
else // Sampling for soft shadows
{
int samples = curLight->samples;
int totalRays = 0;
int intersectedRays = 0;
float offsetValue = 1;
Vector3f softShadow(0.0f);
//3 Rays per sample, x offset, y offset, z offset
for (register int i = 0; i < samples; ++i)
{
float curOffset = (offsetValue/samples)*i;
//Generating 3 Directions
Vector3f offsetDirectionX(dirToLight[0] + curOffset, dirToLight[1], dirToLight[2]);
Vector3f offsetDirectionY(dirToLight[0], dirToLight[1] + curOffset, dirToLight[2]);
Vector3f offsetDirectionZ(dirToLight[0], dirToLight[1], dirToLight[2] + curOffset);
//Generating 3 New Rays
Ray rayX(intersectionPoint, offsetDirectionX);
Ray rayY(intersectionPoint, offsetDirectionY);
Ray rayZ(intersectionPoint, offsetDirectionZ);
//Generating 3 New Hits
Hit hitX(distanceToLight, NULL, NULL);
Hit hitY(distanceToLight, NULL, NULL);
Hit hitZ(distanceToLight, NULL, NULL);
//CHECKING X RAY
if (g->intersectShadowRay(rayX, hitX, EPSILON))
{
intersectedRays++;
}
else
{
softShadow += hit.getMaterial()->Shade(currentRay, hit, offsetDirectionX, lightColor);
}
if (i == 0)
{
totalRays += 1;
continue;
}
else
{
totalRays += 3;
}
//CHECKING Y RAY
if (g->intersectShadowRay(rayY, hitY, EPSILON))
{
intersectedRays++;
}
else
{
softShadow += hit.getMaterial()->Shade(currentRay, hit, offsetDirectionY, lightColor);
}
//CHECKING Z RAY
if (g->intersectShadowRay(rayZ, hitZ, EPSILON))
{
intersectedRays++;
}
else
{
softShadow += hit.getMaterial()->Shade(currentRay, hit, offsetDirectionZ, lightColor);
}
}
//3 Rays per sample, x offset, y offset, z offset
for (register int i = 0; i < samples; ++i)
{
float curOffset = (offsetValue / samples) * i;
//Generating 3 Directions
Vector3f offsetDirectionX(dirToLight[0] - curOffset, dirToLight[1], dirToLight[2]);
Vector3f offsetDirectionY(dirToLight[0], dirToLight[1] - curOffset, dirToLight[2]);
Vector3f offsetDirectionZ(dirToLight[0], dirToLight[1], dirToLight[2] - curOffset);
//Generating 3 New Rays
Ray rayX(intersectionPoint, offsetDirectionX);
Ray rayY(intersectionPoint, offsetDirectionY);
Ray rayZ(intersectionPoint, offsetDirectionZ);
//Generating 3 New Hits
Hit hitX(distanceToLight, NULL, NULL);
Hit hitY(distanceToLight, NULL, NULL);
Hit hitZ(distanceToLight, NULL, NULL);
//CHECKING X RAY
if (g->intersectShadowRay(rayX, hitX, EPSILON))
{
intersectedRays++;
}
else
{
softShadow += hit.getMaterial()->Shade(currentRay, hit, offsetDirectionX, lightColor);
}
if (i == 0)
{
totalRays += 1;
continue;
}
else
{
totalRays += 3;
}
//CHECKING Y RAY
if (g->intersectShadowRay(rayY, hitY, EPSILON))
{
intersectedRays++;
}
else
{
softShadow += hit.getMaterial()->Shade(currentRay, hit, offsetDirectionY, lightColor);
}
//CHECKING Z RAY
if (g->intersectShadowRay(rayZ, hitZ, EPSILON))
{
intersectedRays++;
}
else
{
softShadow += hit.getMaterial()->Shade(currentRay, hit, offsetDirectionZ, lightColor);
}
}
float lightIntensity = (totalRays - intersectedRays) / (totalRays);
//diffShading += lightIntensity * hit.getMaterial()->Shade(currentRay, hit, dirToLight, lightColor);
diffShading += softShadow / totalRays;
}
}
else
{
diffShading += hit.getMaterial()->Shade(currentRay, hit, dirToLight, lightColor);
}
}
pixelColor = ambientLight*Ka + diffShading;
//cout << args << endl;
if (T < args->depth_min)
{
//cout << "if (T < args->depth_min)\n";
depthColor = Vector3f(1.0f);
}
else if (T > args->depth_max)
{
//cout << "else if (T > args->depth_max)\n";
depthColor = Vector3f(0.0f);
}
else
{
//cout << "else\n";
float difference = args->depth_max - args->depth_min;
float new_T = T - args->depth_min;
depthColor = Vector3f(1 - new_T/difference);
}
//cout << "Calculated\n";
}
else
{
//cout << "No Intersection!\n";
pixelColor = m_scene->getBackgroundColor(currentRay.getDirection()); //Change this later
depthColor = Vector3f(0.0f);
normalColor = Vector3f(0.0f);
toReturn = true;
}
if (toReturn)
{
Matrix3f colors(pixelColor, depthColor, normalColor);
return colors;
}
//Checking Reflection and Refraction
if(bounces < m_maxBounces)
{
//--------------------------------------------
//-REFLECTION---------------------------------
//--------------------------------------------
Vector3f reflectedColor(0.0f);
float T = hit.getT();
Vector3f intersectionPoint = currentRay.pointAtParameter(T);
Ray Reflected(intersectionPoint, mirrorDirection(hit.getNormal().normalized(), currentRay.getDirection()));
Hit recursiveHit;
Material* current = hit.getMaterial();
Matrix3f Reflection;
if(current->getRoughness() == 0)
Reflection = traceRay(Reflected, EPSILON, bounces+1, refr_index, recursiveHit);
else
{
float offsetValue = 1;
float LO = -offsetValue;
float HI = offsetValue;
int samples = current->getRoughness();
Vector3f rayOrigin = intersectionPoint;
Vector3f rayDirection = Reflected.getDirection();
Vector3f newColor(0.0f);
#pragma omp parallel for schedule(dynamic)
for (register int k = 0; k < samples; ++k)
{
float xOffset = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
float yOffset = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
float zOffset = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
//Generating New Ray
Vector3f newDirection = Vector3f(rayDirection[0]+xOffset, rayDirection[1]+yOffset, rayDirection[2]+zOffset);
Ray newRay(rayOrigin, newDirection);
//Generating new Hit
Hit newHit = Hit(FLT_MAX, NULL, Vector3f(0, 0, 0));
//Tracing the Ray
Matrix3f tempBuffer = traceRay(newRay,
EPSILON, bounces + 1, refr_index, newHit);
#pragma omp critical
{
//Getting Pixel Color
newColor += tempBuffer.getCol(0);
}
}
newColor = newColor / samples;
Reflection = Matrix3f(0.0f);
Reflection.setCol(0, newColor);
}
if(current != nullptr)
reflectedColor = Reflection.getCol(0)*current->getSpecularColor();
else
reflectedColor = Reflection.getCol(0);
//--------------------------------------------
//-REFRACTION---------------------------------
//--------------------------------------------
Vector3f refractedColor(0.0f);
Vector3f transmitted;
float n = refr_index;
float nt;
bool notTotalReflection;
//If the angle between Ray and Normal is less than 90, means we are inside an object
bool insideObject = (Vector3f::dot(currentRay.getDirection(), hit.getNormal().normalized()))>0;
Vector3f normal = hit.getNormal().normalized();
if(insideObject)
{
normal = normal*-1.0f;
nt = 1.0;
}
else
{
if(hit.getMaterial() != nullptr)
nt = hit.getMaterial()->getRefractionIndex();
else
nt = 1.0f;
}
notTotalReflection = transmittedDirection(normal, currentRay.getDirection(),
refr_index, nt, transmitted); //We are going into the AIR so 1.0 Index
Ray transmittedRay(intersectionPoint, transmitted);
Hit transmittedHit;
if(notTotalReflection)
{
refractedColor = (traceRay(transmittedRay, EPSILON, bounces+1,
nt, transmittedHit)).getCol(0);
if(current != nullptr)
refractedColor = refractedColor*current->getSpecularColor();
//--------------------------------------------
//-CALCULATING WEIGHTS------------------------
//--------------------------------------------
Vector3f t = transmitted;
Vector3f d = currentRay.getDirection();
Vector3f N = normal;
float c;
if(n <= nt)
{
c = abs(Vector3f::dot(-1.0f*d, N));
}
else
{
c = abs(Vector3f::dot(t, N));
}
float Ro = pow((nt-n)/(nt+n), 2);
float R = Ro + (1.0f-Ro) * pow(1-c, 5);
float reflectance = hit.getMaterial()->getShininess() / 100.0f;
pixelColor += + (1-R)*refractedColor + (R * reflectance)*reflectedColor;
}
else
{
float reflectance = hit.getMaterial()->getShininess() / 100.0f;
pixelColor += reflectedColor*reflectance;
}
}
normalColor = hit.getNormal();
Matrix3f colors(pixelColor, depthColor, normalColor);
return colors;
}