Added image-based lighting with HDR images.

This commit is contained in:
Miguel Angel Astor Romero
2017-01-17 17:09:25 -04:00
parent 04618e518e
commit 8d341b9a25
12 changed files with 177 additions and 77 deletions

1
.gitignore vendored
View File

@@ -30,5 +30,6 @@
ray ray
# Others # Others
textures/
*.d *.d
*~ *~

View File

@@ -1,6 +1,6 @@
CXX = g++ CXX = g++
TARGET = ray TARGET = ray
OBJECTS = main.o sampling.o camera.o disk.o plane.o sphere.o phong_brdf.o hsa_brdf.o directional_light.o point_light.o spot_light.o tracer.o path_tracer.o whitted_tracer.o OBJECTS = main.o sampling.o camera.o environment.o disk.o plane.o sphere.o phong_brdf.o hsa_brdf.o directional_light.o point_light.o spot_light.o tracer.o path_tracer.o whitted_tracer.o
DEPENDS = $(OBJECTS:.o=.d) DEPENDS = $(OBJECTS:.o=.d)
CXXFLAGS = -ansi -pedantic -Wall -DGLM_FORCE_RADIANS -fopenmp CXXFLAGS = -ansi -pedantic -Wall -DGLM_FORCE_RADIANS -fopenmp
LDLIBS = -lfreeimage LDLIBS = -lfreeimage

View File

@@ -1,4 +1,4 @@
* Features [9/20] * Features [12/20]
- [X] Perspective projection - [X] Perspective projection
- [X] Ray-sphere intersection - [X] Ray-sphere intersection
@@ -15,15 +15,15 @@
- [ ] Area lights - [ ] Area lights
- [ ] Sphere lights - [ ] Sphere lights
- [ ] Box/planar lights - [ ] Box/planar lights
- [ ] Mesh lights
- [X] Phong shading - [X] Phong shading
- [X] Specular reflections - [X] Specular reflections
- [X] Transmission - [X] Transmission
- [ ] Scene description input files (JSON) - [ ] Scene description input files (JSON)
- [X] Indirect illumination - [X] Indirect illumination
- [ ] Russian roulette - [ ] Glossy reflections
- [ ] Image based lightning - [X] Image based lighting
- [ ] HDR light probes for IBL - [X] HDR light probes for IBL
- [ ] Tone mapping - [X] Tone mapping
- [ ] Texture mapping - [ ] Texture mapping
- [ ] Photon mapping - [ ] Photon mapping

38
environment.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <cmath>
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include "environment.hpp"
using glm::vec2;
using glm::acos;
using glm::pi;
vec3 Environment::get_color(Ray & r) {
float _r;
vec2 tex_coord;
BYTE * bits;
FIRGBF * pixel;
unsigned int pitch;
if (m_texture == NULL)
return m_bckg_color;
else {
if (!m_probe) {
tex_coord = vec2((1.0f + atan2(r.m_direction.x, -r.m_direction.z) / pi<float>()) / 2.0f, acos(r.m_direction.y) / pi<float>());
tex_coord = vec2(tex_coord.x, 1.0f - tex_coord.y);
} else {
_r = (1.0f / pi<float>()) * acos(r.m_direction.z) / glm::sqrt((r.m_direction.x * r.m_direction.x) + (r.m_direction.y * r.m_direction.y));
tex_coord = vec2(r.m_direction.x * _r, r.m_direction.y * _r);
tex_coord += vec2(1.0f, 1.0f);
tex_coord /= 2.0f;
}
tex_coord *= vec2(FreeImage_GetWidth(m_texture) - 1, FreeImage_GetHeight(m_texture) - 1);
pitch = FreeImage_GetPitch(m_texture);
bits = ((BYTE *)FreeImage_GetBits(m_texture)) + (static_cast<unsigned int>(tex_coord.y) * pitch);
pixel = (FIRGBF *)bits;
return vec3(pixel[static_cast<unsigned int>(tex_coord.x)].red, pixel[static_cast<unsigned int>(tex_coord.x)].green, pixel[static_cast<unsigned int>(tex_coord.x)].blue);
}
}

37
environment.hpp Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#ifndef ENVIRONMENT_HPP
#define ENVIRONMENT_HPP
#include <FreeImage.h>
#include <glm/vec3.hpp>
#include "ray.hpp"
using glm::vec3;
class Environment {
public:
Environment(const char * tex_file = NULL, bool light_probe = false, vec3 bckg = vec3(1.0f)): m_bckg_color(bckg), m_probe(light_probe) {
FREE_IMAGE_FORMAT fif;
if (tex_file != NULL) {
fif = FreeImage_GetFIFFromFilename(tex_file);
m_texture = FreeImage_Load(fif, tex_file, 0);
} else
m_texture = NULL;
}
~Environment() {
if (m_texture != NULL)
FreeImage_Unload(m_texture);
}
vec3 get_color(Ray & r);
private:
vec3 m_bckg_color;
FIBITMAP * m_texture;
bool m_probe;
};
#endif

View File

@@ -27,6 +27,7 @@
#include "brdf.hpp" #include "brdf.hpp"
#include "phong_brdf.hpp" #include "phong_brdf.hpp"
#include "hsa_brdf.hpp" #include "hsa_brdf.hpp"
#include "environment.hpp"
using namespace std; using namespace std;
using namespace glm; using namespace glm;
@@ -40,10 +41,10 @@ using namespace glm;
//////////////////////////////////////////// ////////////////////////////////////////////
// Function prototypes. // Function prototypes.
//////////////////////////////////////////// ////////////////////////////////////////////
static void scene_1(vector<Figure *> & vf, vector<Light *> & vl, Camera * c); static void scene_1(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c);
static void scene_2(vector<Figure *> & vf, vector<Light *> & vl, Camera * c); static void scene_2(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c);
static void scene_3(vector<Figure *> & vf, vector<Light *> & vl, Camera * c); static void scene_3(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c);
static void scene_4(vector<Figure *> & vf, vector<Light *> & vl, Camera * c); static void scene_4(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c);
static void print_usage(char ** const argv); static void print_usage(char ** const argv);
static void parse_args(int argc, char ** const argv); static void parse_args(int argc, char ** const argv);
@@ -87,6 +88,7 @@ int main(int argc, char ** argv) {
FIRGBF *pixel; FIRGBF *pixel;
int pitch; int pitch;
Camera * cam; Camera * cam;
Environment * env = NULL;
parse_args(argc, argv); parse_args(argc, argv);
@@ -100,7 +102,7 @@ int main(int argc, char ** argv) {
image[i] = new vec3[g_w]; image[i] = new vec3[g_w];
} }
scene_2(figures, lights, cam); scene_3(figures, lights, env, cam);
// Create the tracer object. // Create the tracer object.
cout << "Rendering the input file: " << ANSI_BOLD_YELLOW << g_input_file << ANSI_RESET_STYLE << endl; cout << "Rendering the input file: " << ANSI_BOLD_YELLOW << g_input_file << ANSI_RESET_STYLE << endl;
@@ -136,7 +138,7 @@ int main(int argc, char ** argv) {
sample = cam->sample_pixel(i, j); sample = cam->sample_pixel(i, j);
r = Ray(normalize(vec3(sample, -0.5f) - vec3(0.0f)), vec3(0.0f)); r = Ray(normalize(vec3(sample, -0.5f) - vec3(0.0f)), vec3(0.0f));
cam->view_to_world(r); cam->view_to_world(r);
image[i][j] += tracer->trace_ray(r, figures, lights, 0); image[i][j] += tracer->trace_ray(r, figures, lights, env, 0);
#pragma omp atomic #pragma omp atomic
current++; current++;
} }
@@ -176,6 +178,8 @@ int main(int argc, char ** argv) {
delete cam; delete cam;
delete tracer; delete tracer;
if (env != NULL)
delete env;
for (size_t i = 0; i < figures.size(); i++) { for (size_t i = 0; i < figures.size(); i++) {
delete figures[i]; delete figures[i];
@@ -344,12 +348,14 @@ void parse_args(int argc, char ** const argv) {
} }
} }
void scene_1(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) { void scene_1(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c) {
Sphere * s; Sphere * s;
Plane * p; Plane * p;
Disk * d; Disk * d;
DirectionalLight * l; DirectionalLight * l;
e = new Environment(NULL, false, vec3(0.7f, 0.4f, 0.05f));
s = new Sphere(1.0f, 1.0f, -2.0f, 0.5f); s = new Sphere(1.0f, 1.0f, -2.0f, 0.5f);
s->m_mat->m_diffuse = vec3(1.0f, 0.0f, 0.0f); s->m_mat->m_diffuse = vec3(1.0f, 0.0f, 0.0f);
vf.push_back(static_cast<Figure *>(s)); vf.push_back(static_cast<Figure *>(s));
@@ -419,7 +425,7 @@ void scene_1(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) {
vl.push_back(static_cast<Light *>(l)); vl.push_back(static_cast<Light *>(l));
} }
void scene_2(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) { void scene_2(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c) {
Sphere * s; Sphere * s;
Plane * p; Plane * p;
Disk * d; Disk * d;
@@ -493,15 +499,17 @@ void scene_2(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) {
vl.push_back(static_cast<Light *>(l)); vl.push_back(static_cast<Light *>(l));
} }
void scene_3(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) { void scene_3(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c) {
Sphere * s; Sphere * s;
Plane * p; Disk * d;
SpotLight * l; // SpotLight * l;
DirectionalLight * l2; // DirectionalLight * l2;
vec3 eye = vec3(0.0f, 1.5f, 0.0f); vec3 eye = vec3(0.0f, 1.5f, 1.0f);
vec3 center = vec3(0.0f, 0.0f, -2.0f); vec3 center = vec3(0.0f, 0.0f, -2.0f);
vec3 left = vec3(-1.0f, 0.0f, 0.0f); vec3 left = vec3(-1.0f, 0.0f, 0.0f);
e = new Environment("textures/pisa.hdr");
c->m_eye = eye; c->m_eye = eye;
c->m_look = center; c->m_look = center;
c->m_up = cross(normalize(center - eye), left); c->m_up = cross(normalize(center - eye), left);
@@ -525,39 +533,44 @@ void scene_3(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) {
// s->m_mat->m_ref_index = 2.6f; // s->m_mat->m_ref_index = 2.6f;
// vf.push_back(static_cast<Figure *>(s)); // vf.push_back(static_cast<Figure *>(s));
s = new Sphere(2.0f, 0.0f, -2.0f, 1.0f, new HeidrichSeidelAnisotropicBRDF(vec3(0.0f, 1.0f, 0.0f))); s = new Sphere(2.0f, 0.0f, -2.0f, 1.5f, new HeidrichSeidelAnisotropicBRDF(vec3(0.0f, 1.0f, 0.0f)));
s->m_mat->m_diffuse = vec3(1.0f, 1.0f, 0.0f); s->m_mat->m_diffuse = vec3(1.0f, 1.0f, 0.0f);
s->m_mat->m_shininess = 128.0f; s->m_mat->m_shininess = 128.0f;
vf.push_back(static_cast<Figure *>(s)); vf.push_back(static_cast<Figure *>(s));
s = new Sphere(-1.0f, 0.0f, -3.25f, 1.0f); s = new Sphere(-1.0f, 0.0f, -3.25f, 1.5f);
s->m_mat->m_diffuse = vec3(1.0f, 0.0f, 1.0f); s->m_mat->m_diffuse = vec3(1.0f, 0.0f, 1.0f);
s->m_mat->m_rho = 0.4f; s->m_mat->m_rho = 0.4f;
vf.push_back(static_cast<Figure *>(s)); vf.push_back(static_cast<Figure *>(s));
p = new Plane(vec3(0.0f, -1.5f, 0.0f), vec3(0.0f, 1.0f, 0.0f)); s = new Sphere(1.0f, 0.0f, -3.25f, 1.5f);
p->m_mat->m_diffuse = vec3(1.0f); s->m_mat->m_diffuse = vec3(1.0f);
p->m_mat->m_specular = vec3(0.0f); s->m_mat->m_rho = 0.4f;
vf.push_back(static_cast<Figure *>(p)); vf.push_back(static_cast<Figure *>(s));
d = new Disk(vec3(1.0f, -1.5f, -3.25f), vec3(0.0f, 1.0f, 0.0f), 3.0f);
d->m_mat->m_diffuse = vec3(0.0f, 0.5f, 0.5f);
d->m_mat->m_specular = vec3(0.0f);
vf.push_back(static_cast<Figure *>(d));
// l = new SpotLight();
// l->m_position = normalize(vec3(-2.0f, 1.5f, -1.0f));
// l->m_diffuse = vec3(1.0f, 1.0f, 0.0f);
// l->m_spot_dir = normalize(vec3(0.5f, 0.0f, -2.5f) - vec3(-2.0f, 1.5f, -1.0f));
// l->m_spot_cutoff = 89.0f;
// l->m_spot_exponent = 10.0f;
// vl.push_back(static_cast<Light *>(l));
l = new SpotLight(); // l2 = new DirectionalLight();
l->m_position = normalize(vec3(-2.0f, 1.5f, -1.0f)); // l2->m_position = normalize(vec3(-1.0f, 0.7f, 1.0f));
l->m_diffuse = vec3(1.0f, 1.0f, 0.0f); // l2->m_diffuse = vec3(1.0f, 1.0f, 1.0f);
l->m_spot_dir = normalize(vec3(0.5f, 0.0f, -2.5f) - vec3(-2.0f, 1.5f, -1.0f)); // vl.push_back(static_cast<Light *>(l2));
l->m_spot_cutoff = 89.0f;
l->m_spot_exponent = 10.0f;
vl.push_back(static_cast<Light *>(l));
l2 = new DirectionalLight(); // l2 = new DirectionalLight();
l2->m_position = normalize(vec3(-1.0f, 0.7f, 1.0f)); // l2->m_position = normalize(vec3(-0.5f, 0.7f, 1.0f));
l2->m_diffuse = vec3(1.0f, 1.0f, 1.0f); // l2->m_diffuse = vec3(0.0f, 0.0f, 1.0f);
vl.push_back(static_cast<Light *>(l2)); // l2->m_specular = vec3(0.0f, 0.0f, 1.0f);
// vl.push_back(static_cast<Light *>(l2));
l2 = new DirectionalLight();
l2->m_position = normalize(vec3(-0.5f, 0.7f, 1.0f));
l2->m_diffuse = vec3(0.0f, 0.0f, 1.0f);
l2->m_specular = vec3(0.0f, 0.0f, 1.0f);
vl.push_back(static_cast<Light *>(l2));
// l = new DirectionalLight(); // l = new DirectionalLight();
// l->m_position = normalize(vec3(1.0f, 0.0f, 1.0f)); // l->m_position = normalize(vec3(1.0f, 0.0f, 1.0f));
@@ -565,12 +578,15 @@ void scene_3(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) {
// vl.push_back(static_cast<Light *>(l)); // vl.push_back(static_cast<Light *>(l));
} }
void scene_4(vector<Figure *> & vf, vector<Light *> & vl, Camera * c) { void scene_4(vector<Figure *> & vf, vector<Light *> & vl, Environment * & e, Camera * c) {
Sphere * s; Sphere * s;
Plane * p; Plane * p;
e = new Environment("textures/pisa.hdr");
s = new Sphere(0.0f, 0.0f, -2.0f, 1.0f); s = new Sphere(0.0f, 0.0f, -2.0f, 1.0f);
s->m_mat->m_diffuse = vec3(1.0f, 1.0f, 0.0f); s->m_mat->m_diffuse = vec3(1.0f);
s->m_mat->m_rho = 0.3f;
vf.push_back(static_cast<Figure *>(s)); vf.push_back(static_cast<Figure *>(s));
p = new Plane(vec3(0.0f, -1.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f)); p = new Plane(vec3(0.0f, -1.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f));

Binary file not shown.

Before

Width:  |  Height:  |  Size: 569 KiB

After

Width:  |  Height:  |  Size: 377 KiB

View File

@@ -10,7 +10,7 @@ using namespace glm;
PathTracer::~PathTracer() { } PathTracer::~PathTracer() { }
vec3 PathTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const { vec3 PathTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, Environment * e, unsigned int rec_level) const {
float t, _t; float t, _t;
Figure * _f; Figure * _f;
vec3 n, color, i_pos, ref, sample, dir_diff_color, dir_spec_color, ind_color, amb_color; vec3 n, color, i_pos, ref, sample, dir_diff_color, dir_spec_color, ind_color, amb_color;
@@ -63,36 +63,35 @@ vec3 PathTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *
sample = sample_hemisphere(r1, r2); sample = sample_hemisphere(r1, r2);
rotate_sample(sample, n); rotate_sample(sample, n);
rr = Ray(normalize(sample), i_pos + (sample * BIAS)); rr = Ray(normalize(sample), i_pos + (sample * BIAS));
ind_color += r1 * trace_ray(rr, v_figures, v_lights, rec_level + 1) / PDF; ind_color += r1 * trace_ray(rr, v_figures, v_lights, e, rec_level + 1) / PDF;
} }
// Calculate environment light contribution // Calculate environment light contribution
if (BCKG_COLOR.r > 0.0f || BCKG_COLOR.g > 0.0f || BCKG_COLOR.b > 0.0f) { vis = true;
vis = true;
r1 = random01(); r1 = random01();
r2 = random01(); r2 = random01();
sample = sample_hemisphere(r1, r2); sample = sample_hemisphere(r1, r2);
rotate_sample(sample, n); rotate_sample(sample, n);
rr = Ray(normalize(sample), i_pos + (sample * BIAS)); rr = Ray(normalize(sample), i_pos + (sample * BIAS));
// Cast a shadow ray to determine visibility. // Cast a shadow ray to determine visibility.
for (size_t f = 0; f < v_figures.size(); f++) { for (size_t f = 0; f < v_figures.size(); f++) {
if (v_figures[f]->intersect(rr, _t)) { if (v_figures[f]->intersect(rr, _t)) {
vis = false; vis = false;
break; break;
}
} }
amb_color = vis ? BCKG_COLOR * max(dot(n, rr.m_direction), 0.0f) / PDF : vec3(0.0f);
} }
amb_color = vis ? e->get_color(rr) * max(dot(n, rr.m_direction), 0.0f) / PDF : vec3(0.0f);
// Add lighting.
color += ((dir_diff_color + ind_color + amb_color) * (_f->m_mat->m_diffuse / pi<float>())) + (_f->m_mat->m_specular * dir_spec_color); color += ((dir_diff_color + ind_color + amb_color) * (_f->m_mat->m_diffuse / pi<float>())) + (_f->m_mat->m_specular * dir_spec_color);
// Determine the specular reflection color. // Determine the specular reflection color.
if (_f->m_mat->m_rho > 0.0f && rec_level < m_max_depth) { if (_f->m_mat->m_rho > 0.0f && rec_level < m_max_depth) {
rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS); rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS);
color += _f->m_mat->m_rho * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += _f->m_mat->m_rho * trace_ray(rr, v_figures, v_lights, e, rec_level + 1);
} else if (_f->m_mat->m_rho > 0.0f && rec_level >= m_max_depth) } else if (_f->m_mat->m_rho > 0.0f && rec_level >= m_max_depth)
return vec3(0.0f); return vec3(0.0f);
@@ -103,14 +102,14 @@ vec3 PathTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *
// Determine the specular reflection color. // Determine the specular reflection color.
if (kr > 0.0f && rec_level < m_max_depth) { if (kr > 0.0f && rec_level < m_max_depth) {
rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS); rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS);
color += kr * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += kr * trace_ray(rr, v_figures, v_lights, e, rec_level + 1);
} else if (rec_level >= m_max_depth) } else if (rec_level >= m_max_depth)
return vec3(0.0f); return vec3(0.0f);
// Determine the transmission color. // Determine the transmission color.
if (_f->m_mat->m_refract && kr < 1.0f && rec_level < m_max_depth) { if (_f->m_mat->m_refract && kr < 1.0f && rec_level < m_max_depth) {
rr = Ray(normalize(refract(r.m_direction, n, r.m_ref_index / _f->m_mat->m_ref_index)), i_pos - n * BIAS, _f->m_mat->m_ref_index); rr = Ray(normalize(refract(r.m_direction, n, r.m_ref_index / _f->m_mat->m_ref_index)), i_pos - n * BIAS, _f->m_mat->m_ref_index);
color += (1.0f - kr) * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += (1.0f - kr) * trace_ray(rr, v_figures, v_lights, e, rec_level + 1);
} else if (rec_level >= m_max_depth) } else if (rec_level >= m_max_depth)
return vec3(0.0f); return vec3(0.0f);
@@ -119,6 +118,10 @@ vec3 PathTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *
// Return final color. // Return final color.
return color; return color;
} else } else {
return BCKG_COLOR; if (e != NULL)
return e->get_color(r);
else
return vec3(0.0f);
}
} }

View File

@@ -12,7 +12,7 @@ public:
virtual ~PathTracer(); virtual ~PathTracer();
virtual vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const; virtual vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, Environment * e, unsigned int rec_level) const;
}; };
#endif #endif

View File

@@ -8,6 +8,7 @@
#include "figure.hpp" #include "figure.hpp"
#include "light.hpp" #include "light.hpp"
#include "environment.hpp"
#include "ray.hpp" #include "ray.hpp"
using std::vector; using std::vector;
@@ -28,7 +29,7 @@ public:
virtual ~Tracer() { } virtual ~Tracer() { }
virtual vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const = 0; virtual vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, Environment * e, unsigned int rec_level) const = 0;
protected: protected:
float fresnel(const vec3 & i, const vec3 & n, const float ir1, const float ir2) const; float fresnel(const vec3 & i, const vec3 & n, const float ir1, const float ir2) const;

View File

@@ -9,7 +9,7 @@ using namespace glm;
WhittedTracer::~WhittedTracer() { } WhittedTracer::~WhittedTracer() { }
vec3 WhittedTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const { vec3 WhittedTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, Environment * e, unsigned int rec_level) const {
float t, _t; float t, _t;
Figure * _f; Figure * _f;
vec3 n, color, i_pos, ref, dir_diff_color, dir_spec_color; vec3 n, color, i_pos, ref, dir_diff_color, dir_spec_color;
@@ -60,7 +60,7 @@ vec3 WhittedTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Ligh
// Determine the specular reflection color. // Determine the specular reflection color.
if (_f->m_mat->m_rho > 0.0f && rec_level < m_max_depth) { if (_f->m_mat->m_rho > 0.0f && rec_level < m_max_depth) {
rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS); rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS);
color += _f->m_mat->m_rho * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += _f->m_mat->m_rho * trace_ray(rr, v_figures, v_lights, e, rec_level + 1);
} else if (_f->m_mat->m_rho > 0.0f && rec_level >= m_max_depth) } else if (_f->m_mat->m_rho > 0.0f && rec_level >= m_max_depth)
return vec3(0.0f); return vec3(0.0f);
@@ -71,14 +71,14 @@ vec3 WhittedTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Ligh
// Determine the specular reflection color. // Determine the specular reflection color.
if (kr > 0.0f && rec_level < m_max_depth) { if (kr > 0.0f && rec_level < m_max_depth) {
rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS); rr = Ray(normalize(reflect(r.m_direction, n)), i_pos + n * BIAS);
color += kr * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += kr * trace_ray(rr, v_figures, v_lights, e, rec_level + 1);
} else if (rec_level >= m_max_depth) } else if (rec_level >= m_max_depth)
return vec3(0.0f); return vec3(0.0f);
// Determine the transmission color. // Determine the transmission color.
if (_f->m_mat->m_refract && kr < 1.0f && rec_level < m_max_depth) { if (_f->m_mat->m_refract && kr < 1.0f && rec_level < m_max_depth) {
rr = Ray(normalize(refract(r.m_direction, n, r.m_ref_index / _f->m_mat->m_ref_index)), i_pos - n * BIAS, _f->m_mat->m_ref_index); rr = Ray(normalize(refract(r.m_direction, n, r.m_ref_index / _f->m_mat->m_ref_index)), i_pos - n * BIAS, _f->m_mat->m_ref_index);
color += (1.0f - kr) * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += (1.0f - kr) * trace_ray(rr, v_figures, v_lights, e, rec_level + 1);
} else if (rec_level >= m_max_depth) } else if (rec_level >= m_max_depth)
return vec3(0.0f); return vec3(0.0f);
@@ -87,6 +87,10 @@ vec3 WhittedTracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Ligh
// Return final color. // Return final color.
return color; return color;
} else } else {
return BCKG_COLOR; if (e != NULL)
return e->get_color(r);
else
return vec3(0.0f);
}
} }

View File

@@ -12,7 +12,7 @@ public:
virtual ~WhittedTracer(); virtual ~WhittedTracer();
virtual vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const; virtual vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, Environment * e, unsigned int rec_level) const;
}; };
#endif #endif