Fixed a bug in pviewer.

This commit is contained in:
2017-03-06 12:46:04 -04:00
parent eb9f107feb
commit 9e75590498
7 changed files with 129 additions and 44 deletions

View File

@@ -40,18 +40,29 @@ void idle(void);
void render_scene(void); void render_scene(void);
void clean_up(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); dlist = glGenLists(1);
glNewList(dlist, GL_COMPILE); { glNewList(dlist, GL_COMPILE); {
glBegin(GL_POINTS); { glBegin(GL_POINTS); {
for (vector<pair<glm::vec3, glm::vec3>>::iterator it = photons.begin(); it != photons.end(); it++) { while (!ifs.eof()) {
glColor3f((*it).second.r, (*it).second.g, (*it).second.b); float x, y, z, dx, dy, dz, r, g, b, rc;
glVertex3f((*it).first.x, (*it).first.y, (*it).first.z); 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(); glEnd();
} }
glEndList(); glEndList();
ifs.close();
} }
int main(int argc, char ** argv) { int main(int argc, char ** argv) {
@@ -60,25 +71,10 @@ int main(int argc, char ** argv) {
ferr = freopen("stderr.txt", "w", stderr); ferr = freopen("stderr.txt", "w", stderr);
#endif #endif
if (argc < 2) { if (argc < 3) {
cerr << "USAGE: " << argv[0] << " FILE" << endl; cerr << "USAGE: " << argv[0] << " FILE MULTIPLIER" << endl;
return EXIT_FAILURE; 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, glm::vec3>(glm::vec3(x, y, z), glm::vec3(r, g, b)));
}
ifs.close();
glutInit(&argc, argv); glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
@@ -92,7 +88,8 @@ int main(int argc, char ** argv) {
atexit(clean_up); atexit(clean_up);
init_gl(); init_gl();
build_dlist(argv[1], atoi(argv[2]));
glutMainLoop(); glutMainLoop();
return 0; return 0;
@@ -103,12 +100,6 @@ void init_gl(void) {
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
/* Enable lightning. */
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
/* Enable textures. */ /* Enable textures. */
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
@@ -122,8 +113,6 @@ void init_gl(void) {
/* Hide the mouse pointer. */ /* Hide the mouse pointer. */
glutSetCursor(GLUT_CURSOR_NONE); glutSetCursor(GLUT_CURSOR_NONE);
build_dlist();
} }
void reshape(int _w, int _h) { void reshape(int _w, int _h) {

View File

@@ -334,14 +334,14 @@ bool kdTree::buildKdTree()
//printTree(); //printTree();
// delete[] xyz; delete[] xyz;
// delete[] yzx; delete[] yzx;
// delete[] zxy; delete[] zxy;
// delete[] xyz_aux; delete[] xyz_aux;
// delete[] xyz_aux2; delete[] xyz_aux2;
// delete[] xyz_aux3; delete[] xyz_aux3;
// delete[] yzx_aux; delete[] yzx_aux;
// delete[] zxy_aux; delete[] zxy_aux;
return true; return true;
} }
@@ -406,7 +406,9 @@ void kdTree::save_photon_list() const {
float r, g, b; float r, g, b;
for (std::vector<Photon>::const_iterator it = Photons.begin(); it != Photons.end(); it++) { for (std::vector<Photon>::const_iterator it = Photons.begin(); it != Photons.end(); it++) {
rgbe2float(r, g, b, (*it).radiance); 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(); ofs.close();
} }

View File

@@ -19,6 +19,12 @@ struct Vec3
float z; float z;
Vec3(float _x = 0.0f, float _y = 0.0f, float _z = 0.0f): x(_x), y(_y), z(_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) inline bool equalFloat(const float x, const float y)
{ {
@@ -47,11 +53,15 @@ struct Photon
Vec3 direction; Vec3 direction;
float ref_index; float ref_index;
unsigned char radiance[4]; 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): 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), position(_p),
direction(_d), direction(_d),
ref_index(_r) ref_index(_r),
r(red),
g(green),
b(blue)
{ {
float2rgbe(radiance, red, green, blue); float2rgbe(radiance, red, green, blue);
} }
@@ -64,7 +74,7 @@ struct Photon
{ {
return (x - std::numeric_limits<float>::epsilon() <= y) && (x + std::numeric_limits<float>::epsilon() >= y); return (x - std::numeric_limits<float>::epsilon() <= y) && (x + std::numeric_limits<float>::epsilon() >= y);
} }
inline bool operator==(const Photon p) 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); return equalFloat(position.x, p.position.x) && equalFloat(position.y, p.position.y) && equalFloat(position.z, p.position.z);

View File

@@ -48,6 +48,7 @@ static const char * OUT_FILE = "output.png";
typedef enum TRACERS { NONE, WHITTED, MONTE_CARLO, JENSEN } tracer_t; typedef enum TRACERS { NONE, WHITTED, MONTE_CARLO, JENSEN } tracer_t;
static char * g_input_file = NULL; static char * g_input_file = NULL;
static char * g_photons_file = NULL;
static char * g_out_file_name = NULL; static char * g_out_file_name = NULL;
static int g_samples = 25; static int g_samples = 25;
static float g_fov = 45.0f; static float g_fov = 45.0f;
@@ -112,8 +113,12 @@ int main(int argc, char ** argv) {
} else if(g_tracer == JENSEN) { } else if(g_tracer == JENSEN) {
cout << "Using " << ANSI_BOLD_YELLOW << "Jensen's photon mapping" << ANSI_RESET_STYLE << " with ray tracing." << endl; 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); 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; if (g_photons_file == NULL) {
p_tracer->build_photon_map(scn, g_photons, false); 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<Tracer *>(p_tracer); tracer = static_cast<Tracer *>(p_tracer);
} else { } else {
@@ -233,6 +238,9 @@ void print_usage(char ** const argv) {
cerr << " \tDefaults to 15000." << endl; cerr << " \tDefaults to 15000." << endl;
cerr << " -h\tHemisphere radius for photon map sampling (> 0)." << endl; cerr << " -h\tHemisphere radius for photon map sampling (> 0)." << endl;
cerr << " \tDefaults to 0.01f ." << 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) { void parse_args(int argc, char ** const argv) {
@@ -246,7 +254,7 @@ void parse_args(int argc, char ** const argv) {
exit(EXIT_FAILURE); 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) { switch (opt) {
case 1: case 1:
g_input_file = (char *)malloc((strlen(optarg) + 1) * sizeof(char)); g_input_file = (char *)malloc((strlen(optarg) + 1) * sizeof(char));
@@ -358,6 +366,11 @@ void parse_args(int argc, char ** const argv) {
break; break;
case 'k':
g_photons_file = (char *)malloc((strlen(optarg) + 1) * sizeof(char));
strcpy(g_photons_file, optarg);
break;
case ':': case ':':
cerr << "Option \"-" << static_cast<char>(optopt) << "\" requires an argument." << endl; cerr << "Option \"-" << static_cast<char>(optopt) << "\" requires an argument." << endl;
print_usage(argv); print_usage(argv);

View File

@@ -1,8 +1,11 @@
#include <iostream> #include <iostream>
#include <fstream>
#include <iomanip> #include <iomanip>
#include <limits> #include <limits>
#include <vector> #include <vector>
#include <utility>
#include <cstdint> #include <cstdint>
#include <cstdlib>
#include <glm/gtc/constants.hpp> #include <glm/gtc/constants.hpp>
@@ -11,9 +14,13 @@
#include "area_light.hpp" #include "area_light.hpp"
using std::cout; using std::cout;
using std::cerr;
using std::endl; using std::endl;
using std::ifstream;
using std::ios;
using std::setw; using std::setw;
using std::vector; using std::vector;
using std::pair;
using std::numeric_limits; using std::numeric_limits;
using namespace glm; 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(); 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) { void PhotonTracer::trace_photon(Photon & ph, Scene * s, const unsigned int rec_level) {
Photon photon; Photon photon;
float t, _t, red, green, blue; float t, _t, red, green, blue;

View File

@@ -14,6 +14,7 @@ public:
virtual vec3 trace_ray(Ray & r, Scene * s, unsigned int rec_level) const; 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(Scene * s, const size_t n_photons_per_ligth = 10000, const bool specular = false);
void build_photon_map(const char * photons_file);
private: private:
float m_h_radius; float m_h_radius;

39
scenes/scene8.json Normal file
View File

@@ -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]
}
}
}