# HG changeset patch # User John Tsiombikas # Date 1334113739 -10800 # Node ID 3d05c261a2f41b479708091a156604574a38b628 # Parent 535762131d349705b843c2208b4eb20684fba144 demo metaballs crash & burn diff -r 535762131d34 -r 3d05c261a2f4 qvolray.pro --- a/qvolray.pro Wed Apr 11 01:44:45 2012 +0200 +++ b/qvolray.pro Wed Apr 11 06:08:59 2012 +0300 @@ -13,4 +13,4 @@ # Input HEADERS += src/sdr.h src/volray.h src/volume.h src/ui.h -SOURCES += src/main.cc src/sdr.c src/volray.cc src/volume.cc src/ui.cc +SOURCES += src/main.cc src/sdr.c src/volray.cc src/volume.cc src/ui.cc src/demo.cc diff -r 535762131d34 -r 3d05c261a2f4 sdr/demo.p.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/demo.p.glsl Wed Apr 11 06:08:59 2012 +0300 @@ -0,0 +1,10 @@ +void main() +{ + float val = 0.0; + float len = length(gl_FragCoord.xyz); + if(len < 0.5) { + val = 1.0; + } + + gl_FragColor = vec4(gl_FragCoord.xyz / len, val); +} diff -r 535762131d34 -r 3d05c261a2f4 sdr/demo.v.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/demo.v.glsl Wed Apr 11 06:08:59 2012 +0300 @@ -0,0 +1,4 @@ +void main() +{ + gl_Position = gl_Vertex; +} diff -r 535762131d34 -r 3d05c261a2f4 src/demo.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/demo.cc Wed Apr 11 06:08:59 2012 +0300 @@ -0,0 +1,61 @@ +#include +#include "demo.h" +#include "sdr.h" +#include "volray.h" + +#define SZ 128 + +static Volume *vol; +static unsigned int sdr_mballs; +static unsigned int fbo; + +bool init_demo() +{ + if(!(sdr_mballs = create_program_load("sdr/demo.v.glsl", "sdr/demo.p.glsl"))) { + return false; + } + + vol = new Volume; + vol->create(SZ, SZ, SZ); + volray_setvolume(vol); + + glGenFramebuffers(1, &fbo); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, + vol->get_texture(), 0, 0); + + unsigned int stat = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if(stat != GL_FRAMEBUFFER_COMPLETE) { + printf("incomplete framebuffer: %u\n", stat); + delete vol; + return false; + } + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + return true; +} + +void draw_demo() +{ + if(volray_getvolume() != vol) { + return; + } + + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + bind_program(sdr_mballs); + + for(int i=0; iget_texture(), 0, i); + + glBegin(GL_QUADS); + glVertex2f(-1, -1); + glVertex2f(1, -1); + glVertex2f(1, 1); + glVertex2f(-1, 1); + glEnd(); + } + + bind_program(0); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} diff -r 535762131d34 -r 3d05c261a2f4 src/demo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/demo.h Wed Apr 11 06:08:59 2012 +0300 @@ -0,0 +1,8 @@ +#ifndef DEMO_H_ +#define DEMO_H_ + +bool init_demo(); +void draw_demo(); + + +#endif // DEMO_H_ diff -r 535762131d34 -r 3d05c261a2f4 src/volray.cc --- a/src/volray.cc Wed Apr 11 01:44:45 2012 +0200 +++ b/src/volray.cc Wed Apr 11 06:08:59 2012 +0300 @@ -14,6 +14,7 @@ #include "sdr.h" #include "volume.h" #include "ui.h" +#include "demo.h" #define XFER_MAP_SZ 512 @@ -49,7 +50,7 @@ static float cur_z = 0.0; static float ray_step = 0.01; -static const Volume *volume; +static Volume *volume; bool volray_init() @@ -71,14 +72,21 @@ set_uniform_int(slice_sdr, "volume", 0); set_uniform_int(slice_sdr, "xfer_tex", 1); + init_demo(); + return true; } -void volray_setvolume(const Volume *vol) +void volray_setvolume(Volume *vol) { volume = vol; } +Volume *volray_getvolume() +{ + return volume; +} + void volray_draw(void) { /* recalculate primary ray texture if needed */ @@ -90,6 +98,8 @@ create_transfer_map(xfer_mean, xfer_sdev); } + draw_demo(); + glClear(GL_COLOR_BUFFER_BIT); if(volume) { diff -r 535762131d34 -r 3d05c261a2f4 src/volray.h --- a/src/volray.h Wed Apr 11 01:44:45 2012 +0200 +++ b/src/volray.h Wed Apr 11 06:08:59 2012 +0300 @@ -4,7 +4,10 @@ #include "volume.h" bool volray_init(); -void volray_setvolume(const Volume *vol); + +void volray_setvolume(Volume *vol); +Volume *volray_getvolume(); + void volray_resize(int xsz, int ysz); void volray_draw(); diff -r 535762131d34 -r 3d05c261a2f4 src/volume.cc --- a/src/volume.cc Wed Apr 11 01:44:45 2012 +0200 +++ b/src/volume.cc Wed Apr 11 06:08:59 2012 +0300 @@ -26,6 +26,19 @@ } } +bool Volume::create(int xsz, int ysz, int zsz, float *data) +{ + if(!tex) + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_3D, tex); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F_ARB, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data); +} + bool Volume::load(const char *fname) { std::list slist; @@ -88,14 +101,7 @@ calc_gradients(voxels, sz[0], sz[1], sz[2]); /* create the volume texture */ - glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_3D, tex); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F_ARB, sz[0], sz[1], sz[2], 0, GL_RGBA, GL_FLOAT, voxels); + create(sz[0], sz[1], sz[2], voxels); /* ... and destroy our copy */ delete [] voxels; diff -r 535762131d34 -r 3d05c261a2f4 src/volume.h --- a/src/volume.h Wed Apr 11 01:44:45 2012 +0200 +++ b/src/volume.h Wed Apr 11 06:08:59 2012 +0300 @@ -16,6 +16,7 @@ Volume(); ~Volume(); + bool create(int xsz, int ysz, int zsz, float *data = 0); bool load(const char *fname); unsigned int get_texture() const;