volmetrics

changeset 11:c5af275b6a60

fixed normals
author Eleni Maria Stea <elene.mst@gmail.com>
date Mon, 27 Jan 2014 01:22:02 +0200
parents 8ffa6d61eb56
children 1272e3335608
files src/main.cc src/volume.cc
diffstat 2 files changed, 27 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/src/main.cc	Sun Jan 26 23:18:13 2014 +0200
     1.2 +++ b/src/main.cc	Mon Jan 27 01:22:02 2014 +0200
     1.3 @@ -23,9 +23,7 @@
     1.4  static float cam_phi, cam_theta, cam_dist = 6;
     1.5  static std::vector<bool> key_state(256);
     1.6  
     1.7 -/////////////////////////////
     1.8 -// debug TODO remove
     1.9 -////////////////////////////
    1.10 +static const char *vol_fname = "data/test1.vol";
    1.11  
    1.12  static Volume *vol;
    1.13  static Mesh *mesh;
    1.14 @@ -40,9 +38,10 @@
    1.15  {
    1.16  	glutInit(&argc, argv);
    1.17  	glutInitWindowSize(512, 512);
    1.18 -	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    1.19 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.20  
    1.21 -	//TODO parse arguments
    1.22 +	if(argv[1])
    1.23 +		vol_fname = argv[1];
    1.24  
    1.25  	glutCreateWindow("My Colonoscopie OEO!");
    1.26  
    1.27 @@ -67,9 +66,16 @@
    1.28  
    1.29  static bool init()
    1.30  {
    1.31 +	glEnable(GL_DEPTH_TEST);
    1.32 +	glEnable(GL_NORMALIZE);
    1.33 +
    1.34 +	glEnable(GL_LIGHTING); //TODO: shaders
    1.35 +	glEnable(GL_LIGHT0);
    1.36 +	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
    1.37 +
    1.38  	vol = new Volume;
    1.39 -	if(!vol->load("data/test1.vol")) {
    1.40 -		fprintf(stderr, "Failed to load test1.vol");
    1.41 +	if(!vol->load(vol_fname)) {
    1.42 +		fprintf(stderr, "Failed to load %s", vol_fname);
    1.43  		return false;
    1.44  	}
    1.45  	mesh = new Mesh;
    1.46 @@ -188,7 +194,6 @@
    1.47  		case 'w':
    1.48  			{
    1.49  				static bool wire;
    1.50 -
    1.51  				wire = !wire;
    1.52  				glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
    1.53  			}
     2.1 --- a/src/volume.cc	Sun Jan 26 23:18:13 2014 +0200
     2.2 +++ b/src/volume.cc	Mon Jan 27 01:22:02 2014 +0200
     2.3 @@ -180,6 +180,19 @@
     2.4  	return pixels[px + img->get_width() * py];
     2.5  }
     2.6  
     2.7 +static void cb_vertex(float x, float y, float z)
     2.8 +{
     2.9 +	float dx = 1.0 / cur_vol->get_slice(0)->get_width();
    2.10 +	float dy = 1.0 / cur_vol->get_slice(0)->get_height();
    2.11 +	float dz = 1.0 / cur_vol->get_slice_count();
    2.12 +	float dfdx = cb_eval(x - dx, y, z) - cb_eval(x + dx, y, z);
    2.13 +	float dfdy = cb_eval(x, y - dy, z) - cb_eval(x, y + dy, z);
    2.14 +	float dfdz = cb_eval(x, y, z - dz) - cb_eval(x, y, z + dz);
    2.15 +
    2.16 +	cur_mesh->add_normal(Vector3(dfdx, dfdy, dfdz));
    2.17 +	cur_mesh->add_vertex(Vector3(x, y, z));
    2.18 +}
    2.19 +
    2.20  void Volume::create_mesh(Mesh *mesh, float threshold)
    2.21  {
    2.22  	cur_mesh = mesh;
    2.23 @@ -188,11 +201,7 @@
    2.24  	metasurface *ms = msurf_create();
    2.25  	msurf_threshold(ms, threshold);
    2.26  	msurf_resolution(ms, width, height, slices.size());
    2.27 -	msurf_vertex_func(ms,
    2.28 -			[](float x, float y, float z){cur_mesh->add_vertex(Vector3(x, y, z));});
    2.29 -	msurf_normal_func(ms,
    2.30 -			[](float x, float y, float z){cur_mesh->add_normal(Vector3(x, y, z));});
    2.31 -
    2.32 +	msurf_vertex_func(ms, cb_vertex);
    2.33  	msurf_eval_func(ms, cb_eval);
    2.34  	msurf_polygonize(ms);
    2.35  	msurf_free(ms);