-
Notifications
You must be signed in to change notification settings - Fork 0
/
RayTracer.cpp
293 lines (238 loc) · 8.26 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
// ========================================================================
// COSC 363 Computer Graphics Lab07
// A simple ray tracer
// ========================================================================
#include <iostream>
#include <cmath>
#include <vector>
#include "Vector.h"
#include "Cylinder.h"
#include "Sphere.h"
#include "Plane.h"
#include "Color.h"
#include "Object.h"
#include "TextureBMP.h"
#include <GL/glut.h>
using namespace std;
const float WIDTH = 20.0;
const float HEIGHT = 20.0;
const float EDIST = 40.0;
const int PPU = 30; //Total 600x600 pixels
const int MAX_STEPS = 5;
const float XMIN = -WIDTH * 0.5;
const float XMAX = WIDTH * 0.5;
const float YMIN = -HEIGHT * 0.5;
const float YMAX = HEIGHT * 0.5;
const float PI = 3.14159265359;
const float DIAMOND = 2.4;
const float AIR = 1;
vector<Object*> sceneObjects;
Vector light;
Color backgroundCol;
TextureBMP earth;
TextureBMP checker;
//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)
{
Color colorSum;
PointBundle q = closestPt(pos, dir);
Vector lightVector = light - q.point;
float lightDist = lightVector.length();
lightVector.normalise();
PointBundle s = closestPt(q.point, lightVector);
if(q.index == -1) return backgroundCol; //no intersection
Color col = sceneObjects[q.index]->getColor(); //Object's colour
Vector n = sceneObjects[q.index]->normal(q.point);
Vector l = light - q.point;
Vector v(-dir.x, -dir.y, -dir.z); //View vector;
l.normalise();
float lDotn = l.dot(n);
if (q.index == 2 && step < MAX_STEPS){
float u = asin(n.x) / PI + 0.5;
float v = asin(n.y) / PI + 0.5;
col = earth.getColorAt(u,v);
}
if (q.index == 3 && step < MAX_STEPS) {
float u = (int) ((q.point.x - 10) / 3) % 2;
float v = (int) ((q.point.z + 20) / 3) % 2;
if ((u && v) || (!u && !v)) {
col = Color::WHITE;
} else {
col = Color::BLACK;
}
}
//////////////////// BROKEN REFRACTION ////////////////////////////////////
////////////// UNCOMMENT IF YOU LIKE BROKEN STUFF /////////////////////////
// if (q.index == 4 && step < MAX_STEPS) {
// Vector entryPt = q.point;
//
// float cosThetaT1 = sqrt(1 - (pow(AIR / DIAMOND, 2) * (1 - pow(dir.dot(n), 2)) ));
// Vector g1Dir = (dir * (AIR/DIAMOND)) - n * ((AIR/DIAMOND) * dir.dot(n) + cosThetaT1);
//
// Vector exitPt = closestPt(entryPt, g1Dir).point;
// Vector n2 = sceneObjects[q.index]->normal(exitPt);
//
// float cosThetaT2 = sqrt(1 - (pow(DIAMOND / AIR, 2) * (1 - pow(g1Dir.dot(n2), 2)) ));
// Vector g2Dir = (g1Dir * (DIAMOND / AIR)) - n2 * ((AIR/DIAMOND) * dir.dot(n2) + cosThetaT2);
// col = trace(exitPt, g2Dir, step + 1);
// }
if (lDotn <= 0) {
colorSum = col.phongLight(backgroundCol, 0.0, 0.0);
} else if (s.index>-1 && s.dist < lightDist) {
colorSum = col.phongLight(backgroundCol, 0.0, 0.0);
} else {
Vector r = ((n * 2) * lDotn) - l; // r = 2(L.n)n – L
r.normalise();
float rDotv = r.dot(v);
float spec;
if(rDotv < 0) {
spec = 0.0;
} else {
spec = pow(rDotv, 5); //Phong exponent = 10
}
colorSum = col.phongLight(backgroundCol, lDotn, spec);
}
if (q.index == 0 && step < MAX_STEPS) {
float nDotV = n.dot(v);
Vector reflectionVector = ((n*2) * (nDotV)) - v;
float reflCoeff = 0.4;
Color reflectionCol = trace(q.point, reflectionVector, step+1);
colorSum.combineColor(reflectionCol, reflCoeff);
}
return colorSum;
}
//---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 halfPixelSize = pixelSize/2.0;
float x1, y1, xc, yc;
Vector eye(0., 0., 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 + halfPixelSize;
for(int j = 0; j < heightInPixels; j++)
{
y1 = YMIN + j*pixelSize;
yc = y1 + halfPixelSize;
Vector dir(xc, yc, -EDIST); //direction of the primary ray
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()
{
//Iniitialize background colour and light's position
backgroundCol = Color::GRAY;
light = Vector(-30.0, 50.0, -5.0);
earth = TextureBMP((char*)"Earth.bmp");
Sphere *sphere1 = new Sphere(Vector(-5, 6, -50), 2.0, Color::RED);
Sphere *sphere2 = new Sphere(Vector(0, 0, -55), 6.0, Color::GRAY);
Sphere *sphere3 = new Sphere(Vector(5, 4, -45), 2.75, Color::WHITE);
Cylinder *cyl = new Cylinder(Vector(7,-10,-45), 2, 5, Color::GREEN);
Sphere *sphere4 = new Sphere(Vector(7,-3,-45), 3, Color::BLUE);
Plane *plane = new Plane(Vector(-40, -10, -40), Vector(40, -10, -40),
Vector(40., -10, -120), Vector(-40., -10, -120), Color::WHITE);
/*
A Lazy cuboid
Bottom, left and back faces can be removed to improve performance, as they're not
visible from the viewport or in the reflection
*/
Plane *cubeBottom = new Plane(Vector(-5, -10, -45), Vector(0., -10, -45),
Vector(0., -10, -55), Vector(-5., -10, -55), Color::RED);
Plane *cubeLeft = new Plane(Vector(-5, -10, -55), Vector(-5, -10, -45),
Vector(-5, -7, -45), Vector(-5, -7, -55), Color::BLUE);
Plane *cubeBack = new Plane(Vector(-5, -10, -55), Vector(-0, -10, -55),
Vector(0, -7, -55), Vector(-5, -7, -55), Color::RED);
Plane *cubeFront = new Plane(Vector(-5, -10, -45), Vector(-0, -10, -45),
Vector(0, -7, -45), Vector(-5, -7, -45), Color::BLUE);
Plane *cubeTop = new Plane(Vector(-5, -7, -45), Vector(0., -7, -45),
Vector(0., -7, -55), Vector(-5., -7, -55), Color::BLUE);
Plane *cubeRight = new Plane(Vector(0, -10, -55), Vector(0, -10, -45),
Vector(0, -7, -45), Vector(0, -7, -55), Color::RED);
sceneObjects.push_back(sphere2); // 0
sceneObjects.push_back(sphere1); // 1
sceneObjects.push_back(sphere3); // 2
sceneObjects.push_back(plane); // 3
sceneObjects.push_back(sphere4); // 4
sceneObjects.push_back(cyl); // 5
sceneObjects.push_back(cubeBottom);
sceneObjects.push_back(cubeLeft);
sceneObjects.push_back(cubeBack);
sceneObjects.push_back(cubeFront);
sceneObjects.push_back(cubeTop);
sceneObjects.push_back(cubeRight);
//The following are OpenGL functions used only for drawing the pixels
//of the ray-traced scene.
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(600, 600);
glutInitWindowPosition(20, 20);
glutCreateWindow("Raytracing");
glutDisplayFunc(display);
initialize();
glutMainLoop();
return 0;
}