glviewvol

view src/rend_fast.cc @ 10:89efc666105c

mostly done
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 31 Dec 2014 05:21:47 +0200
parents 931a6b35f1cd
children 773f89037a35
line source
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "rend_fast.h"
4 #include "sdr.h"
6 #define DEF_PROXY_COUNT 512
7 #define XFER_MAP_SZ 512
9 static unsigned int sdr;
10 static bool have_tex_float;
12 RendererFast::RendererFast()
13 {
14 vol_tex = xfer_tex = 0;
15 vol_tex_valid = xfer_tex_valid = false;
16 proxy_count = DEF_PROXY_COUNT;
17 vbo_proxy_count = 0;
18 }
20 bool RendererFast::init()
21 {
22 if(!sdr) {
23 if(!(sdr = create_program_load("sdr/fast.v.glsl", "sdr/fast.p.glsl"))) {
24 return false;
25 }
26 have_tex_float = GLEW_ARB_texture_float;
27 }
29 glGenTextures(1, &vol_tex);
30 glBindTexture(GL_TEXTURE_3D, vol_tex);
31 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
32 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
33 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
34 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
35 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
37 glGenTextures(1, &xfer_tex);
38 glBindTexture(GL_TEXTURE_1D, xfer_tex);
39 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
40 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
41 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
42 glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGBA16F : GL_RGBA, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
44 glGenBuffers(1, &vbo);
45 return true;
46 }
48 void RendererFast::destroy()
49 {
50 glDeleteTextures(1, &vol_tex);
51 glDeleteTextures(1, &xfer_tex);
52 glDeleteBuffers(1, &vbo);
53 }
55 void RendererFast::set_volume(Volume *vol)
56 {
57 vol_tex_valid = false;
58 Renderer::set_volume(vol);
59 }
61 void RendererFast::set_proxy_count(int n)
62 {
63 proxy_count = n;
64 }
66 int RendererFast::get_proxy_count() const
67 {
68 return proxy_count;
69 }
71 void RendererFast::update(unsigned int msec)
72 {
73 if(!vol) return;
75 // make sure the 3D volume texture is up to date
76 if(!vol_tex_valid) {
77 int xsz, ysz, zsz;
79 if((xsz = vol->num_samples(0)) > 0) {
80 ysz = vol->num_samples(1);
81 zsz = vol->num_samples(2);
82 } else {
83 xsz = ysz = zsz = 256;
84 }
86 printf("updating 3D texture data (%dx%dx%d) ... ", xsz, ysz, zsz);
87 fflush(stdout);
89 int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
91 glBindTexture(GL_TEXTURE_3D, vol_tex);
92 glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
94 float *slice = new float[xsz * ysz * 4];
96 for(int i=0; i<zsz; i++) {
97 float z = (float)i;
99 #pragma omp parallel for schedule(dynamic)
100 for(int j=0; j<ysz; j++) {
101 float y = (float)j;
103 float *pptr = slice + (j * xsz) * 4;
104 for(int k=0; k<xsz; k++) {
105 float x = (float)k;
107 // value in alpha channel
108 pptr[3] = vol->valuef(x, y, z);
109 // normal in rgb
110 vol->normalf(pptr, x, y, z);
111 // shift normal to the [0,1] range in case we don't have tex_float
112 pptr[0] = pptr[0] * 0.5 + 0.5;
113 pptr[1] = pptr[1] * 0.5 + 0.5;
114 pptr[2] = pptr[2] * 0.5 + 0.5;
116 pptr += 4;
117 }
118 }
120 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
121 }
123 printf("done\n");
125 delete [] slice;
127 vol_tex_valid = true;
128 }
130 if(1) {//if(!xfer_tex_valid) {
131 float pixels[XFER_MAP_SZ * 4];
132 float *pptr = pixels;
134 for(int i=0; i<XFER_MAP_SZ; i++) {
135 float x = (float)i / (float)(XFER_MAP_SZ - 1);
137 if(xfer) {
138 xfer->map(x, pptr);
139 pptr[3] = std::max(pptr[0], std::max(pptr[1], pptr[2]));
140 } else {
141 pptr[0] = pptr[1] = pptr[2] = pptr[3] = x;
142 }
143 pptr += 4;
144 }
146 glBindTexture(GL_TEXTURE_1D, xfer_tex);
147 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_RGBA, GL_FLOAT, pixels);
149 xfer_tex_valid = true;
150 }
152 // make sure the proxy object is up to date
153 if(proxy_count != vbo_proxy_count) {
154 static const float pat[][3] = {{-1, -1}, {1, -1}, {1, 1}, {-1, 1}};
156 int nverts = proxy_count * 4;
157 float *verts = new float[nverts * 3];
158 float *vptr = verts;
160 for(int i=0; i<proxy_count; i++) {
161 float z = 2.0 * (float)i / (float)(proxy_count - 1) - 1.0;
163 for(int j=0; j<4; j++) {
164 *vptr++ = pat[j][0];
165 *vptr++ = pat[j][1];
166 *vptr++ = z;
167 }
168 }
170 glBindBuffer(GL_ARRAY_BUFFER, vbo);
171 glBufferData(GL_ARRAY_BUFFER, nverts * 3 * sizeof(float), verts, GL_STATIC_DRAW);
172 glBindBuffer(GL_ARRAY_BUFFER, 0);
174 delete [] verts;
175 vbo_proxy_count = proxy_count;
176 }
177 }
179 void RendererFast::render() const
180 {
181 if(!vol) return;
183 glClear(GL_COLOR_BUFFER_BIT);
185 glUseProgram(sdr);
187 glActiveTexture(GL_TEXTURE0);
188 glBindTexture(GL_TEXTURE_3D, vol_tex);
189 glActiveTexture(GL_TEXTURE1);
190 glBindTexture(GL_TEXTURE_1D, xfer_tex);
192 set_uniform_int(sdr, "vol_tex", 0);
193 set_uniform_int(sdr, "xfer_tex", 1);
194 set_uniform_float(sdr, "zscale", zscale);
196 glEnable(GL_BLEND);
197 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
199 glBindBuffer(GL_ARRAY_BUFFER, vbo);
200 glVertexPointer(3, GL_FLOAT, 0, 0);
201 glBindBuffer(GL_ARRAY_BUFFER, 0);
203 glEnableClientState(GL_VERTEX_ARRAY);
204 glDrawArrays(GL_QUADS, 0, vbo_proxy_count * 4);
205 glDisableClientState(GL_VERTEX_ARRAY);
207 glDisable(GL_BLEND);
209 glUseProgram(0);
210 }