/
raytrace.cpp
462 lines (380 loc) · 11.7 KB
/
raytrace.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//
// template-rt.cpp
//
#define _CRT_SECURE_NO_WARNINGS
#include "matm.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <cfloat>
using namespace std;
#define NO_INTERSECTION -1.0f
#define NOT_VISIBLE -2.0f
#define SHADOW -3.0f
int g_width;
int g_height;
struct Ray {
vec4 origin;
vec4 dir;
};
//DONE: add structs for spheres, lights and anything else you may need.
struct Sphere {
vec4 center;
vec3 scale;
vec4 color;
float k_a;
float k_d;
float k_s;
float k_r;
float n;
mat4 inverse_scale;
};
struct Light {
vec4 position;
vec4 intensity;
};
vector<Sphere> spheres;
vector<Light> lights;
vector<vec4> g_colors;
//background r,g,b
vec4 bg_colors;
//scene's ambient intensity
vec4 ambience;
//output file name
string output;
bool inside = false;
//float t;
float g_left;
float g_right;
float g_top;
float g_bottom;
float g_near;
// -------------------------------------------------------------------
// Input file parsing
vec3 toVec3(vec4 in)
{
return vec3(in[0], in[1], in[2]);
}
vec4 toVec4(const string& s1, const string& s2, const string& s3)
{
stringstream ss(s1 + " " + s2 + " " + s3);
vec4 result;
ss >> result.x >> result.y >> result.z;
result.w = 1.0f;
return result;
}
float toFloat(const string& s)
{
stringstream ss(s);
float f;
ss >> f;
return f;
}
void parseLine(const vector<string>& vs)
{
//DONE: add parsing of NEAR, LEFT, RIGHT, BOTTOM, TOP, SPHERE, LIGHT, BACK, AMBIENT, OUTPUT.
const int num_labels = 11;
const string labels[] = { "NEAR", //0
"LEFT", //1
"RIGHT", //2
"BOTTOM", //3
"TOP", //4
"RES", //5
"SPHERE", //6
"LIGHT", //7
"BACK", //8
"AMBIENT", //9
"OUTPUT" //10
};
unsigned label_id = find(labels, labels + num_labels, vs[0]) - labels;
switch (label_id) {
case 0: g_near = toFloat(vs[1]); break; //NEAR
case 1: g_left = toFloat(vs[1]); break; //LEFT
case 2: g_right = toFloat(vs[1]); break; //RIGHT
case 3: g_bottom = toFloat(vs[1]); break; //BOTTOM
case 4: g_top = toFloat(vs[1]); break; //TOP
case 5: //RES
g_width = (int)toFloat(vs[1]);
g_height = (int)toFloat(vs[2]);
g_colors.resize(g_width * g_height);
break;
case 6: //SPHERE
{
Sphere new_sphere;
new_sphere.center = toVec4(vs[2], vs[3], vs[4]);
new_sphere.scale = vec3(toFloat(vs[5]), toFloat(vs[6]), toFloat(vs[7]));
new_sphere.color = toVec4(vs[8], vs[9], vs[10]);
new_sphere.k_a = toFloat(vs[11]);
new_sphere.k_d = toFloat(vs[12]);
new_sphere.k_s = toFloat(vs[13]);
new_sphere.k_r = toFloat(vs[14]);
new_sphere.n = toFloat(vs[15]);
//store the inverse scale transform for later
mat4 scale_matrix = Scale(new_sphere.scale);
mat4 inverseScale;
InvertMatrix(scale_matrix, inverseScale);
new_sphere.inverse_scale = inverseScale;
spheres.push_back(new_sphere);
}
break;
case 7: //LIGHT
{
Light new_light;
new_light.position = toVec4(vs[2], vs[3], vs[4]);
new_light.intensity = toVec4(vs[5], vs[6], vs[7]);
lights.push_back(new_light);
}
break;
case 8: //BACK
bg_colors = toVec4(vs[1], vs[2], vs[3]);
break;
case 9: //AMBIENT
ambience = toVec4(vs[1], vs[2], vs[3]);
break;
case 10: //OUTPUT
{
output = vs[1];
break;
}
}
}
void loadFile(const char* filename)
{
ifstream is(filename);
if (is.fail())
{
cout << "Could not open file " << filename << endl;
exit(1);
}
string s;
vector<string> vs;
while(!is.eof())
{
vs.clear();
getline(is, s);
istringstream iss(s);
while (!iss.eof())
{
string sub;
iss >> sub;
vs.push_back(sub);
}
parseLine(vs);
}
}
// -------------------------------------------------------------------
// Utilities
void setColor(int ix, int iy, const vec4& color)
{
int iy2 = g_height - iy - 1; // Invert iy coordinate.
g_colors[iy2 * g_width + ix] = color;
}
// -------------------------------------------------------------------
// Intersection routine
// TODO: add your ray-sphere intersection routine here.
float intersect(const Ray& ray, const Sphere& sphere, float threshold) {
float t_1, t_2;
//Transform ray to find intersection with unit sphere
vec4 direction = sphere.inverse_scale * ray.dir;
vec4 ray_to_origin = sphere.inverse_scale * (ray.origin - sphere.center);
//Components of determinant in line-sphere formula
//referenced from Wikipedia - "Line-Sphere Intersection"
float a = dot(direction, direction);
float b = dot(direction, ray_to_origin);
float c = dot(ray_to_origin, ray_to_origin) - (1.0f);
//Evaluate determinant
float determinant = (b*b) - (a*c);
//No intersection
if (determinant < 0)
return NO_INTERSECTION;
t_1 = ((-1 * b) - sqrtf(determinant)) / a;
t_2 = ((-1 * b) + sqrtf(determinant)) / a;
if (t_1 < threshold && t_2 < threshold) {
//Handle shadows
if (t_1 > 0.001f || t_2 > 0.001f)
return SHADOW;
return NOT_VISIBLE;
}
else if (t_1 >= threshold && t_2 >= threshold) {
inside = false;
if (t_2 > t_1)
return t_1;
else
return t_2;
}
else if (threshold == 1.0f) {
//Cutting inside the sphere
inside = true;
if (t_1 < threshold && t_2 >= threshold)
return t_2;
else if (t_2 < threshold && t_1 >= threshold)
return t_1;
}
else
return FLT_MAX;
}
vec4 c_local(const Ray& ray, const Sphere& sphere, const Light& lightsource, vec4 intersection, vec4 normal) {
vec4 pixel_diffuse = vec4(0.0f, 0.0f, 0.0f, 1.0f);
vec4 pixel_specular = vec4(0.0f, 0.0f, 0.0f, 1.0f);
//Diffuse Lighting
vec4 light = normalize(lightsource.position - intersection);
float angle = dot(normal, light);
//Specular Lighting
vec4 v = normalize((ray.origin - intersection));
vec4 r = (2 * normal * angle) - light;
float spec = pow(dot(r, v), sphere.n);
if (angle < 0) {}
else
pixel_specular = (lightsource.intensity * spec * sphere.k_s);
if (angle < 0) {}
else
pixel_diffuse = (lightsource.intensity * angle * sphere.k_d) * sphere.color;
return pixel_diffuse + pixel_specular;
}
// -------------------------------------------------------------------
// Ray tracing
vec4 trace(const Ray& ray, int depth, float threshold)
{
vec4 pixel_color = vec4(0.0f, 0.0f, 0.0f, 1.0f);
vec4 pixel_local = vec4(0.0f, 0.0f, 0.0f, 1.0f);
vec4 pixel_refl = vec4(0.0f, 0.0f, 0.0f, 1.0f);
//bool has_intersection = false;
float closest_t = FLT_MAX;
Sphere intersected_sphere;
bool inside_sphere = false;
//Find the closest intersection between the ray from the eye and a sphere
for (std::vector<Sphere>::iterator sphere = spheres.begin(); sphere != spheres.end(); ++sphere) {
float t = intersect(ray, *sphere, threshold);
if (t == NO_INTERSECTION)
continue;
else {
//has_intersection = true;
if (t < closest_t) {
closest_t = t;
intersected_sphere = *sphere;
inside_sphere = inside;
}
}
}
vec4 intersection = ray.origin + closest_t*ray.dir;
vec4 normal = (intersection - intersected_sphere.center) / (1.0f); //distance from center of transformed sphere to intersection at surface
normal = intersected_sphere.inverse_scale*normal; //untransform that distance
normal = normalize(transpose(intersected_sphere.inverse_scale) * normal); //multiply by the inverse transpose to get the actual normal
//invert the normal if the intersection is inside the sphere
if (inside_sphere)
normal = -normal;
//If within the viewing plane, calculate shadow rays and local illumination
if (closest_t != FLT_MAX && closest_t >= threshold) {
for (std::vector<Light>::iterator light_it = lights.begin(); light_it != lights.end(); ++light_it) {
Ray light_ray;
light_ray.origin = intersection;
light_ray.dir = light_it->position - intersection;
//Flag for object obstructing light
bool shadow = false;
for (std::vector<Sphere>::iterator shadow_sphere = spheres.begin(); shadow_sphere != spheres.end(); ++shadow_sphere) {
if (intersect(light_ray, *shadow_sphere, threshold) == SHADOW) {
shadow = true;
break;
}
}
if (!shadow)
pixel_local += c_local(ray, intersected_sphere, *light_it, intersection, normal);
}
pixel_local += ambience * intersected_sphere.k_a * intersected_sphere.color;
//Calculate contribution from reflected rays
vec4 calc_refl = vec4(0.0f, 0.0f, 0.0f, 1.0f);
Ray reflection;
reflection.origin = intersection;
reflection.dir = -2 * dot(normal, normalize(ray.dir)) * normal + normalize(ray.dir);
if (depth != 3)
calc_refl = trace(reflection, depth + 1, 0.0f);
if (calc_refl.x != bg_colors.x || calc_refl.y != bg_colors.y || calc_refl.z != bg_colors.z || calc_refl.w != bg_colors.w)
pixel_refl = calc_refl * intersected_sphere.k_r;
}
if (closest_t == FLT_MAX || closest_t == NOT_VISIBLE)
return bg_colors;
else {
pixel_color = pixel_local + pixel_refl;
return pixel_color;
}
}
//return the direction from the origin to pixel (ix, iy).
vec4 getDir(int ix, int iy)
{
vec4 dir;
//interpolate to find x and y values
float x = g_left*(1 - ( (float) ix / g_width)) + g_right*( (float) ix / g_width);
float y = g_bottom*(1 - ((float)iy / g_height)) + g_top* ((float)iy / g_height);
float z = -1 * g_near;
dir = vec4(x, y, z, 0.0f);
return dir;
}
void renderPixel(int ix, int iy)
{
Ray ray;
ray.origin = vec4(0.0f, 0.0f, 0.0f, 1.0f);
ray.dir = getDir(ix, iy);
vec4 color = trace(ray, 0, 1.0f);
setColor(ix, iy, color);
}
void render()
{
for (int iy = 0; iy < g_height; iy++)
for (int ix = 0; ix < g_width; ix++)
renderPixel(ix, iy);
}
// -------------------------------------------------------------------
// PPM saving
void savePPM(int Width, int Height, const char* fname, unsigned char* pixels)
{
FILE *fp;
const int maxVal=255;
printf("Saving image %s: %d x %d\n", fname, Width, Height);
fp = fopen(fname,"wb");
if (!fp) {
printf("Unable to open file '%s'\n", fname);
return;
}
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", Width, Height);
fprintf(fp, "%d\n", maxVal);
for(int j = 0; j < Height; j++) {
fwrite(&pixels[j*Width*3], 3, Width, fp);
}
fclose(fp);
}
void saveFile()
{
// Convert color components from floats to unsigned chars.
// Clamp values if out of range.
unsigned char* buf = new unsigned char[g_width * g_height * 3];
for (int y = 0; y < g_height; y++)
for (int x = 0; x < g_width; x++)
for (int i = 0; i < 3; i++) {
if (((float*)g_colors[y*g_width + x])[i] > 1.0f)
((float*)g_colors[y*g_width + x])[i] = 1;
if (((float*)g_colors[y*g_width + x])[i] < 0.0f)
((float*)g_colors[y*g_width + x])[i] = 0;
buf[y*g_width * 3 + x * 3 + i] = (unsigned char)(((float*)g_colors[y*g_width + x])[i] * 255.9f);
}
//DONE: change file name based on input file name.
savePPM(g_width, g_height, output.c_str(), buf);
delete[] buf;
}
// -------------------------------------------------------------------
// Main
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: template-rt <input_file.txt>" << endl;
exit(1);
}
loadFile(argv[1]);
render();
saveFile();
return 0;
}