glviewvol

diff src/rend_fast.cc @ 0:7bdf40403b9c

dicom viewer project underway
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Dec 2014 02:35:58 +0200
parents
children 04330eb80b36
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/rend_fast.cc	Sat Dec 27 02:35:58 2014 +0200
     1.3 @@ -0,0 +1,102 @@
     1.4 +#include "opengl.h"
     1.5 +#include "rend_fast.h"
     1.6 +
     1.7 +RendererFast::RendererFast()
     1.8 +{
     1.9 +	vol_tex = 0;
    1.10 +	vol_tex_valid = false;
    1.11 +}
    1.12 +
    1.13 +bool RendererFast::init()
    1.14 +{
    1.15 +	glGenTextures(1, &vol_tex);
    1.16 +	glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.17 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.18 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.19 +
    1.20 +	return true;
    1.21 +}
    1.22 +
    1.23 +void RendererFast::destroy()
    1.24 +{
    1.25 +	glDeleteTextures(1, &vol_tex);
    1.26 +}
    1.27 +
    1.28 +void RendererFast::update(unsigned int msec)
    1.29 +{
    1.30 +	if(!vol) return;
    1.31 +
    1.32 +	// make sure the 3D volume texture is up to date
    1.33 +	if(!vol_tex_valid) {
    1.34 +		int xsz, ysz, zsz;
    1.35 +
    1.36 +		if((xsz = vol->num_samples(0)) > 0) {
    1.37 +			ysz = vol->num_samples(1);
    1.38 +			zsz = vol->num_samples(2);
    1.39 +		} else {
    1.40 +			xsz = ysz = zsz = 256;
    1.41 +		}
    1.42 +
    1.43 +		int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
    1.44 +
    1.45 +		glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.46 +		glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
    1.47 +
    1.48 +		float *slice = new float[xsz * ysz * 4];
    1.49 +
    1.50 +		for(int i=0; i<zsz; i++) {
    1.51 +			float z = (float)i / (float)(zsz - 1);
    1.52 +			float *pptr = slice;
    1.53 +
    1.54 +			for(int j=0; j<ysz; j++) {
    1.55 +				float y = (float)j / (float)(ysz - 1);
    1.56 +				for(int k=0; k<xsz; k++) {
    1.57 +					float x = (float)k / (float)(xsz - 1);
    1.58 +
    1.59 +					// value in alpha channel
    1.60 +					pptr[3] = vol->valuef(x, y, z);
    1.61 +					// normal in rgb
    1.62 +					vol->normalf(pptr, x, y, z);
    1.63 +					// shift normal to the [0,1] range in case we don't have tex_float
    1.64 +					pptr[0] = pptr[0] * 0.5 + 0.5;
    1.65 +					pptr[1] = pptr[1] * 0.5 + 0.5;
    1.66 +					pptr[2] = pptr[2] * 0.5 + 0.5;
    1.67 +				}
    1.68 +			}
    1.69 +
    1.70 +			glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
    1.71 +		}
    1.72 +
    1.73 +		delete [] slice;
    1.74 +
    1.75 +		vol_tex_valid = true;
    1.76 +	}
    1.77 +}
    1.78 +
    1.79 +void RendererFast::render() const
    1.80 +{
    1.81 +	if(!vol) return;
    1.82 +
    1.83 +	glClear(GL_COLOR_BUFFER_BIT);
    1.84 +
    1.85 +	glMatrixMode(GL_PROJECTION);
    1.86 +	glPushMatrix();
    1.87 +	glLoadIdentity();
    1.88 +
    1.89 +	glMatrixMode(GL_MODELVIEW);
    1.90 +	glPushMatrix();
    1.91 +	glLoadIdentity();
    1.92 +
    1.93 +	glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.94 +	glBegin(GL_QUADS);
    1.95 +	glTexCoord3f(0, 0, 0); glVertex2f(-1, -1);
    1.96 +	glTexCoord3f(1, 0, 0); glVertex2f(1, -1);
    1.97 +	glTexCoord3f(1, 1, 0); glVertex2f(1, 1);
    1.98 +	glTexCoord3f(0, 1, 0); glVertex2f(-1, 1);
    1.99 +	glEnd();
   1.100 +
   1.101 +	glMatrixMode(GL_PROJECTION);
   1.102 +	glPopMatrix();
   1.103 +	glMatrixMode(GL_MODELVIEW);
   1.104 +	glPopMatrix();
   1.105 +}