glviewvol
view src/rend_fast.cc @ 6:f22be47a3572
moved to TransferFuncs completely
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 30 Dec 2014 06:22:54 +0200 |
parents | 5417c25cb238 |
children | 71b479ffb9f7 |
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_RGBA16F : GL_RGBA, 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 void RendererFast::update(unsigned int msec)
55 {
56 if(!vol) return;
58 // make sure the 3D volume texture is up to date
59 if(!vol_tex_valid) {
60 int xsz, ysz, zsz;
62 if((xsz = vol->num_samples(0)) > 0) {
63 ysz = vol->num_samples(1);
64 zsz = vol->num_samples(2);
65 } else {
66 xsz = ysz = zsz = 256;
67 }
69 printf("updating 3D texture data (%dx%dx%d) ... ", xsz, ysz, zsz);
70 fflush(stdout);
72 int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
74 glBindTexture(GL_TEXTURE_3D, vol_tex);
75 glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
77 float *slice = new float[xsz * ysz * 4];
79 for(int i=0; i<zsz; i++) {
80 float z = (float)i;
81 float *pptr = slice;
83 for(int j=0; j<ysz; j++) {
84 float y = (float)j;
85 for(int k=0; k<xsz; k++) {
86 float x = (float)k;
88 // value in alpha channel
89 pptr[3] = vol->valuef(x, y, z);
90 // normal in rgb
91 vol->normalf(pptr, x, y, z);
92 // shift normal to the [0,1] range in case we don't have tex_float
93 pptr[0] = pptr[0] * 0.5 + 0.5;
94 pptr[1] = pptr[1] * 0.5 + 0.5;
95 pptr[2] = pptr[2] * 0.5 + 0.5;
97 pptr += 4;
98 }
99 }
101 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
102 }
104 printf("done\n");
106 delete [] slice;
108 vol_tex_valid = true;
109 }
111 if(1) {//if(!xfer_tex_valid) {
112 float pixels[XFER_MAP_SZ * 4];
113 float *pptr = pixels;
115 for(int i=0; i<XFER_MAP_SZ; i++) {
116 float x = (float)i / (float)(XFER_MAP_SZ - 1);
118 if(xfer) {
119 xfer->map(x, pptr);
120 } else {
121 pptr[0] = pptr[1] = pptr[2] = pptr[3] = x;
122 }
123 pptr += 4;
124 }
126 glBindTexture(GL_TEXTURE_1D, xfer_tex);
127 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_RGBA, GL_FLOAT, pixels);
129 xfer_tex_valid = true;
130 }
131 }
133 void RendererFast::render() const
134 {
135 if(!vol) return;
137 glClear(GL_COLOR_BUFFER_BIT);
139 glMatrixMode(GL_PROJECTION);
140 glPushMatrix();
141 glLoadIdentity();
143 glMatrixMode(GL_MODELVIEW);
144 glPushMatrix();
145 glLoadIdentity();
147 glUseProgram(sdr);
149 glActiveTexture(GL_TEXTURE0);
150 glBindTexture(GL_TEXTURE_3D, vol_tex);
151 glActiveTexture(GL_TEXTURE1);
152 glBindTexture(GL_TEXTURE_1D, xfer_tex);
154 set_uniform_int(sdr, "vol_tex", 0);
155 set_uniform_int(sdr, "xfer_tex", 1);
157 glBegin(GL_QUADS);
158 glTexCoord3f(0, 0, 0.5); glVertex2f(-1, -1);
159 glTexCoord3f(1, 0, 0.5); glVertex2f(1, -1);
160 glTexCoord3f(1, 1, 0.5); glVertex2f(1, 1);
161 glTexCoord3f(0, 1, 0.5); glVertex2f(-1, 1);
162 glEnd();
164 glUseProgram(0);
166 glMatrixMode(GL_PROJECTION);
167 glPopMatrix();
168 glMatrixMode(GL_MODELVIEW);
169 glPopMatrix();
170 }
172 static inline float smoothstep(float a, float b, float x)
173 {
174 if(x < a) return 0.0;
175 if(x >= b) return 1.0;
177 x = (x - a) / (b - a);
178 return x * x * (3.0 - 2.0 * x);
179 }