cppview.cpp
Go to the documentation of this file.
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 
28 #include "libfreenect.hpp"
29 #include <pthread.h>
30 #include <stdio.h>
31 #include <iostream>
32 #include <string.h>
33 #include <cmath>
34 #include <vector>
35 
36 #if defined(__APPLE__)
37 #include <GLUT/glut.h>
38 #include <OpenGL/gl.h>
39 #include <OpenGL/glu.h>
40 #else
41 #include <GL/glut.h>
42 #include <GL/gl.h>
43 #include <GL/glu.h>
44 #endif
45 
46 
47 class Mutex {
48 public:
49  Mutex() {
50  pthread_mutex_init( &m_mutex, NULL );
51  }
52  void lock() {
53  pthread_mutex_lock( &m_mutex );
54  }
55  void unlock() {
56  pthread_mutex_unlock( &m_mutex );
57  }
58 
59  class ScopedLock
60  {
61  Mutex & _mutex;
62  public:
63  ScopedLock(Mutex & mutex)
64  : _mutex(mutex)
65  {
66  _mutex.lock();
67  }
69  {
70  _mutex.unlock();
71  }
72  };
73 private:
74  pthread_mutex_t m_mutex;
75 };
76 
77 /* thanks to Yoda---- from IRC */
79 public:
81  : Freenect::FreenectDevice(_ctx, _index), m_buffer_depth(freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes),m_buffer_video(freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes), m_gamma(2048), m_new_rgb_frame(false), m_new_depth_frame(false)
82  {
83  for( unsigned int i = 0 ; i < 2048 ; i++) {
84  float v = i/2048.0;
85  v = std::pow(v, 3)* 6;
86  m_gamma[i] = v*6*256;
87  }
88  }
89  //~MyFreenectDevice(){}
90  // Do not call directly even in child
91  void VideoCallback(void* _rgb, uint32_t timestamp) {
92  Mutex::ScopedLock lock(m_rgb_mutex);
93  uint8_t* rgb = static_cast<uint8_t*>(_rgb);
94  std::copy(rgb, rgb+getVideoBufferSize(), m_buffer_video.begin());
95  m_new_rgb_frame = true;
96  };
97  // Do not call directly even in child
98  void DepthCallback(void* _depth, uint32_t timestamp) {
99  Mutex::ScopedLock lock(m_depth_mutex);
100  uint16_t* depth = static_cast<uint16_t*>(_depth);
101  for( unsigned int i = 0 ; i < 640*480 ; i++) {
102  int pval = m_gamma[depth[i]];
103  int lb = pval & 0xff;
104  switch (pval>>8) {
105  case 0:
106  m_buffer_depth[3*i+0] = 255;
107  m_buffer_depth[3*i+1] = 255-lb;
108  m_buffer_depth[3*i+2] = 255-lb;
109  break;
110  case 1:
111  m_buffer_depth[3*i+0] = 255;
112  m_buffer_depth[3*i+1] = lb;
113  m_buffer_depth[3*i+2] = 0;
114  break;
115  case 2:
116  m_buffer_depth[3*i+0] = 255-lb;
117  m_buffer_depth[3*i+1] = 255;
118  m_buffer_depth[3*i+2] = 0;
119  break;
120  case 3:
121  m_buffer_depth[3*i+0] = 0;
122  m_buffer_depth[3*i+1] = 255;
123  m_buffer_depth[3*i+2] = lb;
124  break;
125  case 4:
126  m_buffer_depth[3*i+0] = 0;
127  m_buffer_depth[3*i+1] = 255-lb;
128  m_buffer_depth[3*i+2] = 255;
129  break;
130  case 5:
131  m_buffer_depth[3*i+0] = 0;
132  m_buffer_depth[3*i+1] = 0;
133  m_buffer_depth[3*i+2] = 255-lb;
134  break;
135  default:
136  m_buffer_depth[3*i+0] = 0;
137  m_buffer_depth[3*i+1] = 0;
138  m_buffer_depth[3*i+2] = 0;
139  break;
140  }
141  }
142  m_new_depth_frame = true;
143  }
144  bool getRGB(std::vector<uint8_t> &buffer) {
145  Mutex::ScopedLock lock(m_rgb_mutex);
146  if (!m_new_rgb_frame)
147  return false;
148  buffer.swap(m_buffer_video);
149  m_new_rgb_frame = false;
150  return true;
151  }
152 
153  bool getDepth(std::vector<uint8_t> &buffer) {
154  Mutex::ScopedLock lock(m_depth_mutex);
155  if (!m_new_depth_frame)
156  return false;
157  buffer.swap(m_buffer_depth);
158  m_new_depth_frame = false;
159  return true;
160  }
161 
162 private:
163  std::vector<uint8_t> m_buffer_depth;
164  std::vector<uint8_t> m_buffer_video;
165  std::vector<uint16_t> m_gamma;
166  Mutex m_rgb_mutex;
167  Mutex m_depth_mutex;
168  bool m_new_rgb_frame;
169  bool m_new_depth_frame;
170 };
171 
175 
177 GLuint gl_rgb_tex;
178 
179 
180 double freenect_angle(0);
181 int got_frames(0),window(0);
182 int g_argc;
183 char **g_argv;
184 
186 {
187  static std::vector<uint8_t> depth(640*480*4);
188  static std::vector<uint8_t> rgb(640*480*4);
189 
190  // using getTiltDegs() in a closed loop is unstable
191  /*if(device->getState().m_code == TILT_STATUS_STOPPED){
192  freenect_angle = device->getState().getTiltDegs();
193  }*/
194  device->updateState();
195  printf("\r demanded tilt angle: %+4.2f device tilt angle: %+4.2f", freenect_angle, device->getState().getTiltDegs());
196  fflush(stdout);
197 
198  device->getDepth(depth);
199  device->getRGB(rgb);
200 
201  got_frames = 0;
202 
203  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
204  glLoadIdentity();
205 
206  glEnable(GL_TEXTURE_2D);
207 
208  glBindTexture(GL_TEXTURE_2D, gl_depth_tex);
209  glTexImage2D(GL_TEXTURE_2D, 0, 3, 640, 480, 0, GL_RGB, GL_UNSIGNED_BYTE, &depth[0]);
210 
211  glBegin(GL_TRIANGLE_FAN);
212  glColor4f(255.0f, 255.0f, 255.0f, 255.0f);
213  glTexCoord2f(0, 0); glVertex3f(0,0,0);
214  glTexCoord2f(1, 0); glVertex3f(640,0,0);
215  glTexCoord2f(1, 1); glVertex3f(640,480,0);
216  glTexCoord2f(0, 1); glVertex3f(0,480,0);
217  glEnd();
218 
219  glBindTexture(GL_TEXTURE_2D, gl_rgb_tex);
221  glTexImage2D(GL_TEXTURE_2D, 0, 3, 640, 480, 0, GL_RGB, GL_UNSIGNED_BYTE, &rgb[0]);
222  else
223  glTexImage2D(GL_TEXTURE_2D, 0, 1, 640, 480, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &rgb[0]);
224 
225  glBegin(GL_TRIANGLE_FAN);
226  glColor4f(255.0f, 255.0f, 255.0f, 255.0f);
227  glTexCoord2f(0, 0); glVertex3f(640,0,0);
228  glTexCoord2f(1, 0); glVertex3f(1280,0,0);
229  glTexCoord2f(1, 1); glVertex3f(1280,480,0);
230  glTexCoord2f(0, 1); glVertex3f(640,480,0);
231  glEnd();
232 
233  glutSwapBuffers();
234 }
235 
236 void keyPressed(unsigned char key, int x, int y)
237 {
238  if (key == 27) {
239  glutDestroyWindow(window);
240  }
241  if (key == '1') {
242  device->setLed(LED_GREEN);
243  }
244  if (key == '2') {
245  device->setLed(LED_RED);
246  }
247  if (key == '3') {
248  device->setLed(LED_YELLOW);
249  }
250  if (key == '4') {
251  device->setLed(LED_BLINK_GREEN);
252  }
253  if (key == '5') {
254  // 5 is the same as 4
255  device->setLed(LED_BLINK_GREEN);
256  }
257  if (key == '6') {
258  device->setLed(LED_BLINK_RED_YELLOW);
259  }
260  if (key == '0') {
261  device->setLed(LED_OFF);
262  }
263  if (key == 'f') {
268  else
271  }
272 
273  if (key == 'w') {
274  freenect_angle++;
275  if (freenect_angle > 30) {
276  freenect_angle = 30;
277  }
278  }
279  if (key == 's' || key == 'd') {
280  freenect_angle = 0;
281  }
282  if (key == 'x') {
283  freenect_angle--;
284  if (freenect_angle < -30) {
285  freenect_angle = -30;
286  }
287  }
288  if (key == 'e') {
289  freenect_angle = 10;
290  }
291  if (key == 'c') {
292  freenect_angle = -10;
293  }
295 }
296 
297 void ReSizeGLScene(int Width, int Height)
298 {
299  glViewport(0,0,Width,Height);
300  glMatrixMode(GL_PROJECTION);
301  glLoadIdentity();
302  glOrtho (0, 1280, 480, 0, -1.0f, 1.0f);
303  glMatrixMode(GL_MODELVIEW);
304 }
305 
306 void InitGL(int Width, int Height)
307 {
308  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
309  glClearDepth(1.0);
310  glDepthFunc(GL_LESS);
311  glDisable(GL_DEPTH_TEST);
312  glEnable(GL_BLEND);
313  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
314  glShadeModel(GL_SMOOTH);
315  glGenTextures(1, &gl_depth_tex);
316  glBindTexture(GL_TEXTURE_2D, gl_depth_tex);
317  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
318  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
319  glGenTextures(1, &gl_rgb_tex);
320  glBindTexture(GL_TEXTURE_2D, gl_rgb_tex);
321  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
322  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
323  ReSizeGLScene(Width, Height);
324 }
325 
326 void *gl_threadfunc(void *arg)
327 {
328  printf("GL thread\n");
329 
330  glutInit(&g_argc, g_argv);
331 
332  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
333  glutInitWindowSize(1280, 480);
334  glutInitWindowPosition(0, 0);
335 
336  window = glutCreateWindow("LibFreenect");
337 
338  glutDisplayFunc(&DrawGLScene);
339  glutIdleFunc(&DrawGLScene);
340  glutReshapeFunc(&ReSizeGLScene);
341  glutKeyboardFunc(&keyPressed);
342 
343  InitGL(1280, 480);
344 
345  glutMainLoop();
346 
347  return NULL;
348 }
349 
350 int main(int argc, char **argv) {
351  device = &freenect.createDevice<MyFreenectDevice>(0);
352  device->startVideo();
353  device->startDepth();
354  gl_threadfunc(NULL);
355  device->stopVideo();
356  device->stopDepth();
357  return 0;
358 }
pthread_mutex_t m_mutex
Definition: cpp_pc_view.cpp:75
double freenect_angle(0)
Mutex()
Definition: cppview.cpp:49
void DepthCallback(void *_depth, uint32_t timestamp)
Definition: cppview.cpp:98
Freenect::Freenect freenect
Definition: cppview.cpp:172
void setTiltDegrees(double _angle)
Definition: libfreenect.hpp:90
void keyPressed(unsigned char key, int x, int y)
Definition: cppview.cpp:236
freenect_video_format
Definition: libfreenect.h:86
ScopedLock(Mutex &mutex)
Definition: cppview.cpp:63
unsigned short uint16_t
ConcreteDevice & createDevice(int _index)
void setLed(freenect_led_options _option)
Definition: libfreenect.hpp:93
bool getRGB(std::vector< uint8_t > &buffer)
Definition: cppview.cpp:144
void ReSizeGLScene(int Width, int Height)
Definition: cppview.cpp:297
std::vector< uint8_t > m_buffer_depth
Definition: cppview.cpp:163
unsigned char uint8_t
MyFreenectDevice * device
Definition: cppview.cpp:173
void DrawGLScene()
Definition: cppview.cpp:185
int window(0)
void * gl_threadfunc(void *arg)
Definition: cppview.cpp:326
int g_argc
Definition: cppview.cpp:182
MyFreenectDevice(freenect_context *_ctx, int _index)
Definition: cppview.cpp:80
bool getDepth(std::vector< uint16_t > &buffer)
freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt)
Definition: fakenect.c:260
void setVideoFormat(freenect_video_format requested_format, freenect_resolution requested_resolution=FREENECT_RESOLUTION_MEDIUM)
void VideoCallback(void *_rgb, uint32_t timestamp)
Definition: cppview.cpp:91
int got_frames(0)
freenect_video_format requested_format(FREENECT_VIDEO_RGB)
GLuint gl_rgb_tex
Definition: cppview.cpp:177
GLuint gl_depth_tex
Definition: cppview.cpp:176
FreenectTiltState getState() const
Definition: libfreenect.hpp:99
std::vector< uint16_t > m_gamma
Definition: cppview.cpp:165
unsigned int uint32_t
void unlock()
Definition: cppview.cpp:55
char ** g_argv
Definition: cppview.cpp:183
bool getDepth(std::vector< uint8_t > &buffer)
Definition: cppview.cpp:153
freenect_video_format getVideoFormat()
void InitGL(int Width, int Height)
Definition: cppview.cpp:306
int main(int argc, char **argv)
Definition: cppview.cpp:350
void lock()
Definition: cppview.cpp:52


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Thu Jun 6 2019 19:25:38