Added photon map implementation from the book.

This commit is contained in:
Miguel Angel Astor Romero
2017-06-27 10:38:12 -04:00
parent 4ed5eccd15
commit c97a2d4fe3
8 changed files with 747 additions and 38 deletions

View File

@@ -19,6 +19,7 @@ using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::ios;
using std::setw;
using std::vector;
@@ -32,15 +33,16 @@ using namespace glm;
PhotonTracer::~PhotonTracer() { }
vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const {
float t, _t, red, green, blue, kr, radius, r1, r2;
const float radius = m_h_radius * m_h_radius;
float t, _t, /*red, green, blue,*/ kr, r1, r2;
Figure * _f;
vec3 n, color, i_pos, ref, dir_spec_color, p_contrib, c_contrib, sample, amb_color;
Ray mv_r, sr, rr;
bool vis, is_area_light;
AreaLight * al;
Vec3 mn, mx;
vector<Photon> photons;
vector<Photon> caustics;
vector<PhotonAux> photons;
vector<PhotonAux> caustics;
t = numeric_limits<float>::max();
_f = NULL;
@@ -110,7 +112,7 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const {
}
// Calculate photon map contribution
radius = m_h_radius;
/* radius = m_h_radius;
#ifdef ENABLE_KD_TREE
Vec3 vmin(i_pos.x - m_h_radius, i_pos.y - m_h_radius, i_pos.z - m_h_radius);
Vec3 vmax(i_pos.x + m_h_radius, i_pos.y + m_h_radius, i_pos.z + m_h_radius);
@@ -146,7 +148,7 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const {
#else
m_caustics_map.find_by_distance(caustics, i_pos, n, m_h_radius, 1000);
#endif
}
}
for (Photon p : photons) {
p.getColor(red, green, blue);
@@ -154,11 +156,19 @@ vec3 PhotonTracer::trace_ray(Ray & r, Scene * s, unsigned int rec_level) const {
}
p_contrib /= (1.0f - (2.0f / (3.0f * m_cone_filter_k))) * pi<float>() * (radius * radius);
for (Photon p : caustics) {
for (PhotonAux p : caustics) {
p.getColor(red, green, blue);
c_contrib += vec3(red, green, blue);
}
c_contrib /= (1.0f - (2.0f / (3.0f * m_cone_filter_k))) * pi<float>() * (radius * radius);
c_contrib /= (1.0f - (2.0f / (3.0f * m_cone_filter_k))) * pi<float>() * (radius * radius); */
float irrad[3];
float pos[3] {i_pos.x, i_pos.y, i_pos.z};
float normal[3] {n.x, n.y, n.z};
m_photon_map.irradiance_estimate(irrad, pos, normal, m_h_radius, m_max_s_photons);
c_contrib = vec3(irrad[0], irrad[1], irrad[2]);
c_contrib /= (1.0f - (2.0f / (3.0f * m_cone_filter_k))) * pi<float>() * (radius);
// Calculate environment light contribution
vis = true;
@@ -221,7 +231,7 @@ void PhotonTracer::photon_tracing(Scene * s, const size_t n_photons_per_ligth, c
vec3 l_sample, s_normal, h_sample, power;
Vec3 ls, dir;
float r1, r2;
Photon ph;
PhotonAux ph;
uint64_t total = 0, current = 0;
vector<Figure *> spec_figures;
@@ -282,7 +292,7 @@ void PhotonTracer::photon_tracing(Scene * s, const size_t n_photons_per_ligth, c
}
// Create the primary photon.
power = (al->m_figure->m_mat->m_emission / static_cast<float>(n_photons_per_ligth));
power = (al->m_figure->m_mat->m_emission /* / static_cast<float>(n_photons_per_ligth) */);
} else if (pl != NULL) {
l_sample = glm::vec3(pl->m_position.x, pl->m_position.y, pl->m_position.z);
@@ -294,12 +304,12 @@ void PhotonTracer::photon_tracing(Scene * s, const size_t n_photons_per_ligth, c
h_sample = normalize(spec_figures[p % spec_figures.size()]->sample_at_surface() - l_sample);
}
power = (pl->m_diffuse / static_cast<float>(n_photons_per_ligth));
power = (pl->m_diffuse /* / static_cast<float>(n_photons_per_ligth)*/ );
}
ls = Vec3(l_sample.x, l_sample.y, l_sample.z);
dir = Vec3(h_sample.x, h_sample.y, h_sample.z);
ph = Photon(ls, dir, power.r, power.g, power.b, 1.0f);
ph = PhotonAux(ls, dir, power.r, power.g, power.b, 1.0f);
trace_photon(ph, s, 0);
@@ -307,16 +317,33 @@ void PhotonTracer::photon_tracing(Scene * s, const size_t n_photons_per_ligth, c
current++;
}
m_photon_map.scale_photon_power(1.0f / n_photons_per_ligth);
cout << "\r" << setw(3) << static_cast<size_t>((static_cast<double>(current) / static_cast<double>(total)) * 100.0) << "% done.";
}
cout << endl;
cout << "Generated " << ANSI_BOLD_YELLOW << m_photon_map.getNumPhotons() << ANSI_RESET_STYLE << " total photons." << endl;
m_photon_map.save_photon_list(specular ? "caustics.txt" : "photons.txt");
cout << "Generated " << ANSI_BOLD_YELLOW << m_photon_map.stored_photons << ANSI_RESET_STYLE << " total photons." << endl;
//m_photon_map.save_photon_list(specular ? "caustics.txt" : "photons.txt");
string file_name = specular ? "caustics.txt" : "photons.txt";
cout << "Writing photons to \x1b[1;33m" << file_name << "\x1b[m" << endl;
ofstream ofs(file_name, ios::out);
for (int i = 0; i < m_photon_map.stored_photons; i++) {
float r, g, b;
float dir[3];
rgbe2float(r, g, b, m_photon_map.photons[i].power);
m_photon_map.photon_dir(dir, &m_photon_map.photons[i]);
ofs << m_photon_map.photons[i].pos[0] << " " << m_photon_map.photons[i].pos[1] << " " << m_photon_map.photons[i].pos[2] << " " <<
dir[0] << " " << dir[1] << " " << dir[2] << " " <<
r << " " << g << " " << b << " " << m_photon_map.photons[i].ref_index << endl;
}
ofs.close();
}
void PhotonTracer::build_photon_map(const char * photons_file, const bool caustics) {
Photon ph;
PhotonAux ph;
float x, y, z, dx, dy, dz, r, g, b, rc;
ifstream ifs;
@@ -333,10 +360,16 @@ void PhotonTracer::build_photon_map(const char * photons_file, const bool causti
cout << "Reading photon definitions from " << ANSI_BOLD_YELLOW << photons_file << ANSI_RESET_STYLE << "." << endl;
while (!ifs.eof()) {
ifs >> x >> y >> z >> dx >> dy >> dz >> r >> g >> b >> rc;
ph = Photon(Vec3(x, y, z), Vec3(dx, dy, dz), r, g, b, rc);
m_photon_map.addPhoton(ph);
ph = PhotonAux(Vec3(x, y, z), Vec3(dx, dy, dz), r, g, b, rc);
//m_photon_map.addPhoton(ph);
float power[3] {r, g, b};
float pos[3] {x, y, z};
float dir[3] {dx, dy, dz};
m_photon_map.store(power, pos, dir, rc);
}
cout << "Read " << ANSI_BOLD_YELLOW << m_photon_map.getNumPhotons() << ANSI_RESET_STYLE << " photons from the file." << endl;
cout << "Read " << ANSI_BOLD_YELLOW << m_photon_map.stored_photons << ANSI_RESET_STYLE << " photons from the file." << endl;
ifs.close();
@@ -351,10 +384,12 @@ void PhotonTracer::build_photon_map(const bool caustics) {
else
m_caustics_map.buildKdTree();
#endif
m_photon_map.balance();
}
void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_level) {
Photon photon;
void PhotonTracer::trace_photon(PhotonAux & ph, Scene * s, const unsigned int rec_level) {
PhotonAux photon;
float t, _t, red, green, blue;
Figure * _f;
vec3 n, color, i_pos, sample, ph_dir, ph_pos;
@@ -387,8 +422,12 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l
{
p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z);
p_dir = Vec3(-ph.direction.x, -ph.direction.y, -ph.direction.z);
photon = Photon(p_pos, p_dir, red, green, blue, ph.ref_index);
m_photon_map.addPhoton(photon);
photon = PhotonAux(p_pos, p_dir, red, green, blue, ph.ref_index);
//m_photon_map.addPhoton(photon);
float power[3] {red, green, blue};
float pos[3] {p_pos.x, p_pos.y, p_pos.z};
float dir[3] {p_dir.x, p_dir.y, p_dir.z};
m_photon_map.store(power, pos, dir, ph.ref_index);
}
// Generate a photon for diffuse reflection.
@@ -400,7 +439,7 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l
color = (1.0f - _f->m_mat->m_rho) * (vec3(red, green, blue) * (_f->m_mat->m_diffuse / pi<float>()));
p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z);
p_dir = Vec3(sample.x, sample.y, sample.z);
photon = Photon(p_pos, p_dir, color.r, color.g, color.b, ph.ref_index);
photon = PhotonAux(p_pos, p_dir, color.r, color.g, color.b, ph.ref_index);
// Trace diffuse-reflected photon.
if (rec_level < m_max_depth)
@@ -413,7 +452,7 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l
p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z);
ph_dir = normalize(reflect(vec3(ph.direction.x, ph.direction.y, ph.direction.z), n));
p_dir = Vec3(ph_dir.x, ph_dir.y, ph_dir.z);
photon = Photon(p_pos, p_dir, color.r, color.g, color.b, ph.ref_index);
photon = PhotonAux(p_pos, p_dir, color.r, color.g, color.b, ph.ref_index);
trace_photon(photon, s, rec_level + 1);
}
@@ -429,7 +468,7 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l
p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z);
ph_dir = normalize(reflect(vec3(ph.direction.x, ph.direction.y, ph.direction.z), n));
p_dir = Vec3(ph_dir.x, ph_dir.y, ph_dir.z);
photon = Photon(p_pos, p_dir, color.r, color.g, color.b, ph.ref_index);
photon = PhotonAux(p_pos, p_dir, color.r, color.g, color.b, ph.ref_index);
trace_photon(photon, s, rec_level + 1);
}
@@ -440,7 +479,7 @@ void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_l
p_pos = Vec3(i_pos.x, i_pos.y, i_pos.z);
ph_dir = normalize(refract(vec3(ph.direction.x, ph.direction.y, ph.direction.z), n, ph.ref_index / _f->m_mat->m_ref_index));
p_dir = Vec3(ph_dir.x, ph_dir.y, ph_dir.z);
photon = Photon(p_pos, p_dir, color.r, color.g, color.b, _f->m_mat->m_ref_index);
photon = PhotonAux(p_pos, p_dir, color.r, color.g, color.b, _f->m_mat->m_ref_index);
trace_photon(photon, s, rec_level + 1);
}
}