From 811e0a060277fd81d910ace844b3504672ee7bd0 Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Wed, 15 Mar 2017 15:55:35 -0400 Subject: [PATCH] More photons. --- kd_tree.cpp | 4 ++-- kd_tree.hpp | 6 +----- photon_tracer.cpp | 43 +++++++++++++++++++++++-------------------- scenes/scene5.json | 9 --------- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/kd_tree.cpp b/kd_tree.cpp index 5fb2c15..86c543e 100644 --- a/kd_tree.cpp +++ b/kd_tree.cpp @@ -418,7 +418,7 @@ void kdTree::find_by_distance(std::vector & found, const glm::vec3 & poi p_pos = glm::vec3((*it).position.x, (*it).position.y, (*it).position.z); if (glm::distance(p_pos, point) < distance && glm::dot(glm::normalize(p_pos - point), normal) < glm::pi() / 2.0f) found.push_back((*it)); - if (found.size() >= max) - break; + // if (found.size() >= max) + // break; } } diff --git a/kd_tree.hpp b/kd_tree.hpp index 06605a7..2a3e6eb 100644 --- a/kd_tree.hpp +++ b/kd_tree.hpp @@ -20,11 +20,7 @@ struct Vec3 Vec3(float _x = 0.0f, float _y = 0.0f, float _z = 0.0f): x(_x), y(_y), z(_z) { } - Vec3(const Vec3 & other) { - x = other.x; - y = other.y; - z = other.z; - } + Vec3(const Vec3 & other) = default; inline bool equalFloat(const float x, const float y) { diff --git a/photon_tracer.cpp b/photon_tracer.cpp index 808c476..8abfa25 100644 --- a/photon_tracer.cpp +++ b/photon_tracer.cpp @@ -40,6 +40,7 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const { AreaLight * al; Vec3 mn, mx; vector photons; + vector caustics; t = numeric_limits::max(); _f = NULL; @@ -120,14 +121,19 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const { // mx = Vec3(i_pos.x + radius, i_pos.y + radius, i_pos.z + radius); // } m_photon_map.find_by_distance(photons, i_pos, n, m_h_radius, 1000); - - for (vector::iterator it = photons.begin(); it != photons.end(); it++) { - (*it).getColor(red, green, blue); - p_contrib += (_f->m_mat->m_diffuse / pi()) * vec3(red, green, blue); + m_caustics_map.find_by_distance(caustics, i_pos, n, m_h_radius, 1000); + + for (Photon p : photons) { + p.getColor(red, green, blue); + p_contrib += vec3(red, green, blue); } - p_contrib /= (1.0f / pi()) / (m_h_radius * m_h_radius); - // color += (1.0f - _f->m_mat->m_rho) * (((dir_diff_color) * (_f->m_mat->m_diffuse / pi())) + - // (_f->m_mat->m_specular * dir_spec_color) + p_contrib); + for (Photon p : caustics) { + p.getColor(red, green, blue); + p_contrib += vec3(red, green, blue); + } + p_contrib *= (1.0f / pi()) / (m_h_radius * m_h_radius); + // color += (1.0f - _f->m_mat->m_rho) * (((dir_diff_color + p_contrib) * (_f->m_mat->m_diffuse / pi())) + + // (_f->m_mat->m_specular * dir_spec_color)); color += p_contrib; // Determine the specular reflection color. @@ -154,7 +160,6 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const { color += (1.0f - kr) * trace_ray(rr, s, rec_level + 1); } else if (rec_level >= m_max_depth) return vec3(0.0f); - } // Return final color. @@ -165,7 +170,6 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const { } void PhotonTracer::photon_tracing(Scene * s, const size_t n_photons_per_ligth, const bool specular) { - Light * l; AreaLight * al = NULL; PointLight * pl = NULL; vec3 l_sample, s_normal, h_sample, power; @@ -175,18 +179,18 @@ void PhotonTracer::photon_tracing(Scene * s, const size_t n_photons_per_ligth, c uint64_t total = 0, current = 0; vector
spec_figures; - for (vector::iterator it = s->m_lights.begin(); it != s->m_lights.end(); it++) { - total += (*it)->light_type() == Light::AREA || - ((*it)->light_type() == Light::INFINITESIMAL && - (dynamic_cast((*it)) == NULL || dynamic_cast((*it)) == NULL)) ? 1 : 0; + for (Light * light : s->m_lights) { + total += light->light_type() == Light::AREA || + (light->light_type() == Light::INFINITESIMAL && + (dynamic_cast(light) == NULL || dynamic_cast(light) == NULL)) ? 1 : 0; } total *= static_cast(n_photons_per_ligth); // Separate specular objects to build the caustics photon map. if (specular) { - for (vector
::iterator it = s->m_figures.begin(); it != s->m_figures.end(); it++) - if ((*it)->m_mat->m_refract || (*it)->m_mat->m_rho > 0.0f) - spec_figures.push_back((*it)); + for (Figure * sf : s->m_figures) + if (sf->m_mat->m_refract || sf->m_mat->m_rho > 0.0f) + spec_figures.push_back(sf); if (spec_figures.size() == 0) { cout << ANSI_BOLD_YELLOW << "There are no specular objects in the scene." << ANSI_RESET_STYLE << endl; @@ -198,9 +202,7 @@ void PhotonTracer::photon_tracing(Scene * s, const size_t n_photons_per_ligth, c } cout << "Tracing a total of " << ANSI_BOLD_YELLOW << total << ANSI_RESET_STYLE << " primary photons:" << endl; - for (vector::iterator it = s->m_lights.begin(); it != s->m_lights.end(); it++) { - l = *it; - + for (Light * l : s->m_lights) { /* Only area lights and point lights supported right now. */ if (l->light_type() == Light::INFINITESIMAL && (dynamic_cast(l) != NULL || dynamic_cast(l) != NULL)) continue; @@ -306,6 +308,7 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l t = numeric_limits::max(); _f = NULL; + ph.getColor(red, green, blue); // Find the closest intersecting surface. r = Ray(ph.direction.x, ph.direction.y, ph.direction.z, ph.position.x, ph.position.y, ph.position.z); @@ -334,7 +337,6 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l sample = sample_hemisphere(r1, r2); rotate_sample(sample, n); normalize(sample); - ph.getColor(red, green, blue); color = (1.0f - _f->m_mat->m_rho) * (vec3(red, green, blue) * (_f->m_mat->m_diffuse / pi())); p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z); p_dir = Vec3(sample.x, sample.y, sample.z); @@ -354,6 +356,7 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l } } else if (_f->m_mat->m_refract && rec_level < m_max_depth) { + // If the material has transmission enabled, calculate the Fresnel term. kr = fresnel(r.m_direction, n, ph.ref_index, _f->m_mat->m_ref_index); diff --git a/scenes/scene5.json b/scenes/scene5.json index e70c37f..c72aa07 100644 --- a/scenes/scene5.json +++ b/scenes/scene5.json @@ -29,14 +29,5 @@ "diffuse": [0.0, 0.5, 1.0], "specular": [0.0, 0.0, 0.0] } - }, - - "disk": { - "position": [-1.0, 0.0, -3.0], - "normal": [1.0, 0.0, 1.0], - "radius": 3.0, - "material": { - "diffuse": [1.0, 0.5, 0.0] - } } }