volmetrics

changeset 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 c4662d60cd93
children 5ee081af59b8
files data/shaders/vol.f.glsl data/shaders/vol.v.glsl notes src/main.cc
diffstat 4 files changed, 154 insertions(+), 46 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/data/shaders/vol.f.glsl	Sun Apr 27 18:13:44 2014 +0300
     1.3 @@ -0,0 +1,40 @@
     1.4 +varying vec3 pt;
     1.5 +
     1.6 +uniform sampler3D tex;
     1.7 +uniform float tmin;
     1.8 +uniform float tmax;
     1.9 +uniform vec3 res;
    1.10 +
    1.11 +float transfer(float x, float tmin, float tmax)
    1.12 +{
    1.13 +	float dt = 0.25 * (tmax - tmin);
    1.14 +	return smoothstep(tmin - dt, tmin + dt, x) *
    1.15 +		(1.0 - smoothstep(tmax - dt, tmax + dt, x));
    1.16 +}
    1.17 +
    1.18 +vec3 calc_normal(vec3 p, float texel)
    1.19 +{
    1.20 +	vec3 offs = 1.0 / res;
    1.21 +
    1.22 +	float dfdx = texture3D(tex, p + vec3(offs.x, 0.0, 0.0)).x - texel;
    1.23 +	float dfdy = texture3D(tex, p + vec3(0.0, offs.y, 0.0)).x - texel;
    1.24 +	float dfdz = texture3D(tex, p + vec3(0.0, 0.0, offs.z)).x - texel;
    1.25 +
    1.26 +	return normalize(vec3(dfdx, dfdy, dfdz));
    1.27 +}
    1.28 +
    1.29 +void main()
    1.30 +{
    1.31 +	const vec3 ldir = vec3(-1.0, 1.0, 2.0);
    1.32 +
    1.33 +	if(max(pt.x, max(pt.y, pt.z)) > 1.0 || min(pt.x, min(pt.y, pt.z)) < 0.0)
    1.34 +		discard;
    1.35 +
    1.36 +	float texel = texture3D(tex, pt).x;
    1.37 +	float val = transfer(texel, tmin, tmax);
    1.38 +
    1.39 +	vec3 normal = calc_normal(pt, texel);
    1.40 +	float ndotl = max(dot(normalize(ldir), normal), 0.0);
    1.41 +
    1.42 +	gl_FragColor = vec4(ndotl, ndotl, ndotl, val);
    1.43 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/data/shaders/vol.v.glsl	Sun Apr 27 18:13:44 2014 +0300
     2.3 @@ -0,0 +1,16 @@
     2.4 +#version 120
     2.5 +
     2.6 +varying vec3 pt;
     2.7 +
     2.8 +void main()
     2.9 +{
    2.10 +	mat4 mvbb = gl_ModelViewMatrix;
    2.11 +
    2.12 +	mvbb[0][0] = mvbb[1][1] = mvbb[2][2] = 1.0;
    2.13 +	mvbb[0][1] = mvbb[0][2] = mvbb[1][2] = 0.0;
    2.14 +	mvbb[1][0] = mvbb[2][0] = mvbb[2][1] = 0.0;
    2.15 +	
    2.16 +	gl_Position = gl_ProjectionMatrix * mvbb * gl_Vertex;
    2.17 +
    2.18 +	pt = (transpose(gl_NormalMatrix) * gl_Vertex.xyz) * 0.5 + 0.5;
    2.19 +}
     3.1 --- a/notes	Thu Apr 24 21:33:26 2014 +0300
     3.2 +++ b/notes	Sun Apr 27 18:13:44 2014 +0300
     3.3 @@ -1,12 +1,3 @@
     3.4 -na balw to t tou gui -1000 ws 1000 k na ginetai map sto 0 ws 1
     3.5 -
     3.6 -interpolation stin eval
     3.7 -
     3.8 -optimisations
     3.9 -
    3.10 -VBO
    3.11 -
    3.12 -*******************
    3.13 -MOTION sto main.cc pros to kontinotero akro:
    3.14 -map to x apo pixel se [0,1] k.a.
    3.15 -************
    3.16 +a = density
    3.17 +normals
    3.18 +me to gradient: prev k epomeno
     4.1 --- a/src/main.cc	Thu Apr 24 21:33:26 2014 +0300
     4.2 +++ b/src/main.cc	Sun Apr 27 18:13:44 2014 +0300
     4.3 @@ -25,7 +25,10 @@
     4.4  static void reshape_xfer(int x, int y);
     4.5  static void mouse_xfer(int button, int state, int x, int y);
     4.6  static void motion_xfer(int x, int y);
     4.7 -static void volume_preview ();
     4.8 +static void volume_preview();
     4.9 +static void draw_slices();
    4.10 +static void draw_iso();
    4.11 +static void draw_volume();
    4.12  
    4.13  static int mainwin_id, xferwin_id;
    4.14  //todo keyb esc
    4.15 @@ -37,18 +40,23 @@
    4.16  static const char *vol_fname = "data/test1.vol";
    4.17  static const char *vsdr_path = "data/shaders/transfer.v.glsl";
    4.18  static const char *fsdr_path = "data/shaders/transfer.f.glsl";
    4.19 +static const char *vsdr_vol_path = "data/shaders/vol.v.glsl";
    4.20 +static const char *fsdr_vol_path = "data/shaders/vol.f.glsl";
    4.21 +
    4.22 +static unsigned int sprog;
    4.23 +static unsigned int sprog_vol;
    4.24  
    4.25  static Volume *vol;
    4.26  static Mesh *mesh;
    4.27  static float cur_z, thres = 0.5, thres2 = 1.0;
    4.28 -static float slice_z;
    4.29 +static float slice_z = 0.5;
    4.30  
    4.31  static int use_orig_vol_res = 1;
    4.32  static int vol_res[3]; // volume sampling resolution x/y/z
    4.33  
    4.34  static GLUI *ui;
    4.35  
    4.36 -static unsigned int sprog;
    4.37 +static int num_poly = 128;
    4.38  
    4.39  int main(int argc, char **argv)
    4.40  {
    4.41 @@ -89,6 +97,8 @@
    4.42  	glEnable(GL_LIGHT0);
    4.43  	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
    4.44  
    4.45 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    4.46 +
    4.47  	//FIXME shaders setup
    4.48  	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
    4.49  	{
    4.50 @@ -96,6 +106,12 @@
    4.51  		return false;
    4.52  	}
    4.53  
    4.54 +	if(!(sprog_vol = sdr_getprog(vsdr_vol_path, fsdr_vol_path)))
    4.55 +	{
    4.56 +		fprintf(stderr, "Failed to create shader program!\n");
    4.57 +		return false;
    4.58 +	}
    4.59 +
    4.60  	vol = new Volume;
    4.61  	if(!vol->load(vol_fname)) {
    4.62  		fprintf(stderr, "Failed to load %s", vol_fname);
    4.63 @@ -195,7 +211,6 @@
    4.64  {
    4.65  	float aspect = win_xsz / win_ysz;
    4.66  
    4.67 -	glDisable(GL_LIGHTING);
    4.68  	glDisable(GL_DEPTH_TEST);
    4.69  
    4.70  	glUseProgram(sprog);
    4.71 @@ -213,16 +228,16 @@
    4.72  	glMatrixMode(GL_PROJECTION);
    4.73  	glPushMatrix();
    4.74  	glLoadIdentity();
    4.75 -	glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
    4.76 +	glOrtho(0, aspect, 0, 1, -1, 1);
    4.77  
    4.78  	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
    4.79  	glEnable(GL_TEXTURE_3D);
    4.80  	glBegin(GL_QUADS);
    4.81  	glColor3f(1.0, 1.0, 1.0);
    4.82 -	glTexCoord3f(0, 0, slice_z); glVertex3f(-1.0, 1.0, 0.0);
    4.83 -	glTexCoord3f(0, 1, slice_z); glVertex3f(-1.0, 0.5, 0.0);
    4.84 -	glTexCoord3f(1, 1, slice_z); glVertex3f(-0.5, 0.5, 0.0);
    4.85 -	glTexCoord3f(1, 0, slice_z); glVertex3f(-0.5, 1.0, 0.0);
    4.86 +	glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0);
    4.87 +	glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0);
    4.88 +	glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0);
    4.89 +	glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0);
    4.90  	glEnd();
    4.91  	glDisable(GL_TEXTURE_3D);
    4.92  
    4.93 @@ -244,7 +259,72 @@
    4.94  	glUseProgram(0);
    4.95  
    4.96  	glEnable(GL_DEPTH_TEST);
    4.97 -	glEnable(GL_LIGHTING);
    4.98 +}
    4.99 +
   4.100 +static void draw_slices()
   4.101 +{
   4.102 +	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
   4.103 +	glEnable(GL_TEXTURE_3D);
   4.104 +	glBegin(GL_QUADS);
   4.105 +	glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
   4.106 +	glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
   4.107 +	glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
   4.108 +	glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
   4.109 +	glEnd();
   4.110 +	glDisable(GL_TEXTURE_3D);
   4.111 +}
   4.112 +
   4.113 +static void draw_iso()
   4.114 +{
   4.115 +	if(mesh->is_empty()) {
   4.116 +		printf("recalculating isosurface ... ");
   4.117 +		fflush(stdout);
   4.118 +		vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
   4.119 +		printf("done.\n");
   4.120 +	}
   4.121 +	mesh->draw();
   4.122 +}
   4.123 +
   4.124 +static void draw_volume()
   4.125 +{
   4.126 +	glUseProgram(sprog_vol);
   4.127 +
   4.128 +	int tmin_loc = glGetUniformLocation(sprog_vol, "tmin");
   4.129 +	if(tmin_loc != -1)
   4.130 +		glUniform1f(tmin_loc, std::min(thres, thres2));
   4.131 +	int tmax_loc = glGetUniformLocation(sprog_vol, "tmax");
   4.132 +	if(tmax_loc != -1)
   4.133 +		glUniform1f(tmax_loc, std::max(thres, thres2));
   4.134 +
   4.135 +	const Image *img = vol->get_slice(0);
   4.136 +	int res_x = img->get_width();
   4.137 +	int res_y = img->get_height();
   4.138 +	int res_z = vol->get_slice_count();
   4.139 +	int res_loc = glGetUniformLocation(sprog_vol, "res");
   4.140 +	if(res_loc != -1)
   4.141 +		glUniform3f(res_loc, (float)res_x, (float)res_y, (float)res_z);
   4.142 +
   4.143 +	glDisable(GL_DEPTH_TEST);
   4.144 +	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
   4.145 +	glEnable(GL_TEXTURE_3D);
   4.146 +	glEnable(GL_BLEND);
   4.147 +	glBegin(GL_QUADS);
   4.148 +	for(int i=0; i<num_poly; i++)
   4.149 +	{
   4.150 +		float tex_z = (float)i / (float)num_poly;
   4.151 +		float z = 2 * tex_z - 1;
   4.152 +
   4.153 +		glTexCoord3f(0, 0, tex_z); glVertex3f(-2, -2, z * 2);
   4.154 +		glTexCoord3f(0, 1, tex_z); glVertex3f(-2, 2, z * 2);
   4.155 +		glTexCoord3f(1, 1, tex_z); glVertex3f(2, 2, z * 2);
   4.156 +		glTexCoord3f(1, 0, tex_z); glVertex3f(2, -2, z * 2);
   4.157 +	}
   4.158 +	glEnd();
   4.159 +	glDisable(GL_BLEND);
   4.160 +	glDisable(GL_TEXTURE_3D);
   4.161 +	glEnable(GL_DEPTH_TEST);
   4.162 +
   4.163 +	glUseProgram(0);
   4.164  }
   4.165  
   4.166  static void display(void)
   4.167 @@ -257,26 +337,9 @@
   4.168  	glRotatef(cam_phi, 1, 0, 0);
   4.169  	glRotatef(cam_theta, 0, 1, 0);
   4.170  
   4.171 -/*
   4.172 -	glBindTexture(GL_TEXTURE_3D, vol->get_texture());
   4.173 -	glEnable(GL_TEXTURE_3D);
   4.174 -	glBegin(GL_QUADS);
   4.175 -	glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
   4.176 -	glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
   4.177 -	glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
   4.178 -	glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
   4.179 -	glEnd();
   4.180 -	glDisable(GL_TEXTURE_3D);
   4.181 -*/
   4.182 -
   4.183 -/*	if(mesh->is_empty()) {
   4.184 -		printf("recalculating isosurface ... ");
   4.185 -		fflush(stdout);
   4.186 -		vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
   4.187 -		printf("done.\n");
   4.188 -	}
   4.189 -	mesh->draw();
   4.190 -*/
   4.191 +	//draw_slices();
   4.192 +	//draw_iso();
   4.193 +	draw_volume();
   4.194  
   4.195  	volume_preview();
   4.196  	glutSwapBuffers();
   4.197 @@ -290,10 +353,8 @@
   4.198  	glLoadIdentity();
   4.199  	gluPerspective(45, (float)x / (float)y, 0.5, 500);
   4.200  
   4.201 -	if(x != win_xsz || y != win_ysz) {
   4.202 -		win_xsz = x;
   4.203 -		win_ysz = y;
   4.204 -	}
   4.205 +	win_xsz = x;
   4.206 +	win_ysz = y;
   4.207  }
   4.208  
   4.209  static void keyboard(unsigned char key, int x, int y)