-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray_tracer.cpp
303 lines (261 loc) · 12.7 KB
/
ray_tracer.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
// Ray Tracer: ray_tracer.cpp
//
// Author: Wesley Hauwiller
//
// Description: Backbone of the Ray Tracer. Takes a scene description
// and file output writer and generates the color data
// for each pixel in the image by casting a ray though
// the center of each pixel in the image plane and
// returning the color data of the closest geometry
// it intersects.
//
// Copyright (C) 2016 whauwiller.blogspot.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// A full copy of the GNU General Public License may be found at
// <http://www.gnu.org/licenses/>.
//
#include "ray_tracer.h"
#define MAX_RAY_DEPTH 2
RayTracer::RayTracer(){}
RayTracer::RayTracer(Scene* scene, FileWriter* file_writer){
this->scene_ = scene;
this->file_writer_ = file_writer;
}
RayTracer::~RayTracer(){
delete this->scene_;
delete this->file_writer_;
}
/**
* Iterates over each pixel in the image. Casts a ray through the center of the
* pixel on the focal plane, computes the color of the pixel using the ray, and
* writes the color data to the file writer
*/
void RayTracer::run(){
std::stringstream color_datastream;
for (int y = 0; y < this->scene_->getHeightResolution(); y++) {
for (int x = 0; x < this->scene_->getWidthResolution(); x++) {
Ray* primary_ray = generatePrimaryRay(x, y);
RgbColor pixel_color = trace(primary_ray, 1);
//Note: Output to the file is a give and take situation
//We can write the image data to MEMORY and write to DISK with a fast computation time
//We can write the image data to DISK at every pixel to save MEMORY, but slow down computation time
//Write to MEMORY(512x512): 5s Write to DISK(512x512): 1m 40s
color_datastream << pixel_color.getRed() << ' ' << pixel_color.getGreen() << ' ' << pixel_color.getBlue() << " ";
}
color_datastream << std::endl;
}
this->file_writer_->addContent(color_datastream.str());
}
/**
* Normalizes the coordinate (scale between 0 and 1) and shifts it to center of pixel.
* This space is also known as Normalized Device Coordinate (NDC) space.
*
* @param x X-coordinate of the pixel
* @param y Y-coordinate of the pixel
*/
void RayTracer::normalizeAndCenterPixel(double &x, double &y){
x = (x + 0.5) / this->scene_->getWidthResolution();
y = (y + 0.5) / this->scene_->getHeightResolution();
}
/**
* Transforms a coordinate in NDC space to screen space.
* (All the pixels are scaled between -1 and 1)
* Then, the aspect ratio is applied adjusting the range of the x coordinate,
* while leaving the y coordinate static.
*
* @param x X-coordinate in NDC space
* @param y Y-coordinate in NDC space
*/
void RayTracer::convertToScreenSpace(double &x, double &y){
double aspect_ratio = this->scene_->getWidthResolution() / this->scene_->getHeightResolution();
x = ((x * 2) - 1) * aspect_ratio;
y = 1 - (y * 2);
}
/**
* Transforms a coordinate in screen space to camera space. This applies
* the field of view and the distance to the focal plane to determine what
* area of the 3D scene is in frame.
*
* @param x X-coordinate in screen space
* @param y Y-coordinate in screen space
*/
void RayTracer::convertToCameraSpace(double &x, double &y){
x = x * tan(this->scene_->getCameraFieldOfView()) * this->scene_->getDistToImagePlane();
y = y * tan(this->scene_->getCameraFieldOfView()) * this->scene_->getDistToImagePlane();
}
/**
* Compute the initial Ray that needs to be cast to determine the color data at the given pixel
*
* @param x X-coordinate of the pixel
* @param y Y-coordinate of the pixel
* @return Resulting ray
*/
Ray* RayTracer::generatePrimaryRay(double x, double y){
normalizeAndCenterPixel(x, y);
convertToScreenSpace(x, y);
convertToCameraSpace(x, y);
Point3D center_of_pixel (x, y, 0);
Vector3D rayDirection = this->scene_->getCameraOrigin()->computeDirection(¢er_of_pixel, true);
return new Ray(new Point3D(this->scene_->getCameraOrigin()->getX(),
this->scene_->getCameraOrigin()->getY(),
this->scene_->getCameraOrigin()->getZ()),
new Vector3D(rayDirection.getX(),
rayDirection.getY(),
rayDirection.getZ()));
}
/**
* Computes the geometry closest to the camera that the ray has collided with.
* Also computes and returns the point in 3D space that was collided with and the
* normal at that point, if applicable.
*
* @param ray Ray to compute intersections with
* @param nearest_point Point in 3D space that was collided with
* @param normal_at_nearest_point Normal at the point collided with
* @return Pointer to the Geometry object intersected
*/
Geometry* RayTracer::computeNearestIntersection(Ray* ray, Point3D* nearest_point, Vector3D* normal_at_nearest_point){
Geometry* nearest_geometry = NULL;
float nearest_intersection_distance = INFINITY;
Point3D point_hit (0,0,0);
Vector3D normal_hit (1,1,1);
for (int i = 0; i < this->scene_->getGeoListSize(); i++) {
if (this->scene_->getGeoAt(i)->hasIntersection(ray, &point_hit, &normal_hit)) {
float distance = ray->getOrigin()->computeDistance(&point_hit);
if (distance < nearest_intersection_distance) {
nearest_geometry = this->scene_->getGeoAt(i);
nearest_intersection_distance = distance;
nearest_point->setX(point_hit.getX());
nearest_point->setY(point_hit.getY());
nearest_point->setZ(point_hit.getZ());
normal_at_nearest_point->setX(normal_hit.getX());
normal_at_nearest_point->setY(normal_hit.getY());
normal_at_nearest_point->setZ(normal_hit.getZ());
}
}
}
return nearest_geometry;
}
/**
* Generates a flag based on whether the point is in shadow or not.
*
* @param nearest_point Point in 3D space to test whether it is in shadow
* @param casting_light Pointer to the light casting the shadow
* @return Flag determining if the point is in shadow or not
*/
bool RayTracer::computeShadowRay(Point3D* nearest_point, Light* casting_light){
int shadow_flag = 0;
Vector3D direction_to_light = casting_light->getDirectionToLight();
Ray* shadow_ray = new Ray(new Point3D(nearest_point->getX() + (direction_to_light.getX() * FLT_EPSILON),
nearest_point->getY() + (direction_to_light.getY() * FLT_EPSILON),
nearest_point->getZ() + (direction_to_light.getZ() * FLT_EPSILON)),
new Vector3D(direction_to_light.getX(), direction_to_light.getY(), direction_to_light.getZ()));
Point3D point_hit_noop (0,0,0);
Vector3D normal_hit_noop (1,1,1);
for (int i = 0; i < this->scene_->getGeoListSize(); i++) {
if (this->scene_->getGeoAt(i)->hasIntersection(shadow_ray, &point_hit_noop, &normal_hit_noop)) {
shadow_flag = 1;
break;
}
}
delete shadow_ray;
return shadow_flag;
}
/**
* Computes a reflection ray based on the direction the original ray was cast in
* and returns the color data at the end of the reflection ray
*
* @param ray Original ray cast for which reflection needs to be computed
* @param nearest_point Point in 3D space where the ray intersected the geometry
* @param normal_at_nearest_point Normal at the point intersected by the ray
* @param depth_level Current level of recursive ray casting
* @return Color intersected by the reflection ray
*/
RgbColor RayTracer::computeReflection(Ray* ray, Point3D* nearest_point, Vector3D* normal_at_nearest_point, int depth_level){
Vector3D direction_to_eye(-ray->getDirection()->getX(), -ray->getDirection()->getY(), -ray->getDirection()->getZ());
double a = std::max(0.0, normal_at_nearest_point->dot(&direction_to_eye));
Vector3D reflection_direction = ((*normal_at_nearest_point * 2) * a) - direction_to_eye;
Ray* reflection_ray = new Ray(new Point3D(nearest_point->getX(), nearest_point->getY(), nearest_point->getZ()),
new Vector3D(reflection_direction.getX(), reflection_direction.getY(), reflection_direction.getZ()));
return trace(reflection_ray, depth_level + 1);
}
/**
* Computes the color of the pixel based on the Phong Lighting Model
* <https://en.wikipedia.org/wiki/Phong_reflection_model>
*
* Computes an ambient, diffuse, and specular component of the color using
* shadow information four separate vectors:
*
* 1. Surface Normal
* 2. Direction to the Light Source
* 3. Direction to the eye
* 4. Reflection Direction
*
* @param nearest_geometry Geometry object containing the color information
* @param ray Ray being cast from the camera
* @param nearest_point Point in 3D space where the ray intersected the geometry
* @param normal_at_nearest_point Normal at the point intersected by the ray
* @param depth_level Current level of recursive ray casting
* @return Color of the pixel
*/
RgbColor RayTracer::computePhongLightingModel(Geometry* nearest_geometry, Ray* ray, Point3D* nearest_point, Vector3D* normal_at_nearest_point, int depth_level){
RgbColor pixel_color(0,0,0);
if(nearest_geometry->hasReflection() && depth_level <= MAX_RAY_DEPTH){
pixel_color = nearest_geometry->getReflectiveColor() * computeReflection(ray, nearest_point, normal_at_nearest_point, depth_level);
}
for (int i = 0; i < this->scene_->getLightListSize(); i++) {
if(this->scene_->getLightAt(i)->getType() == 0){ //Is Ambient
RgbColor ambient_color = nearest_geometry->getDiffuseColor() * this->scene_->getLightAt(i)->getColor();
pixel_color = pixel_color + ambient_color;
} else if (this->scene_->getLightAt(i)->getType() == 1) { // Is Directional
bool shadow_mask = computeShadowRay(nearest_point, this->scene_->getLightAt(i));
Vector3D direction_to_light = this->scene_->getLightAt(i)->getDirectionToLight();
double a = std::max(0.0, normal_at_nearest_point->dot(&direction_to_light));
Vector3D reflection_direction = ((*normal_at_nearest_point * 2) * a) - direction_to_light;
Vector3D direction_to_eye = ray->getInverseDirection();
double b = std::max(0.0, direction_to_eye.dot(&reflection_direction));
RgbColor diffuse_color = (this->scene_->getLightAt(1)->getColor() * (nearest_geometry->getDiffuseColor() * a)) * !shadow_mask;
RgbColor specular_color = (this->scene_->getLightAt(1)->getColor() * ((nearest_geometry->getSpecularHighlight() * b) ^ nearest_geometry->getPhongConstant())) * !shadow_mask;
pixel_color = pixel_color + diffuse_color + specular_color;
}
}
return pixel_color;
}
/**
* Computes the color data of the pixel based on the ray cast
*
* @param ray Ray to compute intersections and color data by
* @param depth_level Current level of recursive ray casting
* @return Color data of the pixel
*/
RgbColor RayTracer::trace(Ray* ray, int depth_level)
{
Point3D nearest_point (0,0,0);
Vector3D normal_at_nearest_point (1,1,1);
Geometry* nearest_geometry = computeNearestIntersection(ray, &nearest_point, &normal_at_nearest_point);
if (nearest_geometry == NULL){
delete ray;
return this->scene_->getBackgroundColor();
}
RgbColor pixel_color;
switch(nearest_geometry->getShaderType()){
case 0: //Constant Shader
pixel_color = nearest_geometry->getDiffuseColor();
break;
case 1: //Phong Shader
pixel_color = computePhongLightingModel(nearest_geometry, ray, &nearest_point, &normal_at_nearest_point, depth_level);
break;
}
pixel_color.correctOverflow();
delete ray;
return pixel_color;
}