glviewvol

view src/rend_fast.cc @ 7:71b479ffb9f7

curve manipulation works
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 30 Dec 2014 17:28:38 +0200
parents f22be47a3572
children fb6d93471352
line source
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "rend_fast.h"
4 #include "sdr.h"
6 #define XFER_MAP_SZ 1024
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 }
17 bool RendererFast::init()
18 {
19 if(!sdr) {
20 if(!(sdr = create_program_load("sdr/fast.v.glsl", "sdr/fast.p.glsl"))) {
21 return false;
22 }
23 have_tex_float = GLEW_ARB_texture_float;
24 }
26 glGenTextures(1, &vol_tex);
27 glBindTexture(GL_TEXTURE_3D, vol_tex);
28 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
29 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
31 glGenTextures(1, &xfer_tex);
32 glBindTexture(GL_TEXTURE_1D, xfer_tex);
33 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
34 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
35 glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGBA16F : GL_RGBA, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
37 return true;
38 }
40 void RendererFast::destroy()
41 {
42 glDeleteTextures(1, &vol_tex);
43 glDeleteTextures(1, &xfer_tex);
44 }
46 void RendererFast::set_volume(Volume *vol)
47 {
48 vol_tex_valid = false;
49 Renderer::set_volume(vol);
50 }
52 void RendererFast::update(unsigned int msec)
53 {
54 if(!vol) return;
56 // make sure the 3D volume texture is up to date
57 if(!vol_tex_valid) {
58 int xsz, ysz, zsz;
60 if((xsz = vol->num_samples(0)) > 0) {
61 ysz = vol->num_samples(1);
62 zsz = vol->num_samples(2);
63 } else {
64 xsz = ysz = zsz = 256;
65 }
67 printf("updating 3D texture data (%dx%dx%d) ... ", xsz, ysz, zsz);
68 fflush(stdout);
70 int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
72 glBindTexture(GL_TEXTURE_3D, vol_tex);
73 glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
75 float *slice = new float[xsz * ysz * 4];
77 for(int i=0; i<zsz; i++) {
78 float z = (float)i;
79 float *pptr = slice;
81 for(int j=0; j<ysz; j++) {
82 float y = (float)j;
83 for(int k=0; k<xsz; k++) {
84 float x = (float)k;
86 // value in alpha channel
87 pptr[3] = vol->valuef(x, y, z);
88 // normal in rgb
89 vol->normalf(pptr, x, y, z);
90 // shift normal to the [0,1] range in case we don't have tex_float
91 pptr[0] = pptr[0] * 0.5 + 0.5;
92 pptr[1] = pptr[1] * 0.5 + 0.5;
93 pptr[2] = pptr[2] * 0.5 + 0.5;
95 pptr += 4;
96 }
97 }
99 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
100 }
102 printf("done\n");
104 delete [] slice;
106 vol_tex_valid = true;
107 }
109 if(1) {//if(!xfer_tex_valid) {
110 float pixels[XFER_MAP_SZ * 4];
111 float *pptr = pixels;
113 for(int i=0; i<XFER_MAP_SZ; i++) {
114 float x = (float)i / (float)(XFER_MAP_SZ - 1);
116 if(xfer) {
117 xfer->map(x, pptr);
118 pptr[3] = std::max(pptr[0], std::max(pptr[1], pptr[2]));
119 } else {
120 pptr[0] = pptr[1] = pptr[2] = pptr[3] = x;
121 }
122 pptr += 4;
123 }
125 glBindTexture(GL_TEXTURE_1D, xfer_tex);
126 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_RGBA, GL_FLOAT, pixels);
128 xfer_tex_valid = true;
129 }
130 }
132 void RendererFast::render() const
133 {
134 if(!vol) return;
136 glClear(GL_COLOR_BUFFER_BIT);
138 glMatrixMode(GL_PROJECTION);
139 glPushMatrix();
140 glLoadIdentity();
142 glMatrixMode(GL_MODELVIEW);
143 glPushMatrix();
144 glLoadIdentity();
146 glUseProgram(sdr);
148 glActiveTexture(GL_TEXTURE0);
149 glBindTexture(GL_TEXTURE_3D, vol_tex);
150 glActiveTexture(GL_TEXTURE1);
151 glBindTexture(GL_TEXTURE_1D, xfer_tex);
153 set_uniform_int(sdr, "vol_tex", 0);
154 set_uniform_int(sdr, "xfer_tex", 1);
156 glBegin(GL_QUADS);
157 glTexCoord3f(0, 0, 0.5); glVertex2f(-1, -1);
158 glTexCoord3f(1, 0, 0.5); glVertex2f(1, -1);
159 glTexCoord3f(1, 1, 0.5); glVertex2f(1, 1);
160 glTexCoord3f(0, 1, 0.5); glVertex2f(-1, 1);
161 glEnd();
163 glUseProgram(0);
165 glMatrixMode(GL_PROJECTION);
166 glPopMatrix();
167 glMatrixMode(GL_MODELVIEW);
168 glPopMatrix();
169 }