# HG changeset patch # User Eleni Maria Stea # Date 1390778522 -7200 # Node ID c5af275b6a600a6771796b47071ae2c5acd03123 # Parent 8ffa6d61eb56fee482c744b17410cb2f4b216b96 fixed normals diff -r 8ffa6d61eb56 -r c5af275b6a60 src/main.cc --- a/src/main.cc Sun Jan 26 23:18:13 2014 +0200 +++ b/src/main.cc Mon Jan 27 01:22:02 2014 +0200 @@ -23,9 +23,7 @@ static float cam_phi, cam_theta, cam_dist = 6; static std::vector key_state(256); -///////////////////////////// -// debug TODO remove -//////////////////////////// +static const char *vol_fname = "data/test1.vol"; static Volume *vol; static Mesh *mesh; @@ -40,9 +38,10 @@ { glutInit(&argc, argv); glutInitWindowSize(512, 512); - glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); - //TODO parse arguments + if(argv[1]) + vol_fname = argv[1]; glutCreateWindow("My Colonoscopie OEO!"); @@ -67,9 +66,16 @@ static bool init() { + glEnable(GL_DEPTH_TEST); + glEnable(GL_NORMALIZE); + + glEnable(GL_LIGHTING); //TODO: shaders + glEnable(GL_LIGHT0); + glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1); + vol = new Volume; - if(!vol->load("data/test1.vol")) { - fprintf(stderr, "Failed to load test1.vol"); + if(!vol->load(vol_fname)) { + fprintf(stderr, "Failed to load %s", vol_fname); return false; } mesh = new Mesh; @@ -188,7 +194,6 @@ case 'w': { static bool wire; - wire = !wire; glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL); } diff -r 8ffa6d61eb56 -r c5af275b6a60 src/volume.cc --- a/src/volume.cc Sun Jan 26 23:18:13 2014 +0200 +++ b/src/volume.cc Mon Jan 27 01:22:02 2014 +0200 @@ -180,6 +180,19 @@ return pixels[px + img->get_width() * py]; } +static void cb_vertex(float x, float y, float z) +{ + float dx = 1.0 / cur_vol->get_slice(0)->get_width(); + float dy = 1.0 / cur_vol->get_slice(0)->get_height(); + float dz = 1.0 / cur_vol->get_slice_count(); + float dfdx = cb_eval(x - dx, y, z) - cb_eval(x + dx, y, z); + float dfdy = cb_eval(x, y - dy, z) - cb_eval(x, y + dy, z); + float dfdz = cb_eval(x, y, z - dz) - cb_eval(x, y, z + dz); + + cur_mesh->add_normal(Vector3(dfdx, dfdy, dfdz)); + cur_mesh->add_vertex(Vector3(x, y, z)); +} + void Volume::create_mesh(Mesh *mesh, float threshold) { cur_mesh = mesh; @@ -188,11 +201,7 @@ metasurface *ms = msurf_create(); msurf_threshold(ms, threshold); msurf_resolution(ms, width, height, slices.size()); - msurf_vertex_func(ms, - [](float x, float y, float z){cur_mesh->add_vertex(Vector3(x, y, z));}); - msurf_normal_func(ms, - [](float x, float y, float z){cur_mesh->add_normal(Vector3(x, y, z));}); - + msurf_vertex_func(ms, cb_vertex); msurf_eval_func(ms, cb_eval); msurf_polygonize(ms); msurf_free(ms);