-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
316 lines (299 loc) · 9.1 KB
/
Source.cpp
File metadata and controls
316 lines (299 loc) · 9.1 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
//#define SDL_MAIN_HANDLED
#include <stdio.h>
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/half_float.hpp>
#include <iostream>
#include <fstream>
#include <SDL2/SDL.h>
#include <SDL_image.h>
#include "SDL.h"
#include "Light.h"
#include "Object.h"
#include "Ray.h"
#include "Camera.h"
#include "Intersection.h"
using namespace std;
vector<Light> lights;
vector<Sphere> spheres;
vector<Plane> planes;
vector<Triangle> triangles;
int resX;
int resY;
Camera camera;
glm::dvec3 back;
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
SDL_Surface* hello = NULL;
//SDL_Renderer* renderer = NULL;
Ray calculateRay(int i, int j) {
//make sure you divide the other one by resY, the slides are wrong
double a = (double)resX / resY;
glm::dvec3 xFactor = camera.v * (double)i;
glm::dvec3 yFactor = camera.u * (double)j;
glm::dvec3 p = camera.ll + xFactor + yFactor;
glm::dvec3 dir = glm::normalize(p - camera.eye);
return Ray(camera.eye, dir);
}
bool clearPath(Light light, glm::dvec3 pos) {
Ray ray = Ray(pos, light.pos - pos);
double t_val = glm::length(ray.dir);
ray.dir = glm::normalize(ray.dir);
ray.pos = ray.pos + ray.dir * 0.1;
if (spheres.size() > 0) {
for (int s = 0; s < spheres.size(); s++) {
Sphere temp = spheres[s];
glm::dvec3 d = glm::normalize(ray.dir);
glm::dvec3 hold = ray.pos - temp.pos;
double a = 1;
double b = 2 * (d.x * hold.x + d.y * hold.y + d.z * hold.z);
double c = hold.x * hold.x + hold.y * hold.y + hold.z * hold.z -
temp.radius * temp.radius;
//cout << b * b - 4 * c << '\n';
if (b * b - 4 * c >= 0) {
double t2 = (-b + sqrt(b * b - 4 * c)) / 2;
if (t2 > 0.0 && t2 < t_val) {
return false;
}
}
}
}
if (planes.size() > 0) {
for (unsigned int s = 0; s < planes.size(); s++) {
Plane temp = planes[s];
if (glm::dot(ray.dir, temp.norm) != 0) {
glm::dvec3 q = temp.pos - ray.pos;
double t = glm::dot(q, temp.norm) / glm::dot(ray.dir, temp.norm);
if (t > 0 && t <= t_val) {
return false;
}
}
}
}
return true;
}
Intersection firstInter(Ray ray, bool* collided) {
Intersection inter = Intersection();
if (spheres.size() > 0) {
for (int s = 0; s < spheres.size(); s++) {
Sphere temp = spheres[s];
glm::dvec3 d = glm::normalize(ray.dir);
glm::dvec3 hold = ray.pos - temp.pos;
double a = 1;
double b = 2 * (d.x * hold.x + d.y * hold.y + d.z * hold.z);
double c = hold.x * hold.x + hold.y * hold.y + hold.z * hold.z -
temp.radius * temp.radius;
if (b * b - 4 * c >= 0) {
double t1 = (-b - sqrt(b * b - 4 * c)) / 2;
double t2 = (-b + sqrt(b * b - 4 * c)) / 2;
if ((t2 > 0.0) & (t2 < inter.t || (t1 > 0 && t1 < inter.t))) {
*collided = true;
inter.lightVec.clear();
inter.diff = temp.diff;
inter.spec = temp.spec;
if (t1 > 0) {
inter.t = t1;
inter.pos = ray.pos + ray.dir * t1;
inter.norm = glm::normalize(temp.pos - inter.pos);
for (unsigned int i = 0; i < lights.size(); i++) {
if (clearPath(lights[i], inter.pos)) {
inter.lightVec.push_back(lights[i]);
}
}
}
else {
inter.t = t2;
inter.pos = ray.pos + ray.dir * t2;
for (unsigned int i = 0; i < lights.size(); i++) {
if (clearPath(lights[i], inter.pos)) {
inter.lightVec.push_back(lights[i]);
}
}
}
}
}
}
}
if (planes.size() > 0) {
for (unsigned int s = 0; s < planes.size(); s++) {
Plane temp = planes[s];
if (glm::dot(ray.dir, temp.norm) != 0) {
glm::dvec3 q = temp.pos - ray.pos;
double t = glm::dot(q, temp.norm) / glm::dot(ray.dir,temp.norm);
if (t > 0 && inter.t > t) {
inter.t = t;
inter.diff = temp.diff;
inter.spec = temp.spec;
inter.pos = ray.pos + ray.dir * t;
inter.norm = temp.norm;
*collided = true;
inter.lightVec.clear();
for (unsigned int i = 0; i < lights.size(); i++) {
if (clearPath(lights[i], inter.pos)) {
inter.lightVec.push_back(lights[i]);
}
}
}
}
}
}
if (triangles.size() > 0) {
for (unsigned int tri = 0; tri < triangles.size(); tri++) {
Triangle temp = triangles[tri];
glm::dvec3 p = temp.pos2 - temp.pos;
glm::dvec3 q = temp.pos3 - temp.pos;
glm::dvec3 s = ray.pos - temp.pos;
}
}
return inter;
}
glm::dvec3 TraceRay(Ray ray, int depth) {
bool collided = false;
Intersection p = firstInter(ray,&collided);
p.norm = glm::normalize(p.norm);
glm::dvec3 diff = glm::dvec3(0);
glm::dvec3 spec = glm::dvec3(0);
glm::dvec3 reflectCol = glm::dvec3(0);
glm::dvec3 reflect = glm::normalize(glm::reflect(ray.dir, p.norm));
if (collided) {
for (unsigned int i = 0; i < p.lightVec.size(); i++) {
Light tempLight = p.lightVec[i];
//cout << glm::dot(p.norm, ray.dir) << '\n';
diff += p.diff * tempLight.diff * max(0.0, glm::dot(p.norm, ray.dir));
glm::dvec3 h = reflect + ray.dir;
spec += p.spec * tempLight.spec * max(0.0, glm::dot(p.norm, h));
}
if ((p.spec.x > 0 || p.spec.y > 0 || p.spec.z > 0) && depth > 0) {
reflectCol = TraceRay(Ray(p.pos+reflect *0.1, reflect),depth-1);
}
return diff + spec + reflectCol * 0.5;
}
return glm::dvec3(back.x, back.y, back.z);
}
void setPixel(int i, int j, glm::dvec3 color) {
unsigned int r = (unsigned int)(min(max(color.x, 0.0), 1.0) * 255);
unsigned int g = (unsigned int)(min(max(color.y, 0.0), 1.0) * 255);
unsigned int b = (unsigned int)(min(max(color.z, 0.0), 1.0) * 255);
((uint32_t*)hello->pixels)[j * hello->w + i] = (uint32_t)SDL_MapRGB(hello->format, r, g, b);
}
int main(int argc, char* args[] ) {
ifstream scene("scene.txt");
string holder;
scene >> holder >> back.x >> back.y >> back.z;
//cout << holder + '\n';
int max_depth;
scene >> holder >> max_depth;
scene >> holder;
//cout << holder + '\n';
while (strcmp(holder.c_str(), "RESOLUTION") != 0) {
if (strcmp(holder.c_str(), "LIGHT") == 0) {
Light light = Light();
glm::dvec3 temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
light.pos = temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
light.diff = temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
light.spec = temp;
cout << light;
lights.push_back(light);
}
else if (strcmp(holder.c_str(), "SPHERE") == 0) {
Sphere sphere = Sphere();
glm::dvec3 temp;
scene >> holder >> temp.x >> temp.y >> temp.z >> holder >> sphere.radius;
sphere.pos = temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
sphere.diff = temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
sphere.spec = temp;
scene >> holder >> sphere.shininess;
cout << sphere;
spheres.push_back(sphere);
}
else if (strcmp(holder.c_str(), "PLANE") == 0) {
cout << "I'm adding a plane!";
Plane plane = Plane();
glm::dvec3 temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
plane.norm = temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
plane.pos = temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
plane.diff = temp;
scene >> holder >> temp.x >> temp.y >> temp.z;
plane.spec = temp;
scene >> holder >> plane.shininess;
cout << plane;
planes.push_back(plane);
}
scene >> holder;
//cout << holder + '\n';
}
scene >> resX >> resY;
cout << "Resolution: " << resX << ", " << resY << '\n';
glm::dvec3 eye = glm::dvec3(0, 0, 70);//set to 250 later
glm::dvec3 up = glm::dvec3(0, 1, 0);
glm::dvec3 look = glm::dvec3(0, 0, 0);
camera = Camera(eye, up, look, resX, resY);
cout << "Hello I'm working here\n";
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return -1;
}
window = SDL_CreateWindow("RayTracer", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, resX, resY,SDL_WINDOW_SHOWN);
if (window == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return -1;
}
screenSurface = SDL_GetWindowSurface(window);
hello = SDL_CreateRGBSurface(0, resX, resY, 32, 0, 0, 0, 0);
if (hello == NULL) {
SDL_Log("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
exit(1);
}
//beginning of the overall loop
for (int x = 0; x < resX; x++) {
#pragma omp parallel for
for (int y = 0; y < resY; y++) {
Ray ray = calculateRay(x, y);
//Ray ray = Ray(glm::dvec3(0, 0, -5), glm::dvec3(0, 0, 1));
if ((x==0 && y ==0) || (x==resX-1) && (y==resY-1)) {
cout << "x: " << x << ", y: " << y << "\n";
cout << ray.dir.x << ", " << ray.dir.y << ", " << ray.dir.z << "\n";
}
glm::dvec3 color = TraceRay(ray, max_depth);
setPixel(x, y, color);
}
SDL_BlitSurface(hello, 0, screenSurface, 0);
SDL_UpdateWindowSurface(window);
}
cout << "Loop is done\n";
cout << "----------------------------------------\n";
for (int i = 0; i < size(spheres); i++) {
cout << spheres[i];
}
cout << "sizeof planes: " << size(planes) << '\n';
for (int i = 0; i < size(planes); i++) {
cout << planes[i];
}
SDL_SaveBMP(screenSurface, "endResult.bmp");
SDL_Delay(5000);
planes.clear();
spheres.clear();
lights.clear();
SDL_FreeSurface(hello);
hello = NULL;
SDL_FreeSurface(screenSurface);
screenSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
return 0;
}