|
From: Yoda-JM <yo...@us...> - 2010-12-13 12:25:59
|
Module: performous
Branch: kinect
Commit: 99a9631e72302f9370601f6c13f73a013f6d78ed
Author: Vincent Le Ligeour <yo...@us...>
Date: Mon Dec 13 13:25:38 2010 +0100
Added some more Kinect logic
---
game/kinect.cc | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 87 insertions(+), 5 deletions(-)
diff --git a/game/kinect.cc b/game/kinect.cc
index 15d5511..cbc03ec 100644
--- a/game/kinect.cc
+++ b/game/kinect.cc
@@ -2,33 +2,111 @@
#ifdef USE_KINECT
#include <libfreenect.hpp>
+#include <boost/thread.hpp>
+#include "xtime.hh"
+#include "surface.hh"
class MyFreenectDevice : public Freenect::FreenectDevice {
public:
- MyFreenectDevice(freenect_context *_ctx, int _index) : Freenect::FreenectDevice(_ctx, _index) {
+ MyFreenectDevice(freenect_context *_ctx, int _index) : Freenect::FreenectDevice(_ctx, _index), m_video_has_changed(false), m_depth_has_changed(false), m_buffer_depth(FREENECT_DEPTH_11BIT_SIZE), m_buffer_video(FREENECT_VIDEO_RGB_SIZE) {
}
void VideoCallback(void *, uint32_t) {
- //std::cout << "RGB callback" << std::endl;
+ //std::cout << "Video callback" << std::endl;
+ boost::mutex::scoped_lock l(m_mutex_video);
+ m_video_has_changed = true;
};
void DepthCallback(void *, uint32_t) {
//std::cout << "Depth callback" << std::endl;
+ boost::mutex::scoped_lock l(m_mutex_depth);
+ m_depth_has_changed = true;
+ }
+ bool getDepth(std::vector<boost::uint8_t> &_buffer) {
+ if(m_depth_has_changed) {
+ // update buffer
+ boost::mutex::scoped_lock l(m_mutex_depth);
+ _buffer = m_buffer_depth;
+ m_depth_has_changed = false;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ bool getVideo(std::vector<boost::uint8_t> &_buffer) {
+ if(m_video_has_changed) {
+ // update buffer
+ boost::mutex::scoped_lock l(m_mutex_video);
+ _buffer = m_buffer_video;
+ m_video_has_changed = false;
+ return true;
+ } else {
+ return false;
+ }
}
-
private:
+ boost::mutex m_mutex_depth;
+ boost::mutex m_mutex_video;
+ std::vector<boost::uint16_t> m_buffer_depth;
+ std::vector<boost::uint8_t> m_buffer_video;
+ volatile bool m_video_has_changed;
+ volatile bool m_depth_has_changed;
};
+// Screen positions:
+//
+// +-----+---------+---------+----+
+// | | | | |
+// | | YELLOW | ORANGE | |
+// | | | | |
+// |-----+---+-----+----+----+----+
+// | | | |
+// | RED | BLUE | GREEN |
+// | | | |
+// |---------+----------+---------+
+// | |
+// | KICK |
+// | |
+// +------------------------------+
+
class Kinect::Impl {
public:
- Impl() : m_freenect(), m_device(m_freenect.createDevice(0)) {};
+ Impl() : m_freenect(), m_device(m_freenect.createDevice(0)), m_quit(false), m_running(false) {
+ // Start thread
+ m_thread.reset(new boost::thread(boost::ref(*this)));
+ };
~Impl() {
stop();
+ m_quit = true;
+ m_thread->join();
+ }
+ void render() {
+ static std::vector<boost::uint8_t> m_buffer_rgb;
+ if(m_device.getVideo(m_buffer_rgb)) {
+ // update texture
+ m_surface.load(FREENECT_FRAME_W, FREENECT_FRAME_H, pix::RGB, &m_buffer_rgb[0]);
+ }
+ // render texture
+ m_surface.draw();
+ };
+ /// Thread runs here, don't call directly
+ void operator()() {
+ while (!m_quit) {
+ if (m_running) {
+ static std::vector<boost::uint16_t> m_buffer_depth;
+ if(m_device.getVideo(m_buffer_depth)) {
+ // compute stuff here
+ }
+ }
+ // Sleep a little, much if the cam isn't active
+ boost::thread::sleep(now() + (m_running ? 0.015 : 0.5));
+ }
}
- void render() {};
void start() {
m_device.startDepth();
m_device.startVideo();
+ m_running = true;
}
void stop() {
+ m_running = false;
m_device.stopDepth();
m_device.stopVideo();
}
@@ -36,6 +114,10 @@ class Kinect::Impl {
private:
Freenect::Freenect<MyFreenectDevice> m_freenect;
MyFreenectDevice& m_device;
+ boost::scoped_ptr<boost::thread> m_thread;
+ volatile bool m_quit;
+ volatile bool m_running;
+ Surface m_surface;
};
#else // USE_KINECT
|