glviewvol

diff src/rend_fast.cc @ 4:04330eb80b36

lots of stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Dec 2014 05:41:36 +0200
parents 7bdf40403b9c
children 5417c25cb238
line diff
     1.1 --- a/src/rend_fast.cc	Sun Dec 28 21:48:15 2014 +0200
     1.2 +++ b/src/rend_fast.cc	Mon Dec 29 05:41:36 2014 +0200
     1.3 @@ -1,25 +1,58 @@
     1.4 +#include <stdio.h>
     1.5  #include "opengl.h"
     1.6  #include "rend_fast.h"
     1.7 +#include "sdr.h"
     1.8 +
     1.9 +#define XFER_MAP_SZ		1024
    1.10 +
    1.11 +static unsigned int sdr;
    1.12 +static bool have_tex_float;
    1.13  
    1.14  RendererFast::RendererFast()
    1.15  {
    1.16 -	vol_tex = 0;
    1.17 -	vol_tex_valid = false;
    1.18 +	vol_tex = xfer_tex = 0;
    1.19 +	vol_tex_valid = xfer_tex_valid = false;
    1.20  }
    1.21  
    1.22  bool RendererFast::init()
    1.23  {
    1.24 +	if(!sdr) {
    1.25 +		if(!(sdr = create_program_load("sdr/fast.v.glsl", "sdr/fast.p.glsl"))) {
    1.26 +			return false;
    1.27 +		}
    1.28 +		have_tex_float = GLEW_ARB_texture_float;
    1.29 +	}
    1.30 +
    1.31  	glGenTextures(1, &vol_tex);
    1.32  	glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.33  	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.34  	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.35  
    1.36 +	glGenTextures(1, &xfer_tex);
    1.37 +	glBindTexture(GL_TEXTURE_1D, xfer_tex);
    1.38 +	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.39 +	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.40 +	glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGB16F : GL_RGB, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
    1.41 +
    1.42  	return true;
    1.43  }
    1.44  
    1.45  void RendererFast::destroy()
    1.46  {
    1.47  	glDeleteTextures(1, &vol_tex);
    1.48 +	glDeleteTextures(1, &xfer_tex);
    1.49 +}
    1.50 +
    1.51 +void RendererFast::set_volume(Volume *vol)
    1.52 +{
    1.53 +	vol_tex_valid = false;
    1.54 +	Renderer::set_volume(vol);
    1.55 +}
    1.56 +
    1.57 +Curve &RendererFast::transfer_curve(int color)
    1.58 +{
    1.59 +	xfer_tex_valid = false;
    1.60 +	return Renderer::transfer_curve(color);
    1.61  }
    1.62  
    1.63  void RendererFast::update(unsigned int msec)
    1.64 @@ -37,6 +70,9 @@
    1.65  			xsz = ysz = zsz = 256;
    1.66  		}
    1.67  
    1.68 +		printf("updating 3D texture data (%dx%dx%d) ... ", xsz, ysz, zsz);
    1.69 +		fflush(stdout);
    1.70 +
    1.71  		int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
    1.72  
    1.73  		glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.74 @@ -45,13 +81,13 @@
    1.75  		float *slice = new float[xsz * ysz * 4];
    1.76  
    1.77  		for(int i=0; i<zsz; i++) {
    1.78 -			float z = (float)i / (float)(zsz - 1);
    1.79 +			float z = (float)i;
    1.80  			float *pptr = slice;
    1.81  
    1.82  			for(int j=0; j<ysz; j++) {
    1.83 -				float y = (float)j / (float)(ysz - 1);
    1.84 +				float y = (float)j;
    1.85  				for(int k=0; k<xsz; k++) {
    1.86 -					float x = (float)k / (float)(xsz - 1);
    1.87 +					float x = (float)k;
    1.88  
    1.89  					// value in alpha channel
    1.90  					pptr[3] = vol->valuef(x, y, z);
    1.91 @@ -61,16 +97,37 @@
    1.92  					pptr[0] = pptr[0] * 0.5 + 0.5;
    1.93  					pptr[1] = pptr[1] * 0.5 + 0.5;
    1.94  					pptr[2] = pptr[2] * 0.5 + 0.5;
    1.95 +
    1.96 +					pptr += 4;
    1.97  				}
    1.98  			}
    1.99  
   1.100  			glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
   1.101  		}
   1.102  
   1.103 +		printf("done\n");
   1.104 +
   1.105  		delete [] slice;
   1.106  
   1.107  		vol_tex_valid = true;
   1.108  	}
   1.109 +
   1.110 +	if(!xfer_tex_valid) {
   1.111 +		float pixels[XFER_MAP_SZ * 3];
   1.112 +		float *pptr = pixels;
   1.113 +
   1.114 +		for(int i=0; i<XFER_MAP_SZ; i++) {
   1.115 +			float x = (float)i / (float)(XFER_MAP_SZ - 1);
   1.116 +			*pptr++ = xfer[0].value(x);
   1.117 +			*pptr++ = xfer[1].value(x);
   1.118 +			*pptr++ = xfer[2].value(x);
   1.119 +		}
   1.120 +
   1.121 +		glBindTexture(GL_TEXTURE_1D, xfer_tex);
   1.122 +		glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_RGB, GL_FLOAT, pixels);
   1.123 +
   1.124 +		xfer_tex_valid = true;
   1.125 +	}
   1.126  }
   1.127  
   1.128  void RendererFast::render() const
   1.129 @@ -87,14 +144,25 @@
   1.130  	glPushMatrix();
   1.131  	glLoadIdentity();
   1.132  
   1.133 +	glUseProgram(sdr);
   1.134 +
   1.135 +	glActiveTexture(GL_TEXTURE0);
   1.136  	glBindTexture(GL_TEXTURE_3D, vol_tex);
   1.137 +	glActiveTexture(GL_TEXTURE1);
   1.138 +	glBindTexture(GL_TEXTURE_1D, xfer_tex);
   1.139 +
   1.140 +	set_uniform_int(sdr, "vol_tex", 0);
   1.141 +	set_uniform_int(sdr, "xfer_tex", 1);
   1.142 +
   1.143  	glBegin(GL_QUADS);
   1.144 -	glTexCoord3f(0, 0, 0); glVertex2f(-1, -1);
   1.145 -	glTexCoord3f(1, 0, 0); glVertex2f(1, -1);
   1.146 -	glTexCoord3f(1, 1, 0); glVertex2f(1, 1);
   1.147 -	glTexCoord3f(0, 1, 0); glVertex2f(-1, 1);
   1.148 +	glTexCoord3f(0, 0, 0.5); glVertex2f(-1, -1);
   1.149 +	glTexCoord3f(1, 0, 0.5); glVertex2f(1, -1);
   1.150 +	glTexCoord3f(1, 1, 0.5); glVertex2f(1, 1);
   1.151 +	glTexCoord3f(0, 1, 0.5); glVertex2f(-1, 1);
   1.152  	glEnd();
   1.153  
   1.154 +	glUseProgram(0);
   1.155 +
   1.156  	glMatrixMode(GL_PROJECTION);
   1.157  	glPopMatrix();
   1.158  	glMatrixMode(GL_MODELVIEW);