-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytracer_v1.cpp
executable file
·386 lines (294 loc) · 13.2 KB
/
raytracer_v1.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
#include <iostream>
#include "parser.h"
#include "ppm.h"
#include <cmath>
#include "raytracer_math.h"
#include <limits>
typedef unsigned char RGB[3];
enum IntersecType {none, sphere, triangle, mesh};
typedef struct
{
IntersecType obj_type;
float t;
int obj_id;
int face_id;//if it is a mesh we need to find which face it is
}IntersectionData;
void RayIntersecObj(const parser::Scene &scene,parser::Ray ray, IntersectionData &closest_obj_data);
parser::Intersection intersectRaySphere(const parser::Scene &scene, parser::Ray &eye_ray, int sphere_id);
parser::Vec3f intersectRayFace(const parser::Scene &scene, parser::Ray &eye_ray, const parser::Face &face);
float Determinant();
int main(int argc, char* argv[])
{
// Sample usage for reading an XML scene file
parser::Scene scene;
scene.loadFromXml(argv[1]);
int i,j,k,bar;
int width, height;
int cam_id;
int num_of_cameras;
parser::Vec3f temp_vec;
float temp;
num_of_cameras = scene.cameras.size();
for(cam_id = 0; cam_id < num_of_cameras; cam_id++)
{
parser::Camera cam;
parser::Vec3f e;//origin
parser::Vec3f w,v,u;//coordinate system
parser::Vec3f ray;
parser::Vec3f q;// top corner coordinate
parser::Vec3f s;// pixel coordinate
parser::Vec3f d;// d in the eq r(t)= e+dt
float pixel_width, pixel_height, p_height, p_width;
cam = scene.cameras[cam_id];
width = cam.image_width;
height = cam.image_height;
unsigned char* image = new unsigned char [width * height * 3];
e = cam.position;
w = vectorScalerMult(-1.0, cam.gaze);
v = cam.up;
u = crossProduct(v, w);
pixel_width = (cam.near_plane.y - cam.near_plane.x) / cam.image_width;
pixel_height = (cam.near_plane.w - cam.near_plane.z) / cam.image_height;
temp_vec = vectorScalerMult(cam.near_plane.x, u);//l.u
q = vectorSum(cam.position, temp_vec);//e + l.u
temp_vec = vectorScalerMult(cam.near_plane.w, v);//t.v
q = vectorSum(q, temp_vec); // += t.v
temp_vec = vectorScalerMult(cam.near_distance, cam.gaze);
q = vectorSum(q, temp_vec);
//q has the position of top left corner
i = 0; //pixels' color value
p_height = pixel_height*0.5;
p_width = pixel_width*0.5;
for(int y=0; y < cam.image_height; y++){
for(int x=0; x < cam.image_width; x++){
//first compute pixel coordinates;
parser::Ray r;
//IntersecType i_type = none;//whether it intersects or not
parser::Material material;
IntersectionData closest_obj_data;
parser::Vec3f reflection_point;//this is the point our ray r hits the object
parser::Vec3f normal_vector;//normal vector of our surface
parser::Vec3f light_ray;//light ray
float light_distance = -1;
closest_obj_data.obj_type = none;
closest_obj_data.t = __FLT_MAX__;
closest_obj_data.obj_id = -1;
closest_obj_data.face_id = -1;//if it is a mesh we need to find which face it is
temp_vec = vectorScalerMult(x*pixel_width+p_width, u);
s = vectorSum(q, temp_vec);//s = q + s_u.u
temp_vec = vectorScalerMult( -1.0 * (y*pixel_height + p_height), v);// - s_v . v
s = vectorSum(s, temp_vec);
//s = q + s_u.u - s_v.v
d = vectorSum(s, vectorScalerMult(-1.0, e));//s-e
r.e = e;
r.d = d;
//float intersect.t1, intersect.t2;//different solutions of the equation
//calculate spheres' closest
RayIntersecObj(scene,r,closest_obj_data);
//find the colour of that material
int numberOfLightSources = scene.point_lights.size();
parser::Vec3f center;
switch (closest_obj_data.obj_type){
case none:
parser::Vec3i bg;//backgroung
bg = scene.background_color;
image[i++] = bg.x;
image[i++] = bg.y;
image[i++] = bg.z;
break;
case sphere:
material = scene.materials[scene.spheres[closest_obj_data.obj_id].material_id - 1];
//TODO
//calculating Lambartian shading (i.e diffuse component)
//find the intersection point namely S
// r(t) = S
// n = (S-C)/|S-C|
//s-c
center = scene.vertex_data[scene.spheres[closest_obj_data.obj_id].center_vertex_id -1];
reflection_point = vectorSum(r.e, vectorScalerMult(closest_obj_data.t, r.d));//e + dt (S in your notes)
normal_vector = vectorSum(reflection_point, vectorScalerMult(-1, center));// S-E (see your notes)
normal_vector = normalize(normal_vector);//n (unit normal of the sphere at that point)
//now compute the light sources' normals and diffuse component for each source
parser::Vec3f L;
L.x=0;
L.y=0;
L.z=0;
for(j = 0; j<numberOfLightSources; j++)
{
parser::PointLight point_light = scene.point_lights[j];
float cosine_theta = 0;
light_ray = vectorSum(point_light.position, vectorScalerMult(-1,reflection_point));
light_distance = VECTOR_LENGTH(light_ray);//distance of light source
light_ray = normalize(light_ray);// now light ray is normalized
cosine_theta = dotProduct(light_ray, normal_vector);
cosine_theta = MAX(0, cosine_theta);
point_light.intensity = vectorScalerMult(1/(light_distance*light_distance), point_light.intensity );//calculate I/r^2
L = vectorSum(L, clampColor(vectorScalerMult(cosine_theta, elementViseMultiply(material.diffuse, point_light.intensity))));
}
L = clampColor(vectorSum(L, elementViseMultiply(scene.ambient_light, material.ambient)));//
image[i++] = L.x ;
image[i++] = L.y ;
image[i++] = L.z ;
//image[i++] = scene.ambient_light.x * material.ambient.x;//R
//image[i++] = scene.ambient_light.y * material.ambient.y;//G
//image[i++] = scene.ambient_light.z * material.ambient.z;//B
break;
case triangle:
material = scene.materials[scene.triangles[closest_obj_data.obj_id].material_id - 1];
image[i++] = scene.ambient_light.x * material.ambient.x;//R
image[i++] = scene.ambient_light.y * material.ambient.y;//G
image[i++] = scene.ambient_light.z * material.ambient.z;//B
break;
case mesh:
material = scene.materials[scene.meshes[closest_obj_data.obj_id].material_id - 1];
image[i++] = scene.ambient_light.x * material.ambient.x;//R
image[i++] = scene.ambient_light.y * material.ambient.y;//G
image[i++] = scene.ambient_light.z * material.ambient.z;//B
break;
// default:
// break;
}
}
}
//print to ppm
//write to file
write_ppm(scene.cameras[cam_id].image_name.c_str(), image, width, height);
}
//write_ppm("test.ppm", image, width, height);
}
// TODO
// add face id in return along as mesh id
void RayIntersecObj(const parser::Scene &scene,parser::Ray ray, IntersectionData &closest_obj_data){
int numOfSpheres = scene.spheres.size();
int numOfTriangles = scene.triangles.size();
int numOfMeshes = scene.meshes.size();
closest_obj_data.t = __FLT_MAX__;//closest objects intersection parameter
closest_obj_data.obj_id = -1;
closest_obj_data.obj_type = none;
for(int i = 0; i < numOfSpheres; i++){
parser::Intersection intersect = intersectRaySphere(scene,ray, i);
if(intersect.discriminant >= 0 ){
//meaning they intersect
if(intersect.t1 < intersect.t2 && intersect.t1 > 0 && intersect.t1 < closest_obj_data.t)
{
closest_obj_data.t = intersect.t1;
closest_obj_data.obj_id = i;
closest_obj_data.obj_type = sphere;
}
else if(intersect.t2 > 0 && intersect.t2 < closest_obj_data.t)
{
closest_obj_data.t = intersect.t2;
closest_obj_data.obj_id = i;
closest_obj_data.obj_type = sphere;
}
}
}
for(int i = 0; i<numOfTriangles;i++){
parser::Vec3f b_g_t = intersectRayFace(scene,ray,scene.triangles[i].indices);
if(b_g_t.z > 0 && b_g_t.z < closest_obj_data.t && b_g_t.x >= 0 && b_g_t.y >= 0){
if(b_g_t.y <= 1 && b_g_t.x <= (1 - b_g_t.y)){
closest_obj_data.t = b_g_t.z;
closest_obj_data.obj_id = i;
closest_obj_data.obj_type = triangle;
}
}
}
for (size_t j = 0; j < numOfMeshes; j++){
parser::Mesh currmesh = scene.meshes[j];
int numOfFaces = currmesh.faces.size();
for(int i = 0; i<numOfFaces;i++){
parser::Vec3f b_g_t = intersectRayFace(scene,ray,currmesh.faces[i]);
if(b_g_t.z > 0 && b_g_t.z < closest_obj_data.t && b_g_t.x >= 0 && b_g_t.y >= 0){
if(b_g_t.y <= 1 && b_g_t.x <= (1 - b_g_t.y)){
closest_obj_data.t = b_g_t.z;
closest_obj_data.obj_id = j;
closest_obj_data.obj_type = mesh;
closest_obj_data.face_id = i;
}
}
}
}
//return closest_obj_data.obj_id;
}
parser::Intersection intersectRaySphere(const parser::Scene &scene, parser::Ray &eye_ray, int sphere_id){
parser::Intersection intersect;
parser::Vec3f c;
float radius;
float temp;
c = scene.vertex_data[scene.spheres[sphere_id].center_vertex_id -1 ];
radius = scene.spheres[sphere_id].radius;
parser::Vec3f e_c; //e-c and d^2 is freq used so assign it to a variable
float d_sqr;
e_c = vectorSum(eye_ray.e, vectorScalerMult(-1.0, c));
d_sqr = dotProduct(eye_ray.d, eye_ray.d);
temp = dotProduct(eye_ray.d, e_c);
temp *= temp;//(d.(e-c))^2
intersect.discriminant = temp;
intersect.discriminant -= d_sqr * (dotProduct(e_c, e_c) - radius*radius);
if(intersect.discriminant >= 0)
{
intersect.t1 = -dotProduct(eye_ray.d, e_c);
intersect.t2 = intersect.t1;
intersect.t1 += sqrt(intersect.discriminant);//-b + sqrt delta
intersect.t1 /= d_sqr;
intersect.t2 -= sqrt(intersect.discriminant);//-b - sqrt delta
intersect.t2 /= d_sqr;
return intersect;
}
else
{
intersect.t1 = -1;
intersect.t2 = -1;
return intersect;
}
}
float Determinant(float matrix[][3]){
float det = 0;
det += matrix[0][0] * matrix[1][1] * matrix[2][2];
det += matrix[0][1] * matrix[1][2] * matrix[2][0];
det += matrix[0][2] * matrix[1][0] * matrix[2][1];
det -= matrix[2][0] * matrix[1][1] * matrix[0][2];
det -= matrix[2][1] * matrix[1][2] * matrix[0][0];
det -= matrix[2][2] * matrix[1][0] * matrix[0][1];
return det;
}
parser::Vec3f intersectRayFace(const parser::Scene &scene, parser::Ray &eye_ray, const parser::Face &face){
parser::Vec3f A,B,C,res;
float a,b,c,d,e,f,g,h,i,j,k,l;
float ei_hf,gf_di,dh_eg,ak_jb,jc_al,bl_kc;
float M = -1;
// if do not intersec
res.x = -1;
res.y = -1;
res.z = __FLT_MAX__;
A = scene.vertex_data[face.v0_id-1];
B = scene.vertex_data[face.v1_id-1];
C = scene.vertex_data[face.v2_id-1];
a = A.x - B.x;
b = A.y - B.y;
c = A.z - B.z;
d = A.x - C.x;
e = A.y - C.y;
f = A.z - C.z;
g = eye_ray.d.x;
h = eye_ray.d.y;
i = eye_ray.d.z;
j = A.x - eye_ray.e.x;
k = A.y - eye_ray.e.y;
l = A.z - eye_ray.e.z;
gf_di = g*f - d*i;
dh_eg = d*h - e*g;
ei_hf = e*i - h*f;
jc_al = j*c - a*l;
bl_kc = b*l - k*c;
ak_jb = a*k - j*b;
// TODO
// try parallization
M = a*ei_hf + b*gf_di + c*dh_eg;
if(M==0.0)
return res;
res.x = (j*ei_hf + k*gf_di + l*dh_eg)/M; // beta
res.y = (i*ak_jb + h*jc_al + g*bl_kc)/M; // gama
res.z = -(f*ak_jb + e*jc_al + d*bl_kc)/M; // t
return res;
}