-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTracer.cpp
More file actions
356 lines (300 loc) · 10 KB
/
Copy pathRayTracer.cpp
File metadata and controls
356 lines (300 loc) · 10 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
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
// ========================================================================
// COSC 363 Computer Graphics
// Raytracing Assignment 2
// Name: Mathew Hylkema
// ID: 35742180
// ========================================================================
#include <iostream>
#include <cmath>
#include <vector>
#include "Vector.h"
#include "Sphere.h"
#include "Color.h"
#include "Object.h"
#include "Plane.h"
#include <GL/glut.h>
//--added--//
#include "Cone.h"
#include "Cylinder.h"
using namespace std;
const float WIDTH = 20.0;
const float HEIGHT = 20.0;
const float EDIST = 40.0;
const int PPU = 50;
const int MAX_STEPS = 4;
const float XMIN = -WIDTH * 0.5;
const float XMAX = WIDTH * 0.5;
const float YMIN = -HEIGHT * 0.5;
const float YMAX = HEIGHT * 0.5;
bool refractiveBool = false;
vector<Object*> sceneObjects;
Vector light = Vector(3, 19, -20.0);
Color backgroundCol = Color::GRAY;
//A useful struct
struct PointBundle
{
Vector point;
int index;
float dist;
};
/*
* This function compares the given ray with all objects in the scene
* and computes the closest point of intersection.
*/
PointBundle closestPt(Vector pos, Vector dir)
{
Vector point(0, 0, 0);
float min = 10000.0;
PointBundle out = {point, -1, 0.0};
for(unsigned int i = 0; i < sceneObjects.size(); i++)
{
float t = sceneObjects[i]->intersect(pos, dir);
if(t > 0) //Intersects the object
{
point = pos + dir*t;
if(t < min)
{
out.point = point;
out.index = i;
out.dist = t;
min = t;
}
}
}
return out;
}
/*
* Computes the colour value obtained by tracing a ray.
* If reflections and refractions are to be included, then secondary rays will
* have to be traced from the point, by converting this method to a recursive
* procedure.
*/
Color trace(Vector pos, Vector dir, int step)
{
PointBundle q = closestPt(pos, dir);
Color colorSum;
Color refractCol = Color::BLACK;
if(q.index == -1) return backgroundCol; //no intersection
Color col = sceneObjects[q.index]->getColor(); //Object's colour
float non_color = 1.0;
float Object = 1.01;
//--vectors--//
Vector n = sceneObjects[q.index]->normal(q.point);
Vector l = light - q.point;
float lightDist = l.length();
l.normalise();
float lDotn = l.dot(n);
//----Object Intersection vectors----//
Vector r = ((n * 2) * lDotn) - l;
r.normalise();
Vector viewVector(-dir.x, -dir.y, -dir.z);
float rDotv = r.dot(viewVector);
float spec;
if(rDotv < 0) spec = 0.0;
else spec = pow(rDotv, 10);;
PointBundle s = closestPt(q.point, l);
//--Reflection vectors--//
float vDotn = viewVector.dot(n);
Vector reflectionVector = ((n*2) * vDotn) - viewVector;
reflectionVector.normalise();
//--Textured Floor--//
if (q.index == 5) {
if ((int(q.point.x) - int(q.point.z)) % 2 == 1)
col = Color::BLACK;
else
col = Color::WHITE;
}
if (q.index == 2) {
if (int(q.point.x) % 7 == 0)
col = Color::RED;
else if (int(q.point.x) % 7 == 1)
col = Color::ORANGE;
else if (int(q.point.x) % 7 == 2)
col = Color::YELLOW;
else if (int(q.point.x) % 7 == 3)
col = Color::GREEN;
else if (int(q.point.x) % 7 == 4)
col = Color::BLUE;
else if (int(q.point.x) % 7 == 5)
col = Color(0.3f,0,0.5);
else col = Color(0.55,0,1);
}
//--Refraction--//
if (q.index == 0 && step < MAX_STEPS) {
float ratio = non_color/Object;
float cos;
if (refractiveBool) {
ratio = Object/non_color;
cos = sqrt(1 - ((ratio) * (ratio)) * (1 - (dir.dot(n) * dir.dot(n))));
n = n * (-1);
refractiveBool = !refractiveBool;
} else {
cos = sqrt(1 - ((ratio) * (ratio)) * (1 - (dir.dot(n) * dir.dot(n))));
refractiveBool = !refractiveBool;
}
Vector refractionVector = dir * ratio - n * ((ratio) * (dir.dot(n)) + cos);
refractionVector.normalise();
refractCol = trace(q.point, refractionVector, step+1);
}
//--Background--//
if((s.index>-1 && s.dist < lightDist)|| lDotn <= 0)
colorSum = col.phongLight(backgroundCol, 0.0, 0.0);
//--Shadow--//
else {
if (s.dist > lightDist) {
colorSum = col.phongLight(backgroundCol, lDotn, 0);
} else {
colorSum = col.phongLight(backgroundCol, lDotn, spec);
}
}
//--Reflection--//
// Spheres
if(((q.index == 1) || (q.index == 3) || (q.index == -1)) && step < MAX_STEPS)
{
float reflCoeff = 1;
Color reflectionCol = trace(q.point, reflectionVector, step+1);
colorSum.combineColor(reflectionCol, reflCoeff);
}
// Roof
if(((q.index == 9)) && step < MAX_STEPS)
{
float reflCoeff = 0.7;
Color reflectionCol = trace(q.point, reflectionVector, step+1);
colorSum.combineColor(reflectionCol, reflCoeff);
}
// Floor
if(((q.index == 5)) && step < MAX_STEPS)
{
float reflCoeff = 0.5;
Color reflectionCol = trace(q.point, reflectionVector, step+1);
colorSum.combineColor(reflectionCol, reflCoeff);
}
colorSum.combineColor(refractCol, 1);
return colorSum;
}
Color Average(Vector eye, Vector dir, float pixel, float x1, float y1)
{
int size = 4; // Sampling size (Between 4 and 8)
float R = 0, G = 0, B = 0;
float part_pixel = pixel/size;
Color PixelSet[size];
Vector direction;
int object = 0;
for (int x=0; x < size; x++) {
for (int y=0; y <size; y++) {
if (((x % 2) == 1) && ((y % 2) == 1)) {
direction = Vector(x1 + (part_pixel * x), y1 + (part_pixel * y), -EDIST);
direction.normalise();
PixelSet[object] = trace(eye, direction, 1);
object++;
}
}
}
for (int i=0; i<size; i++){
R += PixelSet[i].r;
G += PixelSet[i].g;
B += PixelSet[i].b;
}
Color col(R/size,G/size,B/size);
return col;
}
//---The main display module -----------------------------------------------------------
// In a ray tracing application, it just displays the ray traced image by drawing
// each pixel as quads.
//---------------------------------------------------------------------------------------
void display()
{
int widthInPixels = (int)(WIDTH * PPU);
int heightInPixels = (int)(HEIGHT * PPU);
float pixelSize = 1.0/PPU;
float halfPixel = pixelSize/2.0;
float x1, y1, xc, yc;
Vector eye(10, 10, 0.);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS); //Each pixel is a quad.
for(int i = 0; i < widthInPixels; i++) //Scan every "pixel"
{
x1 = XMIN + i*pixelSize;
xc = x1 + halfPixel;
for(int j = 0; j < heightInPixels; j++)
{
y1 = YMIN + j*pixelSize;
yc = y1 + halfPixel;
Vector dir(xc, yc, -EDIST); //direction of the primary ray
//--Super Sampling--//
Color col_ss = Average(eye,dir,pixelSize,x1,y1);
Color col = col_ss;
// Used if Supersampling isnt in use.
//dir.normalise(); //Normalise this direction
//Color col = trace (eye, dir, 1); //Trace the primary ray and get the colour value
glColor3f(col.r, col.g, col.b);
glVertex2f(x1, y1); //Draw each pixel with its color value
glVertex2f(x1 + pixelSize, y1);
glVertex2f(x1 + pixelSize, y1 + pixelSize);
glVertex2f(x1, y1 + pixelSize);
}
}
glEnd();
glFlush();
}
void initialize()
{
//--Glass Sphere--//
Sphere *sphere0 = new Sphere(Vector(10, 6, -50), 3.0, Color::BLACK); // 11, 3, -60
sceneObjects.push_back(sphere0);
//--Spheres--//
Sphere *sphere1 = new Sphere(Vector(12, 8, -92), 8.0, Color::BLUE);
sceneObjects.push_back(sphere1);
Sphere *sphere2 = new Sphere(Vector(5, 5, -75), 5.0, Color::GREEN);
sceneObjects.push_back(sphere2);
Sphere *sphere3 = new Sphere(Vector(6, 2, -55), 2.0, Color::BLACK);
sceneObjects.push_back(sphere3);
//--Cone--//
Cone *cone4 = new Cone(Vector(16, 8, -52), 1.5, 5.0, Color::RED);
sceneObjects.push_back(cone4);
//--Ground--//
Plane *plane5 = new Plane(Vector(0, 0, -40), Vector(20, 0, -40), Vector(20, 0, -100), Vector(0, 0, -100), Color::WHITE);
sceneObjects.push_back(plane5); //Ground
Plane *plane6 = new Plane(Vector(0, 0, -100), Vector(20, 0, -100), Vector(20, 20, -100), Vector(0, 20, -100), Color::PURPLE);
sceneObjects.push_back(plane6); //Background
Plane *plane7 = new Plane(Vector(0, 0, -40), Vector(0, 0, -100), Vector(0, 20, -100), Vector(0, 20, -40), Color::PURPLE);
sceneObjects.push_back(plane7); //Leftside
Plane *plane8 = new Plane(Vector(20, 0, -100), Vector(20, 0, -40), Vector(20, 20, -40), Vector(20, 20, -100), Color::PURPLE);
sceneObjects.push_back(plane8); //Rightside
Plane *plane9 = new Plane(Vector(0, 20, -100), Vector(20, 20, -100), Vector(20, 20, -40), Vector(0, 20, -40), Color::BLACK);
sceneObjects.push_back(plane9); //Roof
//--Box--//
Plane *plane10 = new Plane(Vector(14, 4, -50), Vector(18, 4, -50), Vector(18, 4, -54), Vector(14, 4, -54), Color::YELLOW);
sceneObjects.push_back(plane10); //Top side
Plane *plane11 = new Plane(Vector(14, 0, -54), Vector(14, 0, -50), Vector(14, 4, -50), Vector(14, 4, -54), Color::YELLOW);
sceneObjects.push_back(plane11); //Left side
Plane *plane12 = new Plane(Vector(14, 0, -50), Vector(18, 0, -50), Vector(18, 4, -50), Vector(14, 4, -50), Color::YELLOW);
sceneObjects.push_back(plane12); //front side
Plane *plane13 = new Plane(Vector(18, 0, -50), Vector(18, 0, -54), Vector(18, 4, -54), Vector(18, 4, -50), Color::YELLOW);
sceneObjects.push_back(plane13); //right side
Plane *plane14 = new Plane(Vector(18, 0, -54), Vector(14, 0, -54), Vector(14, 4, -54), Vector(18, 4, -54), Color::YELLOW);
sceneObjects.push_back(plane14); //back side
//--Cylinder--//
Cylinder *cylinder15 = new Cylinder(Vector(16, 4, -52), 1.5, 4.0, Color::GREEN);
sceneObjects.push_back(cylinder15);
//--Sphere--//
Sphere *sphere16 = new Sphere(Vector(16, 14.5, -52), 2.0, Color::WHITE);
sceneObjects.push_back(sphere16);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(XMIN, XMAX, YMIN, YMAX);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0, 0, 0, 1);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(20, 20);
glutCreateWindow("Raytracing - Matt Hylkema");
glutDisplayFunc(display);
initialize();
glutMainLoop();
return 0;
}