invisible

view src/main.cc @ 18:ad78eb81988d

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