qvolray

view src/demo.cc @ 36:70b937008134

demo :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Apr 2012 07:19:01 +0300
parents c27ce79632db
children
line source
1 #include <random>
2 #include <functional>
3 #include <QTimer>
4 #include <assert.h>
5 #include <GL/glew.h>
6 #include "demo.h"
7 #include "sdr.h"
8 #include "volray.h"
9 #include "ui.h"
11 #define SZ 128
12 #define TIMER_INTERVAL 33
15 Demo::Demo()
16 {
17 vol = 0;
18 sdr_mballs = 0;
19 fbo = 0;
20 timer = 0;
21 balls = 0;
22 tmsec = 0;
23 }
25 Demo::~Demo()
26 {
27 glDeleteFramebuffersEXT(1, &fbo);
28 free_program(sdr_mballs);
29 delete vol;
30 delete timer;
32 delete [] balls;
33 }
35 void Demo::timer_func()
36 {
37 float tsec = tmsec / 1000.0;
38 const float speed = 1.0;
40 for(int i=0; i<num_balls; i++) {
41 float t = tsec * speed;
42 int idx = (int)t % MB_PATH_SZ;
43 t -= floor(t);
45 Vector3 a = balls[i].path[idx];
46 Vector3 b = balls[i].path[(idx + 1) % MB_PATH_SZ];
47 balls[i].pos = lerp(a, b, t);
49 char name[32];
51 sprintf(name, "pos[%d]", i);
52 set_uniform_float3(sdr_mballs, name, balls[i].pos.x, balls[i].pos.y, balls[i].pos.z);
54 sprintf(name, "energy[%d]", i);
55 set_uniform_float(sdr_mballs, name, balls[i].energy);
56 }
58 post_redisplay();
60 tmsec += TIMER_INTERVAL;
61 }
63 bool Demo::init()
64 {
65 if(!(sdr_mballs = create_program_load("sdr/demo.v.glsl", "sdr/demo.p.glsl"))) {
66 return false;
67 }
69 vol = new Volume;
70 vol->create(SZ, SZ, SZ);
71 volray_setvolume(vol);
73 assert(glGenFramebuffersEXT);
74 assert(glBindFramebufferEXT);
75 assert(glFramebufferTexture3DEXT);
76 assert(glCheckFramebufferStatusEXT);
78 glGenFramebuffersEXT(1, &fbo);
79 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
80 glFramebufferTexture3DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D,
81 vol->get_texture(), 0, 0);
83 unsigned int stat = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
84 if(stat != GL_FRAMEBUFFER_COMPLETE) {
85 printf("incomplete framebuffer: %u\n", stat);
86 delete vol;
87 return false;
88 }
89 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
91 // initialize metaballs
92 auto rnd_energy = std::bind(std::uniform_real_distribution<float>(0.7, 1.6), std::mt19937());
93 auto rnd_path = std::bind(std::uniform_real_distribution<float>(-0.5, 0.5), std::mt19937());
95 num_balls = 4;
96 balls = new MetaBall[4];
98 for(int i=0; i<4; i++) {
99 balls[i].energy = rnd_energy();
101 for(int j=0; j<MB_PATH_SZ; j++) {
102 balls[i].path[j].x = rnd_path();
103 balls[i].path[j].y = rnd_path();
104 balls[i].path[j].z = rnd_path();
105 }
106 }
108 // update timer
109 timer = new QTimer(this);
110 timer->setSingleShot(false);
111 connect(timer, SIGNAL(timeout()), this, SLOT(timer_func()));
112 timer->start(TIMER_INTERVAL);
114 return true;
115 }
117 void Demo::draw() const
118 {
119 if(volray_getvolume() != vol) {
120 if(timer->isActive()) {
121 timer->stop();
122 }
123 return;
124 } else {
125 if(!timer->isActive()) {
126 timer->start(TIMER_INTERVAL);
127 }
128 }
130 glPushAttrib(GL_VIEWPORT_BIT);
131 glViewport(0, 0, SZ, SZ);
133 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
134 bind_program(sdr_mballs);
136 for(int i=0; i<SZ; i++) {
137 glFramebufferTexture3DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D,
138 vol->get_texture(), 0, i);
140 float z = (float)i / (float)SZ;
142 glBegin(GL_QUADS);
143 glTexCoord3f(0, 0, z);
144 glVertex2f(-1, -1);
145 glTexCoord3f(1, 0, z);
146 glVertex2f(1, -1);
147 glTexCoord3f(1, 1, z);
148 glVertex2f(1, 1);
149 glTexCoord3f(0, 1, z);
150 glVertex2f(-1, 1);
151 glEnd();
152 }
154 bind_program(0);
155 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
157 glPopAttrib();
158 }