Added camera abstraction.

This commit is contained in:
Miguel Angel Astor Romero
2017-01-12 17:23:11 -04:00
parent 991d339b65
commit 724a98f8a0
12 changed files with 215 additions and 93 deletions

View File

@@ -1,23 +1,17 @@
#include <cstdlib>
#include <glm/gtc/constants.hpp>
#include "tracer.hpp"
using namespace glm;
using glm::dot;
using glm::normalize;
using glm::refract;
const float BIAS = 0.000001f;
const vec3 BCKG_COLOR = vec3(1.0f);
float Tracer::random01() const {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
float Tracer::fresnel(const vec3 & i, const vec3 & n, const float ir1, const float ir2) const {
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));
float sin_t2 = (ir1 / ir2) * glm::sqrt(1.0f - (cos_t2 * cos_t2));
if (sin_t2 >= 1.0f)
return 1.0f;
@@ -27,44 +21,3 @@ float Tracer::fresnel(const vec3 & i, const vec3 & n, const float ir1, const flo
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));
pxNDC = (static_cast<float>(j) + random01()) / m_w;
pxS = (2.0f * pxNDC) - 1.0f;
pxS *= m_a_ratio * tan(radians(m_fov / 2));
return vec2(pxS, pyS);
}
/* 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);
}