volmetrics

diff src/main.cc @ 25:4b6c952a83bd

works todo: 1- ta quads na nai to res tou volume * 2 2- conservative quad 3- to aspect ratio tou preview
author Eleni Maria Stea <elene.mst@gmail.com>
date Sun, 27 Apr 2014 18:13:44 +0300
parents 930c063ae346
children 5ee081af59b8
line diff
     1.1 --- a/src/main.cc	Thu Apr 24 21:33:26 2014 +0300
     1.2 +++ b/src/main.cc	Sun Apr 27 18:13:44 2014 +0300
     1.3 @@ -25,7 +25,10 @@
     1.4  static void reshape_xfer(int x, int y);
     1.5  static void mouse_xfer(int button, int state, int x, int y);
     1.6  static void motion_xfer(int x, int y);
     1.7 -static void volume_preview ();
     1.8 +static void volume_preview();
     1.9 +static void draw_slices();
    1.10 +static void draw_iso();
    1.11 +static void draw_volume();
    1.12  
    1.13  static int mainwin_id, xferwin_id;
    1.14  //todo keyb esc
    1.15 @@ -37,18 +40,23 @@
    1.16  static const char *vol_fname = "data/test1.vol";
    1.17  static const char *vsdr_path = "data/shaders/transfer.v.glsl";
    1.18  static const char *fsdr_path = "data/shaders/transfer.f.glsl";
    1.19 +static const char *vsdr_vol_path = "data/shaders/vol.v.glsl";
    1.20 +static const char *fsdr_vol_path = "data/shaders/vol.f.glsl";
    1.21 +
    1.22 +static unsigned int sprog;
    1.23 +static unsigned int sprog_vol;
    1.24  
    1.25  static Volume *vol;
    1.26  static Mesh *mesh;
    1.27  static float cur_z, thres = 0.5, thres2 = 1.0;
    1.28 -static float slice_z;
    1.29 +static float slice_z = 0.5;
    1.30  
    1.31  static int use_orig_vol_res = 1;
    1.32  static int vol_res[3]; // volume sampling resolution x/y/z
    1.33  
    1.34  static GLUI *ui;
    1.35  
    1.36 -static unsigned int sprog;
    1.37 +static int num_poly = 128;
    1.38  
    1.39  int main(int argc, char **argv)
    1.40  {
    1.41 @@ -89,6 +97,8 @@
    1.42  	glEnable(GL_LIGHT0);
    1.43  	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
    1.44  
    1.45 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.46 +
    1.47  	//FIXME shaders setup
    1.48  	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
    1.49  	{
    1.50 @@ -96,6 +106,12 @@
    1.51  		return false;
    1.52  	}
    1.53  
    1.54 +	if(!(sprog_vol = sdr_getprog(vsdr_vol_path, fsdr_vol_path)))
    1.55 +	{
    1.56 +		fprintf(stderr, "Failed to create shader program!\n");
    1.57 +		return false;
    1.58 +	}
    1.59 +
    1.60  	vol = new Volume;
    1.61  	if(!vol->load(vol_fname)) {
    1.62  		fprintf(stderr, "Failed to load %s", vol_fname);
    1.63 @@ -195,7 +211,6 @@
    1.64  {
    1.65  	float aspect = win_xsz / win_ysz;
    1.66  
    1.67 -	glDisable(GL_LIGHTING);
    1.68  	glDisable(GL_DEPTH_TEST);
    1.69  
    1.70  	glUseProgram(sprog);
    1.71 @@ -213,16 +228,16 @@
    1.72  	glMatrixMode(GL_PROJECTION);
    1.73  	glPushMatrix();
    1.74  	glLoadIdentity();
    1.75 -	glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
    1.76 +	glOrtho(0, aspect, 0, 1, -1, 1);
    1.77  
    1.78  	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
    1.79  	glEnable(GL_TEXTURE_3D);
    1.80  	glBegin(GL_QUADS);
    1.81  	glColor3f(1.0, 1.0, 1.0);
    1.82 -	glTexCoord3f(0, 0, slice_z); glVertex3f(-1.0, 1.0, 0.0);
    1.83 -	glTexCoord3f(0, 1, slice_z); glVertex3f(-1.0, 0.5, 0.0);
    1.84 -	glTexCoord3f(1, 1, slice_z); glVertex3f(-0.5, 0.5, 0.0);
    1.85 -	glTexCoord3f(1, 0, slice_z); glVertex3f(-0.5, 1.0, 0.0);
    1.86 +	glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0);
    1.87 +	glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0);
    1.88 +	glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0);
    1.89 +	glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0);
    1.90  	glEnd();
    1.91  	glDisable(GL_TEXTURE_3D);
    1.92  
    1.93 @@ -244,7 +259,72 @@
    1.94  	glUseProgram(0);
    1.95  
    1.96  	glEnable(GL_DEPTH_TEST);
    1.97 -	glEnable(GL_LIGHTING);
    1.98 +}
    1.99 +
   1.100 +static void draw_slices()
   1.101 +{
   1.102 +	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
   1.103 +	glEnable(GL_TEXTURE_3D);
   1.104 +	glBegin(GL_QUADS);
   1.105 +	glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
   1.106 +	glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
   1.107 +	glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
   1.108 +	glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
   1.109 +	glEnd();
   1.110 +	glDisable(GL_TEXTURE_3D);
   1.111 +}
   1.112 +
   1.113 +static void draw_iso()
   1.114 +{
   1.115 +	if(mesh->is_empty()) {
   1.116 +		printf("recalculating isosurface ... ");
   1.117 +		fflush(stdout);
   1.118 +		vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
   1.119 +		printf("done.\n");
   1.120 +	}
   1.121 +	mesh->draw();
   1.122 +}
   1.123 +
   1.124 +static void draw_volume()
   1.125 +{
   1.126 +	glUseProgram(sprog_vol);
   1.127 +
   1.128 +	int tmin_loc = glGetUniformLocation(sprog_vol, "tmin");
   1.129 +	if(tmin_loc != -1)
   1.130 +		glUniform1f(tmin_loc, std::min(thres, thres2));
   1.131 +	int tmax_loc = glGetUniformLocation(sprog_vol, "tmax");
   1.132 +	if(tmax_loc != -1)
   1.133 +		glUniform1f(tmax_loc, std::max(thres, thres2));
   1.134 +
   1.135 +	const Image *img = vol->get_slice(0);
   1.136 +	int res_x = img->get_width();
   1.137 +	int res_y = img->get_height();
   1.138 +	int res_z = vol->get_slice_count();
   1.139 +	int res_loc = glGetUniformLocation(sprog_vol, "res");
   1.140 +	if(res_loc != -1)
   1.141 +		glUniform3f(res_loc, (float)res_x, (float)res_y, (float)res_z);
   1.142 +
   1.143 +	glDisable(GL_DEPTH_TEST);
   1.144 +	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
   1.145 +	glEnable(GL_TEXTURE_3D);
   1.146 +	glEnable(GL_BLEND);
   1.147 +	glBegin(GL_QUADS);
   1.148 +	for(int i=0; i<num_poly; i++)
   1.149 +	{
   1.150 +		float tex_z = (float)i / (float)num_poly;
   1.151 +		float z = 2 * tex_z - 1;
   1.152 +
   1.153 +		glTexCoord3f(0, 0, tex_z); glVertex3f(-2, -2, z * 2);
   1.154 +		glTexCoord3f(0, 1, tex_z); glVertex3f(-2, 2, z * 2);
   1.155 +		glTexCoord3f(1, 1, tex_z); glVertex3f(2, 2, z * 2);
   1.156 +		glTexCoord3f(1, 0, tex_z); glVertex3f(2, -2, z * 2);
   1.157 +	}
   1.158 +	glEnd();
   1.159 +	glDisable(GL_BLEND);
   1.160 +	glDisable(GL_TEXTURE_3D);
   1.161 +	glEnable(GL_DEPTH_TEST);
   1.162 +
   1.163 +	glUseProgram(0);
   1.164  }
   1.165  
   1.166  static void display(void)
   1.167 @@ -257,26 +337,9 @@
   1.168  	glRotatef(cam_phi, 1, 0, 0);
   1.169  	glRotatef(cam_theta, 0, 1, 0);
   1.170  
   1.171 -/*
   1.172 -	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
   1.173 -	glEnable(GL_TEXTURE_3D);
   1.174 -	glBegin(GL_QUADS);
   1.175 -	glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
   1.176 -	glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
   1.177 -	glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
   1.178 -	glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
   1.179 -	glEnd();
   1.180 -	glDisable(GL_TEXTURE_3D);
   1.181 -*/
   1.182 -
   1.183 -/*	if(mesh->is_empty()) {
   1.184 -		printf("recalculating isosurface ... ");
   1.185 -		fflush(stdout);
   1.186 -		vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
   1.187 -		printf("done.\n");
   1.188 -	}
   1.189 -	mesh->draw();
   1.190 -*/
   1.191 +	//draw_slices();
   1.192 +	//draw_iso();
   1.193 +	draw_volume();
   1.194  
   1.195  	volume_preview();
   1.196  	glutSwapBuffers();
   1.197 @@ -290,10 +353,8 @@
   1.198  	glLoadIdentity();
   1.199  	gluPerspective(45, (float)x / (float)y, 0.5, 500);
   1.200  
   1.201 -	if(x != win_xsz || y != win_ysz) {
   1.202 -		win_xsz = x;
   1.203 -		win_ysz = y;
   1.204 -	}
   1.205 +	win_xsz = x;
   1.206 +	win_ysz = y;
   1.207  }
   1.208  
   1.209  static void keyboard(unsigned char key, int x, int y)