qvolray
changeset 18:3d05c261a2f4
demo metaballs crash & burn
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 11 Apr 2012 06:08:59 +0300 |
parents | 535762131d34 |
children | 784d3d321caa |
files | qvolray.pro sdr/demo.p.glsl sdr/demo.v.glsl src/demo.cc src/demo.h src/volray.cc src/volray.h src/volume.cc src/volume.h |
diffstat | 9 files changed, 115 insertions(+), 12 deletions(-) [+] |
line diff
1.1 --- a/qvolray.pro Wed Apr 11 01:44:45 2012 +0200 1.2 +++ b/qvolray.pro Wed Apr 11 06:08:59 2012 +0300 1.3 @@ -13,4 +13,4 @@ 1.4 1.5 # Input 1.6 HEADERS += src/sdr.h src/volray.h src/volume.h src/ui.h 1.7 -SOURCES += src/main.cc src/sdr.c src/volray.cc src/volume.cc src/ui.cc 1.8 +SOURCES += src/main.cc src/sdr.c src/volray.cc src/volume.cc src/ui.cc src/demo.cc
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/sdr/demo.p.glsl Wed Apr 11 06:08:59 2012 +0300 2.3 @@ -0,0 +1,10 @@ 2.4 +void main() 2.5 +{ 2.6 + float val = 0.0; 2.7 + float len = length(gl_FragCoord.xyz); 2.8 + if(len < 0.5) { 2.9 + val = 1.0; 2.10 + } 2.11 + 2.12 + gl_FragColor = vec4(gl_FragCoord.xyz / len, val); 2.13 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/sdr/demo.v.glsl Wed Apr 11 06:08:59 2012 +0300 3.3 @@ -0,0 +1,4 @@ 3.4 +void main() 3.5 +{ 3.6 + gl_Position = gl_Vertex; 3.7 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/demo.cc Wed Apr 11 06:08:59 2012 +0300 4.3 @@ -0,0 +1,61 @@ 4.4 +#include <GL/glew.h> 4.5 +#include "demo.h" 4.6 +#include "sdr.h" 4.7 +#include "volray.h" 4.8 + 4.9 +#define SZ 128 4.10 + 4.11 +static Volume *vol; 4.12 +static unsigned int sdr_mballs; 4.13 +static unsigned int fbo; 4.14 + 4.15 +bool init_demo() 4.16 +{ 4.17 + if(!(sdr_mballs = create_program_load("sdr/demo.v.glsl", "sdr/demo.p.glsl"))) { 4.18 + return false; 4.19 + } 4.20 + 4.21 + vol = new Volume; 4.22 + vol->create(SZ, SZ, SZ); 4.23 + volray_setvolume(vol); 4.24 + 4.25 + glGenFramebuffers(1, &fbo); 4.26 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 4.27 + glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, 4.28 + vol->get_texture(), 0, 0); 4.29 + 4.30 + unsigned int stat = glCheckFramebufferStatus(GL_FRAMEBUFFER); 4.31 + if(stat != GL_FRAMEBUFFER_COMPLETE) { 4.32 + printf("incomplete framebuffer: %u\n", stat); 4.33 + delete vol; 4.34 + return false; 4.35 + } 4.36 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 4.37 + 4.38 + return true; 4.39 +} 4.40 + 4.41 +void draw_demo() 4.42 +{ 4.43 + if(volray_getvolume() != vol) { 4.44 + return; 4.45 + } 4.46 + 4.47 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 4.48 + bind_program(sdr_mballs); 4.49 + 4.50 + for(int i=0; i<SZ; i++) { 4.51 + glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, 4.52 + vol->get_texture(), 0, i); 4.53 + 4.54 + glBegin(GL_QUADS); 4.55 + glVertex2f(-1, -1); 4.56 + glVertex2f(1, -1); 4.57 + glVertex2f(1, 1); 4.58 + glVertex2f(-1, 1); 4.59 + glEnd(); 4.60 + } 4.61 + 4.62 + bind_program(0); 4.63 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 4.64 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/demo.h Wed Apr 11 06:08:59 2012 +0300 5.3 @@ -0,0 +1,8 @@ 5.4 +#ifndef DEMO_H_ 5.5 +#define DEMO_H_ 5.6 + 5.7 +bool init_demo(); 5.8 +void draw_demo(); 5.9 + 5.10 + 5.11 +#endif // DEMO_H_
6.1 --- a/src/volray.cc Wed Apr 11 01:44:45 2012 +0200 6.2 +++ b/src/volray.cc Wed Apr 11 06:08:59 2012 +0300 6.3 @@ -14,6 +14,7 @@ 6.4 #include "sdr.h" 6.5 #include "volume.h" 6.6 #include "ui.h" 6.7 +#include "demo.h" 6.8 6.9 #define XFER_MAP_SZ 512 6.10 6.11 @@ -49,7 +50,7 @@ 6.12 static float cur_z = 0.0; 6.13 static float ray_step = 0.01; 6.14 6.15 -static const Volume *volume; 6.16 +static Volume *volume; 6.17 6.18 6.19 bool volray_init() 6.20 @@ -71,14 +72,21 @@ 6.21 set_uniform_int(slice_sdr, "volume", 0); 6.22 set_uniform_int(slice_sdr, "xfer_tex", 1); 6.23 6.24 + init_demo(); 6.25 + 6.26 return true; 6.27 } 6.28 6.29 -void volray_setvolume(const Volume *vol) 6.30 +void volray_setvolume(Volume *vol) 6.31 { 6.32 volume = vol; 6.33 } 6.34 6.35 +Volume *volray_getvolume() 6.36 +{ 6.37 + return volume; 6.38 +} 6.39 + 6.40 void volray_draw(void) 6.41 { 6.42 /* recalculate primary ray texture if needed */ 6.43 @@ -90,6 +98,8 @@ 6.44 create_transfer_map(xfer_mean, xfer_sdev); 6.45 } 6.46 6.47 + draw_demo(); 6.48 + 6.49 glClear(GL_COLOR_BUFFER_BIT); 6.50 6.51 if(volume) {
7.1 --- a/src/volray.h Wed Apr 11 01:44:45 2012 +0200 7.2 +++ b/src/volray.h Wed Apr 11 06:08:59 2012 +0300 7.3 @@ -4,7 +4,10 @@ 7.4 #include "volume.h" 7.5 7.6 bool volray_init(); 7.7 -void volray_setvolume(const Volume *vol); 7.8 + 7.9 +void volray_setvolume(Volume *vol); 7.10 +Volume *volray_getvolume(); 7.11 + 7.12 void volray_resize(int xsz, int ysz); 7.13 void volray_draw(); 7.14
8.1 --- a/src/volume.cc Wed Apr 11 01:44:45 2012 +0200 8.2 +++ b/src/volume.cc Wed Apr 11 06:08:59 2012 +0300 8.3 @@ -26,6 +26,19 @@ 8.4 } 8.5 } 8.6 8.7 +bool Volume::create(int xsz, int ysz, int zsz, float *data) 8.8 +{ 8.9 + if(!tex) 8.10 + glGenTextures(1, &tex); 8.11 + glBindTexture(GL_TEXTURE_3D, tex); 8.12 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 8.13 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 8.14 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 8.15 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 8.16 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); 8.17 + glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F_ARB, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data); 8.18 +} 8.19 + 8.20 bool Volume::load(const char *fname) 8.21 { 8.22 std::list<std::string> slist; 8.23 @@ -88,14 +101,7 @@ 8.24 calc_gradients(voxels, sz[0], sz[1], sz[2]); 8.25 8.26 /* create the volume texture */ 8.27 - glGenTextures(1, &tex); 8.28 - glBindTexture(GL_TEXTURE_3D, tex); 8.29 - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 8.30 - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 8.31 - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 8.32 - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 8.33 - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); 8.34 - glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F_ARB, sz[0], sz[1], sz[2], 0, GL_RGBA, GL_FLOAT, voxels); 8.35 + create(sz[0], sz[1], sz[2], voxels); 8.36 8.37 /* ... and destroy our copy */ 8.38 delete [] voxels;
9.1 --- a/src/volume.h Wed Apr 11 01:44:45 2012 +0200 9.2 +++ b/src/volume.h Wed Apr 11 06:08:59 2012 +0300 9.3 @@ -16,6 +16,7 @@ 9.4 Volume(); 9.5 ~Volume(); 9.6 9.7 + bool create(int xsz, int ysz, int zsz, float *data = 0); 9.8 bool load(const char *fname); 9.9 9.10 unsigned int get_texture() const;