invisible

annotate src/main.cc @ 23:b50ad2711f5f

fixed fov aspect projection et c, added shaders todo: segmentation
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sat, 16 Nov 2013 23:29:15 +0200
parents 531a814d4d6b
children 96b022f1210e
rev   line source
eleni@2 1 #include <GL/glew.h>
eleni@2 2 #include <GL/glut.h>
eleni@12 3
eleni@23 4 #include <math.h>
eleni@0 5 #include <stdio.h>
eleni@0 6
eleni@0 7 #include "kinect.h"
eleni@2 8 #include "frame.h"
eleni@21 9 #include "sdr.h"
eleni@12 10 #include "tesquad.h"
eleni@14 11 #include "texture.h"
eleni@0 12
eleni@23 13 #define FOV 45.0
eleni@23 14 #define QUAD_DIST 20.0 /*depth*/
eleni@17 15
eleni@0 16 freenect_context *kin_ctx;
eleni@0 17 freenect_device *kin_dev;
eleni@0 18 KinectParams kin_params;
eleni@2 19 Frame *frame;
eleni@0 20
eleni@14 21 static const char *filename = "data/textures/wallpaper.jpg";
eleni@23 22 static const char *vsdr_path = "data/shaders/invisible.v.glsl";
eleni@23 23 static const char *fsdr_path = "data/shaders/invisible.f.glsl";
eleni@23 24 static unsigned int sprog;
eleni@17 25 static unsigned int drawing;
eleni@17 26 static unsigned int debugging;
eleni@14 27 static unsigned char tex;
eleni@23 28 static int tex_height;
eleni@23 29 static int tex_width;
eleni@23 30 static float aspect;
eleni@13 31 static bool show;
eleni@13 32
eleni@2 33 static void cleanup();
eleni@23 34 static bool init();
eleni@23 35 static void init_tessquad();
eleni@2 36
eleni@2 37 static void display();
eleni@2 38 static void reshape(int w, int h);
eleni@2 39 static void keyb(unsigned char key, int x, int y);
eleni@2 40 static void idle();
eleni@2 41
eleni@2 42 bool has_video;
eleni@2 43 bool has_depth;
eleni@2 44
eleni@2 45 int main(int argc, char **argv)
eleni@0 46 {
eleni@0 47 if(!init_kinect(&kin_ctx, &kin_dev, &kin_params))
eleni@0 48 return 1;
eleni@0 49
eleni@7 50 if(!init_kinect_frames(kin_ctx, kin_dev, &kin_params)) {
eleni@2 51 stop_kinect(kin_ctx, kin_dev);
eleni@2 52 return 1;
eleni@2 53 }
eleni@2 54
eleni@2 55 glutInitWindowSize(800, 600);
eleni@2 56 glutInit(&argc, argv);
eleni@2 57 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
eleni@2 58 glutCreateWindow("Test Kinect");
eleni@2 59
eleni@2 60 glewInit();
eleni@2 61
eleni@2 62 glutDisplayFunc(display);
eleni@2 63 glutReshapeFunc(reshape);
eleni@2 64 glutKeyboardFunc(keyb);
eleni@2 65 glutIdleFunc(idle);
eleni@2 66
eleni@14 67 atexit(cleanup);
eleni@23 68 if(!init()) {
eleni@23 69 return 1;
eleni@23 70 }
eleni@2 71
eleni@2 72 glutMainLoop();
eleni@2 73 }
eleni@2 74
eleni@2 75 static void display()
eleni@2 76 {
eleni@14 77 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
eleni@2 78
eleni@2 79 glMatrixMode(GL_MODELVIEW);
eleni@2 80 glLoadIdentity();
eleni@2 81
eleni@2 82 has_depth = false; has_video = false;
eleni@2 83 if(freenect_process_events(kin_ctx) != 0) {
eleni@2 84 fprintf(stderr, "Failed to process events.\n");
eleni@2 85 exit(0);
eleni@2 86 }
eleni@2 87 frame->process();
eleni@2 88
eleni@23 89 glUseProgram(sprog);
eleni@23 90
eleni@23 91 // draw video frame
eleni@23 92 glMatrixMode(GL_MODELVIEW);
eleni@23 93 glPushMatrix();
eleni@23 94 glTranslatef(0, 0, -QUAD_DIST);
eleni@23 95
eleni@23 96 glActiveTexture(GL_TEXTURE0);
eleni@23 97 glEnable(GL_TEXTURE_2D);
eleni@23 98 glBindTexture(GL_TEXTURE_2D, tex);
eleni@23 99 glActiveTexture(GL_TEXTURE1);
eleni@23 100 glEnable(GL_TEXTURE_2D);
eleni@23 101 glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
eleni@23 102
eleni@17 103 glCallList(drawing);
eleni@23 104
eleni@23 105 glActiveTexture(GL_TEXTURE1);
eleni@23 106 glDisable(GL_TEXTURE_2D);
eleni@23 107 glActiveTexture(GL_TEXTURE0);
eleni@23 108 glDisable(GL_TEXTURE_2D);
eleni@23 109
eleni@23 110 glMatrixMode(GL_MODELVIEW);
eleni@23 111 glPopMatrix();
eleni@23 112
eleni@23 113 glUseProgram(0);
eleni@23 114
eleni@17 115 if(show)
eleni@17 116 glCallList(debugging);
eleni@17 117 glFlush();
eleni@12 118
eleni@2 119 glutSwapBuffers();
eleni@2 120 }
eleni@2 121
eleni@2 122 static void reshape(int w, int h)
eleni@2 123 {
eleni@23 124 aspect = (float)w / (float)h;
eleni@2 125 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
eleni@2 126 glMatrixMode(GL_PROJECTION);
eleni@2 127 glLoadIdentity();
eleni@23 128 gluPerspective(FOV, aspect, 1, 1000);
eleni@23 129 init_tessquad();
eleni@2 130 }
eleni@2 131
eleni@2 132 static void keyb(unsigned char key, int x, int y)
eleni@2 133 {
eleni@2 134 switch(key) {
eleni@13 135 case 's':
eleni@13 136 if(show)
eleni@13 137 show = false;
eleni@13 138 else
eleni@13 139 show = true;
eleni@13 140 break;
eleni@2 141 case 27:
eleni@2 142 exit(0);
eleni@2 143 default:
eleni@2 144 break;
eleni@2 145 }
eleni@2 146 }
eleni@2 147
eleni@2 148 static void idle()
eleni@2 149 {
eleni@2 150 glutPostRedisplay();
eleni@2 151 }
eleni@2 152
eleni@2 153 static void cleanup()
eleni@2 154 {
eleni@23 155 glDeleteLists(drawing, 1);
eleni@23 156 glDeleteLists(debugging, 1);
eleni@23 157
eleni@2 158 stop_kinect_frames(kin_dev);
eleni@0 159 stop_kinect(kin_ctx, kin_dev);
eleni@0 160 }
eleni@17 161
eleni@23 162 static bool init()
eleni@17 163 {
eleni@23 164 glClearColor(1, 0, 0, 1);
eleni@17 165
eleni@17 166 frame = new Frame;
eleni@23 167 if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
eleni@17 168 fprintf(stderr, "Failed to load texture: %s\n", filename);
eleni@23 169 return false;
eleni@17 170 }
eleni@17 171
eleni@21 172 /* shaders setup */
eleni@23 173 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
eleni@23 174 fprintf(stderr, "Failed to create shader program!\n");
eleni@23 175 return false;
eleni@23 176 }
eleni@21 177
eleni@23 178 glUseProgram(sprog);
eleni@23 179 int dloc = glGetUniformLocation(sprog, "depth_tex");
eleni@23 180 if(dloc != -1)
eleni@23 181 glUniform1i(dloc, 1);
eleni@23 182 int tloc = glGetUniformLocation(sprog, "tex");
eleni@23 183 if(tloc != -1)
eleni@23 184 glUniform1i(tloc, 0);
eleni@23 185 glUseProgram(0);
eleni@17 186
eleni@18 187 /* debugging */
eleni@17 188 debugging = glGenLists(1);
eleni@17 189 glNewList(debugging, GL_COMPILE);
eleni@17 190 glEnable(GL_TEXTURE_2D);
eleni@17 191 glBindTexture(GL_TEXTURE_2D, frame->video_tex);
eleni@17 192 draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
eleni@17 193 glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
eleni@17 194 draw_tess_quad(0, -1, 1, 2, 1, 1, true);
eleni@17 195 glDisable(GL_TEXTURE_2D);
eleni@17 196 glEndList();
eleni@23 197
eleni@23 198 return true;
eleni@17 199 }
eleni@23 200
eleni@23 201 static void init_tessquad()
eleni@23 202 {
eleni@23 203 if(drawing)
eleni@23 204 glDeleteLists(drawing, 1);
eleni@23 205
eleni@23 206 drawing = glGenLists(1);
eleni@23 207 glNewList(drawing, GL_COMPILE);
eleni@23 208 float fov_rads = FOV / 180.0 * M_PI;
eleni@23 209 float ysz = QUAD_DIST * tan(fov_rads / 2.0);
eleni@23 210 float xsz = ysz * aspect;
eleni@23 211 draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
eleni@23 212 glEndList();
eleni@23 213 }