-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
313 lines (255 loc) · 10.1 KB
/
main.cpp
File metadata and controls
313 lines (255 loc) · 10.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
#include <fstream>
#include <vector>
#include <cmath>
#include <CImg.h>
#include "glm.hpp"
#include "OBJloader.h"
#include "NeededMath.h"
#include "geometry.h"
using namespace std;
using namespace cimg_library;
using namespace glm;
// Signatures
void loadScene(ifstream &file, Scene &scene);
vec3 readVec3(ifstream &file);
// Main
int main() {
Scene scene;
ifstream inFile;
string filename;
// Ask the user for a scene file filename to parse
cout << "Please enter a filename of a scenefile to load:" << endl;
cin >> filename;
// Opening the file
inFile.open(filename);
if (!inFile) {
cerr << "Unable to open file " << filename;
exit(1); // call system to stop
}
// Create scene
loadScene(inFile, scene);
inFile.close();
cout << "Scene successfully loaded." << endl;
// Set const for shooting ray
const float tanHalf = tan(scene.cam.fov / 2);
const int HEIGHT = tan(scene.cam.fov / 2) * 2 *
scene.cam.focalLength; // Here FOV has been loaded and converted to radians already.
const int WIDTH = scene.cam.aspectRatio * HEIGHT;
// Creates an image with three channels and sets it to black
CImg<float> image(WIDTH, HEIGHT, 1, 3, 0);
// Shoot rays
for (int i = -WIDTH / 2; i < WIDTH / 2; i++) {
// Current pixel x, calculated from ray.x coordinate (i)
int imgX = i + WIDTH / 2;
for (int j = HEIGHT / 2; j > -HEIGHT / 2; j--) {
// Current pixel y, calculated from ray.y coordinate (j)
int imgY = -j + HEIGHT / 2;
double t;
vec3 pixelColor = vec3();
// Create ray for current pixel
Ray ray = Ray(scene.cam.position, normalize(vec3(i, j, -scene.cam.focalLength)) );
// Check intersection with each objs
float closestScalar = INFINITY;
Renderable *closestObj;
for (int k = 0; k < scene.objs.size(); k++) {
// Return only value > 0
t = scene.objs[k]->intersect(ray);
// The closest intersection only
if (t < closestScalar && t > 0) {
closestScalar = t;
closestObj = scene.objs[k];
}
}
// Color pixel at calculated intersection
if (closestScalar < INFINITY) {
// Compute intersection world coord
vec3 pointIntersect = ray.origin + (ray.direction * closestScalar);
vec3 result = vec3(); // Will contain the diffuse + specular contributions of the lights
// Cast shadow rays
float bias = 0.001f;
for (int l = 0; l < scene.lights.size(); l++) {
Light *light = scene.lights[l];
vec3 normal = closestObj->getNormalAt(pointIntersect);
vec3 shadowDir = light->position - pointIntersect;
Ray shadowRay = Ray(pointIntersect + normal * bias, normalize(shadowDir) );
// Check if in shadow or not
bool lit = true;
for (int k = 0; k < scene.objs.size(); k++) {
t = scene.objs[k]->intersect(shadowRay);
// If we detect something in between intersection and light
if (t < 1) {
lit = false;
break;
}
}
// If still considered in the light
if (lit) {
// Computing Phong Model
vec3 light_reflection = reflect(normalize(-shadowRay.direction), normalize(normal) );
vec3 diffuseCoef = closestObj->material.diffuse * (float)glm::max(dot(normalize(normal), normalize(shadowRay.direction) ), 0.0);
vec3 specularCoef = closestObj->material.specular * (float)pow(glm::max(dot(light_reflection, -ray.direction), 0.0), closestObj->material.shininess);
// Diffuse
result += light->diffuseColor * diffuseCoef;
// Specular
result += light->specularColor * specularCoef;
}
}
// Adding ambient + result
pixelColor += closestObj->material.ambient + result;
// Scale and clamp color
pixelColor = pixelColor * 255.f;
clampColor(pixelColor);
// Paint the pixel
image(imgX,imgY,0) = pixelColor.x;
image(imgX,imgY,1) = pixelColor.y;
image(imgX,imgY,2) = pixelColor.z;
}
}
}
// Save img
image.save("render.bmp");
// Display img
CImgDisplay main_disp(image, "Render");
while (!main_disp.is_closed()) {
main_disp.wait();
}
// End process
return 0;
}
/**
* Parse scene file and create relative objects to build the scene
* @param file
*/
void loadScene(ifstream &file, Scene &scene) {
string token;
// Until there is no more tokens
while (file >> token) {
if (token == "camera") {
for (int i = 0; i < 4; i++) {
file >> token;
if (token == "pos:") {
scene.cam.position = readVec3(file);
} else if (token == "fov:") {
file >> token;
scene.cam.fov = std::stod(token) * (M_PI / 180);
} else if (token == "f:") {
file >> token;
scene.cam.focalLength = std::stof(token);
} else if (token == "a:") {
file >> token;
scene.cam.aspectRatio = std::stof(token);
}
}
} else if (token == "sphere") {
Sphere *sphere = new Sphere(vec3(0,0,0), 0);
Material mat;
for (int i = 0; i < 6; i++) {
file >> token;
if (token == "pos:") {
sphere->position = readVec3(file);
} else if (token == "rad:") {
file >> token;
sphere->radius = std::stod(token);
} else if (token == "amb:") {
mat.ambient = readVec3(file);
} else if (token == "dif:") {
mat.diffuse = readVec3(file);
} else if (token == "spe:") {
mat.specular = readVec3(file);
} else if (token == "shi:") {
file >> token;
mat.shininess = std::stof(token);
}
}
sphere->material = mat;
// Add to scene
scene.objs.push_back(sphere);
} else if (token == "plane") {
Plane *plane = new Plane();
Material mat;
for (int i = 0; i < 6; i++) {
file >> token;
if (token == "pos:") {
plane->position = readVec3(file);
} else if (token == "nor:") {
plane->normal = readVec3(file);
} else if (token == "amb:") {
mat.ambient = readVec3(file);
} else if (token == "dif:") {
mat.diffuse = readVec3(file);
} else if (token == "spe:") {
mat.specular = readVec3(file);
} else if (token == "shi:") {
file >> token;
mat.shininess = std::stof(token);
}
}
plane->material = mat;
// Add to scene
scene.objs.push_back(plane);
} else if (token == "light") {
Light *light = new Light();
for (int i = 0; i < 3; i++) {
file >> token;
if (token == "pos:") {
light->position = readVec3(file);
} else if (token == "dif:") {
light->diffuseColor = readVec3(file);
} else if (token == "spe:") {
light->specularColor = readVec3(file);
}
}
// Add to scene
scene.lights.push_back(light);
} else if(token == "mesh"){
Mesh mesh;
Material mat;
for(int i=0; i<5; i++){
file >> token;
if(token == "file:"){
file >> token;
// Load OBJ and create triangles for the mesh
string path = "scenes/";
path.append(token);
vector<vec3> vertices;
vector<vec3> normals;
vector<vec2> UVs;
// Load the OBJ data
loadOBJ(path.c_str(), vertices, normals, UVs);
// Build triangle out of the vertices data
for(int t=0; t<vertices.size(); t+=3){
Triangle *tri = new Triangle(vertices[t], vertices[t+1], vertices[t+2]);
mesh.triangles.push_back(tri);
}
} else if (token == "amb:") {
mat.ambient = readVec3(file);
} else if (token == "dif:") {
mat.diffuse = readVec3(file);
} else if (token == "spe:") {
mat.specular = readVec3(file);
} else if (token == "shi:") {
file >> token;
mat.shininess = std::stof(token);
}
}
// Assign material to all triangles
// TODO: structure this in a better way
for(int k=0; k<mesh.triangles.size(); k++){
mesh.triangles[k]->material = mat;
scene.objs.push_back(mesh.triangles[k]);
}
}
}
}
/**
* Read the next 3 tokens, considered as numerical values, and return a Vec3 out of them
* @param file
* @return
*/
vec3 readVec3(ifstream &file) {
double x, y, z;
file >> x;
file >> y;
file >> z;
return {x, y, z};
}