Photon mapping can be executed now. Buggy as heck.
This commit is contained in:
@@ -1,24 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
#include <glm/gtc/constants.hpp>
|
||||
|
||||
#include "photon_tracer.hpp"
|
||||
#include "sampling.hpp"
|
||||
#include "area_light.hpp"
|
||||
#include "rgbe.hpp"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::setw;
|
||||
using std::vector;
|
||||
using std::numeric_limits;
|
||||
using namespace glm;
|
||||
|
||||
#define ANSI_BOLD_YELLOW "\x1b[1;33m"
|
||||
#define ANSI_RESET_STYLE "\x1b[m"
|
||||
|
||||
PhotonTracer::~PhotonTracer() { }
|
||||
|
||||
vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const {
|
||||
float t, _t;
|
||||
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, p_contrib;
|
||||
Ray mv_r, sr, rr;
|
||||
bool vis, is_area_light;
|
||||
float kr;
|
||||
AreaLight * al;
|
||||
Vec3 mn;
|
||||
Vec3 mx;
|
||||
vector<Photon> photons;
|
||||
float red, green, blue;
|
||||
|
||||
t = numeric_limits<float>::max();
|
||||
_f = NULL;
|
||||
@@ -97,6 +112,17 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const {
|
||||
} else if (_f->m_mat->m_rho > 0.0f && rec_level >= m_max_depth)
|
||||
return vec3(0.0f);
|
||||
|
||||
// TODO: Change photon map search method for hemisphere search.
|
||||
mn = Vec3(i_pos.x - m_h_radius, i_pos.y - m_h_radius, i_pos.z - m_h_radius);
|
||||
mx = Vec3(i_pos.x + m_h_radius, i_pos.y + m_h_radius, i_pos.z + m_h_radius);
|
||||
photons = m_photon_map.findInRange(mn, mx);
|
||||
for (vector<Photon>::iterator it = photons.begin(); it != photons.end(); it++) {
|
||||
rgbe2float(red, green, blue, (*it).radiance);
|
||||
p_contrib += vec3(red, green, blue);
|
||||
}
|
||||
p_contrib /= pi<float>() * (m_h_radius * m_h_radius);
|
||||
color += p_contrib;
|
||||
|
||||
} else {
|
||||
// If the material has transmission enabled, calculate the Fresnel term.
|
||||
kr = fresnel(r.m_direction, n, r.m_ref_index, _f->m_mat->m_ref_index);
|
||||
@@ -130,17 +156,26 @@ void PhotonTracer::build_photon_map(Scene * s, const size_t n_photons_per_ligth,
|
||||
vec3 l_sample, s_normal, h_sample;
|
||||
float r1, r2;
|
||||
Ray rr;
|
||||
size_t total = 0, current = 0;
|
||||
|
||||
for (vector<Light *>::iterator it = s->m_lights.begin(); it != s->m_lights.end(); it++) {
|
||||
total += (*it)->light_type() == Light::AREA ? 1 : 0;
|
||||
}
|
||||
total *= n_photons_per_ligth;
|
||||
|
||||
cout << "Tracing a total of " << ANSI_BOLD_YELLOW << total << ANSI_RESET_STYLE << " primary photons:" << endl;
|
||||
|
||||
for (vector<Light *>::iterator it = s->m_lights.begin(); it != s->m_lights.end(); it++) {
|
||||
l = *it;
|
||||
|
||||
/* Only area lights supported right now. */
|
||||
if (l->light_type() != Light::AREA)
|
||||
continue;
|
||||
|
||||
al = static_cast<AreaLight *>(l);
|
||||
|
||||
#pragma omp parallel for schedule(dynamic, 1) private(l_sample, s_normal, h_sample, r1, r2, rr) shared(current)
|
||||
for (size_t p = 0; p < n_photons_per_ligth; p++) {
|
||||
l = *it;
|
||||
|
||||
/* Only area lights supported right now. */
|
||||
if (l->light_type() != Light::AREA)
|
||||
continue;
|
||||
|
||||
al = static_cast<AreaLight *>(l);
|
||||
|
||||
if (!specular) {
|
||||
l_sample = al->sample_at_surface();
|
||||
s_normal = al->normal_at_last_sample();
|
||||
@@ -156,8 +191,17 @@ void PhotonTracer::build_photon_map(Scene * s, const size_t n_photons_per_ligth,
|
||||
}
|
||||
|
||||
trace_photon(rr, s, 0, specular);
|
||||
|
||||
#pragma omp atomic
|
||||
current++;
|
||||
}
|
||||
|
||||
cout << "\r" << setw(3) << static_cast<size_t>((static_cast<double>(current) / static_cast<double>(total)) * 100.0) << "% done.";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
cout << "Building photon map Kd-tree." << endl;
|
||||
m_photon_map.buildKdTree();
|
||||
}
|
||||
|
||||
vec3 PhotonTracer::trace_photon(Ray &r, Scene * s, const unsigned int rec_level, const bool specular) {
|
||||
@@ -199,7 +243,11 @@ vec3 PhotonTracer::trace_photon(Ray &r, Scene * s, const unsigned int rec_level,
|
||||
if (is_area_light) {
|
||||
p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z);
|
||||
photon = Photon(p_pos, _f->m_mat->m_emission.r, _f->m_mat->m_emission.g, _f->m_mat->m_emission.b);
|
||||
m_photon_map.addPhoton(photon);
|
||||
|
||||
#pragma omp critical
|
||||
{
|
||||
m_photon_map.addPhoton(photon);
|
||||
}
|
||||
|
||||
return _f->m_mat->m_emission;
|
||||
|
||||
@@ -312,7 +360,10 @@ vec3 PhotonTracer::trace_photon(Ray &r, Scene * s, const unsigned int rec_level,
|
||||
|
||||
p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z);
|
||||
photon = Photon(p_pos, color.r, color.g, color.b);
|
||||
m_photon_map.addPhoton(photon);
|
||||
#pragma omp critical
|
||||
{
|
||||
m_photon_map.addPhoton(photon);
|
||||
}
|
||||
|
||||
// Return final color.
|
||||
return color;
|
||||
|
||||
Reference in New Issue
Block a user