Added indirect illumination.

This commit is contained in:
2017-01-05 06:16:14 -04:00
parent 3f13372071
commit 96fe34975e
11 changed files with 268 additions and 59196 deletions

View File

@@ -1,4 +1,4 @@
* Features [7/19] * Features [8/20]
- [X] Perspective projection - [X] Perspective projection
- [X] Ray-sphere intersection - [X] Ray-sphere intersection
@@ -20,7 +20,7 @@
- [X] Specular reflections - [X] Specular reflections
- [X] Transmission - [X] Transmission
- [ ] Scene description input files (JSON) - [ ] Scene description input files (JSON)
- [ ] Indirect illumination - [X] Indirect illumination
- [ ] Russian roulette - [ ] Russian roulette
- [ ] Image based lightning - [ ] Image based lightning
- [ ] HDR light probes for IBL - [ ] HDR light probes for IBL

View File

@@ -20,12 +20,19 @@ inline float DirectionalLight::distance(vec3 point) {
return numeric_limits<float>::max(); return numeric_limits<float>::max();
} }
vec3 DirectionalLight::shade(vec3 normal, Ray & r, float t, Material & m) const { vec3 DirectionalLight::diffuse(vec3 normal, Ray & r, float t, Material & m) const {
float n_dot_l, r_dot_l; float n_dot_l;
vec3 color, ref; vec3 color;
n_dot_l = max(dot(normal, m_position), 0.0f); n_dot_l = max(dot(normal, m_position), 0.0f);
color += (m.m_diffuse / pi<float>()) * m_diffuse * n_dot_l; color += m_diffuse * n_dot_l;
return color;
}
vec3 DirectionalLight::specular(vec3 normal, Ray & r, float t, Material & m) const {
float r_dot_l;
vec3 color, ref;
ref = reflect(m_position, normal); ref = reflect(m_position, normal);
r_dot_l = pow(max(dot(ref, r.m_direction), 0.0f), m.m_shininess); r_dot_l = pow(max(dot(ref, r.m_direction), 0.0f), m.m_shininess);

View File

@@ -20,7 +20,8 @@ public:
virtual vec3 direction(vec3 point); virtual vec3 direction(vec3 point);
virtual float distance(vec3 point); virtual float distance(vec3 point);
virtual vec3 shade(vec3 normal, Ray & r, float t, Material & m) const; virtual vec3 diffuse(vec3 normal, Ray & r, float t, Material & m) const;
virtual vec3 specular(vec3 normal, Ray & r, float t, Material & m) const;
}; };
#endif #endif

View File

@@ -19,7 +19,8 @@ public:
virtual vec3 direction(vec3 point) = 0; virtual vec3 direction(vec3 point) = 0;
virtual float distance(vec3 point) = 0; virtual float distance(vec3 point) = 0;
virtual vec3 shade(vec3 normal, Ray & r, float t, Material & m) const = 0; virtual vec3 diffuse(vec3 normal, Ray & r, float t, Material & m) const = 0;
virtual vec3 specular(vec3 normal, Ray & r, float t, Material & m) const = 0;
}; };
#endif #endif

View File

@@ -97,7 +97,7 @@ int main(int argc, char ** argv) {
scene_2(figures, lights, i_model_view); scene_2(figures, lights, i_model_view);
tracer = Tracer(g_h, g_w, g_fov); tracer = Tracer(g_h, g_w, g_fov, true);
total = g_h * g_w * g_samples; total = g_h * g_w * g_samples;
@@ -137,9 +137,9 @@ int main(int argc, char ** argv) {
fprintf(out, "P6 %d %d %d ", g_w, g_h, 255); fprintf(out, "P6 %d %d %d ", g_w, g_h, 255);
for (int i = 0; i < g_h; i++) { for (int i = 0; i < g_h; i++) {
for (int j = 0; j < g_w; j++) { for (int j = 0; j < g_w; j++) {
fputc(static_cast<int>(image[i][j].r * 255.0f), out); fputc(static_cast<int>(pow(image[i][j].r, 1.0f / 2.2f) * 255.0f), out);
fputc(static_cast<int>(image[i][j].g * 255.0f), out); fputc(static_cast<int>(pow(image[i][j].g, 1.0f / 2.2f) * 255.0f), out);
fputc(static_cast<int>(image[i][j].b * 255.0f), out); fputc(static_cast<int>(pow(image[i][j].b, 1.0f / 2.2f) * 255.0f), out);
} }
} }
fclose(out); fclose(out);
@@ -229,14 +229,11 @@ static void scene_1(vector<Figure *> & vf, vector<Light *> & vl, mat4x4 & i_mode
static void scene_2(vector<Figure *> & vf, vector<Light *> & vl, mat4x4 & i_model_view) { static void scene_2(vector<Figure *> & vf, vector<Light *> & vl, mat4x4 & i_model_view) {
Sphere * s; Sphere * s;
Plane * p; Plane * p;
Disk * d;
PointLight * l; PointLight * l;
s = new Sphere(0.2f, 0.0f, -0.75f, 0.25f); s = new Sphere(0.2f, 0.0f, -0.75f, 0.25f);
s->m_mat.m_diffuse = vec3(0.0f); s->m_mat.m_diffuse = vec3(1.0f);
s->m_mat.m_specular = vec3(0.0f);
s->m_mat.m_rho = 0.1f;
s->m_mat.m_refract = true;
s->m_mat.m_ref_index = 2.33f;
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));
@@ -259,15 +256,38 @@ static void scene_2(vector<Figure *> & vf, vector<Light *> & vl, mat4x4 & i_mode
p->m_mat.m_diffuse = vec3(1.0f, 0.0f, 1.0f); p->m_mat.m_diffuse = vec3(1.0f, 0.0f, 1.0f);
vf.push_back(static_cast<Figure *>(p)); vf.push_back(static_cast<Figure *>(p));
p = new Plane(vec3(0.0f, 0.0f, 0.1f), vec3(0.0f, 0.0f, -1.0f)); p = new Plane(vec3(0.0f, 0.0f, 1.1f), vec3(0.0f, 0.0f, -1.0f));
p->m_mat.m_diffuse = vec3(1.0f, 1.0f, 1.0f); p->m_mat.m_diffuse = vec3(1.0f, 1.0f, 1.0f);
vf.push_back(static_cast<Figure *>(p)); vf.push_back(static_cast<Figure *>(p));
s = new Sphere(-0.4f, -0.5f, -1.5f, 0.5f); s = new Sphere(-0.5f, -0.5f, -1.5f, 0.5f);
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_rho = 0.9f; s->m_mat.m_rho = 0.9f;
vf.push_back(static_cast<Figure *>(s)); vf.push_back(static_cast<Figure *>(s));
s = new Sphere(-0.5f, -0.5f, 0.6f, 0.5f);
s->m_mat.m_diffuse = vec3(1.0f, 1.0f, 0.0f);
s->m_mat.m_rho = 0.1f;
s->m_mat.m_refract = true;
s->m_mat.m_ref_index = 1.33f;
vf.push_back(static_cast<Figure *>(s));
d = new Disk(vec3(-0.25f, 1.0f, -1.0f), vec3(1.0f, 0.0f, 0.0f), 0.25f);
d->m_mat.m_diffuse = vec3(1.0f);
vf.push_back(static_cast<Figure *>(d));
d = new Disk(vec3(0.25f, 1.0f, -1.0f), vec3(-1.0f, 0.0f, 0.0f), 0.25f);
d->m_mat.m_diffuse = vec3(1.0f);
vf.push_back(static_cast<Figure *>(d));
d = new Disk(vec3(0.0f, 1.0f, -1.25f), vec3(0.0f, 0.0f, 1.0f), 0.25f);
d->m_mat.m_diffuse = vec3(1.0f);
vf.push_back(static_cast<Figure *>(d));
d = new Disk(vec3(0.0f, 1.0f, -0.75f), vec3(0.0f, 0.0f, -1.0f), 0.25f);
d->m_mat.m_diffuse = vec3(1.0f);
vf.push_back(static_cast<Figure *>(d));
l = new PointLight(); l = new PointLight();
l->m_position = vec3(0.0f, 0.9f, -1.0f); l->m_position = vec3(0.0f, 0.9f, -1.0f);
l->m_diffuse = vec3(1.0f); l->m_diffuse = vec3(1.0f);
@@ -302,7 +322,7 @@ static void scene_3(vector<Figure *> & vf, vector<Light *> & vl, mat4x4 & i_mode
s = new Sphere(2.0f, 0.0f, -2.0f, 1.0f); s = new Sphere(2.0f, 0.0f, -2.0f, 1.0f);
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.6f; s->m_mat.m_rho = 1.0f;
vf.push_back(static_cast<Figure *>(s)); vf.push_back(static_cast<Figure *>(s));
s = new Sphere(-1.0f, 0.25f, -3.25f, 1.0f); s = new Sphere(-1.0f, 0.25f, -3.25f, 1.0f);
@@ -311,13 +331,13 @@ static void scene_3(vector<Figure *> & vf, vector<Light *> & vl, mat4x4 & i_mode
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)); p = new Plane(vec3(0.0f, -1.5f, 0.0f), vec3(0.0f, 1.0f, 0.0f));
p->m_mat.m_diffuse = vec3(1.0f, 1.0f, 1.0f); p->m_mat.m_diffuse = vec3(1.0f);
p->m_mat.m_specular = vec3(0.0f); p->m_mat.m_specular = vec3(0.0f);
vf.push_back(static_cast<Figure *>(p)); vf.push_back(static_cast<Figure *>(p));
l = new DirectionalLight(); l = new DirectionalLight();
l->m_position = normalize(vec3(-1.0f, 1.0f, -1.0f)); l->m_position = normalize(vec3(-1.0f, 1.0f, -1.0f));
l->m_diffuse = vec3(0.0f, 1.0f, 0.0f); l->m_diffuse = vec3(1.0f, 1.0f, 1.0f);
l->m_specular = vec3(0.0f, 1.0f, 0.0f); l->m_specular = vec3(0.0f, 1.0f, 0.0f);
vl.push_back(static_cast<Light *>(l)); vl.push_back(static_cast<Light *>(l));
@@ -334,7 +354,7 @@ static void scene_3(vector<Figure *> & vf, vector<Light *> & vl, mat4x4 & i_mode
vl.push_back(static_cast<Light *>(l)); vl.push_back(static_cast<Light *>(l));
l = new DirectionalLight(); l = new DirectionalLight();
l->m_position = normalize(vec3(0.0f, 1.0f, 0.0f)); l->m_position = normalize(vec3(1.0f, 0.0f, 1.0f));
l->m_diffuse = vec3(0.5f); l->m_diffuse = vec3(0.5f);
vl.push_back(static_cast<Light *>(l)); vl.push_back(static_cast<Light *>(l));

View File

@@ -14,8 +14,10 @@ public:
float m_shininess; float m_shininess;
float m_ref_index; float m_ref_index;
bool m_refract; bool m_refract;
float kd;
float ks;
Material(): m_diffuse(vec3(1.0f)), m_specular(vec3(1.0f)), m_rho(0.0f), m_shininess(89.0f), m_ref_index(1.0f), m_refract(false) { } Material(): m_diffuse(vec3(1.0f)), m_specular(vec3(1.0f)), m_rho(0.0f), m_shininess(89.0f), m_ref_index(1.0f), m_refract(false), kd(0.4f), ks(0.4f) { }
Material(const Material & m) { Material(const Material & m) {
m_diffuse = m.m_diffuse; m_diffuse = m.m_diffuse;

59217
output.ppm

File diff suppressed because one or more lines are too long

View File

@@ -19,8 +19,8 @@ inline float PointLight::distance(vec3 point) {
return length(m_position - point); return length(m_position - point);
} }
vec3 PointLight::shade(vec3 normal, Ray & r, float t, Material & m) const { vec3 PointLight::diffuse(vec3 normal, Ray & r, float t, Material & m) const {
float d, att, n_dot_l, r_dot_l; float d, att, n_dot_l;
vec3 color, i_pos, l_dir, ref; vec3 color, i_pos, l_dir, ref;
i_pos = r.m_origin + t * r.m_direction; i_pos = r.m_origin + t * r.m_direction;
@@ -30,7 +30,20 @@ vec3 PointLight::shade(vec3 normal, Ray & r, float t, Material & m) const {
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d))); att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
n_dot_l = max(dot(normal, l_dir), 0.0f); n_dot_l = max(dot(normal, l_dir), 0.0f);
color += att * (m.m_diffuse / pi<float>()) * m_diffuse * n_dot_l; color += att * m_diffuse * n_dot_l;
return color;
}
vec3 PointLight::specular(vec3 normal, Ray & r, float t, Material & m) const {
float d, att, r_dot_l;
vec3 color, i_pos, l_dir, ref;
i_pos = r.m_origin + t * r.m_direction;
l_dir = m_position - i_pos;
d = length(l_dir);
l_dir = normalize(l_dir);
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
ref = reflect(l_dir, normal); ref = reflect(l_dir, normal);
r_dot_l = pow(max(dot(ref, r.m_direction), 0.0f), m.m_shininess); r_dot_l = pow(max(dot(ref, r.m_direction), 0.0f), m.m_shininess);

View File

@@ -24,7 +24,8 @@ public:
virtual vec3 direction(vec3 point); virtual vec3 direction(vec3 point);
virtual float distance(vec3 point); virtual float distance(vec3 point);
virtual vec3 shade(vec3 normal, Ray & r, float t, Material & m) const; virtual vec3 diffuse(vec3 normal, Ray & r, float t, Material & m) const;
virtual vec3 specular(vec3 normal, Ray & r, float t, Material & m) const;
}; };
#endif #endif

View File

@@ -2,24 +2,19 @@
#include <limits> #include <limits>
#include <cstdlib> #include <cstdlib>
#include <glm/gtc/constants.hpp>
#include "tracer.hpp" #include "tracer.hpp"
#define MAX_RECURSION 9 #define MAX_RECURSION 3
#define BIAS 0.000001f #define BIAS 0.000001f
using namespace std; using namespace std;
using std::numeric_limits; using std::numeric_limits;
using glm::normalize; using namespace glm;
using glm::radians;
using glm::dot;
using glm::reflect;
using glm::refract;
using glm::clamp;
using glm::tan;
using glm::sqrt;
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f); static const vec3 BCKG_COLOR = vec3(0.0f);
static inline float random01() { static inline float random01() {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
@@ -56,14 +51,15 @@ vec2 Tracer::sample_pixel(int i, int j) const {
vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const { vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const {
float t, _t; float t, _t;
Figure * _f; Figure * _f;
vec3 n, color, i_pos, ref; vec3 n, color, i_pos, ref, sample, dir_diff_color, dir_spec_color, ind_color;
Ray mv_r, sr, rr; Ray mv_r, sr, rr;
bool vis; bool vis;
float kr; float kr, r1, r2;
t = numeric_limits<float>::max(); t = numeric_limits<float>::max();
_f = NULL; _f = NULL;
// Find the closest intersecting surface.
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(r, _t) && _t < t) { if (v_figures[f]->intersect(r, _t) && _t < t) {
t = _t; t = _t;
@@ -71,14 +67,21 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> &
} }
} }
// If this ray intersects something:
if (_f != NULL) { if (_f != NULL) {
// Take the intersection point and the normal of the surface at that point.
i_pos = r.m_origin + (t * r.m_direction); i_pos = r.m_origin + (t * r.m_direction);
n = _f->normal_at_int(r, t); n = _f->normal_at_int(r, t);
// Check if the material is not reflective/refractive.
if( !_f->m_mat.m_refract && _f->m_mat.m_rho == 0.0f) {
// Calculate the direct lighting.
for (size_t l = 0; l < v_lights.size(); l++) { for (size_t l = 0; l < v_lights.size(); l++) {
// For every light source
vis = true; vis = true;
sr = Ray(v_lights[l]->direction(i_pos), i_pos + n * BIAS);
// Cast a shadow ray to determine visibility.
sr = Ray(v_lights[l]->direction(i_pos), i_pos + n * BIAS);
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(sr, _t) && _t < v_lights[l]->distance(i_pos)) { if (v_figures[f]->intersect(sr, _t) && _t < v_lights[l]->distance(i_pos)) {
vis = false; vis = false;
@@ -86,30 +89,77 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> &
} }
} }
color += (vis ? 1.0f : 0.0f) * v_lights[l]->shade(n, r, t, _f->m_mat); // Evaluate the shading model accounting for visibility.
dir_diff_color += (vis ? 1.0f : 0.0f) * v_lights[l]->diffuse(n, r, t, _f->m_mat);
dir_spec_color += (vis ? 1.0f : 0.0f) * v_lights[l]->specular(n, r, t, _f->m_mat);
} }
// If enabled, calculate indirect lighting contribution.
if (indirect_l && rec_level < MAX_RECURSION) {
r1 = random01();
r2 = random01();
sample = sample_hemisphere(r1, r2);
rotate_sample(sample, n);
rr = Ray(normalize(sample), i_pos + (sample * BIAS));
ind_color += r1 * trace_ray(rr, v_figures, v_lights, rec_level + 1) / (1.0f / (2.0f * pi<float>()));
}
color += ((dir_diff_color + ind_color) * (_f->m_mat.m_diffuse / pi<float>())) + dir_spec_color;
} else {
// If the material has reflection/transmission enabled.
// Calculate the Fresnel term if the surface is refracting.
if (_f->m_mat.m_refract) if (_f->m_mat.m_refract)
kr = fresnel(r.m_direction, n, r.m_ref_index, _f->m_mat.m_ref_index); kr = fresnel(r.m_direction, n, r.m_ref_index, _f->m_mat.m_ref_index);
else else
kr = _f->m_mat.m_rho; kr = _f->m_mat.m_rho;
// Determinte the specular reflection color.
if (kr > 0.0f && rec_level < MAX_RECURSION) { if (kr > 0.0f && rec_level < MAX_RECURSION) {
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 * kr * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += _f->m_mat.m_rho * kr * trace_ray(rr, v_figures, v_lights, rec_level + 1);
} else if (rec_level >= MAX_RECURSION) } else if (rec_level >= MAX_RECURSION)
return vec3(BCKG_COLOR); return vec3(0.0f);
// Determine the transmission color.
if (_f->m_mat.m_refract && kr < 1.0f && rec_level < MAX_RECURSION) { if (_f->m_mat.m_refract && kr < 1.0f && rec_level < MAX_RECURSION) {
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 - _f->m_mat.m_rho) * (1.0f - kr) * trace_ray(rr, v_figures, v_lights, rec_level + 1); color += (1.0f - _f->m_mat.m_rho) * (1.0f - kr) * trace_ray(rr, v_figures, v_lights, rec_level + 1);
} else if (rec_level >= MAX_RECURSION) } else if (rec_level >= MAX_RECURSION)
return vec3(BCKG_COLOR); return vec3(0.0f);
}
// Return final color.
return clamp(color, 0.0f, 1.0f); return clamp(color, 0.0f, 1.0f);
} else } else
return vec3(BCKG_COLOR); return vec3(BCKG_COLOR);
} }
/* Helper functions pretty much taken from scratchapixel.com */
void Tracer::create_coords_system(const vec3 &n, vec3 &nt, vec3 &nb) const {
if (abs(n.x) > abs(n.y))
nt = normalize(vec3(n.z, 0.0f, -n.x));
else
nt = normalize(vec3(0.0f, -n.z, n.y));
nb = normalize(cross(n, nt));
}
vec3 Tracer::sample_hemisphere(const float r1, const float r2) const {
float sin_t = sqrt(1.0f - (r1 * r1));
float phi = 2 * pi<float>() * r2;
float x = sin_t * cos(phi);
float z = sin_t * sin(phi);
return vec3(x, r1, z);
}
void Tracer::rotate_sample(vec3 & sample, const vec3 & n) const {
vec3 nt, nb;
mat3 rot_m;
create_coords_system(n, nt, nb);
sample = vec3(sample.x * nb.x + sample.y * n.x + sample.z * nt.x,
sample.x * nb.y + sample.y * n.y + sample.z * nt.y,
sample.x * nb.z + sample.y * n.z + sample.z * nt.z);
}

View File

@@ -21,15 +21,21 @@ public:
int m_w; int m_w;
float m_fov; float m_fov;
float m_a_ratio; float m_a_ratio;
bool indirect_l;
Tracer(): m_h(480), m_w(640), m_fov(90.0f), m_a_ratio(640.0f / 480.0f) { } Tracer(): m_h(480), m_w(640), m_fov(90.0f), m_a_ratio(640.0f / 480.0f), indirect_l(false) { }
Tracer(int h, int w, float fov): m_h(h), m_w(w), m_fov(fov) { Tracer(int h, int w, float fov, bool il): m_h(h), m_w(w), m_fov(fov), indirect_l(il) {
m_a_ratio = static_cast<float>(w) / static_cast<float>(h); m_a_ratio = static_cast<float>(w) / static_cast<float>(h);
}; };
vec2 sample_pixel(int i, int j) const; vec2 sample_pixel(int i, int j) const;
vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const; vec3 trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> & v_lights, unsigned int rec_level) const;
private:
void create_coords_system(const vec3 &n, vec3 &nt, vec3 &nb) const;
vec3 sample_hemisphere(const float r1, const float r2) const;
void rotate_sample(vec3 & sample, const vec3 & n) const;
}; };
#endif #endif