glviewvol

view src/rend_fast.cc @ 3:32c4a7160350

den kanei kryo stin ellada
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Dec 2014 21:48:15 +0200
parents
children 04330eb80b36
line source
1 #include "opengl.h"
2 #include "rend_fast.h"
4 RendererFast::RendererFast()
5 {
6 vol_tex = 0;
7 vol_tex_valid = false;
8 }
10 bool RendererFast::init()
11 {
12 glGenTextures(1, &vol_tex);
13 glBindTexture(GL_TEXTURE_3D, vol_tex);
14 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
15 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
17 return true;
18 }
20 void RendererFast::destroy()
21 {
22 glDeleteTextures(1, &vol_tex);
23 }
25 void RendererFast::update(unsigned int msec)
26 {
27 if(!vol) return;
29 // make sure the 3D volume texture is up to date
30 if(!vol_tex_valid) {
31 int xsz, ysz, zsz;
33 if((xsz = vol->num_samples(0)) > 0) {
34 ysz = vol->num_samples(1);
35 zsz = vol->num_samples(2);
36 } else {
37 xsz = ysz = zsz = 256;
38 }
40 int int_fmt = GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA;
42 glBindTexture(GL_TEXTURE_3D, vol_tex);
43 glTexImage3D(GL_TEXTURE_3D, 0, int_fmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, 0);
45 float *slice = new float[xsz * ysz * 4];
47 for(int i=0; i<zsz; i++) {
48 float z = (float)i / (float)(zsz - 1);
49 float *pptr = slice;
51 for(int j=0; j<ysz; j++) {
52 float y = (float)j / (float)(ysz - 1);
53 for(int k=0; k<xsz; k++) {
54 float x = (float)k / (float)(xsz - 1);
56 // value in alpha channel
57 pptr[3] = vol->valuef(x, y, z);
58 // normal in rgb
59 vol->normalf(pptr, x, y, z);
60 // shift normal to the [0,1] range in case we don't have tex_float
61 pptr[0] = pptr[0] * 0.5 + 0.5;
62 pptr[1] = pptr[1] * 0.5 + 0.5;
63 pptr[2] = pptr[2] * 0.5 + 0.5;
64 }
65 }
67 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_FLOAT, slice);
68 }
70 delete [] slice;
72 vol_tex_valid = true;
73 }
74 }
76 void RendererFast::render() const
77 {
78 if(!vol) return;
80 glClear(GL_COLOR_BUFFER_BIT);
82 glMatrixMode(GL_PROJECTION);
83 glPushMatrix();
84 glLoadIdentity();
86 glMatrixMode(GL_MODELVIEW);
87 glPushMatrix();
88 glLoadIdentity();
90 glBindTexture(GL_TEXTURE_3D, vol_tex);
91 glBegin(GL_QUADS);
92 glTexCoord3f(0, 0, 0); glVertex2f(-1, -1);
93 glTexCoord3f(1, 0, 0); glVertex2f(1, -1);
94 glTexCoord3f(1, 1, 0); glVertex2f(1, 1);
95 glTexCoord3f(0, 1, 0); glVertex2f(-1, 1);
96 glEnd();
98 glMatrixMode(GL_PROJECTION);
99 glPopMatrix();
100 glMatrixMode(GL_MODELVIEW);
101 glPopMatrix();
102 }