-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraytrace.cpp
342 lines (283 loc) · 9.97 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
#include "SceneBuilder.h"
#include <time.h>
#define COLOR_DEPTH 255
#define RAD_TO_DEGREE 180/M_PI
using namespace std;
clock_t start, end;
bool anti_aliased = false;
bool dof = false;
double fovx, fovy;
list<vec3> pixels;
string output_file;
int width, height;
//Camera settings
vec3 cam_pos;
vec3 cam_up;
vec3 cam_lookat;
double cam_angle;
int cam_depth;
vec3 background_color;
double z_depth;
//Scene objects
list <Geometry*> shapes;
list <Light> lights;
vec3 ambient_light;
//Create the final ppm image file
void makePPM(string filename){
int color_depth = COLOR_DEPTH;
ofstream file (filename.c_str());
file << "P3\n" << "#" << filename << endl << width << " " << height << endl << color_depth << endl;
for(list<vec3>::iterator iter = pixels.begin(); iter!=pixels.end(); ++iter){
file << (int)iter->x << " " << (int)iter->y << " " << (int)iter->z << endl;
}
file.close();
}
//Adds ambient component to color vector c
void calcAmbient(vec3 &c, Geometry *shape){
c = shape->getColor() * ambient_light * shape->getAmbient();
}
//Add diffuse component to color vector c
void calcDiffuse(vec3 &c, Geometry *shape, Ray lightRay, vec3 N, double intensity){
vec3 L = lightRay.getDirection();
double diff = max((double)0, shape->getDiffuse() * dot(N,L));
c += intensity * diff * shape->getColor();
}
//Add specular component to color vector c
void calcSpecular(vec3 &c, Geometry *shape, Ray eyeRay, Ray lightRay, vec3 N, Light light, double intensity){
vec3 E = eyeRay.getDirection();
vec3 L = lightRay.getDirection();
//Get reflected ray from Normal and
vec3 R = normalize((2 * dot(N,L)*N) - L);
double d = dot(E,R);
if(d > 0){
double spec = pow(d,shape->getCosPower()) * shape->getSpecular();
c += intensity * spec * light.getColor();
}
}
//Main raytracing algorithm
vec3 castRays(Ray ray, int depth){
//Until we reach the max depth, keep casting rays
if(depth <= cam_depth){
vec3 color, tmp_normal, normal, ray_dir;
Ray shadowRay, cameraRay;
double tmp_t, t;
Geometry *shape;
bool hit = false;
ray_dir = ray.getDirection();
//Check all objects to see if ray hits any
for(list<Geometry*>::iterator iter = shapes.begin(); iter!=shapes.end(); ++iter){
if((*iter)->colision(ray, tmp_t, tmp_normal)){
hit = true;
//Simple depth test to see if object is closer than last object that was hit
if(tmp_t<z_depth){
z_depth = tmp_t;
shape = *iter;
t = tmp_t;
normal = tmp_normal;
}
}
}
z_depth = 9999999999;
//Ray hit something, calculate illumination
if(hit){
//Add ambient light contribution
calcAmbient(color,shape);
//Minus acounts for rounding errors that may have previously ocurred.
//Makes sure the hit position isn't on the wrong side of the surface
vec3 hitPos = cam_pos + ((t-0.0000001) * ray_dir);
bool obstructed = false;
//Check shadow ray to each light and see if we need to add lighting
for(list<Light>::iterator light = lights.begin(); light!=lights.end(); ++light){
int light_type = light->getType();
//Check to see if shadow ray hits any other geometry
for(list<Geometry*>::iterator iter = shapes.begin(); iter!=shapes.end(); ++iter){
//Point or spot light
if(light_type == 1 | light_type == 3){
shadowRay = Ray(hitPos,light->getPosition());
if((*iter)->colision(shadowRay)){
obstructed = true;
break;
}
}
//Parallel light
else if(light_type == 2){
shadowRay = Ray(hitPos,light->getPosition());
shadowRay.setDirection(normalize(-light->getPosition()));
if((*iter)->colision(shadowRay)){
obstructed = true;
break;
}
}
}
//If shadow ray isn't obstructed by any objects, get illumination
if(!obstructed){
cameraRay = Ray(hitPos,cam_pos);
normal = normal;
//Spot light
if(light_type == 3){
double theta = light->getAngle1();
double phi = light->getAngle2();
double angle = RAD_TO_DEGREE * acos(dot(-shadowRay.getDirection(), light->getDirection()));
if(angle < theta){
calcDiffuse(color, shape, shadowRay, normal,1.0);
calcSpecular(color, shape, cameraRay, shadowRay, normal, *light,1.0);
}
else if(angle < phi){
double att = 1;
double mag = ((1.0/angle - cos(phi/2.0))/(cos(theta/2.0) - cos(phi/2.0)));
mag = theta/angle;
calcDiffuse(color, shape, shadowRay, normal, mag);
calcSpecular(color, shape, cameraRay, shadowRay, normal, *light, mag);
}
}
//Point or Parallel light
else{
calcDiffuse(color, shape, shadowRay, normal,1.0);
calcSpecular(color, shape, cameraRay, shadowRay, normal, *light,1.0);
}
}
obstructed = false;
}
//If object is reflective, cast reflected ray
double kr = shape->getReflectance();
if(kr > 0.0){
vec3 R = normalize((2 * dot(normal,-ray_dir)*normal) + ray_dir);
Ray reflected_ray = Ray(hitPos,vec3(0,0,0));
reflected_ray.setDirection(R);
vec3 reflect_color = castRays(reflected_ray,depth+1);
color += (kr * reflect_color);
}
//If object transmits, calculate transmited light
double kt = shape->getTransmitance();
if(kt > 0.0){
vec3 R = normalize(normal * 2 * dot(normal,ray_dir));
Ray reflected_ray = Ray(hitPos,vec3(0,0,0));
reflected_ray.setDirection(R);
vec3 reflect_color = castRays(reflected_ray,depth+1);
color += (kt * reflect_color);
}
return color;
}
//If ray hits nothing, return background color
else{
return (background_color);
}
}
else{
return vec3(0,0,0);
}
}
void drawImage(){
Ray cast;
double x_dir, y_dir, pixel_size;
vec3 pixel_color;
double tan_x = tan(fovx);
double tan_y = tan(fovy);
double a = ((2.0*(double)1-(double)width)/(double)width);
double b = ((2.0*(double)2-(double)width)/(double)width);
pixel_size = (b - a / tan_x)/4.0;
//Loop over each pixel and cast rays into scene
for(int j = height-1; j>=0; j--){
for(int i = 0; i<width; i++){
x_dir = ((2.0*(double)i-(double)width)/(double)width) * tan_x;
y_dir = ((2.0*(double)j-(double)height)/(double)height) * tan_y;
//Takes the average of 4 pixels around the current pixel
if(anti_aliased){
cast = Ray(cam_pos, vec3(x_dir,y_dir,0.0) + cam_lookat);
pixel_color = castRays(cast, 1);
cast = Ray(cam_pos, vec3(x_dir + pixel_size,y_dir,0.0) + cam_lookat);
pixel_color += castRays(cast, 1);
cast = Ray(cam_pos, vec3(x_dir,y_dir + pixel_size,0.0) + cam_lookat);
pixel_color += castRays(cast, 1);
cast = Ray(cam_pos, vec3(x_dir + pixel_size,y_dir + pixel_size,0.0) + cam_lookat);
pixel_color += castRays(cast, 1);
pixel_color = pixel_color/4.0;
}
else if(dof){
double cx = cam_lookat.x;
double cy = cam_lookat.y;
double cz = cam_lookat.z;
double offset = 0.015;
cast = Ray(cam_pos, vec3(x_dir,y_dir,0.0) + cam_lookat);
pixel_color = castRays(cast, 1);
/*
cast = Ray(vec3(cam_pos.x + offset,cam_pos.y,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
cast = Ray(vec3(cam_pos.x - offset,cam_pos.y,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
cast = Ray(vec3(cam_pos.x,cam_pos.y - offset,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
cast = Ray(vec3(cam_pos.x,cam_pos.y + offset,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
*/
cast = Ray(vec3(cam_pos.x + offset,cam_pos.y,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
cast = Ray(vec3(cam_pos.x - offset,cam_pos.y,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
cast = Ray(vec3(cam_pos.x,cam_pos.y - offset,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
cast = Ray(vec3(cam_pos.x,cam_pos.y + offset,cam_pos.z), vec3(x_dir,y_dir,0.0) + vec3(cx,cy,cz));
pixel_color += castRays(cast, 1);
/*
cast = Ray(vec3(cam_pos.x,cam_pos.y,cam_pos.z), vec3(0.0,0.0,0.0));
cast.setDirection(vec3(x_dir,y_dir,0.0) + vec3(cx,cy + offset,cz));
pixel_color += castRays(cast, 1);
*/
pixel_color = pixel_color/5.0;
}
else{
//Create ray and cast it into the scene. Push returned color to pixel array
cast = Ray(cam_pos, vec3(x_dir,y_dir,0.0) + cam_lookat);
pixel_color = castRays(cast, 1);
}
pixels.push_back(vec3(pixel_color.x * COLOR_DEPTH, pixel_color.y * COLOR_DEPTH, pixel_color.z * COLOR_DEPTH));
}
}
}
//Calculate width of viewing frame in world coordinates
void setUpView(){
fovx = cam_angle/180.0 * M_PI;
fovy = height/width * fovx;
}
int main(int argc, char* argv[]){
//Parse scene file and store values;
if(argc > 1){
SceneBuilder scene = SceneBuilder(argv[1]);
printf("Parsing scene file:%s and setting up geometry!\n\n\n",argv[1]);
width = scene.getWidth();
height = scene.getHeight();
output_file = scene.getOutputFile();
background_color = scene.getBackgroundColor();
cam_pos = scene.getCamPos();
cam_up = scene.getCamUp();
cam_lookat = scene.getCamLookat();
cam_angle = scene.getCamAngle();
cam_depth = scene.getCamDepth();
shapes = scene.getShapes();
lights = scene.getLights();
ambient_light = scene.getAmbientLight();
}
//Check for extra arguments
if(argc > 2){
string arg = argv[2];
if(arg.compare("anti")==0){
printf("Adding anti-aliased effect!\n");
anti_aliased = true;
}
if(arg.compare("dof")==0){
printf("Adding depth of field effect!\n");
dof = true;
}
}
z_depth = 9999999999;
setUpView();
start = clock();
printf("Tracing Rays!\n\n\n");
drawImage();
end = clock()-start;
double sec = (double)end/(double)CLOCKS_PER_SEC;
printf("Finished! Took %f seconds to render.\n",sec);
printf("Output file is: %s\n\n",output_file.c_str());
makePPM(output_file);
return 0;
}