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@5
|
6 static inline float smoothstep(float a, float b, float x);
|
nuclear@5
|
7
|
nuclear@4
|
8 #define XFER_MAP_SZ 1024
|
nuclear@4
|
9
|
nuclear@4
|
10 static unsigned int sdr;
|
nuclear@4
|
11 static bool have_tex_float;
|
nuclear@0
|
12
|
nuclear@0
|
13 RendererFast::RendererFast()
|
nuclear@0
|
14 {
|
nuclear@4
|
15 vol_tex = xfer_tex = 0;
|
nuclear@4
|
16 vol_tex_valid = xfer_tex_valid = false;
|
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@0
|
32
|
nuclear@4
|
33 glGenTextures(1, &xfer_tex);
|
nuclear@4
|
34 glBindTexture(GL_TEXTURE_1D, xfer_tex);
|
nuclear@4
|
35 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@4
|
36 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@6
|
37 glTexImage1D(GL_TEXTURE_1D, 0, have_tex_float ? GL_RGBA16F : GL_RGBA, XFER_MAP_SZ, 0, GL_RGB, GL_FLOAT, 0);
|
nuclear@4
|
38
|
nuclear@0
|
39 return true;
|
nuclear@0
|
40 }
|
nuclear@0
|
41
|
nuclear@0
|
42 void RendererFast::destroy()
|
nuclear@0
|
43 {
|
nuclear@0
|
44 glDeleteTextures(1, &vol_tex);
|
nuclear@4
|
45 glDeleteTextures(1, &xfer_tex);
|
nuclear@4
|
46 }
|
nuclear@4
|
47
|
nuclear@4
|
48 void RendererFast::set_volume(Volume *vol)
|
nuclear@4
|
49 {
|
nuclear@4
|
50 vol_tex_valid = false;
|
nuclear@4
|
51 Renderer::set_volume(vol);
|
nuclear@4
|
52 }
|
nuclear@4
|
53
|
nuclear@0
|
54 void RendererFast::update(unsigned int msec)
|
nuclear@0
|
55 {
|
nuclear@0
|
56 if(!vol) return;
|
nuclear@0
|
57
|
nuclear@0
|
58 // make sure the 3D volume texture is up to date
|
nuclear@0
|
59 if(!vol_tex_valid) {
|
nuclear@0
|
60 int xsz, ysz, zsz;
|
nuclear@0
|
61
|
nuclear@0
|
62 if((xsz = vol->num_samples(0)) > 0) {
|
nuclear@0
|
63 ysz = vol->num_samples(1);
|
nuclear@0
|
64 zsz = vol->num_samples(2);
|
nuclear@0
|
65 } else {
|
nuclear@0
|
66 xsz = ysz = zsz = 256;
|
nuclear@0
|
67 }
|
nuclear@0
|
68
|
nuclear@4
|
69 printf("updating 3D texture data (%dx%dx%d) ... ", xsz, ysz, zsz);
|
nuclear@4
|
70 fflush(stdout);
|
nuclear@4
|
71
|
nuclear@0
|
72 int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
|
nuclear@0
|
73
|
nuclear@0
|
74 glBindTexture(GL_TEXTURE_3D, vol_tex);
|
nuclear@0
|
75 glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
|
nuclear@0
|
76
|
nuclear@0
|
77 float *slice = new float[xsz * ysz * 4];
|
nuclear@0
|
78
|
nuclear@0
|
79 for(int i=0; i<zsz; i++) {
|
nuclear@4
|
80 float z = (float)i;
|
nuclear@0
|
81 float *pptr = slice;
|
nuclear@0
|
82
|
nuclear@0
|
83 for(int j=0; j<ysz; j++) {
|
nuclear@4
|
84 float y = (float)j;
|
nuclear@0
|
85 for(int k=0; k<xsz; k++) {
|
nuclear@4
|
86 float x = (float)k;
|
nuclear@0
|
87
|
nuclear@0
|
88 // value in alpha channel
|
nuclear@0
|
89 pptr[3] = vol->valuef(x, y, z);
|
nuclear@0
|
90 // normal in rgb
|
nuclear@0
|
91 vol->normalf(pptr, x, y, z);
|
nuclear@0
|
92 // shift normal to the [0,1] range in case we don't have tex_float
|
nuclear@0
|
93 pptr[0] = pptr[0] * 0.5 + 0.5;
|
nuclear@0
|
94 pptr[1] = pptr[1] * 0.5 + 0.5;
|
nuclear@0
|
95 pptr[2] = pptr[2] * 0.5 + 0.5;
|
nuclear@4
|
96
|
nuclear@4
|
97 pptr += 4;
|
nuclear@0
|
98 }
|
nuclear@0
|
99 }
|
nuclear@0
|
100
|
nuclear@0
|
101 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
|
nuclear@0
|
102 }
|
nuclear@0
|
103
|
nuclear@4
|
104 printf("done\n");
|
nuclear@4
|
105
|
nuclear@0
|
106 delete [] slice;
|
nuclear@0
|
107
|
nuclear@0
|
108 vol_tex_valid = true;
|
nuclear@0
|
109 }
|
nuclear@4
|
110
|
nuclear@6
|
111 if(1) {//if(!xfer_tex_valid) {
|
nuclear@6
|
112 float pixels[XFER_MAP_SZ * 4];
|
nuclear@4
|
113 float *pptr = pixels;
|
nuclear@4
|
114
|
nuclear@4
|
115 for(int i=0; i<XFER_MAP_SZ; i++) {
|
nuclear@4
|
116 float x = (float)i / (float)(XFER_MAP_SZ - 1);
|
nuclear@5
|
117
|
nuclear@6
|
118 if(xfer) {
|
nuclear@6
|
119 xfer->map(x, pptr);
|
nuclear@6
|
120 } else {
|
nuclear@6
|
121 pptr[0] = pptr[1] = pptr[2] = pptr[3] = x;
|
nuclear@6
|
122 }
|
nuclear@6
|
123 pptr += 4;
|
nuclear@4
|
124 }
|
nuclear@4
|
125
|
nuclear@4
|
126 glBindTexture(GL_TEXTURE_1D, xfer_tex);
|
nuclear@6
|
127 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_RGBA, GL_FLOAT, pixels);
|
nuclear@4
|
128
|
nuclear@4
|
129 xfer_tex_valid = true;
|
nuclear@4
|
130 }
|
nuclear@0
|
131 }
|
nuclear@0
|
132
|
nuclear@0
|
133 void RendererFast::render() const
|
nuclear@0
|
134 {
|
nuclear@0
|
135 if(!vol) return;
|
nuclear@0
|
136
|
nuclear@0
|
137 glClear(GL_COLOR_BUFFER_BIT);
|
nuclear@0
|
138
|
nuclear@0
|
139 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
140 glPushMatrix();
|
nuclear@0
|
141 glLoadIdentity();
|
nuclear@0
|
142
|
nuclear@0
|
143 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
144 glPushMatrix();
|
nuclear@0
|
145 glLoadIdentity();
|
nuclear@0
|
146
|
nuclear@4
|
147 glUseProgram(sdr);
|
nuclear@4
|
148
|
nuclear@4
|
149 glActiveTexture(GL_TEXTURE0);
|
nuclear@0
|
150 glBindTexture(GL_TEXTURE_3D, vol_tex);
|
nuclear@4
|
151 glActiveTexture(GL_TEXTURE1);
|
nuclear@4
|
152 glBindTexture(GL_TEXTURE_1D, xfer_tex);
|
nuclear@4
|
153
|
nuclear@4
|
154 set_uniform_int(sdr, "vol_tex", 0);
|
nuclear@4
|
155 set_uniform_int(sdr, "xfer_tex", 1);
|
nuclear@4
|
156
|
nuclear@0
|
157 glBegin(GL_QUADS);
|
nuclear@4
|
158 glTexCoord3f(0, 0, 0.5); glVertex2f(-1, -1);
|
nuclear@4
|
159 glTexCoord3f(1, 0, 0.5); glVertex2f(1, -1);
|
nuclear@4
|
160 glTexCoord3f(1, 1, 0.5); glVertex2f(1, 1);
|
nuclear@4
|
161 glTexCoord3f(0, 1, 0.5); glVertex2f(-1, 1);
|
nuclear@0
|
162 glEnd();
|
nuclear@0
|
163
|
nuclear@4
|
164 glUseProgram(0);
|
nuclear@4
|
165
|
nuclear@0
|
166 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
167 glPopMatrix();
|
nuclear@0
|
168 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
169 glPopMatrix();
|
nuclear@0
|
170 }
|
nuclear@5
|
171
|
nuclear@5
|
172 static inline float smoothstep(float a, float b, float x)
|
nuclear@5
|
173 {
|
nuclear@5
|
174 if(x < a) return 0.0;
|
nuclear@5
|
175 if(x >= b) return 1.0;
|
nuclear@5
|
176
|
nuclear@5
|
177 x = (x - a) / (b - a);
|
nuclear@5
|
178 return x * x * (3.0 - 2.0 * x);
|
nuclear@5
|
179 }
|