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