glviewvol

diff src/rend_fast.cc @ 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
line diff
     1.1 --- a/src/rend_fast.cc	Tue Dec 30 17:28:38 2014 +0200
     1.2 +++ b/src/rend_fast.cc	Tue Dec 30 20:03:32 2014 +0200
     1.3 @@ -3,7 +3,7 @@
     1.4  #include "rend_fast.h"
     1.5  #include "sdr.h"
     1.6  
     1.7 -#define XFER_MAP_SZ		1024
     1.8 +#define XFER_MAP_SZ		512
     1.9  
    1.10  static unsigned int sdr;
    1.11  static bool have_tex_float;
    1.12 @@ -12,6 +12,8 @@
    1.13  {
    1.14  	vol_tex = xfer_tex = 0;
    1.15  	vol_tex_valid = xfer_tex_valid = false;
    1.16 +	proxy_count = 256;
    1.17 +	vbo_proxy_count = 0;
    1.18  }
    1.19  
    1.20  bool RendererFast::init()
    1.21 @@ -27,13 +29,18 @@
    1.22  	glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.23  	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.24  	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.25 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    1.26 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    1.27 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    1.28  
    1.29  	glGenTextures(1, &xfer_tex);
    1.30  	glBindTexture(GL_TEXTURE_1D, xfer_tex);
    1.31  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.32  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.33 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    1.34  	glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGBA16F : GL_RGBA, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
    1.35  
    1.36 +	glGenBuffers(1, &vbo);
    1.37  	return true;
    1.38  }
    1.39  
    1.40 @@ -41,6 +48,7 @@
    1.41  {
    1.42  	glDeleteTextures(1, &vol_tex);
    1.43  	glDeleteTextures(1, &xfer_tex);
    1.44 +	glDeleteBuffers(1, &vbo);
    1.45  }
    1.46  
    1.47  void RendererFast::set_volume(Volume *vol)
    1.48 @@ -49,6 +57,16 @@
    1.49  	Renderer::set_volume(vol);
    1.50  }
    1.51  
    1.52 +void RendererFast::set_proxy_count(int n)
    1.53 +{
    1.54 +	proxy_count = n;
    1.55 +}
    1.56 +
    1.57 +int RendererFast::get_proxy_count() const
    1.58 +{
    1.59 +	return proxy_count;
    1.60 +}
    1.61 +
    1.62  void RendererFast::update(unsigned int msec)
    1.63  {
    1.64  	if(!vol) return;
    1.65 @@ -127,6 +145,32 @@
    1.66  
    1.67  		xfer_tex_valid = true;
    1.68  	}
    1.69 +
    1.70 +	// make sure the proxy object is up to date
    1.71 +	if(proxy_count != vbo_proxy_count) {
    1.72 +		static const float pat[][3] = {{-1, -1}, {1, -1}, {1, 1}, {-1, 1}};
    1.73 +
    1.74 +		int nverts = proxy_count * 4;
    1.75 +		float *verts = new float[nverts * 3];
    1.76 +		float *vptr = verts;
    1.77 +
    1.78 +		for(int i=0; i<proxy_count; i++) {
    1.79 +			float z = 2.0 * (float)i / (float)(proxy_count - 1) - 1.0;
    1.80 +
    1.81 +			for(int j=0; j<4; j++) {
    1.82 +				*vptr++ = pat[j][0];
    1.83 +				*vptr++ = pat[j][1];
    1.84 +				*vptr++ = z;
    1.85 +			}
    1.86 +		}
    1.87 +
    1.88 +		glBindBuffer(GL_ARRAY_BUFFER, vbo);
    1.89 +		glBufferData(GL_ARRAY_BUFFER, nverts * 3 * sizeof(float), verts, GL_STATIC_DRAW);
    1.90 +		glBindBuffer(GL_ARRAY_BUFFER, 0);
    1.91 +
    1.92 +		delete [] verts;
    1.93 +		vbo_proxy_count = proxy_count;
    1.94 +	}
    1.95  }
    1.96  
    1.97  void RendererFast::render() const
    1.98 @@ -135,14 +179,6 @@
    1.99  
   1.100  	glClear(GL_COLOR_BUFFER_BIT);
   1.101  
   1.102 -	glMatrixMode(GL_PROJECTION);
   1.103 -	glPushMatrix();
   1.104 -	glLoadIdentity();
   1.105 -
   1.106 -	glMatrixMode(GL_MODELVIEW);
   1.107 -	glPushMatrix();
   1.108 -	glLoadIdentity();
   1.109 -
   1.110  	glUseProgram(sdr);
   1.111  
   1.112  	glActiveTexture(GL_TEXTURE0);
   1.113 @@ -153,17 +189,18 @@
   1.114  	set_uniform_int(sdr, "vol_tex", 0);
   1.115  	set_uniform_int(sdr, "xfer_tex", 1);
   1.116  
   1.117 -	glBegin(GL_QUADS);
   1.118 -	glTexCoord3f(0, 0, 0.5); glVertex2f(-1, -1);
   1.119 -	glTexCoord3f(1, 0, 0.5); glVertex2f(1, -1);
   1.120 -	glTexCoord3f(1, 1, 0.5); glVertex2f(1, 1);
   1.121 -	glTexCoord3f(0, 1, 0.5); glVertex2f(-1, 1);
   1.122 -	glEnd();
   1.123 +	glEnable(GL_BLEND);
   1.124 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.125 +
   1.126 +	glBindBuffer(GL_ARRAY_BUFFER, vbo);
   1.127 +	glVertexPointer(3, GL_FLOAT, 0, 0);
   1.128 +	glBindBuffer(GL_ARRAY_BUFFER, 0);
   1.129 +
   1.130 +	glEnableClientState(GL_VERTEX_ARRAY);
   1.131 +	glDrawArrays(GL_QUADS, 0, vbo_proxy_count * 4);
   1.132 +	glDisableClientState(GL_VERTEX_ARRAY);
   1.133 +
   1.134 +	glDisable(GL_BLEND);
   1.135  
   1.136  	glUseProgram(0);
   1.137 -
   1.138 -	glMatrixMode(GL_PROJECTION);
   1.139 -	glPopMatrix();
   1.140 -	glMatrixMode(GL_MODELVIEW);
   1.141 -	glPopMatrix();
   1.142  }