glviewvol

changeset 8:fb6d93471352

main thing done
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 30 Dec 2014 20:03:32 +0200
parents 71b479ffb9f7
children 931a6b35f1cd
files Makefile sdr/fast.p.glsl sdr/fast.v.glsl src/dicomview.cc src/main.cc src/rend_fast.cc src/rend_fast.h
diffstat 7 files changed, 125 insertions(+), 27 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Tue Dec 30 17:28:38 2014 +0200
     1.2 +++ b/Makefile	Tue Dec 30 20:03:32 2014 +0200
     1.3 @@ -5,7 +5,7 @@
     1.4  
     1.5  bin = dicomview
     1.6  
     1.7 -#opt = -O3 -ffast-math
     1.8 +opt = -O3 -ffast-math
     1.9  dbg = -g
    1.10  def = -DUSE_GLUT
    1.11  #inc =
     2.1 --- a/sdr/fast.p.glsl	Tue Dec 30 17:28:38 2014 +0200
     2.2 +++ b/sdr/fast.p.glsl	Tue Dec 30 20:03:32 2014 +0200
     2.3 @@ -3,9 +3,17 @@
     2.4  
     2.5  void main()
     2.6  {
     2.7 +	vec3 tc = gl_TexCoord[0].xyz;
     2.8 +	float max_manh_dist = max(tc.x, max(tc.y, tc.z));
     2.9 +	float min_manh_dist = min(tc.x, min(tc.y, tc.z));
    2.10 +	float border = step(0.0, min_manh_dist) * (1.0 - step(1.0, max_manh_dist));
    2.11 +
    2.12  	vec4 voxel = texture3D(vol_tex, gl_TexCoord[0].xyz);
    2.13  	vec4 color = texture1D(xfer_tex, voxel.a);
    2.14  
    2.15 -	gl_FragColor.rgb = color.rgb;
    2.16 -	gl_FragColor.a = 1.0;
    2.17 +	float alpha = color.a * border;
    2.18 +	if(alpha < 0.001) discard;
    2.19 +
    2.20 +	gl_FragColor.rgb = voxel.rgb;
    2.21 +	gl_FragColor.a = alpha;
    2.22  }
     3.1 --- a/sdr/fast.v.glsl	Tue Dec 30 17:28:38 2014 +0200
     3.2 +++ b/sdr/fast.v.glsl	Tue Dec 30 20:03:32 2014 +0200
     3.3 @@ -1,5 +1,10 @@
     3.4  void main()
     3.5  {
     3.6 -	gl_Position = ftransform();
     3.7 -	gl_TexCoord[0] = gl_MultiTexCoord0;
     3.8 +	mat4 mvmat = gl_ModelViewMatrix;
     3.9 +	mvmat[0][0] = mvmat[1][1] = mvmat[2][2] = 1.0;
    3.10 +	mvmat[0][1] = mvmat[0][2] = mvmat[1][2] = mvmat[1][0] = mvmat[2][0] = mvmat[2][1] = 0.0;
    3.11 +
    3.12 +	vec4 vertex = gl_Vertex;
    3.13 +	gl_Position = gl_ProjectionMatrix * mvmat * vertex;
    3.14 +	gl_TexCoord[0].xyz = gl_NormalMatrix * (vertex.xyz * vec3(1.0, 1.0, -1.0) * 1.732051) * 0.5 + 0.5;
    3.15  }
     4.1 --- a/src/dicomview.cc	Tue Dec 30 17:28:38 2014 +0200
     4.2 +++ b/src/dicomview.cc	Tue Dec 30 20:03:32 2014 +0200
     4.3 @@ -8,7 +8,8 @@
     4.4  #include "xfer_view.h"
     4.5  
     4.6  static int win_width, win_height;
     4.7 -static float cam_theta, cam_phi, cam_dist = 6;
     4.8 +static float cam_theta, cam_phi, cam_dist = 4;
     4.9 +static float pre_rot = -90;
    4.10  static int splitter_y = -1;
    4.11  
    4.12  #define SPLITTER_WIDTH			5
    4.13 @@ -81,7 +82,7 @@
    4.14  	glMatrixMode(GL_MODELVIEW);
    4.15  	glLoadIdentity();
    4.16  	glTranslatef(0, 0, -cam_dist);
    4.17 -	glRotatef(cam_phi, 1, 0, 0);
    4.18 +	glRotatef(cam_phi + pre_rot, 1, 0, 0);
    4.19  	glRotatef(cam_theta, 0, 1, 0);
    4.20  
    4.21  	rend->update(0);
    4.22 @@ -135,10 +136,40 @@
    4.23  
    4.24  void ev_keyboard(int key, int press, int x, int y)
    4.25  {
    4.26 +	RendererFast *fr;
    4.27 +
    4.28  	if(press) {
    4.29  		switch(key) {
    4.30  		case 27:
    4.31  			quit();
    4.32 +
    4.33 +		case '=':
    4.34 +			if((fr = dynamic_cast<RendererFast*>(rend))) {
    4.35 +				int n = fr->get_proxy_count();
    4.36 +				int add = n / 4;
    4.37 +				n += add < 1 ? 1 : add;
    4.38 +				printf("proxy count: %d\n", n);
    4.39 +				fr->set_proxy_count(n);
    4.40 +			}
    4.41 +			redisplay();
    4.42 +			break;
    4.43 +
    4.44 +		case '-':
    4.45 +			if((fr = dynamic_cast<RendererFast*>(rend))) {
    4.46 +				int n = fr->get_proxy_count();
    4.47 +				int sub = n / 4;
    4.48 +				n -= sub < 1 ? 1 : sub;
    4.49 +
    4.50 +				if(n < 1) n = 1;
    4.51 +
    4.52 +				printf("proxy count: %d\n", n);
    4.53 +				fr->set_proxy_count(n);
    4.54 +			}
    4.55 +			redisplay();
    4.56 +			break;
    4.57 +
    4.58 +		default:
    4.59 +			break;
    4.60  		}
    4.61  	}
    4.62  }
     5.1 --- a/src/main.cc	Tue Dec 30 17:28:38 2014 +0200
     5.2 +++ b/src/main.cc	Tue Dec 30 20:03:32 2014 +0200
     5.3 @@ -95,6 +95,17 @@
     5.4  static void key_down(unsigned char key, int x, int y)
     5.5  {
     5.6  	mod = glutGetModifiers();
     5.7 +
     5.8 +	switch(key) {
     5.9 +	case 'i':
    5.10 +		{
    5.11 +			static bool fullrate;
    5.12 +			fullrate = !fullrate;
    5.13 +			glutIdleFunc(fullrate ? glutPostRedisplay : 0);
    5.14 +		}
    5.15 +		break;
    5.16 +	}
    5.17 +
    5.18  	ev_keyboard(key, 1, x, y);
    5.19  }
    5.20  
     6.1 --- a/src/rend_fast.cc	Tue Dec 30 17:28:38 2014 +0200
     6.2 +++ b/src/rend_fast.cc	Tue Dec 30 20:03:32 2014 +0200
     6.3 @@ -3,7 +3,7 @@
     6.4  #include "rend_fast.h"
     6.5  #include "sdr.h"
     6.6  
     6.7 -#define XFER_MAP_SZ		1024
     6.8 +#define XFER_MAP_SZ		512
     6.9  
    6.10  static unsigned int sdr;
    6.11  static bool have_tex_float;
    6.12 @@ -12,6 +12,8 @@
    6.13  {
    6.14  	vol_tex = xfer_tex = 0;
    6.15  	vol_tex_valid = xfer_tex_valid = false;
    6.16 +	proxy_count = 256;
    6.17 +	vbo_proxy_count = 0;
    6.18  }
    6.19  
    6.20  bool RendererFast::init()
    6.21 @@ -27,13 +29,18 @@
    6.22  	glBindTexture(GL_TEXTURE_3D, vol_tex);
    6.23  	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    6.24  	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    6.25 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    6.26 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    6.27 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    6.28  
    6.29  	glGenTextures(1, &xfer_tex);
    6.30  	glBindTexture(GL_TEXTURE_1D, xfer_tex);
    6.31  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    6.32  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    6.33 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    6.34  	glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGBA16F : GL_RGBA, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
    6.35  
    6.36 +	glGenBuffers(1, &vbo);
    6.37  	return true;
    6.38  }
    6.39  
    6.40 @@ -41,6 +48,7 @@
    6.41  {
    6.42  	glDeleteTextures(1, &vol_tex);
    6.43  	glDeleteTextures(1, &xfer_tex);
    6.44 +	glDeleteBuffers(1, &vbo);
    6.45  }
    6.46  
    6.47  void RendererFast::set_volume(Volume *vol)
    6.48 @@ -49,6 +57,16 @@
    6.49  	Renderer::set_volume(vol);
    6.50  }
    6.51  
    6.52 +void RendererFast::set_proxy_count(int n)
    6.53 +{
    6.54 +	proxy_count = n;
    6.55 +}
    6.56 +
    6.57 +int RendererFast::get_proxy_count() const
    6.58 +{
    6.59 +	return proxy_count;
    6.60 +}
    6.61 +
    6.62  void RendererFast::update(unsigned int msec)
    6.63  {
    6.64  	if(!vol) return;
    6.65 @@ -127,6 +145,32 @@
    6.66  
    6.67  		xfer_tex_valid = true;
    6.68  	}
    6.69 +
    6.70 +	// make sure the proxy object is up to date
    6.71 +	if(proxy_count != vbo_proxy_count) {
    6.72 +		static const float pat[][3] = {{-1, -1}, {1, -1}, {1, 1}, {-1, 1}};
    6.73 +
    6.74 +		int nverts = proxy_count * 4;
    6.75 +		float *verts = new float[nverts * 3];
    6.76 +		float *vptr = verts;
    6.77 +
    6.78 +		for(int i=0; i<proxy_count; i++) {
    6.79 +			float z = 2.0 * (float)i / (float)(proxy_count - 1) - 1.0;
    6.80 +
    6.81 +			for(int j=0; j<4; j++) {
    6.82 +				*vptr++ = pat[j][0];
    6.83 +				*vptr++ = pat[j][1];
    6.84 +				*vptr++ = z;
    6.85 +			}
    6.86 +		}
    6.87 +
    6.88 +		glBindBuffer(GL_ARRAY_BUFFER, vbo);
    6.89 +		glBufferData(GL_ARRAY_BUFFER, nverts * 3 * sizeof(float), verts, GL_STATIC_DRAW);
    6.90 +		glBindBuffer(GL_ARRAY_BUFFER, 0);
    6.91 +
    6.92 +		delete [] verts;
    6.93 +		vbo_proxy_count = proxy_count;
    6.94 +	}
    6.95  }
    6.96  
    6.97  void RendererFast::render() const
    6.98 @@ -135,14 +179,6 @@
    6.99  
   6.100  	glClear(GL_COLOR_BUFFER_BIT);
   6.101  
   6.102 -	glMatrixMode(GL_PROJECTION);
   6.103 -	glPushMatrix();
   6.104 -	glLoadIdentity();
   6.105 -
   6.106 -	glMatrixMode(GL_MODELVIEW);
   6.107 -	glPushMatrix();
   6.108 -	glLoadIdentity();
   6.109 -
   6.110  	glUseProgram(sdr);
   6.111  
   6.112  	glActiveTexture(GL_TEXTURE0);
   6.113 @@ -153,17 +189,18 @@
   6.114  	set_uniform_int(sdr, "vol_tex", 0);
   6.115  	set_uniform_int(sdr, "xfer_tex", 1);
   6.116  
   6.117 -	glBegin(GL_QUADS);
   6.118 -	glTexCoord3f(0, 0, 0.5); glVertex2f(-1, -1);
   6.119 -	glTexCoord3f(1, 0, 0.5); glVertex2f(1, -1);
   6.120 -	glTexCoord3f(1, 1, 0.5); glVertex2f(1, 1);
   6.121 -	glTexCoord3f(0, 1, 0.5); glVertex2f(-1, 1);
   6.122 -	glEnd();
   6.123 +	glEnable(GL_BLEND);
   6.124 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   6.125 +
   6.126 +	glBindBuffer(GL_ARRAY_BUFFER, vbo);
   6.127 +	glVertexPointer(3, GL_FLOAT, 0, 0);
   6.128 +	glBindBuffer(GL_ARRAY_BUFFER, 0);
   6.129 +
   6.130 +	glEnableClientState(GL_VERTEX_ARRAY);
   6.131 +	glDrawArrays(GL_QUADS, 0, vbo_proxy_count * 4);
   6.132 +	glDisableClientState(GL_VERTEX_ARRAY);
   6.133 +
   6.134 +	glDisable(GL_BLEND);
   6.135  
   6.136  	glUseProgram(0);
   6.137 -
   6.138 -	glMatrixMode(GL_PROJECTION);
   6.139 -	glPopMatrix();
   6.140 -	glMatrixMode(GL_MODELVIEW);
   6.141 -	glPopMatrix();
   6.142  }
     7.1 --- a/src/rend_fast.h	Tue Dec 30 17:28:38 2014 +0200
     7.2 +++ b/src/rend_fast.h	Tue Dec 30 20:03:32 2014 +0200
     7.3 @@ -8,6 +8,9 @@
     7.4  	unsigned int vol_tex, xfer_tex;
     7.5  	bool vol_tex_valid, xfer_tex_valid;
     7.6  
     7.7 +	int proxy_count, vbo_proxy_count;
     7.8 +	unsigned int vbo;
     7.9 +
    7.10  public:
    7.11  	RendererFast();
    7.12  
    7.13 @@ -16,6 +19,9 @@
    7.14  
    7.15  	void set_volume(Volume *vol);
    7.16  
    7.17 +	void set_proxy_count(int n);
    7.18 +	int get_proxy_count() const;
    7.19 +
    7.20  	void update(unsigned int msec);
    7.21  	void render() const;
    7.22  };