glviewvol

view 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 source
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "rend_fast.h"
4 #include "sdr.h"
6 #define XFER_MAP_SZ 512
8 static unsigned int sdr;
9 static bool have_tex_float;
11 RendererFast::RendererFast()
12 {
13 vol_tex = xfer_tex = 0;
14 vol_tex_valid = xfer_tex_valid = false;
15 proxy_count = 256;
16 vbo_proxy_count = 0;
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);
32 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
33 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
34 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
36 glGenTextures(1, &xfer_tex);
37 glBindTexture(GL_TEXTURE_1D, xfer_tex);
38 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
39 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
40 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
41 glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGBA16F : GL_RGBA, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
43 glGenBuffers(1, &vbo);
44 return true;
45 }
47 void RendererFast::destroy()
48 {
49 glDeleteTextures(1, &vol_tex);
50 glDeleteTextures(1, &xfer_tex);
51 glDeleteBuffers(1, &vbo);
52 }
54 void RendererFast::set_volume(Volume *vol)
55 {
56 vol_tex_valid = false;
57 Renderer::set_volume(vol);
58 }
60 void RendererFast::set_proxy_count(int n)
61 {
62 proxy_count = n;
63 }
65 int RendererFast::get_proxy_count() const
66 {
67 return proxy_count;
68 }
70 void RendererFast::update(unsigned int msec)
71 {
72 if(!vol) return;
74 // make sure the 3D volume texture is up to date
75 if(!vol_tex_valid) {
76 int xsz, ysz, zsz;
78 if((xsz = vol->num_samples(0)) > 0) {
79 ysz = vol->num_samples(1);
80 zsz = vol->num_samples(2);
81 } else {
82 xsz = ysz = zsz = 256;
83 }
85 printf("updating 3D texture data (%dx%dx%d) ... ", xsz, ysz, zsz);
86 fflush(stdout);
88 int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
90 glBindTexture(GL_TEXTURE_3D, vol_tex);
91 glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
93 float *slice = new float[xsz * ysz * 4];
95 for(int i=0; i<zsz; i++) {
96 float z = (float)i;
97 float *pptr = slice;
99 for(int j=0; j<ysz; j++) {
100 float y = (float)j;
101 for(int k=0; k<xsz; k++) {
102 float x = (float)k;
104 // value in alpha channel
105 pptr[3] = vol->valuef(x, y, z);
106 // normal in rgb
107 vol->normalf(pptr, x, y, z);
108 // shift normal to the [0,1] range in case we don't have tex_float
109 pptr[0] = pptr[0] * 0.5 + 0.5;
110 pptr[1] = pptr[1] * 0.5 + 0.5;
111 pptr[2] = pptr[2] * 0.5 + 0.5;
113 pptr += 4;
114 }
115 }
117 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
118 }
120 printf("done\n");
122 delete [] slice;
124 vol_tex_valid = true;
125 }
127 if(1) {//if(!xfer_tex_valid) {
128 float pixels[XFER_MAP_SZ * 4];
129 float *pptr = pixels;
131 for(int i=0; i<XFER_MAP_SZ; i++) {
132 float x = (float)i / (float)(XFER_MAP_SZ - 1);
134 if(xfer) {
135 xfer->map(x, pptr);
136 pptr[3] = std::max(pptr[0], std::max(pptr[1], pptr[2]));
137 } else {
138 pptr[0] = pptr[1] = pptr[2] = pptr[3] = x;
139 }
140 pptr += 4;
141 }
143 glBindTexture(GL_TEXTURE_1D, xfer_tex);
144 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_RGBA, GL_FLOAT, pixels);
146 xfer_tex_valid = true;
147 }
149 // make sure the proxy object is up to date
150 if(proxy_count != vbo_proxy_count) {
151 static const float pat[][3] = {{-1, -1}, {1, -1}, {1, 1}, {-1, 1}};
153 int nverts = proxy_count * 4;
154 float *verts = new float[nverts * 3];
155 float *vptr = verts;
157 for(int i=0; i<proxy_count; i++) {
158 float z = 2.0 * (float)i / (float)(proxy_count - 1) - 1.0;
160 for(int j=0; j<4; j++) {
161 *vptr++ = pat[j][0];
162 *vptr++ = pat[j][1];
163 *vptr++ = z;
164 }
165 }
167 glBindBuffer(GL_ARRAY_BUFFER, vbo);
168 glBufferData(GL_ARRAY_BUFFER, nverts * 3 * sizeof(float), verts, GL_STATIC_DRAW);
169 glBindBuffer(GL_ARRAY_BUFFER, 0);
171 delete [] verts;
172 vbo_proxy_count = proxy_count;
173 }
174 }
176 void RendererFast::render() const
177 {
178 if(!vol) return;
180 glClear(GL_COLOR_BUFFER_BIT);
182 glUseProgram(sdr);
184 glActiveTexture(GL_TEXTURE0);
185 glBindTexture(GL_TEXTURE_3D, vol_tex);
186 glActiveTexture(GL_TEXTURE1);
187 glBindTexture(GL_TEXTURE_1D, xfer_tex);
189 set_uniform_int(sdr, "vol_tex", 0);
190 set_uniform_int(sdr, "xfer_tex", 1);
192 glEnable(GL_BLEND);
193 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
195 glBindBuffer(GL_ARRAY_BUFFER, vbo);
196 glVertexPointer(3, GL_FLOAT, 0, 0);
197 glBindBuffer(GL_ARRAY_BUFFER, 0);
199 glEnableClientState(GL_VERTEX_ARRAY);
200 glDrawArrays(GL_QUADS, 0, vbo_proxy_count * 4);
201 glDisableClientState(GL_VERTEX_ARRAY);
203 glDisable(GL_BLEND);
205 glUseProgram(0);
206 }