-
Notifications
You must be signed in to change notification settings - Fork 0
/
raytracer.cpp
336 lines (283 loc) · 9.1 KB
/
raytracer.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
/***********************************************************
Starter code for Assignment 3
This code was originally written by Jack Wang for
CSC418, SPRING 2005
Implementations of functions in raytracer.h,
and the main function which specifies the
scene to be rendered.
***********************************************************/
#include "raytracer.h"
#include "bmp_io.h"
#include <cmath>
#include <iostream>
#include <cstdlib>
Raytracer::Raytracer() : _lightSource(NULL) {
_root = new SceneDagNode();
}
Raytracer::~Raytracer() {
delete _root;
}
SceneDagNode* Raytracer::addObject( SceneDagNode* parent,
SceneObject* obj, Material* mat ) {
SceneDagNode* node = new SceneDagNode( obj, mat );
node->parent = parent;
node->next = NULL;
node->child = NULL;
// Add the object to the parent's child list, this means
// whatever transformation applied to the parent will also
// be applied to the child.
if (parent->child == NULL) {
parent->child = node;
}
else {
parent = parent->child;
while (parent->next != NULL) {
parent = parent->next;
}
parent->next = node;
}
return node;;
}
LightListNode* Raytracer::addLightSource( LightSource* light ) {
LightListNode* tmp = _lightSource;
_lightSource = new LightListNode( light, tmp );
return _lightSource;
}
void Raytracer::rotate( SceneDagNode* node, char axis, double angle ) {
Matrix4x4 rotation;
double toRadian = 2*M_PI/360.0;
int i;
for (i = 0; i < 2; i++) {
switch(axis) {
case 'x':
rotation[0][0] = 1;
rotation[1][1] = cos(angle*toRadian);
rotation[1][2] = -sin(angle*toRadian);
rotation[2][1] = sin(angle*toRadian);
rotation[2][2] = cos(angle*toRadian);
rotation[3][3] = 1;
break;
case 'y':
rotation[0][0] = cos(angle*toRadian);
rotation[0][2] = sin(angle*toRadian);
rotation[1][1] = 1;
rotation[2][0] = -sin(angle*toRadian);
rotation[2][2] = cos(angle*toRadian);
rotation[3][3] = 1;
break;
case 'z':
rotation[0][0] = cos(angle*toRadian);
rotation[0][1] = -sin(angle*toRadian);
rotation[1][0] = sin(angle*toRadian);
rotation[1][1] = cos(angle*toRadian);
rotation[2][2] = 1;
rotation[3][3] = 1;
break;
}
if (i == 0) {
node->trans = node->trans*rotation;
angle = -angle;
}
else {
node->invtrans = rotation*node->invtrans;
}
}
}
void Raytracer::translate( SceneDagNode* node, Vector3D trans ) {
Matrix4x4 translation;
translation[0][3] = trans[0];
translation[1][3] = trans[1];
translation[2][3] = trans[2];
node->trans = node->trans*translation;
translation[0][3] = -trans[0];
translation[1][3] = -trans[1];
translation[2][3] = -trans[2];
node->invtrans = translation*node->invtrans;
}
void Raytracer::scale( SceneDagNode* node, Point3D origin, double factor[3] ) {
Matrix4x4 scale;
scale[0][0] = factor[0];
scale[0][3] = origin[0] - factor[0] * origin[0];
scale[1][1] = factor[1];
scale[1][3] = origin[1] - factor[1] * origin[1];
scale[2][2] = factor[2];
scale[2][3] = origin[2] - factor[2] * origin[2];
node->trans = node->trans*scale;
scale[0][0] = 1/factor[0];
scale[0][3] = origin[0] - 1/factor[0] * origin[0];
scale[1][1] = 1/factor[1];
scale[1][3] = origin[1] - 1/factor[1] * origin[1];
scale[2][2] = 1/factor[2];
scale[2][3] = origin[2] - 1/factor[2] * origin[2];
node->invtrans = scale*node->invtrans;
}
Matrix4x4 Raytracer::initInvViewMatrix( Point3D eye, Vector3D view,
Vector3D up ) {
Matrix4x4 mat;
Vector3D w;
view.normalize();
up = up - up.dot(view)*view;
up.normalize();
w = view.cross(up);
mat[0][0] = w[0];
mat[1][0] = w[1];
mat[2][0] = w[2];
mat[0][1] = up[0];
mat[1][1] = up[1];
mat[2][1] = up[2];
mat[0][2] = -view[0];
mat[1][2] = -view[1];
mat[2][2] = -view[2];
mat[0][3] = eye[0];
mat[1][3] = eye[1];
mat[2][3] = eye[2];
return mat;
}
void Raytracer::traverseScene( SceneDagNode* node, Ray3D& ray ) {
SceneDagNode *childPtr;
// Applies transformation of the current node to the global
// transformation matrices.
_modelToWorld = _modelToWorld*node->trans;
_worldToModel = node->invtrans*_worldToModel;
if (node->obj) {
// Perform intersection.
if (node->obj->intersect(ray, _worldToModel, _modelToWorld)) {
ray.intersection.mat = node->mat;
}
}
// Traverse the children.
childPtr = node->child;
while (childPtr != NULL) {
traverseScene(childPtr, ray);
childPtr = childPtr->next;
}
// Removes transformation of the current node from the global
// transformation matrices.
_worldToModel = node->trans*_worldToModel;
_modelToWorld = _modelToWorld*node->invtrans;
}
void Raytracer::computeShading( Ray3D& ray ) {
LightListNode* curLight = _lightSource;
for (;;) {
if (curLight == NULL) break;
// Each lightSource provides its own shading function.
// Implement shadows here if needed.
curLight->light->shade(ray);
curLight = curLight->next;
}
}
void Raytracer::initPixelBuffer() {
int numbytes = _scrWidth * _scrHeight * sizeof(unsigned char);
_rbuffer = new unsigned char[numbytes];
_gbuffer = new unsigned char[numbytes];
_bbuffer = new unsigned char[numbytes];
for (int i = 0; i < _scrHeight; i++) {
for (int j = 0; j < _scrWidth; j++) {
_rbuffer[i*_scrWidth+j] = 0;
_gbuffer[i*_scrWidth+j] = 0;
_bbuffer[i*_scrWidth+j] = 0;
}
}
}
void Raytracer::flushPixelBuffer( char *file_name ) {
bmp_write( file_name, _scrWidth, _scrHeight, _rbuffer, _gbuffer, _bbuffer );
delete _rbuffer;
delete _gbuffer;
delete _bbuffer;
}
Colour Raytracer::shadeRay( Ray3D& ray ) {
Colour col(0.0, 0.0, 0.0);
traverseScene(_root, ray);
// Don't bother shading if the ray didn't hit
// anything.
if (!ray.intersection.none) {
computeShading(ray);
col = ray.col;
}
// You'll want to call shadeRay recursively (with a different ray,
// of course) here to implement reflection/refraction effects.
return col;
}
void Raytracer::render( int width, int height, Point3D eye, Vector3D view,
Vector3D up, double fov, char* fileName ) {
Matrix4x4 viewToWorld;
_scrWidth = width;
_scrHeight = height;
double factor = (double(height)/2)/tan(fov*M_PI/360.0);
initPixelBuffer();
viewToWorld = initInvViewMatrix(eye, view, up);
// Construct a ray for each pixel.
for (int i = 0; i < _scrHeight; i++) {
for (int j = 0; j < _scrWidth; j++) {
// Sets up ray origin and direction in view space,
// image plane is at z = -1.
Point3D origin(0, 0, 0);
Point3D imagePlane;
imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor;
imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor;
imagePlane[2] = -1;
// TODO: Convert ray to world space and call
// shadeRay(ray) to generate pixel colour.
Ray3D ray;
ray.origin = viewToWorld*imagePlane;
ray.dir = ray.origin - eye;
ray.dir.normalize();
Colour col = shadeRay(ray);
_rbuffer[i*width+j] = int(col[0]*255);
_gbuffer[i*width+j] = int(col[1]*255);
_bbuffer[i*width+j] = int(col[2]*255);
}
}
flushPixelBuffer(fileName);
}
int main(int argc, char* argv[])
{
// Build your scene and setup your camera here, by calling
// functions from Raytracer. The code here sets up an example
// scene and renders it from two different view points, DO NOT
// change this if you're just implementing part one of the
// assignment.
Raytracer raytracer;
int width = 320;
int height = 240;
if (argc == 3) {
width = atoi(argv[1]);
height = atoi(argv[2]);
}
// Camera parameters.
Point3D eye(0, 0, 1);
Vector3D view(0, 0, -1);
Vector3D up(0, 1, 0);
double fov = 60;
// Defines a material for shading.
Material gold( Colour(0.3, 0.3, 0.3), Colour(0.75164, 0.60648, 0.22648),
Colour(0.628281, 0.555802, 0.366065),
51.2 );
Material jade( Colour(0, 0, 0), Colour(0.54, 0.89, 0.63),
Colour(0.316228, 0.316228, 0.316228),
12.8 );
// Defines a point light source.
raytracer.addLightSource( new PointLight(Point3D(0, 0, 5),
Colour(0.9, 0.9, 0.9) ) );
// Add a unit square into the scene with material mat.
SceneDagNode* sphere = raytracer.addObject( new UnitSphere(), &gold );
SceneDagNode* plane = raytracer.addObject( new UnitSquare(), &jade );
// Apply some transformations to the unit square.
double factor1[3] = { 1.0, 2.0, 1.0 };
double factor2[3] = { 6.0, 6.0, 6.0 };
raytracer.translate(sphere, Vector3D(0, 0, -5));
raytracer.rotate(sphere, 'x', -45);
raytracer.rotate(sphere, 'z', 45);
raytracer.scale(sphere, Point3D(0, 0, 0), factor1);
raytracer.translate(plane, Vector3D(0, 0, -7));
raytracer.rotate(plane, 'z', 45);
raytracer.scale(plane, Point3D(0, 0, 0), factor2);
// Render the scene, feel free to make the image smaller for
// testing purposes.
raytracer.render(width, height, eye, view, up, fov, "view1.bmp");
// Render it from a different point of view.
Point3D eye2(4, 2, 1);
Vector3D view2(-4, -2, -6);
raytracer.render(width, height, eye2, view2, up, fov, "view2.bmp");
return 0;
}