|
| 1 | +/* |
| 2 | + * This file is part of the OpenKinect Project. http://www.openkinect.org |
| 3 | + * |
| 4 | + * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file |
| 5 | + * for details. |
| 6 | + * |
| 7 | + * This code is licensed to you under the terms of the Apache License, version |
| 8 | + * 2.0, or, at your option, the terms of the GNU General Public License, |
| 9 | + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, |
| 10 | + * or the following URLs: |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * http://www.gnu.org/licenses/gpl-2.0.txt |
| 13 | + * |
| 14 | + * If you redistribute this file in source form, modified or unmodified, you |
| 15 | + * may: |
| 16 | + * 1) Leave this header intact and distribute it under the same terms, |
| 17 | + * accompanying it with the APACHE20 and GPL20 files, or |
| 18 | + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or |
| 19 | + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file |
| 20 | + * In all cases you must keep the copyright notice intact and include a copy |
| 21 | + * of the CONTRIB file. |
| 22 | + * |
| 23 | + * Binary distributions must follow the binary distribution requirements of |
| 24 | + * either License. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "libfreenect.h" |
| 28 | +#include "libfreenect_sync.h" |
| 29 | +#include <stdio.h> |
| 30 | + |
| 31 | +#if defined(__APPLE__) |
| 32 | +#include <GLUT/glut.h> |
| 33 | +#include <OpenGL/gl.h> |
| 34 | +#include <OpenGL/glu.h> |
| 35 | +#else |
| 36 | +#include <GL/glut.h> |
| 37 | +#include <GL/gl.h> |
| 38 | +#include <GL/glu.h> |
| 39 | +#endif |
| 40 | + |
| 41 | +#include <math.h> |
| 42 | + |
| 43 | +int window; |
| 44 | +GLuint gl_rgb_tex; |
| 45 | +int mx=-1,my=-1; // Prevous mouse coordinates |
| 46 | +int rotangles[2] = {0}; // Panning angles |
| 47 | +float zoom = 1; // zoom factor |
| 48 | +int color = 1; // Use the RGB texture or just draw it as color |
| 49 | + |
| 50 | +// Do the projection from u,v,depth to X,Y,Z directly in an opengl matrix |
| 51 | +void LoadVertexMatrix() |
| 52 | +{ |
| 53 | + float f = 590.0f; |
| 54 | + float a = -0.0030711f; |
| 55 | + float b = 3.3309495f; |
| 56 | + float cx = 320.0f; |
| 57 | + float cy = 240.0f; |
| 58 | + float mat[4][4] = { |
| 59 | + {1/f, 0, 0, 0}, |
| 60 | + {0, -1/f, 0, 0}, |
| 61 | + {0, 0, 0, a}, |
| 62 | + {-cx/f,cy/f, -1, b}}; |
| 63 | + glMultMatrixf(mat); |
| 64 | +} |
| 65 | + |
| 66 | +void mouseMoved(int x, int y) |
| 67 | +{ |
| 68 | + if (mx>=0 && my>=0) { |
| 69 | + rotangles[0] += y-my; |
| 70 | + rotangles[1] += x-mx; |
| 71 | + } |
| 72 | + mx = x; |
| 73 | + my = y; |
| 74 | +} |
| 75 | + |
| 76 | +void mousePress(int button, int state, int x, int y) |
| 77 | +{ |
| 78 | + if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { |
| 79 | + mx = x; |
| 80 | + my = y; |
| 81 | + } |
| 82 | + if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { |
| 83 | + mx = -1; |
| 84 | + my = -1; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void DrawGLScene() |
| 89 | +{ |
| 90 | + short *depth = 0; |
| 91 | + char *rgb = 0; |
| 92 | + int ts; |
| 93 | + freenect_sync_get_depth(&depth, &ts); |
| 94 | + freenect_sync_get_rgb(&rgb, &ts); |
| 95 | + |
| 96 | + static unsigned int indices[480][640]; |
| 97 | + static short xyz[480][640][3]; |
| 98 | + static float uv[480][640][2]; |
| 99 | + int i,j; |
| 100 | + for (i = 0; i < 480; i++) { |
| 101 | + for (j = 0; j < 640; j++) { |
| 102 | + xyz[i][j][0] = j; |
| 103 | + xyz[i][j][1] = i; |
| 104 | + xyz[i][j][2] = depth[i*640+j]; |
| 105 | + uv[i][j][0] = j/480.0f; |
| 106 | + uv[i][j][1] = i/640.0f; |
| 107 | + indices[i][j] = i*640+j; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 112 | + glLoadIdentity(); |
| 113 | + |
| 114 | + glPushMatrix(); |
| 115 | + glScalef(zoom,zoom,1); |
| 116 | + glTranslatef(0,0,-3.5); |
| 117 | + glRotatef(rotangles[0], 1,0,0); |
| 118 | + glRotatef(rotangles[1], 0,1,0); |
| 119 | + glTranslatef(0,0,1.5); |
| 120 | + |
| 121 | + LoadVertexMatrix(); |
| 122 | + glPointSize(1); |
| 123 | + |
| 124 | + glEnableClientState(GL_VERTEX_ARRAY); |
| 125 | + glVertexPointer(3, GL_SHORT, 0, xyz); |
| 126 | + glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 127 | + glTexCoordPointer(2, GL_FLOAT, 0, uv); |
| 128 | + |
| 129 | + if (color) glEnable(GL_TEXTURE_2D); |
| 130 | + glBindTexture(GL_TEXTURE_2D, gl_rgb_tex); |
| 131 | + glTexImage2D(GL_TEXTURE_2D, 0, 3, 640, 480, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb); |
| 132 | + |
| 133 | + glDrawElements(GL_POINTS, 640*480, GL_UNSIGNED_INT, indices); |
| 134 | + glPopMatrix(); |
| 135 | + glDisable(GL_TEXTURE_2D); |
| 136 | + |
| 137 | + free(rgb); |
| 138 | + free(depth); |
| 139 | + |
| 140 | + glutSwapBuffers(); |
| 141 | +} |
| 142 | + |
| 143 | +void keyPressed(unsigned char key, int x, int y) |
| 144 | +{ |
| 145 | + if (key == 27) { |
| 146 | + glutDestroyWindow(window); |
| 147 | + exit(0); |
| 148 | + } |
| 149 | + if (key == 'w') zoom *= 1.1f; |
| 150 | + if (key == 's') zoom /= 1.1f; |
| 151 | + if (key == 'c') color = !color; |
| 152 | +} |
| 153 | + |
| 154 | +void ReSizeGLScene(int Width, int Height) |
| 155 | +{ |
| 156 | + glViewport(0,0,Width,Height); |
| 157 | + glMatrixMode(GL_PROJECTION); |
| 158 | + glLoadIdentity(); |
| 159 | + gluPerspective(60, 4/3., 0.3, 200); |
| 160 | + glMatrixMode(GL_MODELVIEW); |
| 161 | +} |
| 162 | + |
| 163 | +void InitGL(int Width, int Height) |
| 164 | +{ |
| 165 | + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 166 | + glEnable(GL_DEPTH_TEST); |
| 167 | + glGenTextures(1, &gl_rgb_tex); |
| 168 | + glBindTexture(GL_TEXTURE_2D, gl_rgb_tex); |
| 169 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 170 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 171 | + ReSizeGLScene(Width, Height); |
| 172 | +} |
| 173 | + |
| 174 | +int main(int argc, char **argv) |
| 175 | +{ |
| 176 | + glutInit(&argc, argv); |
| 177 | + |
| 178 | + glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); |
| 179 | + glutInitWindowSize(640, 480); |
| 180 | + glutInitWindowPosition(0, 0); |
| 181 | + |
| 182 | + window = glutCreateWindow("LibFreenect"); |
| 183 | + |
| 184 | + glutDisplayFunc(&DrawGLScene); |
| 185 | + glutIdleFunc(&DrawGLScene); |
| 186 | + glutReshapeFunc(&ReSizeGLScene); |
| 187 | + glutKeyboardFunc(&keyPressed); |
| 188 | + glutMotionFunc(&mouseMoved); |
| 189 | + glutMouseFunc(&mousePress); |
| 190 | + |
| 191 | + InitGL(640, 480); |
| 192 | + |
| 193 | + glutMainLoop(); |
| 194 | + |
| 195 | + return NULL; |
| 196 | +} |
| 197 | + |
0 commit comments