Added refractions and fresnel.
This commit is contained in:
42
tracer.cpp
42
tracer.cpp
@@ -1,8 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <omp.h>
|
||||
|
||||
#include "tracer.hpp"
|
||||
|
||||
#define MAX_RECURSION 9
|
||||
@@ -15,8 +14,10 @@ using glm::normalize;
|
||||
using glm::radians;
|
||||
using glm::dot;
|
||||
using glm::reflect;
|
||||
using glm::refract;
|
||||
using glm::clamp;
|
||||
using glm::tan;
|
||||
using glm::cos;
|
||||
|
||||
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f);
|
||||
|
||||
@@ -24,16 +25,30 @@ static inline float random01() {
|
||||
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
|
||||
}
|
||||
|
||||
static float fresnel(const vec3 & i, const vec3 & n, const float ir1, const float ir2) {
|
||||
float cos_t1 = dot(i, n);
|
||||
float cos_t2 = dot(normalize(refract(i, n, ir1 / ir2)), n);
|
||||
float sin_t2 = (ir1 / ir2) * sqrt(1.0f - (cos_t2 * cos_t2));
|
||||
|
||||
if (sin_t2 >= 1.0f)
|
||||
return 1.0f;
|
||||
|
||||
float fr_par = ((ir2 * cos_t1) - (ir1 * cos_t2)) / ((ir2 * cos_t1) + (ir1 * cos_t2));
|
||||
float fr_per = ((ir1 * cos_t2) - (ir2 * cos_t1)) / ((ir1 * cos_t2) + (ir2 * cos_t1));
|
||||
|
||||
return ((fr_par * fr_par) + (fr_per * fr_per)) / 2.0f;
|
||||
}
|
||||
|
||||
vec2 Tracer::sample_pixel(int i, int j) const {
|
||||
float pxNDC;
|
||||
float pyNDC;
|
||||
float pxS;
|
||||
float pyS;
|
||||
pyNDC = (static_cast<float>(i) + random01()) / m_h;
|
||||
pyS = (1.0f - (2.0f * pyNDC)) * tan(radians(m_fov) / 2);
|
||||
pyS = (1.0f - (2.0f * pyNDC)) * tan(radians(m_fov / 2));
|
||||
pxNDC = (static_cast<float>(j) + random01()) / m_w;
|
||||
pxS = (2.0f * pxNDC) - 1.0f;
|
||||
pxS *= m_a_ratio * tan(radians(m_fov) / 2);
|
||||
pxS *= m_a_ratio * tan(radians(m_fov / 2));
|
||||
|
||||
return vec2(pxS, pyS);
|
||||
}
|
||||
@@ -44,6 +59,7 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> &
|
||||
vec3 n, color, i_pos, ref;
|
||||
Ray sr, rr;
|
||||
bool vis;
|
||||
float kr;
|
||||
|
||||
t = numeric_limits<float>::max();
|
||||
_f = NULL;
|
||||
@@ -73,9 +89,21 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & v_figures, vector<Light *> &
|
||||
color += (vis ? 1.0f : 0.0f) * v_lights[l]->shade(n, r, _f->m_mat);
|
||||
}
|
||||
|
||||
if (_f->m_mat.m_rho > 0.0f && rec_level < MAX_RECURSION) {
|
||||
rr = Ray(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);
|
||||
if (_f->m_mat.m_refract)
|
||||
kr = fresnel(r.m_direction, n, r.m_ref_index, _f->m_mat.m_ref_index);
|
||||
else
|
||||
kr = _f->m_mat.m_rho;
|
||||
|
||||
if (kr > 0.0f && rec_level < MAX_RECURSION) {
|
||||
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);
|
||||
|
||||
} else if (rec_level >= MAX_RECURSION)
|
||||
return vec3(BCKG_COLOR);
|
||||
|
||||
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);
|
||||
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)
|
||||
return vec3(BCKG_COLOR);
|
||||
|
||||
Reference in New Issue
Block a user