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