invisible

view src/main.cc @ 26:61d593076c56

foo
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 17 Nov 2013 23:02:40 +0200
parents 96b022f1210e
children
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/wp2.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;
33 static bool fullscreen;
35 static float phi = 0;
36 static float theta = 0;
37 static float distance = 10;
38 static int prev_x = -1, prev_y = -1;
39 static int bn;
41 static void cleanup();
42 static bool init();
43 static void init_tessquad();
45 static void display();
46 static void reshape(int w, int h);
47 static void keyb(unsigned char key, int x, int y);
48 static void mouse(int button, int status, int x, int y);
49 static void motion(int x, int y);
50 static void idle();
52 bool has_video;
53 bool has_depth;
55 int main(int argc, char **argv)
56 {
57 if(!init_kinect(&kin_ctx, &kin_dev, &kin_params))
58 return 1;
60 if(!init_kinect_frames(kin_ctx, kin_dev, &kin_params)) {
61 stop_kinect(kin_ctx, kin_dev);
62 return 1;
63 }
65 glutInitWindowSize(800, 600);
66 glutInit(&argc, argv);
67 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
68 glutCreateWindow("Test Kinect");
70 glewInit();
72 glutDisplayFunc(display);
73 glutReshapeFunc(reshape);
74 glutKeyboardFunc(keyb);
75 glutMouseFunc(mouse);
76 glutMotionFunc(motion);
77 glutIdleFunc(idle);
79 atexit(cleanup);
80 if(!init()) {
81 return 1;
82 }
84 glutMainLoop();
85 }
87 static bool init()
88 {
89 glClearColor(1, 0, 0, 1);
91 frame = new Frame;
92 if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
93 fprintf(stderr, "Failed to load texture: %s\n", filename);
94 return false;
95 }
97 /* shaders setup */
98 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
99 fprintf(stderr, "Failed to create shader program!\n");
100 return false;
101 }
103 glUseProgram(sprog);
104 int dloc = glGetUniformLocation(sprog, "depth_tex");
105 if(dloc != -1)
106 glUniform1i(dloc, 1);
107 int tloc = glGetUniformLocation(sprog, "tex");
108 if(tloc != -1)
109 glUniform1i(tloc, 0);
110 glUseProgram(0);
112 /* debugging */
113 debugging = glGenLists(1);
114 glNewList(debugging, GL_COMPILE);
115 glEnable(GL_TEXTURE_2D);
116 glBindTexture(GL_TEXTURE_2D, frame->video_tex);
117 draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
118 glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
119 draw_tess_quad(0, -1, 1, 2, 1, 1, true);
120 glDisable(GL_TEXTURE_2D);
121 glEndList();
123 return true;
124 }
126 static void init_tessquad()
127 {
128 if(drawing)
129 glDeleteLists(drawing, 1);
131 drawing = glGenLists(1);
132 glNewList(drawing, GL_COMPILE);
133 float fov_rads = FOV / 180.0 * M_PI;
134 float ysz = QUAD_DIST * tan(fov_rads / 2.0);
135 float xsz = ysz * aspect;
136 draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
137 glEndList();
138 }
140 static void display()
141 {
142 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144 has_depth = false; has_video = false;
145 if(freenect_process_events(kin_ctx) != 0) {
146 fprintf(stderr, "Failed to process events.\n");
147 exit(0);
148 }
149 frame->process();
151 glMatrixMode(GL_MODELVIEW);
152 glLoadIdentity();
154 // camera
155 glRotatef(phi, 1, 0, 0);
156 glRotatef(theta, 0, 1, 0);
157 glTranslatef(0, 0, -distance);
159 glUseProgram(sprog);
161 // draw video frame
163 if(wireframe)
164 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
166 glMatrixMode(GL_MODELVIEW);
167 glPushMatrix();
168 glTranslatef(0, 0, -QUAD_DIST);
170 glActiveTexture(GL_TEXTURE0);
171 glEnable(GL_TEXTURE_2D);
172 glBindTexture(GL_TEXTURE_2D, tex);
173 glActiveTexture(GL_TEXTURE1);
174 glEnable(GL_TEXTURE_2D);
175 glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
177 glCallList(drawing);
179 glActiveTexture(GL_TEXTURE1);
180 glDisable(GL_TEXTURE_2D);
181 glActiveTexture(GL_TEXTURE0);
182 glDisable(GL_TEXTURE_2D);
184 glMatrixMode(GL_MODELVIEW);
185 glPopMatrix();
187 glUseProgram(0);
189 if(wireframe)
190 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
192 if(show)
193 glCallList(debugging);
194 glFlush();
196 glutSwapBuffers();
197 }
199 static void reshape(int w, int h)
200 {
201 aspect = (float)w / (float)h;
202 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
203 glMatrixMode(GL_PROJECTION);
204 glLoadIdentity();
205 gluPerspective(FOV, aspect, 1, 1000);
206 init_tessquad();
207 }
209 static void keyb(unsigned char key, int x, int y)
210 {
211 switch(key) {
212 case 's':
213 show = show ? false : true;
214 break;
215 case 'w':
216 wireframe = wireframe ? false : true;
217 break;
218 case 'r':
219 theta = 0;
220 phi = 0;
221 distance = 0;
222 break;
223 case 'f':
224 glutFullScreen();
225 break;
226 case 27:
227 exit(0);
228 default:
229 break;
230 }
231 }
233 static void mouse(int button, int state, int x, int y)
234 {
235 bn = button;
236 prev_x = x;
237 prev_y = y;
238 }
240 static void motion(int x, int y)
241 {
242 switch(bn) {
243 case GLUT_LEFT_BUTTON:
244 theta += x - prev_x;
245 phi += y - prev_y;
247 theta = theta < 0 ? 360 + theta : theta;
248 theta = theta > 360 ? theta - 360 : theta;
250 phi = phi < -90 ? 90 + phi : phi;
251 phi = phi > 90 ? phi - 90 : phi;
252 break;
254 case GLUT_RIGHT_BUTTON:
255 distance *= (y - prev_y) * 0.01 + 1;
256 if(distance < 0.0) {
257 distance = 0.0;
258 }
259 break;
261 default:
262 break;
263 }
265 prev_x = x;
266 prev_y = y;
267 }
269 static void idle()
270 {
271 glutPostRedisplay();
272 }
274 static void cleanup()
275 {
276 glDeleteLists(drawing, 1);
277 glDeleteLists(debugging, 1);
279 stop_kinect_frames(kin_dev);
280 stop_kinect(kin_ctx, kin_dev);
281 }