qvolray

annotate src/demo.cc @ 25:63bc059778d0

fixed ray marching
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Apr 2012 01:13:43 +0300
parents 4c62be57fc1a
children 70b937008134
rev   line source
nuclear@24 1 #include <assert.h>
nuclear@18 2 #include <GL/glew.h>
nuclear@18 3 #include "demo.h"
nuclear@18 4 #include "sdr.h"
nuclear@18 5 #include "volray.h"
nuclear@18 6
nuclear@18 7 #define SZ 128
nuclear@18 8
nuclear@18 9 static Volume *vol;
nuclear@18 10 static unsigned int sdr_mballs;
nuclear@18 11 static unsigned int fbo;
nuclear@18 12
nuclear@18 13 bool init_demo()
nuclear@18 14 {
nuclear@18 15 if(!(sdr_mballs = create_program_load("sdr/demo.v.glsl", "sdr/demo.p.glsl"))) {
nuclear@18 16 return false;
nuclear@18 17 }
nuclear@18 18
nuclear@18 19 vol = new Volume;
nuclear@18 20 vol->create(SZ, SZ, SZ);
nuclear@18 21 volray_setvolume(vol);
nuclear@18 22
nuclear@24 23 assert(glGenFramebuffersEXT);
nuclear@24 24 assert(glBindFramebufferEXT);
nuclear@24 25 assert(glFramebufferTexture3DEXT);
nuclear@24 26 assert(glCheckFramebufferStatusEXT);
nuclear@24 27
nuclear@24 28 glGenFramebuffersEXT(1, &fbo);
nuclear@24 29 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
nuclear@24 30 glFramebufferTexture3DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D,
nuclear@18 31 vol->get_texture(), 0, 0);
nuclear@18 32
nuclear@24 33 unsigned int stat = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
nuclear@18 34 if(stat != GL_FRAMEBUFFER_COMPLETE) {
nuclear@18 35 printf("incomplete framebuffer: %u\n", stat);
nuclear@18 36 delete vol;
nuclear@18 37 return false;
nuclear@18 38 }
nuclear@24 39 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@18 40
nuclear@18 41 return true;
nuclear@18 42 }
nuclear@18 43
nuclear@18 44 void draw_demo()
nuclear@18 45 {
nuclear@18 46 if(volray_getvolume() != vol) {
nuclear@18 47 return;
nuclear@18 48 }
nuclear@18 49
nuclear@21 50 glPushAttrib(GL_VIEWPORT_BIT);
nuclear@21 51 glViewport(0, 0, SZ, SZ);
nuclear@21 52
nuclear@24 53 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
nuclear@18 54 bind_program(sdr_mballs);
nuclear@18 55
nuclear@18 56 for(int i=0; i<SZ; i++) {
nuclear@24 57 glFramebufferTexture3DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D,
nuclear@18 58 vol->get_texture(), 0, i);
nuclear@18 59
nuclear@21 60 float z = (float)i / (float)SZ;
nuclear@21 61
nuclear@18 62 glBegin(GL_QUADS);
nuclear@21 63 glTexCoord3f(0, 0, z);
nuclear@18 64 glVertex2f(-1, -1);
nuclear@21 65 glTexCoord3f(1, 0, z);
nuclear@18 66 glVertex2f(1, -1);
nuclear@21 67 glTexCoord3f(1, 1, z);
nuclear@18 68 glVertex2f(1, 1);
nuclear@21 69 glTexCoord3f(0, 1, z);
nuclear@18 70 glVertex2f(-1, 1);
nuclear@18 71 glEnd();
nuclear@18 72 }
nuclear@18 73
nuclear@18 74 bind_program(0);
nuclear@24 75 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@21 76
nuclear@21 77 glPopAttrib();
nuclear@18 78 }