Files
PhotonMF/tracer.cpp

107 lines
2.6 KiB
C++
Raw Normal View History

2016-12-26 19:14:21 -04:00
#include <iostream>
2016-12-26 15:41:29 -04:00
#include <limits>
#include <cstdlib>
#include <cmath>
#include <omp.h>
#include "tracer.hpp"
2016-12-26 20:58:51 -04:00
#define PI 3.14159265358979f
2016-12-27 15:26:49 -04:00
#define SHININESS 89.0f
2016-12-27 19:22:34 -04:00
#define MAX_RECURSION 9
#define BIAS 0.000001f
2016-12-26 20:58:51 -04:00
2016-12-26 15:41:29 -04:00
using namespace std;
2016-12-26 19:14:21 -04:00
using std::numeric_limits;
using glm::normalize;
using glm::radians;
using glm::dot;
2016-12-27 15:26:49 -04:00
using glm::reflect;
using glm::clamp;
2016-12-26 19:14:21 -04:00
2016-12-26 15:41:29 -04:00
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f);
2016-12-26 19:14:21 -04:00
static inline float max(float a, float b) {
return a >= b ? a : b;
}
2016-12-26 15:41:29 -04:00
static inline float random01() {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
2016-12-27 15:26:49 -04:00
Tracer::Tracer(): m_h(480), m_w(640), m_fov(90.0f), m_a_ratio(640.0f / 480.0f) {}
2016-12-26 15:41:29 -04:00
Tracer::Tracer(int h, int w, float fov): m_h(h), m_w(w), m_fov(fov) {
2016-12-27 15:26:49 -04:00
m_a_ratio = static_cast<float>(w) / static_cast<float>(h);
2016-12-26 15:41:29 -04:00
}
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);
}
vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, unsigned int rec_level) const {
2016-12-26 20:58:51 -04:00
size_t f_index;
2016-12-26 19:14:21 -04:00
float t, _t, n_dot_l;
2016-12-26 15:41:29 -04:00
Figure * _f;
2016-12-27 15:26:49 -04:00
vec3 n, color, i_pos, ref;
2016-12-27 19:22:34 -04:00
Ray sr, rr;
2016-12-26 20:58:51 -04:00
bool vis;
2016-12-26 15:41:29 -04:00
t = numeric_limits<float>::max();
_f = NULL;
for (size_t f = 0; f < vf.size(); f++) {
2016-12-26 19:14:21 -04:00
if (vf[f]->intersect(r, _t) && _t < t) {
2016-12-26 15:41:29 -04:00
t = _t;
_f = vf[f];
2016-12-26 20:58:51 -04:00
f_index = f;
2016-12-26 15:41:29 -04:00
}
}
2016-12-26 19:14:21 -04:00
if (_f != NULL) {
2016-12-26 20:58:51 -04:00
i_pos = r.m_origin + (t * r.m_direction);
n = _f->normal_at_int(r, t);
if (_f->rho > 0.0f && rec_level < MAX_RECURSION) {
2016-12-27 19:22:34 -04:00
rr = Ray(reflect(r.m_direction, n), i_pos + n * BIAS);
color = _f->rho * trace_ray(rr, vf, vl, rec_level + 1);
} else if (rec_level >= MAX_RECURSION)
return vec3(BCKG_COLOR);
2016-12-26 19:14:21 -04:00
for (size_t l = 0; l < vl.size(); l++) {
2016-12-26 20:58:51 -04:00
vis = true;
2016-12-29 22:32:32 -04:00
sr = Ray(vl[l]->m_position, i_pos + n * BIAS);
2016-12-26 20:58:51 -04:00
for (size_t f = 0; f < vf.size(); f++) {
2016-12-29 22:32:32 -04:00
if (vf[f]->intersect(sr, _t)) {
2016-12-26 20:58:51 -04:00
vis = false;
break;
}
}
2016-12-27 19:22:34 -04:00
color += 0.08f * BCKG_COLOR;
2016-12-26 20:58:51 -04:00
n_dot_l = max(dot(n, vl[l]->m_position), 0.0);
color += (vis ? 1.0f : 0.0f) * (_f->color / PI) * vl[l]->m_diffuse * n_dot_l;
2016-12-27 15:26:49 -04:00
ref = reflect(vl[l]->m_position, n);
color += (vis ? 1.0f : 0.0f) * vl[l]->m_specular * pow(max(dot(ref, r.m_direction), 0.0f), SHININESS);
2016-12-26 19:14:21 -04:00
}
2016-12-27 15:26:49 -04:00
return clamp(color, 0.0f, 1.0f);
2016-12-26 19:14:21 -04:00
} else
2016-12-26 15:41:29 -04:00
return vec3(BCKG_COLOR);
}