qvolray
changeset 36:70b937008134
demo :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 15 Apr 2012 07:19:01 +0300 |
parents | 6ca076bf5084 |
children | 450d4c50470f |
files | qvolray.pro sdr/demo.p.glsl src/demo.cc src/demo.h src/volray.cc |
diffstat | 5 files changed, 159 insertions(+), 14 deletions(-) [+] |
line diff
1.1 --- a/qvolray.pro Sun Apr 15 02:22:13 2012 +0300 1.2 +++ b/qvolray.pro Sun Apr 15 07:19:01 2012 +0300 1.3 @@ -26,7 +26,8 @@ 1.4 HEADERS = src/ui.h \ 1.5 src/ui_maingl.h \ 1.6 src/ui_sliceview.h \ 1.7 - src/ui_xferview.h 1.8 + src/ui_xferview.h \ 1.9 + src/demo.h 1.10 1.11 SOURCES = src/main.cc \ 1.12 src/sdr.c \
2.1 --- a/sdr/demo.p.glsl Sun Apr 15 02:22:13 2012 +0300 2.2 +++ b/sdr/demo.p.glsl Sun Apr 15 07:19:01 2012 +0300 2.3 @@ -1,9 +1,32 @@ 2.4 +#define NBALLS 3 2.5 + 2.6 +uniform float energy[NBALLS]; 2.7 +uniform vec3 pos[NBALLS]; 2.8 + 2.9 +float eval(vec3 p) 2.10 +{ 2.11 + float val = 0.0; 2.12 + 2.13 + for(int i=0; i<NBALLS; i++) { 2.14 + vec3 dif = p - pos[i]; 2.15 + float len_sq = dot(dif, dif); 2.16 + val += 0.1 * energy[i] / len_sq; 2.17 + } 2.18 + return val; 2.19 +} 2.20 + 2.21 +#define OFFS 0.01 2.22 + 2.23 void main() 2.24 { 2.25 vec3 pos = gl_TexCoord[0].xyz * 2.0 - 1.0; 2.26 2.27 - float len = length(pos); 2.28 - float val = 0.1 / (len * len); 2.29 + float val = eval(pos); 2.30 2.31 - gl_FragColor = vec4(-pos / len, val); 2.32 + vec3 grad; 2.33 + grad.x = eval(pos + vec3(OFFS, 0.0, 0.0)) - eval(pos - vec3(OFFS, 0.0, 0.0)); 2.34 + grad.y = eval(pos + vec3(0.0, OFFS, 0.0)) - eval(pos - vec3(0.0, OFFS, 0.0)); 2.35 + grad.z = eval(pos + vec3(0.0, 0.0, OFFS)) - eval(pos - vec3(0.0, 0.0, OFFS)); 2.36 + 2.37 + gl_FragColor = vec4(grad, val); 2.38 }
3.1 --- a/src/demo.cc Sun Apr 15 02:22:13 2012 +0300 3.2 +++ b/src/demo.cc Sun Apr 15 07:19:01 2012 +0300 3.3 @@ -1,16 +1,66 @@ 3.4 +#include <random> 3.5 +#include <functional> 3.6 +#include <QTimer> 3.7 #include <assert.h> 3.8 #include <GL/glew.h> 3.9 #include "demo.h" 3.10 #include "sdr.h" 3.11 #include "volray.h" 3.12 +#include "ui.h" 3.13 3.14 -#define SZ 128 3.15 +#define SZ 128 3.16 +#define TIMER_INTERVAL 33 3.17 3.18 -static Volume *vol; 3.19 -static unsigned int sdr_mballs; 3.20 -static unsigned int fbo; 3.21 3.22 -bool init_demo() 3.23 +Demo::Demo() 3.24 +{ 3.25 + vol = 0; 3.26 + sdr_mballs = 0; 3.27 + fbo = 0; 3.28 + timer = 0; 3.29 + balls = 0; 3.30 + tmsec = 0; 3.31 +} 3.32 + 3.33 +Demo::~Demo() 3.34 +{ 3.35 + glDeleteFramebuffersEXT(1, &fbo); 3.36 + free_program(sdr_mballs); 3.37 + delete vol; 3.38 + delete timer; 3.39 + 3.40 + delete [] balls; 3.41 +} 3.42 + 3.43 +void Demo::timer_func() 3.44 +{ 3.45 + float tsec = tmsec / 1000.0; 3.46 + const float speed = 1.0; 3.47 + 3.48 + for(int i=0; i<num_balls; i++) { 3.49 + float t = tsec * speed; 3.50 + int idx = (int)t % MB_PATH_SZ; 3.51 + t -= floor(t); 3.52 + 3.53 + Vector3 a = balls[i].path[idx]; 3.54 + Vector3 b = balls[i].path[(idx + 1) % MB_PATH_SZ]; 3.55 + balls[i].pos = lerp(a, b, t); 3.56 + 3.57 + char name[32]; 3.58 + 3.59 + sprintf(name, "pos[%d]", i); 3.60 + set_uniform_float3(sdr_mballs, name, balls[i].pos.x, balls[i].pos.y, balls[i].pos.z); 3.61 + 3.62 + sprintf(name, "energy[%d]", i); 3.63 + set_uniform_float(sdr_mballs, name, balls[i].energy); 3.64 + } 3.65 + 3.66 + post_redisplay(); 3.67 + 3.68 + tmsec += TIMER_INTERVAL; 3.69 +} 3.70 + 3.71 +bool Demo::init() 3.72 { 3.73 if(!(sdr_mballs = create_program_load("sdr/demo.v.glsl", "sdr/demo.p.glsl"))) { 3.74 return false; 3.75 @@ -38,13 +88,43 @@ 3.76 } 3.77 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); 3.78 3.79 + // initialize metaballs 3.80 + auto rnd_energy = std::bind(std::uniform_real_distribution<float>(0.7, 1.6), std::mt19937()); 3.81 + auto rnd_path = std::bind(std::uniform_real_distribution<float>(-0.5, 0.5), std::mt19937()); 3.82 + 3.83 + num_balls = 4; 3.84 + balls = new MetaBall[4]; 3.85 + 3.86 + for(int i=0; i<4; i++) { 3.87 + balls[i].energy = rnd_energy(); 3.88 + 3.89 + for(int j=0; j<MB_PATH_SZ; j++) { 3.90 + balls[i].path[j].x = rnd_path(); 3.91 + balls[i].path[j].y = rnd_path(); 3.92 + balls[i].path[j].z = rnd_path(); 3.93 + } 3.94 + } 3.95 + 3.96 + // update timer 3.97 + timer = new QTimer(this); 3.98 + timer->setSingleShot(false); 3.99 + connect(timer, SIGNAL(timeout()), this, SLOT(timer_func())); 3.100 + timer->start(TIMER_INTERVAL); 3.101 + 3.102 return true; 3.103 } 3.104 3.105 -void draw_demo() 3.106 +void Demo::draw() const 3.107 { 3.108 if(volray_getvolume() != vol) { 3.109 + if(timer->isActive()) { 3.110 + timer->stop(); 3.111 + } 3.112 return; 3.113 + } else { 3.114 + if(!timer->isActive()) { 3.115 + timer->start(TIMER_INTERVAL); 3.116 + } 3.117 } 3.118 3.119 glPushAttrib(GL_VIEWPORT_BIT);
4.1 --- a/src/demo.h Sun Apr 15 02:22:13 2012 +0300 4.2 +++ b/src/demo.h Sun Apr 15 07:19:01 2012 +0300 4.3 @@ -1,8 +1,45 @@ 4.4 #ifndef DEMO_H_ 4.5 #define DEMO_H_ 4.6 4.7 -bool init_demo(); 4.8 -void draw_demo(); 4.9 +#include <QObject> 4.10 +#include <QTimer> 4.11 +#include <vmath/vmath.h> 4.12 + 4.13 +class Volume; 4.14 + 4.15 +#define MB_PATH_SZ 5 4.16 + 4.17 +struct MetaBall { 4.18 + float energy; 4.19 + Vector3 pos; 4.20 + 4.21 + Vector3 path[MB_PATH_SZ]; 4.22 +}; 4.23 + 4.24 +class Demo : public QObject { 4.25 +private: 4.26 + Q_OBJECT 4.27 + 4.28 + Volume *vol; 4.29 + unsigned int sdr_mballs; 4.30 + unsigned int fbo; 4.31 + 4.32 + QTimer *timer; 4.33 + MetaBall *balls; 4.34 + int num_balls; 4.35 + 4.36 + unsigned long tmsec; 4.37 + 4.38 +private slots: 4.39 + void timer_func(); 4.40 + 4.41 +public: 4.42 + Demo(); 4.43 + ~Demo(); 4.44 + 4.45 + bool init(); 4.46 + void draw() const; 4.47 +}; 4.48 4.49 4.50 #endif // DEMO_H_
5.1 --- a/src/volray.cc Sun Apr 15 02:22:13 2012 +0300 5.2 +++ b/src/volray.cc Sun Apr 15 07:19:01 2012 +0300 5.3 @@ -48,6 +48,7 @@ 5.4 static bool clip_z; 5.5 static float ray_step = 0.01; 5.6 5.7 +static Demo *demo; 5.8 static Volume *volume; 5.9 5.10 static bool dbg_noray; 5.11 @@ -72,7 +73,10 @@ 5.12 set_uniform_int(slice_sdr, "volume", 0); 5.13 set_uniform_int(slice_sdr, "xfer_tex", 1); 5.14 5.15 - init_demo(); 5.16 + demo = new Demo; 5.17 + if(!demo->init()) { 5.18 + return false; 5.19 + } 5.20 5.21 return true; 5.22 } 5.23 @@ -141,7 +145,7 @@ 5.24 if(dbg_noray) 5.25 return; 5.26 5.27 - draw_demo(); 5.28 + demo->draw(); 5.29 5.30 if(volume) { 5.31 render_volume();