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