From 9e75590498cf186df2156122401f35689ca095cd Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Mon, 6 Mar 2017 12:46:04 -0400 Subject: [PATCH] Fixed a bug in pviewer. --- PhotonViewer/main.cpp | 49 +++++++++++++++++-------------------------- kd_tree.cpp | 20 ++++++++++-------- kd_tree.hpp | 14 +++++++++++-- main.cpp | 19 ++++++++++++++--- photon_tracer.cpp | 31 +++++++++++++++++++++++++++ photon_tracer.hpp | 1 + scenes/scene8.json | 39 ++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 44 deletions(-) create mode 100644 scenes/scene8.json diff --git a/PhotonViewer/main.cpp b/PhotonViewer/main.cpp index 7c819c4..5fde3dd 100644 --- a/PhotonViewer/main.cpp +++ b/PhotonViewer/main.cpp @@ -40,18 +40,29 @@ void idle(void); void render_scene(void); void clean_up(void); -void build_dlist() { +void build_dlist(const char * file_name, int color_multiplier) { + ifstream ifs(file_name, ios::in); + + if (!ifs.is_open()) { + cerr << "Failed to open the file " << file_name << " for reading." << endl; + exit(EXIT_FAILURE); + } + dlist = glGenLists(1); glNewList(dlist, GL_COMPILE); { glBegin(GL_POINTS); { - for (vector>::iterator it = photons.begin(); it != photons.end(); it++) { - glColor3f((*it).second.r, (*it).second.g, (*it).second.b); - glVertex3f((*it).first.x, (*it).first.y, (*it).first.z); + while (!ifs.eof()) { + float x, y, z, dx, dy, dz, r, g, b, rc; + ifs >> x >> y >> z >> dx >> dy >> dz >> r >> g >> b >> rc; + glColor3f(color_multiplier * r, color_multiplier * g, color_multiplier * b); + glVertex3f(x, y, z); } } glEnd(); } glEndList(); + + ifs.close(); } int main(int argc, char ** argv) { @@ -60,25 +71,10 @@ int main(int argc, char ** argv) { ferr = freopen("stderr.txt", "w", stderr); #endif - if (argc < 2) { - cerr << "USAGE: " << argv[0] << " FILE" << endl; + if (argc < 3) { + cerr << "USAGE: " << argv[0] << " FILE MULTIPLIER" << endl; return EXIT_FAILURE; } - - ifstream ifs(argv[1], ios::in); - - if (!ifs.is_open()) { - cerr << "Failed to open the file " << argv[1] << " for reading." << endl; - return EXIT_FAILURE; - } - - while (!ifs.eof()) { - float x, y, z, r, g, b; - ifs >> x >> y >> z >> r >> g >> b; - photons.push_back(pair(glm::vec3(x, y, z), glm::vec3(r, g, b))); - } - - ifs.close(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); @@ -92,7 +88,8 @@ int main(int argc, char ** argv) { atexit(clean_up); init_gl(); - + build_dlist(argv[1], atoi(argv[2])); + glutMainLoop(); return 0; @@ -103,12 +100,6 @@ void init_gl(void) { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); - /* Enable lightning. */ - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_NORMALIZE); - glShadeModel(GL_SMOOTH); - /* Enable textures. */ glEnable(GL_TEXTURE_2D); @@ -122,8 +113,6 @@ void init_gl(void) { /* Hide the mouse pointer. */ glutSetCursor(GLUT_CURSOR_NONE); - - build_dlist(); } void reshape(int _w, int _h) { diff --git a/kd_tree.cpp b/kd_tree.cpp index 23121a5..6054e4e 100644 --- a/kd_tree.cpp +++ b/kd_tree.cpp @@ -334,14 +334,14 @@ bool kdTree::buildKdTree() //printTree(); - // delete[] xyz; - // delete[] yzx; - // delete[] zxy; - // delete[] xyz_aux; - // delete[] xyz_aux2; - // delete[] xyz_aux3; - // delete[] yzx_aux; - // delete[] zxy_aux; + delete[] xyz; + delete[] yzx; + delete[] zxy; + delete[] xyz_aux; + delete[] xyz_aux2; + delete[] xyz_aux3; + delete[] yzx_aux; + delete[] zxy_aux; return true; } @@ -406,7 +406,9 @@ void kdTree::save_photon_list() const { float r, g, b; for (std::vector::const_iterator it = Photons.begin(); it != Photons.end(); it++) { rgbe2float(r, g, b, (*it).radiance); - ofs << (*it).position.x << " " << (*it).position.y << " " << (*it).position.z << " " << r << " " << g << " " << b << endl; + ofs << (*it).position.x << " " << (*it).position.y << " " << (*it).position.z << " " << + (*it).direction.x << " " << (*it).direction.y << " " << (*it).direction.z << " " << + (*it).r << " " << (*it).g << " " << (*it).b << " " << (*it).ref_index << endl; } ofs.close(); } diff --git a/kd_tree.hpp b/kd_tree.hpp index 7a15579..14f88dc 100644 --- a/kd_tree.hpp +++ b/kd_tree.hpp @@ -19,6 +19,12 @@ struct Vec3 float z; 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; + } inline bool equalFloat(const float x, const float y) { @@ -47,11 +53,15 @@ struct Photon Vec3 direction; float ref_index; unsigned char radiance[4]; + float r, g, b; Photon(Vec3 _p = Vec3(), Vec3 _d = Vec3(), float red = 0.0f, float green = 0.0f, float blue = 0.0f, float _r = 1.0f): position(_p), direction(_d), - ref_index(_r) + ref_index(_r), + r(red), + g(green), + b(blue) { float2rgbe(radiance, red, green, blue); } @@ -64,7 +74,7 @@ struct Photon { return (x - std::numeric_limits::epsilon() <= y) && (x + std::numeric_limits::epsilon() >= y); } - + inline bool operator==(const Photon p) { return equalFloat(position.x, p.position.x) && equalFloat(position.y, p.position.y) && equalFloat(position.z, p.position.z); diff --git a/main.cpp b/main.cpp index 56e4be9..c58377d 100644 --- a/main.cpp +++ b/main.cpp @@ -48,6 +48,7 @@ static const char * OUT_FILE = "output.png"; typedef enum TRACERS { NONE, WHITTED, MONTE_CARLO, JENSEN } tracer_t; static char * g_input_file = NULL; +static char * g_photons_file = NULL; static char * g_out_file_name = NULL; static int g_samples = 25; static float g_fov = 45.0f; @@ -112,8 +113,12 @@ int main(int argc, char ** argv) { } else if(g_tracer == JENSEN) { cout << "Using " << ANSI_BOLD_YELLOW << "Jensen's photon mapping" << ANSI_RESET_STYLE << " with ray tracing." << endl; p_tracer = new PhotonTracer(g_max_depth, g_p_sample_radius); - cout << "Building photon map with " << ANSI_BOLD_YELLOW << g_photons << ANSI_RESET_STYLE << " primary photons per light source." << endl; - p_tracer->build_photon_map(scn, g_photons, false); + if (g_photons_file == NULL) { + cout << "Building photon map with " << ANSI_BOLD_YELLOW << g_photons << ANSI_RESET_STYLE << " primary photons per light source." << endl; + p_tracer->build_photon_map(scn, g_photons, false); + } else { + p_tracer->build_photon_map(g_photons_file); + } tracer = static_cast(p_tracer); } else { @@ -233,6 +238,9 @@ void print_usage(char ** const argv) { cerr << " \tDefaults to 15000." << endl; cerr << " -h\tHemisphere radius for photon map sampling (> 0)." << endl; cerr << " \tDefaults to 0.01f ." << endl; + cerr << " -k\tFile with photon definitions." << endl; + cerr << " \tSkips the photon tracing step using" << endl; + cerr << " \tthe photons defined in the specified file." << endl; } void parse_args(int argc, char ** const argv) { @@ -246,7 +254,7 @@ void parse_args(int argc, char ** const argv) { exit(EXIT_FAILURE); } - while((opt = getopt(argc, argv, "-:t:s:w:f:o:r:g:e:p:h:")) != -1) { + while((opt = getopt(argc, argv, "-:t:s:w:f:o:r:g:e:p:h:k:")) != -1) { switch (opt) { case 1: g_input_file = (char *)malloc((strlen(optarg) + 1) * sizeof(char)); @@ -358,6 +366,11 @@ void parse_args(int argc, char ** const argv) { break; + case 'k': + g_photons_file = (char *)malloc((strlen(optarg) + 1) * sizeof(char)); + strcpy(g_photons_file, optarg); + break; + case ':': cerr << "Option \"-" << static_cast(optopt) << "\" requires an argument." << endl; print_usage(argv); diff --git a/photon_tracer.cpp b/photon_tracer.cpp index 9072bec..13df9c8 100644 --- a/photon_tracer.cpp +++ b/photon_tracer.cpp @@ -1,8 +1,11 @@ #include +#include #include #include #include +#include #include +#include #include @@ -11,9 +14,13 @@ #include "area_light.hpp" using std::cout; +using std::cerr; using std::endl; +using std::ifstream; +using std::ios; using std::setw; using std::vector; +using std::pair; using std::numeric_limits; using namespace glm; @@ -220,6 +227,30 @@ void PhotonTracer::build_photon_map(Scene * s, const size_t n_photons_per_ligth, m_photon_map.buildKdTree(); } +void PhotonTracer::build_photon_map(const char * photons_file) { + Photon ph; + float x, y, z, dx, dy, dz, r, g, b, rc; + ifstream ifs(photons_file, ios::in); + + if (!ifs.is_open()) { + cerr << "Failed to open the file " << photons_file << " for reading." << endl; + exit(EXIT_FAILURE); + } + + 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); + } + cout << "Read " << ANSI_BOLD_YELLOW << m_photon_map.getNumPhotons() << ANSI_RESET_STYLE << " photons from the file." << endl; + + ifs.close(); + + cout << "Building photon map Kd-tree." << endl; + m_photon_map.buildKdTree(); +} + void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_level) { Photon photon; float t, _t, red, green, blue; diff --git a/photon_tracer.hpp b/photon_tracer.hpp index 9ccf8d9..944db85 100644 --- a/photon_tracer.hpp +++ b/photon_tracer.hpp @@ -14,6 +14,7 @@ public: virtual vec3 trace_ray(Ray & r, Scene * s, unsigned int rec_level) const; void build_photon_map(Scene * s, const size_t n_photons_per_ligth = 10000, const bool specular = false); + void build_photon_map(const char * photons_file); private: float m_h_radius; diff --git a/scenes/scene8.json b/scenes/scene8.json new file mode 100644 index 0000000..88a2ce9 --- /dev/null +++ b/scenes/scene8.json @@ -0,0 +1,39 @@ +{ + "environment": { + "color": [0.0, 0.0, 0.0] + }, + + "camera": { + "eye": [2.0, 0.0, 0.0], + "look": [-1.0, -0.5, 0.0], + "left": [0.0, 0.0, 1.0], + "translation": [1.0, 0.0, 0.0] + }, + + "sphere":{ + "position": [0.0, 0.0, 0.0], + "radius": 1.0, + "material": { + "diffuse": [0.0, 0.25, 1.0], + "rho": 0.2 + } + }, + + "sphere_area_light":{ + "position": [0.0, 0.0, -15.0], + "radius": 10.0, + "material": { + "emission": [10.0, 10.0, 10.0] + } + }, + + "disk": { + "position": [0, -1.0, 0], + "normal": [0.0, 1.0, 0.0], + "radius": 2.0, + "material": { + "diffuse": [1.0, 1.0, 1.0], + "specular": [0.0, 0.0, 0.0] + } + } +}