eleni@2: #include eleni@2: #include eleni@12: eleni@23: #include eleni@0: #include eleni@0: eleni@0: #include "kinect.h" eleni@2: #include "frame.h" eleni@21: #include "sdr.h" eleni@12: #include "tesquad.h" eleni@14: #include "texture.h" eleni@0: eleni@23: #define FOV 45.0 eleni@23: #define QUAD_DIST 20.0 /*depth*/ eleni@17: eleni@0: freenect_context *kin_ctx; eleni@0: freenect_device *kin_dev; eleni@0: KinectParams kin_params; eleni@2: Frame *frame; eleni@0: eleni@14: static const char *filename = "data/textures/wallpaper.jpg"; eleni@23: static const char *vsdr_path = "data/shaders/invisible.v.glsl"; eleni@23: static const char *fsdr_path = "data/shaders/invisible.f.glsl"; eleni@23: static unsigned int sprog; eleni@17: static unsigned int drawing; eleni@17: static unsigned int debugging; eleni@14: static unsigned char tex; eleni@23: static int tex_height; eleni@23: static int tex_width; eleni@23: static float aspect; eleni@13: static bool show; eleni@13: eleni@2: static void cleanup(); eleni@23: static bool init(); eleni@23: static void init_tessquad(); eleni@2: eleni@2: static void display(); eleni@2: static void reshape(int w, int h); eleni@2: static void keyb(unsigned char key, int x, int y); eleni@2: static void idle(); eleni@2: eleni@2: bool has_video; eleni@2: bool has_depth; eleni@2: eleni@2: int main(int argc, char **argv) eleni@0: { eleni@0: if(!init_kinect(&kin_ctx, &kin_dev, &kin_params)) eleni@0: return 1; eleni@0: eleni@7: if(!init_kinect_frames(kin_ctx, kin_dev, &kin_params)) { eleni@2: stop_kinect(kin_ctx, kin_dev); eleni@2: return 1; eleni@2: } eleni@2: eleni@2: glutInitWindowSize(800, 600); eleni@2: glutInit(&argc, argv); eleni@2: glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); eleni@2: glutCreateWindow("Test Kinect"); eleni@2: eleni@2: glewInit(); eleni@2: eleni@2: glutDisplayFunc(display); eleni@2: glutReshapeFunc(reshape); eleni@2: glutKeyboardFunc(keyb); eleni@2: glutIdleFunc(idle); eleni@2: eleni@14: atexit(cleanup); eleni@23: if(!init()) { eleni@23: return 1; eleni@23: } eleni@2: eleni@2: glutMainLoop(); eleni@2: } eleni@2: eleni@2: static void display() eleni@2: { eleni@14: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); eleni@2: eleni@2: glMatrixMode(GL_MODELVIEW); eleni@2: glLoadIdentity(); eleni@2: eleni@2: has_depth = false; has_video = false; eleni@2: if(freenect_process_events(kin_ctx) != 0) { eleni@2: fprintf(stderr, "Failed to process events.\n"); eleni@2: exit(0); eleni@2: } eleni@2: frame->process(); eleni@2: eleni@23: glUseProgram(sprog); eleni@23: eleni@23: // draw video frame eleni@23: glMatrixMode(GL_MODELVIEW); eleni@23: glPushMatrix(); eleni@23: glTranslatef(0, 0, -QUAD_DIST); eleni@23: eleni@23: glActiveTexture(GL_TEXTURE0); eleni@23: glEnable(GL_TEXTURE_2D); eleni@23: glBindTexture(GL_TEXTURE_2D, tex); eleni@23: glActiveTexture(GL_TEXTURE1); eleni@23: glEnable(GL_TEXTURE_2D); eleni@23: glBindTexture(GL_TEXTURE_2D, frame->depth_tex); eleni@23: eleni@17: glCallList(drawing); eleni@23: eleni@23: glActiveTexture(GL_TEXTURE1); eleni@23: glDisable(GL_TEXTURE_2D); eleni@23: glActiveTexture(GL_TEXTURE0); eleni@23: glDisable(GL_TEXTURE_2D); eleni@23: eleni@23: glMatrixMode(GL_MODELVIEW); eleni@23: glPopMatrix(); eleni@23: eleni@23: glUseProgram(0); eleni@23: eleni@17: if(show) eleni@17: glCallList(debugging); eleni@17: glFlush(); eleni@12: eleni@2: glutSwapBuffers(); eleni@2: } eleni@2: eleni@2: static void reshape(int w, int h) eleni@2: { eleni@23: aspect = (float)w / (float)h; eleni@2: glViewport(0, 0, (GLsizei) w, (GLsizei) h); eleni@2: glMatrixMode(GL_PROJECTION); eleni@2: glLoadIdentity(); eleni@23: gluPerspective(FOV, aspect, 1, 1000); eleni@23: init_tessquad(); eleni@2: } eleni@2: eleni@2: static void keyb(unsigned char key, int x, int y) eleni@2: { eleni@2: switch(key) { eleni@13: case 's': eleni@13: if(show) eleni@13: show = false; eleni@13: else eleni@13: show = true; eleni@13: break; eleni@2: case 27: eleni@2: exit(0); eleni@2: default: eleni@2: break; eleni@2: } eleni@2: } eleni@2: eleni@2: static void idle() eleni@2: { eleni@2: glutPostRedisplay(); eleni@2: } eleni@2: eleni@2: static void cleanup() eleni@2: { eleni@23: glDeleteLists(drawing, 1); eleni@23: glDeleteLists(debugging, 1); eleni@23: eleni@2: stop_kinect_frames(kin_dev); eleni@0: stop_kinect(kin_ctx, kin_dev); eleni@0: } eleni@17: eleni@23: static bool init() eleni@17: { eleni@23: glClearColor(1, 0, 0, 1); eleni@17: eleni@17: frame = new Frame; eleni@23: if(!(tex = load_texture(filename, &tex_width, &tex_height))) { eleni@17: fprintf(stderr, "Failed to load texture: %s\n", filename); eleni@23: return false; eleni@17: } eleni@17: eleni@21: /* shaders setup */ eleni@23: if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) { eleni@23: fprintf(stderr, "Failed to create shader program!\n"); eleni@23: return false; eleni@23: } eleni@21: eleni@23: glUseProgram(sprog); eleni@23: int dloc = glGetUniformLocation(sprog, "depth_tex"); eleni@23: if(dloc != -1) eleni@23: glUniform1i(dloc, 1); eleni@23: int tloc = glGetUniformLocation(sprog, "tex"); eleni@23: if(tloc != -1) eleni@23: glUniform1i(tloc, 0); eleni@23: glUseProgram(0); eleni@17: eleni@18: /* debugging */ eleni@17: debugging = glGenLists(1); eleni@17: glNewList(debugging, GL_COMPILE); eleni@17: glEnable(GL_TEXTURE_2D); eleni@17: glBindTexture(GL_TEXTURE_2D, frame->video_tex); eleni@17: draw_tess_quad(-1, -1, 1, 2, 1, 1, true); eleni@17: glBindTexture(GL_TEXTURE_2D, frame->depth_tex); eleni@17: draw_tess_quad(0, -1, 1, 2, 1, 1, true); eleni@17: glDisable(GL_TEXTURE_2D); eleni@17: glEndList(); eleni@23: eleni@23: return true; eleni@17: } eleni@23: eleni@23: static void init_tessquad() eleni@23: { eleni@23: if(drawing) eleni@23: glDeleteLists(drawing, 1); eleni@23: eleni@23: drawing = glGenLists(1); eleni@23: glNewList(drawing, GL_COMPILE); eleni@23: float fov_rads = FOV / 180.0 * M_PI; eleni@23: float ysz = QUAD_DIST * tan(fov_rads / 2.0); eleni@23: float xsz = ysz * aspect; eleni@23: draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1); eleni@23: glEndList(); eleni@23: }