-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
349 lines (246 loc) · 8.44 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
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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <glm/glm.hpp>
#include <ppl.h>
#include <SDL.h>
#include "Shape.h"
#include "Sphere.h"
#include "Plane.h"
#include "Triangle.h"
#include "objloader.h"
#include "AAB.h"
using namespace std;
using namespace glm;
void createShapes();
vec3 rayTrace(vec3 p, vec3 v, int d);
void setPixel(SDL_Surface* surface);
bool drawPixel(SDL_Surface* surface, int i, int j, int x, int y, int z);
float height = 640.0f;
float width = 840.0f;
bool recurseReflect = false;
bool hardShadows = true;
float printOnce = 1;
float FOV = 90.0f;
SDL_Event event = { 0 };
int maxDepth = 5;
vector<Shape*> shapeVector;
vec3 **image = new vec3*[width];
AAB* areaLight = new AAB(vec3(-4.5f, 20, -4.5f), vec3(9, 0.1f, 9));
bool drawPixel(SDL_Surface* surface, int x, int y, int r, int g, int b) {
if (y < 0 || y >= height || x < 0 || x >= width) {
return false;
}
Uint32 colorSDL = SDL_MapRGB(surface->format, r, g, b);
Uint32* bufp;
bufp = (Uint32*)surface->pixels + y * surface->pitch / 4 + x;
*bufp = colorSDL;
bufp += surface->pitch / 4;
return true;
}
void setPixel(SDL_Surface* surface) {
concurrency::parallel_for(int(0), (int)width, [&](int i) {
for (int j = 0; j < height; ++j) {
float pixNormX = (i + 0.5f) / width;
float pixNormY = (j + 0.5f) / height;
float imageAspectRatio = width / height;
float pixRemapX = (2 * pixNormX - 1) * imageAspectRatio;
float pixRemapY = 1 - 2 * pixNormY;
float pixCameraX = pixRemapX * tan(radians(FOV) / 2);
float pixCameraY = pixRemapY * tan(radians(FOV) / 2);
vec3 PcameraSpace = vec3(pixCameraX, pixCameraY, -1);
vec3 rayOrigin = vec3(0);
vec3 rayDirection = normalize(PcameraSpace - rayOrigin);
vec3 pixelColour = rayTrace(rayOrigin, rayDirection, 0);
image[i][j] = pixelColour;
drawPixel(surface, i, j, std::min(1.0f, image[i][j].x) * 255, std::min(1.0f, image[i][j].y) * 255, std::min(1.0f, image[i][j].z) * 255);
}
});
}
void createShapes() {
//======================= SPHERES =======================
shapeVector.push_back(new Plane(vec3(0, -4, 0), vec3(0, 1, 0), vec3(0.3f)));
shapeVector.push_back(new Sphere(vec3(0, 0, -20), 4, vec3(1, 0.32f, 0.36f)));
shapeVector.push_back(new Sphere(vec3(5, -1, -15), 2, vec3(0.9f, 0.76f, 0.46f)));
shapeVector.push_back(new Sphere(vec3(5, 0, -25), 3, vec3(0.65f, 0.77f, 0.97f)));
shapeVector.push_back(new Sphere(vec3(-5.5f, 0, -15), 3, vec3(0.9f)));
//======================================================
////======================= TEAPOT =======================
//shapeVector.push_back(new Plane(vec3(0, -2.8, 0), vec3(0, 1, 0), vec3(0.3f)));
//vector<vec3> teaVertices;
//vector<vec3> teaNormals;
//bool loadTea = loadOBJ("Models/teapot_simple.obj", teaVertices, teaNormals);
//for (int i = 0; i < teaVertices.size(); i++) {
// teaVertices[i].z -= 6;
// teaVertices[i].y -= 2;
//}
//for (int i = 0; i < teaVertices.size(); i += 3) {
// shapeVector.push_back(new Triangle(teaVertices[i], teaVertices[i + 1], teaVertices[i + 2], teaNormals[i], teaNormals[i + 1], teaNormals[i + 2], vec3(0, 0.6f, 1)));
//}
////======================================================
}
vec3 rayTrace(vec3 refRayOrigin, vec3 refRayDirection, int depth) {
vec3 reflectColour, pixelColour;
vec3 uvw, minUVW = vec3(0);
float t0 = 0.0f;
float minT = FLT_MAX;
int shapeHit = -1;
for (int k = 0; k < shapeVector.size(); ++k) {
bool hit = shapeVector[k]->Intersection(refRayOrigin, refRayDirection, uvw.x, uvw.y, uvw.z, &t0);
if (hit && t0 < minT) {
minT = t0;
minUVW.x = uvw.x;
minUVW.y = uvw.y;
minUVW.z = uvw.z;
shapeHit = k;
}
}
if (shapeHit != -1) {
vec3 p0 = refRayOrigin + (minT * refRayDirection);
vec3 areaLightPos = vec3(areaLight->position.x, areaLight->position.y, areaLight->position.z);
vec3 areaLightSize = vec3(areaLight->size.x, areaLight->size.y, areaLight->size.z);
vec3 areaLightCenter = vec3(areaLightPos.x + (areaLightSize.x / 2), areaLightPos.y + (areaLightSize.y / 2), areaLightPos.z + (areaLightSize.z / 2));
vec3 lightIntensity = vec3(1);
vec3 diffuseColour, specularColour = vec3(0);
int shininess = 0;
vec3 normal = normalize(shapeVector[shapeHit]->CalculateNormal(p0, minUVW.x, minUVW.y, minUVW.z, &shininess, &diffuseColour, &specularColour));
vec3 ambient = shapeVector[shapeHit]->colour * vec3(0.07f);
vec3 lightRay = normalize(areaLightCenter - p0);
vec3 diffuse = diffuseColour * lightIntensity * glm::max(0.0f, dot(lightRay, normal));
vec3 reflection = normalize(2 * (dot(lightRay, normal)) * normal - lightRay);
float maxCalc = glm::max(0.0f, dot(reflection, normalize(refRayOrigin - p0)));
vec3 specular = specularColour * lightIntensity * pow(maxCalc, shininess);
if (recurseReflect) {
if ((depth < maxDepth) && (shininess > 0)) {
vec3 reflectionRayDirection = refRayDirection - 2 * dot(refRayDirection, normal) * normal;
vec3 reflectionRayOrigin = p0 + (normal * 1e-4f);
reflectColour = reflectColour + 0.25f * rayTrace(reflectionRayOrigin, reflectionRayDirection, depth + 1);
return pixelColour = diffuse + specular + reflectColour;
}
}
int lightShapeHit = -1;
if (hardShadows) {
for (int l = 0; l < shapeVector.size(); ++l) {
bool lightingHit = shapeVector[l]->Intersection(p0 + (1e-4f * normal), lightRay, uvw.x, uvw.y, uvw.z, &t0);
if (lightingHit && t0 < minT) {
minT = t0;
lightShapeHit = l;
}
}
if (lightShapeHit != -1) {
pixelColour = ambient;
}
else {
pixelColour = (diffuse + specular);
}
}
else {
float sample = 9.0f;
float softIncrement = areaLightSize.x / sample;
float hitRays = 1.0f;
float totalRays = 0.0f;
totalRays = pow(sample, 2);
for (float m = 0; m < areaLightSize.x; m += softIncrement) {
for (float n = 0; n < areaLightSize.z; n += softIncrement) {
float t0s = 0.0f;
float minTs = FLT_MAX;
lightShapeHit = -1;
vec3 UVWs = vec3(0);
areaLightPos = vec3(m, areaLightPos.y, n);
lightRay = normalize(areaLightPos - p0);
for (int l = 0; l < shapeVector.size(); ++l) {
if (shapeHit != l) {
bool lightingHit = shapeVector[l]->Intersection(p0 + (1e-4f * normal), lightRay, UVWs.x, UVWs.y, UVWs.z, &t0s);
if (lightingHit && t0s < minTs) {
minTs = t0s;
lightShapeHit = l;
}
}
}
if (lightShapeHit != -1) {
hitRays = hitRays - (1 / totalRays);
}
}
}
pixelColour = vec3((hitRays) * (diffuse + specular));
}
}
else {
pixelColour = vec3(0.1, 0.2, 0.3);
}
return pixelColour;
}
bool done(bool quit_if_esc, bool delay) {
if (delay) {
SDL_Delay(5);
}
bool done = false;
if (!SDL_PollEvent(&event)) {
return 0;
}
if (event.type == SDL_QUIT) {
done = true;
}
else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
done = true;
break;
default:
break;
}
}
return done;
}
int main(int argc, char* args[]) {
for (int i = 0; i < width; ++i) {
image[i] = new vec3[height];
}
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
window = SDL_CreateWindow("Ray Tracing", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
screenSurface = SDL_GetWindowSurface(window);
while (!done(true, false)) {
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0, 0, 0));
createShapes();
setPixel(screenSurface);
SDL_UpdateWindowSurface(window);
if (printOnce == 1) {
printOnce = 0;
cout << "Press s to toggle hard/soft shadows\n(Soft shadows will be laggy)\n\n" << "Press space to toggle between teapot and spheres\n\n" << "Press r to toggle the reflection\n\n" << "Use up and down to change the FOV\n" << endl;
}
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_UP:
if (FOV > 20.0f) FOV -= 10.0f;
cout << "Decreasing the FOV\n";
break;
case SDLK_DOWN:
if (FOV < 150.0f) FOV += 10.0f;
cout << "Increasing the FOV\n";
break;
case SDLK_s:
hardShadows = !hardShadows;
cout << "Switching the shadows, please wait...\n";
break;
case SDLK_r:
recurseReflect = !recurseReflect;
cout << "Switching the reflections, please wait...\n";
break;
default:
break;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
delete areaLight;
for (int i = 0; i < width; ++i) {
delete[] image[i];
}
delete[] image;
for (int i = 0; i < shapeVector.size(); ++i) {
delete shapeVector[i];
}
return 0;
}