invisible

annotate src/main.cc @ 17:136460b6c9af

display lists
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 10 Nov 2013 00:36:58 +0200
parents 97dad98482f5
children ad78eb81988d
rev   line source
eleni@2 1 #include <GL/glew.h>
eleni@2 2 #include <GL/glut.h>
eleni@12 3
eleni@0 4 #include <stdio.h>
eleni@0 5
eleni@0 6 #include "kinect.h"
eleni@2 7 #include "frame.h"
eleni@12 8 #include "tesquad.h"
eleni@14 9 #include "texture.h"
eleni@0 10
eleni@17 11 #define ASPECT 1
eleni@17 12
eleni@0 13 freenect_context *kin_ctx;
eleni@0 14 freenect_device *kin_dev;
eleni@0 15 KinectParams kin_params;
eleni@2 16 Frame *frame;
eleni@0 17
eleni@14 18 static const char *filename = "data/textures/wallpaper.jpg";
eleni@17 19 static unsigned int drawing;
eleni@17 20 static unsigned int debugging;
eleni@14 21 static unsigned char tex;
eleni@13 22 static bool show;
eleni@13 23
eleni@2 24 static void cleanup();
eleni@17 25 static void init();
eleni@2 26
eleni@2 27 static void display();
eleni@2 28 static void reshape(int w, int h);
eleni@2 29 static void keyb(unsigned char key, int x, int y);
eleni@2 30 static void idle();
eleni@2 31
eleni@2 32 bool has_video;
eleni@2 33 bool has_depth;
eleni@2 34
eleni@2 35 int main(int argc, char **argv)
eleni@0 36 {
eleni@0 37 if(!init_kinect(&kin_ctx, &kin_dev, &kin_params))
eleni@0 38 return 1;
eleni@0 39
eleni@7 40 if(!init_kinect_frames(kin_ctx, kin_dev, &kin_params)) {
eleni@2 41 stop_kinect(kin_ctx, kin_dev);
eleni@2 42 return 1;
eleni@2 43 }
eleni@2 44
eleni@2 45 glutInitWindowSize(800, 600);
eleni@2 46 glutInit(&argc, argv);
eleni@2 47 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
eleni@2 48 glutCreateWindow("Test Kinect");
eleni@2 49
eleni@2 50 glewInit();
eleni@2 51
eleni@2 52 glutDisplayFunc(display);
eleni@2 53 glutReshapeFunc(reshape);
eleni@2 54 glutKeyboardFunc(keyb);
eleni@2 55 glutIdleFunc(idle);
eleni@2 56
eleni@14 57 atexit(cleanup);
eleni@17 58 init();
eleni@2 59
eleni@2 60 glutMainLoop();
eleni@2 61 }
eleni@2 62
eleni@2 63 static void display()
eleni@2 64 {
eleni@14 65 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
eleni@2 66
eleni@2 67 glMatrixMode(GL_MODELVIEW);
eleni@2 68 glLoadIdentity();
eleni@2 69
eleni@2 70 has_depth = false; has_video = false;
eleni@2 71 if(freenect_process_events(kin_ctx) != 0) {
eleni@2 72 fprintf(stderr, "Failed to process events.\n");
eleni@2 73 exit(0);
eleni@2 74 }
eleni@2 75 frame->process();
eleni@2 76
eleni@17 77 glCallList(drawing);
eleni@17 78 if(show)
eleni@17 79 glCallList(debugging);
eleni@14 80
eleni@17 81 glFlush();
eleni@12 82
eleni@2 83 glutSwapBuffers();
eleni@2 84 }
eleni@2 85
eleni@2 86 static void reshape(int w, int h)
eleni@2 87 {
eleni@2 88 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
eleni@2 89 glMatrixMode(GL_PROJECTION);
eleni@2 90 glLoadIdentity();
eleni@2 91 gluPerspective(45, (float)w / (float)h, 1, 1000);
eleni@2 92 }
eleni@2 93
eleni@2 94 static void keyb(unsigned char key, int x, int y)
eleni@2 95 {
eleni@2 96 switch(key) {
eleni@13 97 case 's':
eleni@13 98 if(show)
eleni@13 99 show = false;
eleni@13 100 else
eleni@13 101 show = true;
eleni@13 102 break;
eleni@2 103 case 27:
eleni@2 104 exit(0);
eleni@2 105 default:
eleni@2 106 break;
eleni@2 107 }
eleni@2 108 }
eleni@2 109
eleni@2 110 static void idle()
eleni@2 111 {
eleni@2 112 glutPostRedisplay();
eleni@2 113 }
eleni@2 114
eleni@2 115 static void cleanup()
eleni@2 116 {
eleni@2 117 stop_kinect_frames(kin_dev);
eleni@0 118 stop_kinect(kin_ctx, kin_dev);
eleni@0 119 }
eleni@17 120
eleni@17 121 static void init()
eleni@17 122 {
eleni@17 123 glClearColor(1, 1, 1, 1);
eleni@17 124
eleni@17 125 frame = new Frame;
eleni@17 126 if(!(tex = load_texture(filename))) {
eleni@17 127 fprintf(stderr, "Failed to load texture: %s\n", filename);
eleni@17 128 exit(1);
eleni@17 129 }
eleni@17 130
eleni@17 131 drawing = glGenLists(1);
eleni@17 132 glNewList(drawing, GL_COMPILE);
eleni@17 133
eleni@17 134 /* drawing */
eleni@17 135 glMatrixMode(GL_MODELVIEW);
eleni@17 136 glPushMatrix();
eleni@17 137 glTranslatef(0, 0, -24);
eleni@17 138 glEnable(GL_TEXTURE_2D);
eleni@17 139 glBindTexture(GL_TEXTURE_2D, tex);
eleni@17 140 draw_tess_quad(-14, -14 * 1/ASPECT, 28, 28 * 1 / ASPECT, 850, 850 * 1 / ASPECT);
eleni@17 141 glDisable(GL_TEXTURE_2D);
eleni@17 142 glPopMatrix();
eleni@17 143 glMatrixMode(GL_MODELVIEW);
eleni@17 144 glEndList();
eleni@17 145
eleni@17 146 debugging = glGenLists(1);
eleni@17 147 glNewList(debugging, GL_COMPILE);
eleni@17 148 glEnable(GL_TEXTURE_2D);
eleni@17 149 glBindTexture(GL_TEXTURE_2D, frame->video_tex);
eleni@17 150 draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
eleni@17 151 glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
eleni@17 152 draw_tess_quad(0, -1, 1, 2, 1, 1, true);
eleni@17 153 glDisable(GL_TEXTURE_2D);
eleni@17 154 glEndList();
eleni@17 155 }