-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.cpp
More file actions
executable file
·258 lines (203 loc) · 7.96 KB
/
Copy pathtrace.cpp
File metadata and controls
executable file
·258 lines (203 loc) · 7.96 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
#include <cstdio>
#include <GL/glut.h>
#include <cmath>
#include <cstdlib>
#include <time.h>
// GLM lib for matrix calculation
#include "include/glm/glm.hpp"
#include "include/glm/gtc/matrix_transform.hpp"
#include "include/glm/gtc/type_ptr.hpp"
#include "include/glm/gtx/rotate_vector.hpp"
#include "global.h"
#include "object.h"
//
// Global variables
//
extern int win_width;
extern int win_height;
extern glm::vec3 frame[WIN_HEIGHT][WIN_WIDTH];
extern float image_width;
extern float image_height;
extern glm::vec3 eye_pos;
extern float image_plane;
extern glm::vec3 background_clr;
extern glm::vec3 null_clr;
extern Object *scene;
// light 1 position and color
extern glm::vec3 light1;
extern glm::vec3 light1_ambient;
extern glm::vec3 light1_diffuse;
extern glm::vec3 light1_specular;
// global ambient term
extern glm::vec3 global_ambient;
// light decay parameters
extern float decay_a;
extern float decay_b;
extern float decay_c;
extern int shadow_on;
extern int reflect_on;
extern int step_max;
extern int refract_on;
extern int difref_on;
extern int antiAlias_on;
/////////////////////////////////////////////////////////////////////
inline float max(float a,float b){ return a>b?a:b; }
inline float random(float a, float b){
return ( (float) rand() / RAND_MAX * (b-a) + a );
}
const float precision = 0.00001;
/*********************************************************************
* Phong illumination
*********************************************************************/
glm::vec3 phong(glm::vec3 point, glm::vec3 viewDir, glm::vec3 surf_norm, Object *obj) {
// ambient
glm::vec3 mat_ambient;
mat_ambient = obj->GetAmbient(point);
glm::vec3 ambient = light1_ambient * mat_ambient ;
// calc decay factor
float dist = glm::distance(light1, point);
float decay = 1 / ( decay_a + decay_b * dist + decay_c * dist * dist );
//printf("decay : %f\n",decay);
// detect shadow
glm::vec3 lightDir = glm::normalize(light1 - point);
glm::vec3 hit;
bool shadow = false;
if( shadow_on ) {
if ( intersectScene(point, lightDir, &hit, obj->index) != NULL )
shadow = true;
else if( obj->Intersect(point,lightDir,&hit, false) > precision )
shadow = true;
}
// diffuse
surf_norm = glm::normalize(surf_norm);
//printf(" before GetDiffuse\n");
glm::vec3 mat_diffuse = obj->GetDiffuse(point);
//printf(" after GetDiffuse\n");
glm::vec3 diffuse = decay * (light1_diffuse * mat_diffuse ) * max(glm::dot(surf_norm, lightDir),0) ;
//printf(" after calc diffuse\n");
// specular
//glm::vec3 reflectDir = glm::normalize(glm::rotate(lightDir, glm::radians(180.0f), surf_norm));
glm::vec3 reflectDir = 2 * glm::dot(surf_norm,lightDir) * surf_norm - lightDir ;
reflectDir = glm::normalize(reflectDir);
viewDir = glm::normalize(viewDir);
float reflectTerm = max(glm::dot(reflectDir, viewDir),0);
glm::vec3 specular = decay * ( light1_specular * obj->mat_specular) *
(float) pow( reflectTerm, obj->mat_shineness) ;
//if(shadow) specular = glm::vec3(0);
// calc color
glm::vec3 color = global_ambient * mat_ambient + ambient;
if(!shadow)
color += diffuse + specular;
//color = specular;
//color = global_ambient + ambient ;
return color;
}
/************************************************************************
* This is the recursive ray tracer
************************************************************************/
glm::vec3 recursive_ray_trace(glm::vec3 eye, glm::vec3 ray,int ignore, int step) {
Object* S = NULL;
glm::vec3 hit;
S = intersectScene(eye, ray, &hit, ignore);
//printf("%d : after intersect scene (type: '%c')\n", step, S==NULL?'N':S->type);
glm::vec3 color;
if(S == NULL)
color = background_clr;
else {
//color = glm::vec3(1.0,1.0,1.0);
glm::vec3 viewDir = glm::normalize(eye - hit);
glm::vec3 surf_norm = S->GetNormal(hit);
//printf(" after get normal\n");
color = phong(hit,viewDir, surf_norm, S );
//printf(" after phong\n");
if(reflect_on && step < step_max){
//printf(" enter reflect\n");
glm::vec3 reflectDir = glm::normalize(glm::rotate(viewDir, glm::radians(180.0f), surf_norm));
glm::vec3 color_rf = recursive_ray_trace(hit, reflectDir, S->index, step+1);
color += color_rf * S->reflectance ;
//printf(" exit reflect\n");
}
if(refract_on && step < step_max && S->refract ){
glm::vec3 outRay, outPoint;
if(S->Refract(ray, hit, &outRay, &outPoint)){
glm::vec3 color_rfr = recursive_ray_trace(outPoint, outRay, S->index, step+1);
color += color_rfr * S->refractance;
}
}
if(difref_on && step < 2){
for(int i=0;i<DIFFUSE_RAYS;i++){
glm::vec3 difrefDir = glm::normalize(glm::rotate(viewDir, glm::radians(180.0f), surf_norm));
glm::vec3 axis = glm::cross(viewDir, surf_norm);
float angle1 = random(-5.0f,0.0f);
difrefDir = glm::rotate(difrefDir, glm::radians(angle1), axis);
float angle2 = random(-5.0f,5.0f);
difrefDir = glm::rotate(difrefDir, glm::radians(angle2), surf_norm);
difrefDir = glm::normalize(difrefDir);
glm::vec3 color_difref = recursive_ray_trace(hit, difrefDir, S->index, step+1);
color += color_difref * float(0.1);
}
}
}
return color;
}
/*********************************************************************
* This function traverses all the pixels and cast rays. It calls the
* recursive ray tracer and assign return color to frame
*********************************************************************/
void ray_trace() {
int i, j;
float x_grid_size = image_width / float(win_width);
float y_grid_size = image_height / float(win_height);
float x_start = -0.5 * image_width;
float y_start = -0.5 * image_height;
glm::vec3 ret_color;
glm::vec3 cur_pixel_pos;
// ray is cast through center of pixel
cur_pixel_pos.x = x_start + 0.5 * x_grid_size;
cur_pixel_pos.y = y_start + 0.5 * y_grid_size;
cur_pixel_pos.z = image_plane;
float antiAlias[5][2] = {
{-0.25, +0.25},
{+0.25, +0.25},
{0,0},
{-0.25, -0.25},
{+0.25, -0.25}
};
srand(time(NULL));
for (i=0; i<win_height; i++) {
for (j=0; j<win_width; j++) {
ret_color = glm::vec3(0,0,0);
if(antiAlias_on){
for(int k=0;k<5;k++){
glm::vec3 pixel_pos = cur_pixel_pos + glm::vec3(antiAlias[k][0] * x_grid_size,antiAlias[k][1] * y_grid_size,0);
glm::vec3 ray = glm::normalize(pixel_pos - eye_pos);
ret_color += recursive_ray_trace(eye_pos,ray,0,0);
}
ret_color /= 5;
}
else{
//ray = get_vec(eye_pos, cur_pixel_pos);
glm::vec3 ray = cur_pixel_pos - eye_pos;
//normalize(&ray);
ray = glm::normalize(ray);
//
// You need to change this!!!
//
ret_color = recursive_ray_trace(eye_pos,ray,0,0);
//else ret_color = background_clr; // just background for now
// Parallel rays can be cast instead using below
//
// ray.x = ray.y = 0;
// ray.z = -1.0;
// ret_color = recursive_ray_trace(cur_pixel_pos, ray, 1);
// Checkboard for testing
// glm::vec3 clr = glm::vec3(float(i/32), 0, float(j/32));
//ret_color = clr;
}
frame[i][j] = ret_color;
cur_pixel_pos.x += x_grid_size;
}
cur_pixel_pos.y += y_grid_size;
cur_pixel_pos.x = x_start;
}
}