glviewvol

view src/rend_fast.cc @ 5:5417c25cb238

moving to a simpler transfer function
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Dec 2014 15:59:55 +0200
parents 04330eb80b36
children f22be47a3572
line source
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "rend_fast.h"
4 #include "sdr.h"
6 static inline float smoothstep(float a, float b, float x);
8 #define XFER_MAP_SZ 1024
10 static unsigned int sdr;
11 static bool have_tex_float;
13 RendererFast::RendererFast()
14 {
15 vol_tex = xfer_tex = 0;
16 vol_tex_valid = xfer_tex_valid = false;
17 }
19 bool RendererFast::init()
20 {
21 if(!sdr) {
22 if(!(sdr = create_program_load("sdr/fast.v.glsl", "sdr/fast.p.glsl"))) {
23 return false;
24 }
25 have_tex_float = GLEW_ARB_texture_float;
26 }
28 glGenTextures(1, &vol_tex);
29 glBindTexture(GL_TEXTURE_3D, vol_tex);
30 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
31 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
33 glGenTextures(1, &xfer_tex);
34 glBindTexture(GL_TEXTURE_1D, xfer_tex);
35 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
36 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
37 glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGB16F : GL_RGB, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
39 return true;
40 }
42 void RendererFast::destroy()
43 {
44 glDeleteTextures(1, &vol_tex);
45 glDeleteTextures(1, &xfer_tex);
46 }
48 void RendererFast::set_volume(Volume *vol)
49 {
50 vol_tex_valid = false;
51 Renderer::set_volume(vol);
52 }
54 Curve &RendererFast::transfer_curve(int color)
55 {
56 xfer_tex_valid = false;
57 return Renderer::transfer_curve(color);
58 }
60 void RendererFast::set_simple_transfer(float low, float high)
61 {
62 xfer_tex_valid = false;
63 Renderer::set_simple_transfer(low, high);
64 }
66 void RendererFast::update(unsigned int msec)
67 {
68 if(!vol) return;
70 // make sure the 3D volume texture is up to date
71 if(!vol_tex_valid) {
72 int xsz, ysz, zsz;
74 if((xsz = vol->num_samples(0)) > 0) {
75 ysz = vol->num_samples(1);
76 zsz = vol->num_samples(2);
77 } else {
78 xsz = ysz = zsz = 256;
79 }
81 printf("updating 3D texture data (%dx%dx%d) ... ", xsz, ysz, zsz);
82 fflush(stdout);
84 int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
86 glBindTexture(GL_TEXTURE_3D, vol_tex);
87 glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
89 float *slice = new float[xsz * ysz * 4];
91 for(int i=0; i<zsz; i++) {
92 float z = (float)i;
93 float *pptr = slice;
95 for(int j=0; j<ysz; j++) {
96 float y = (float)j;
97 for(int k=0; k<xsz; k++) {
98 float x = (float)k;
100 // value in alpha channel
101 pptr[3] = vol->valuef(x, y, z);
102 // normal in rgb
103 vol->normalf(pptr, x, y, z);
104 // shift normal to the [0,1] range in case we don't have tex_float
105 pptr[0] = pptr[0] * 0.5 + 0.5;
106 pptr[1] = pptr[1] * 0.5 + 0.5;
107 pptr[2] = pptr[2] * 0.5 + 0.5;
109 pptr += 4;
110 }
111 }
113 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
114 }
116 printf("done\n");
118 delete [] slice;
120 vol_tex_valid = true;
121 }
123 if(!xfer_tex_valid) {
124 float pixels[XFER_MAP_SZ * 3];
125 float *pptr = pixels;
127 for(int i=0; i<XFER_MAP_SZ; i++) {
128 float x = (float)i / (float)(XFER_MAP_SZ - 1);
130 // TODO make 0.1 a tweakable parameter
131 float val = smoothstep(xfer_low - 0.1, xfer_low + 0.1, x);
132 val *= 1.0 - smoothstep(xfer_high - 0.1, xfer_high + 0.1, x);
133 *pptr++ = val;
134 *pptr++ = val;
135 *pptr++ = val;
136 /*
137 *pptr++ = xfer[0].value(x);
138 *pptr++ = xfer[1].value(x);
139 *pptr++ = xfer[2].value(x);
140 */
141 }
143 glBindTexture(GL_TEXTURE_1D, xfer_tex);
144 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_RGB, GL_FLOAT, pixels);
146 xfer_tex_valid = true;
147 }
148 }
150 void RendererFast::render() const
151 {
152 if(!vol) return;
154 glClear(GL_COLOR_BUFFER_BIT);
156 glMatrixMode(GL_PROJECTION);
157 glPushMatrix();
158 glLoadIdentity();
160 glMatrixMode(GL_MODELVIEW);
161 glPushMatrix();
162 glLoadIdentity();
164 glUseProgram(sdr);
166 glActiveTexture(GL_TEXTURE0);
167 glBindTexture(GL_TEXTURE_3D, vol_tex);
168 glActiveTexture(GL_TEXTURE1);
169 glBindTexture(GL_TEXTURE_1D, xfer_tex);
171 set_uniform_int(sdr, "vol_tex", 0);
172 set_uniform_int(sdr, "xfer_tex", 1);
174 glBegin(GL_QUADS);
175 glTexCoord3f(0, 0, 0.5); glVertex2f(-1, -1);
176 glTexCoord3f(1, 0, 0.5); glVertex2f(1, -1);
177 glTexCoord3f(1, 1, 0.5); glVertex2f(1, 1);
178 glTexCoord3f(0, 1, 0.5); glVertex2f(-1, 1);
179 glEnd();
181 glUseProgram(0);
183 glMatrixMode(GL_PROJECTION);
184 glPopMatrix();
185 glMatrixMode(GL_MODELVIEW);
186 glPopMatrix();
187 }
189 static inline float smoothstep(float a, float b, float x)
190 {
191 if(x < a) return 0.0;
192 if(x >= b) return 1.0;
194 x = (x - a) / (b - a);
195 return x * x * (3.0 - 2.0 * x);
196 }