invisible
view src/main.cc @ 23:b50ad2711f5f
fixed fov aspect projection et c, added shaders
todo: segmentation
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Sat, 16 Nov 2013 23:29:15 +0200 |
parents | 531a814d4d6b |
children | 96b022f1210e |
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 int sprog;
25 static unsigned int drawing;
26 static unsigned int debugging;
27 static unsigned char tex;
28 static int tex_height;
29 static int tex_width;
30 static float aspect;
31 static bool show;
33 static void cleanup();
34 static bool init();
35 static void init_tessquad();
37 static void display();
38 static void reshape(int w, int h);
39 static void keyb(unsigned char key, int x, int y);
40 static void idle();
42 bool has_video;
43 bool has_depth;
45 int main(int argc, char **argv)
46 {
47 if(!init_kinect(&kin_ctx, &kin_dev, &kin_params))
48 return 1;
50 if(!init_kinect_frames(kin_ctx, kin_dev, &kin_params)) {
51 stop_kinect(kin_ctx, kin_dev);
52 return 1;
53 }
55 glutInitWindowSize(800, 600);
56 glutInit(&argc, argv);
57 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
58 glutCreateWindow("Test Kinect");
60 glewInit();
62 glutDisplayFunc(display);
63 glutReshapeFunc(reshape);
64 glutKeyboardFunc(keyb);
65 glutIdleFunc(idle);
67 atexit(cleanup);
68 if(!init()) {
69 return 1;
70 }
72 glutMainLoop();
73 }
75 static void display()
76 {
77 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
79 glMatrixMode(GL_MODELVIEW);
80 glLoadIdentity();
82 has_depth = false; has_video = false;
83 if(freenect_process_events(kin_ctx) != 0) {
84 fprintf(stderr, "Failed to process events.\n");
85 exit(0);
86 }
87 frame->process();
89 glUseProgram(sprog);
91 // draw video frame
92 glMatrixMode(GL_MODELVIEW);
93 glPushMatrix();
94 glTranslatef(0, 0, -QUAD_DIST);
96 glActiveTexture(GL_TEXTURE0);
97 glEnable(GL_TEXTURE_2D);
98 glBindTexture(GL_TEXTURE_2D, tex);
99 glActiveTexture(GL_TEXTURE1);
100 glEnable(GL_TEXTURE_2D);
101 glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
103 glCallList(drawing);
105 glActiveTexture(GL_TEXTURE1);
106 glDisable(GL_TEXTURE_2D);
107 glActiveTexture(GL_TEXTURE0);
108 glDisable(GL_TEXTURE_2D);
110 glMatrixMode(GL_MODELVIEW);
111 glPopMatrix();
113 glUseProgram(0);
115 if(show)
116 glCallList(debugging);
117 glFlush();
119 glutSwapBuffers();
120 }
122 static void reshape(int w, int h)
123 {
124 aspect = (float)w / (float)h;
125 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
126 glMatrixMode(GL_PROJECTION);
127 glLoadIdentity();
128 gluPerspective(FOV, aspect, 1, 1000);
129 init_tessquad();
130 }
132 static void keyb(unsigned char key, int x, int y)
133 {
134 switch(key) {
135 case 's':
136 if(show)
137 show = false;
138 else
139 show = true;
140 break;
141 case 27:
142 exit(0);
143 default:
144 break;
145 }
146 }
148 static void idle()
149 {
150 glutPostRedisplay();
151 }
153 static void cleanup()
154 {
155 glDeleteLists(drawing, 1);
156 glDeleteLists(debugging, 1);
158 stop_kinect_frames(kin_dev);
159 stop_kinect(kin_ctx, kin_dev);
160 }
162 static bool init()
163 {
164 glClearColor(1, 0, 0, 1);
166 frame = new Frame;
167 if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
168 fprintf(stderr, "Failed to load texture: %s\n", filename);
169 return false;
170 }
172 /* shaders setup */
173 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
174 fprintf(stderr, "Failed to create shader program!\n");
175 return false;
176 }
178 glUseProgram(sprog);
179 int dloc = glGetUniformLocation(sprog, "depth_tex");
180 if(dloc != -1)
181 glUniform1i(dloc, 1);
182 int tloc = glGetUniformLocation(sprog, "tex");
183 if(tloc != -1)
184 glUniform1i(tloc, 0);
185 glUseProgram(0);
187 /* debugging */
188 debugging = glGenLists(1);
189 glNewList(debugging, GL_COMPILE);
190 glEnable(GL_TEXTURE_2D);
191 glBindTexture(GL_TEXTURE_2D, frame->video_tex);
192 draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
193 glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
194 draw_tess_quad(0, -1, 1, 2, 1, 1, true);
195 glDisable(GL_TEXTURE_2D);
196 glEndList();
198 return true;
199 }
201 static void init_tessquad()
202 {
203 if(drawing)
204 glDeleteLists(drawing, 1);
206 drawing = glGenLists(1);
207 glNewList(drawing, GL_COMPILE);
208 float fov_rads = FOV / 180.0 * M_PI;
209 float ysz = QUAD_DIST * tan(fov_rads / 2.0);
210 float xsz = ysz * aspect;
211 draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
212 glEndList();
213 }