invisible

changeset 2:b0b90ef993a0

backup
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Wed, 09 Oct 2013 22:56:42 +0300
parents fdbd55eaa14e
children b479136a1ab3
files Makefile src/frame.cc src/frame.h src/kinect.cc src/kinect.h src/main.cc
diffstat 6 files changed, 174 insertions(+), 33 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Sat Oct 05 19:02:40 2013 +0300
     1.2 +++ b/Makefile	Wed Oct 09 22:56:42 2013 +0300
     1.3 @@ -3,7 +3,7 @@
     1.4  dep = $(obj:.o=.d)
     1.5  bin = invisible
     1.6  
     1.7 -dbg = -g -std=c++11
     1.8 +dbg = -g
     1.9  opt = -O0
    1.10  
    1.11  CXX = g++
     2.1 --- a/src/frame.cc	Sat Oct 05 19:02:40 2013 +0300
     2.2 +++ b/src/frame.cc	Wed Oct 09 22:56:42 2013 +0300
     2.3 @@ -1,27 +1,50 @@
     2.4 +#include <cv.h>
     2.5 +#include <pthread.h>
     2.6  #include <stdio.h>
     2.7 -#include <pthread.h>
     2.8 +#include <string.h>
     2.9  
    2.10  #include "frame.h"
    2.11 +#include "kinect.h"
    2.12  
    2.13 -static pthread_mutex_t video_mutex = PTHREAD_MUTEX_INITIALIZER;
    2.14 -static pthread_mutex_t depth_mutex = PTHREAD_MUTEX_INITIALIZER;
    2.15 +extern Frame *frame;
    2.16 +extern KinectParams *kin_params;
    2.17  
    2.18 -void init_frame_mx()
    2.19 +extern bool has_video;
    2.20 +extern bool has_depth;
    2.21 +
    2.22 +Frame::Frame()
    2.23  {
    2.24 -	pthread_mutex_init(&video_mutex, NULL);
    2.25 -	pthread_mutex_init(&depth_mutex, NULL);
    2.26 +	if(video_buf.empty()) {
    2.27 +		video_buf = cv::Mat(FREENECT_VIDEO_WIDTH, FREENECT_VIDEO_HEIGHT, CV_32FC3, cv::Scalar(0, 0, 0));
    2.28 +	}
    2.29 +	if(depth_buf.empty()) {
    2.30 +		depth_buf = cv::Mat(FREENECT_DEPTH_WIDTH, FREENECT_VIDEO_HEIGHT, CV_16UC1, 0);
    2.31 +	}
    2.32  }
    2.33  
    2.34 -void video_cb(freenect_device *kin_dev, void *rgb, uint32_t time)
    2.35 +void Frame::process()
    2.36  {
    2.37 -	pthread_mutex_lock(&video_mutex);
    2.38 -	printf("Started video.\n");
    2.39 -	pthread_mutex_unlock(&video_mutex);
    2.40 +	if(has_video && has_depth)
    2.41 +		printf("Depth and Rgb\n");
    2.42 +}
    2.43 +
    2.44 +void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
    2.45 +{
    2.46 +	if(!video) {
    2.47 +		has_video = false;
    2.48 +		return;
    2.49 +	}
    2.50 +
    2.51 +	//copy *video to video_buf if fail => has_video = false
    2.52 +	has_video = true;
    2.53  }
    2.54  
    2.55  void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
    2.56  {
    2.57 -	pthread_mutex_lock(&depth_mutex);
    2.58 -	printf("Started depth.\n");
    2.59 -	pthread_mutex_unlock(&depth_mutex);
    2.60 +	if(!depth) {
    2.61 +		has_depth = false;
    2.62 +		return;
    2.63 +	}
    2.64 +
    2.65 +	has_depth = true;
    2.66  }
     3.1 --- a/src/frame.h	Sat Oct 05 19:02:40 2013 +0300
     3.2 +++ b/src/frame.h	Wed Oct 09 22:56:42 2013 +0300
     3.3 @@ -2,19 +2,17 @@
     3.4  #define FRAME_H_
     3.5  
     3.6  #include <libfreenect.h>
     3.7 +#include <cv.h>
     3.8  
     3.9 -class Frame {
    3.10 -private:
    3.11 -	int width;
    3.12 -	int height;
    3.13 +struct Frame {
    3.14 +	cv::Mat depth_buf;
    3.15 +	cv::Mat video_buf;
    3.16  
    3.17 -public:
    3.18  	Frame();
    3.19 -
    3.20 +	void process();
    3.21  };
    3.22  
    3.23 -void init_frame_mx();
    3.24 -void video_cb(freenect_device *kin_dev, void *rgb, uint32_t time);
    3.25 +void video_cb(freenect_device *kin_dev, void *video, uint32_t time);
    3.26  void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time);
    3.27  
    3.28  #endif // FRAME_H_
     4.1 --- a/src/kinect.cc	Sat Oct 05 19:02:40 2013 +0300
     4.2 +++ b/src/kinect.cc	Wed Oct 09 22:56:42 2013 +0300
     4.3 @@ -33,12 +33,16 @@
     4.4  	}
     4.5  
     4.6  	if(freenect_set_led(*kin_dev, kin_params->led_color) < 0) {
     4.7 +		fprintf(stderr, "Failed to set kinect LED\n");
     4.8  		stop_kinect(*kin_ctx, *kin_dev);
     4.9 -		fprintf(stderr, "Failed to set kinect LED\n");
    4.10  		return false;
    4.11  	}
    4.12  
    4.13 -	init_frame_mx();
    4.14 +	return true;
    4.15 +}
    4.16 +
    4.17 +bool init_kinect_frames(freenect_context **kin_ctx, freenect_device **kin_dev,
    4.18 +		KinectParams *kin_params) {
    4.19  
    4.20  	freenect_set_video_callback(*kin_dev, video_cb);
    4.21  	freenect_set_depth_callback(*kin_dev, depth_cb);
    4.22 @@ -46,7 +50,6 @@
    4.23  	if(freenect_set_video_mode(*kin_dev,
    4.24  			freenect_find_video_mode(kin_params->video_res,
    4.25  				kin_params->video_format)) < 0) {
    4.26 -		stop_kinect(*kin_ctx, *kin_dev);
    4.27  		fprintf(stderr, "Failed to set kinect video mode.\n");
    4.28  		return false;
    4.29  	}
    4.30 @@ -54,7 +57,6 @@
    4.31  	if(freenect_set_depth_mode(*kin_dev,
    4.32  				freenect_find_depth_mode(kin_params->depth_res,
    4.33  					kin_params->depth_format)) < 0) {
    4.34 -		stop_kinect(*kin_ctx, *kin_dev);
    4.35  		fprintf(stderr, "Failed to set kinect depth mode.\n");
    4.36  		return false;
    4.37  	}
    4.38 @@ -72,12 +74,25 @@
    4.39  	return true;
    4.40  }
    4.41  
    4.42 +void stop_kinect_video_frames(freenect_device *kin_dev)
    4.43 +{
    4.44 +	freenect_stop_video(kin_dev);
    4.45 +}
    4.46 +
    4.47 +void stop_kinect_depth_frames(freenect_device *kin_dev)
    4.48 +{
    4.49 +	freenect_stop_depth(kin_dev);
    4.50 +}
    4.51 +
    4.52 +void stop_kinect_frames(freenect_device *kin_dev)
    4.53 +{
    4.54 +	stop_kinect_video_frames(kin_dev);
    4.55 +	stop_kinect_depth_frames(kin_dev);
    4.56 +}
    4.57 +
    4.58  void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev)
    4.59  {
    4.60  	freenect_set_led(kin_dev, LED_OFF);
    4.61 -
    4.62 -	freenect_stop_depth(kin_dev);
    4.63 -	freenect_stop_video(kin_dev);
    4.64  	freenect_close_device(kin_dev);
    4.65  	freenect_shutdown(kin_ctx);
    4.66  }
     5.1 --- a/src/kinect.h	Sat Oct 05 19:02:40 2013 +0300
     5.2 +++ b/src/kinect.h	Wed Oct 09 22:56:42 2013 +0300
     5.3 @@ -3,10 +3,20 @@
     5.4  
     5.5  #include <libfreenect.h>
     5.6  
     5.7 -#define FREENECT_ANGLE 30
     5.8 +/* from specs */
     5.9 +#define FREENECT_VIDEO_WIDTH 640
    5.10 +#define FREENECT_VIDEO_HEIGHT 480
    5.11 +#define FREENECT_DEPTH_WIDTH 640
    5.12 +#define FREENECT_DEPTH_HEIGHT 480
    5.13  
    5.14  struct KinectParams {
    5.15  	double angle;
    5.16 +
    5.17 +	int video_width;
    5.18 +	int video_height;
    5.19 +	int depth_width;
    5.20 +	int depth_height;
    5.21 +
    5.22  	freenect_led_options led_color;
    5.23  	freenect_video_format video_format;
    5.24  	freenect_depth_format depth_format;
    5.25 @@ -16,6 +26,12 @@
    5.26  	KinectParams()
    5.27  	{
    5.28  		angle = 10;
    5.29 +
    5.30 +		video_width = FREENECT_VIDEO_WIDTH;
    5.31 +		video_height = FREENECT_DEPTH_HEIGHT;
    5.32 +		depth_width = FREENECT_VIDEO_WIDTH;
    5.33 +		depth_height = FREENECT_DEPTH_HEIGHT;
    5.34 +
    5.35  		led_color = LED_RED;
    5.36  		video_format = FREENECT_VIDEO_RGB;
    5.37  		depth_format = FREENECT_DEPTH_11BIT;
    5.38 @@ -25,6 +41,12 @@
    5.39  };
    5.40  
    5.41  bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params);
    5.42 +bool init_kinect_frames(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params);
    5.43 +
    5.44 +void stop_kinect_video_frames(freenect_device *kin_dev);
    5.45 +void stop_kinect_depth_frames(freenect_device *kin_dev);
    5.46 +void stop_kinect_frames(freenect_device *kin_dev);
    5.47 +
    5.48  void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev);
    5.49  
    5.50  #endif // KINECT_H_
     6.1 --- a/src/main.cc	Sat Oct 05 19:02:40 2013 +0300
     6.2 +++ b/src/main.cc	Wed Oct 09 22:56:42 2013 +0300
     6.3 @@ -1,17 +1,100 @@
     6.4 +#include <GL/glew.h>
     6.5 +#include <GL/glut.h>
     6.6  #include <stdio.h>
     6.7  
     6.8 +#include <pthread.h>
     6.9 +
    6.10  #include "kinect.h"
    6.11 +#include "frame.h"
    6.12  
    6.13  freenect_context *kin_ctx;
    6.14  freenect_device *kin_dev;
    6.15  KinectParams kin_params;
    6.16 +Frame *frame;
    6.17  
    6.18 -int main()
    6.19 +static void cleanup();
    6.20 +
    6.21 +static void display();
    6.22 +static void reshape(int w, int h);
    6.23 +static void keyb(unsigned char key, int x, int y);
    6.24 +static void idle();
    6.25 +
    6.26 +bool has_video;
    6.27 +bool has_depth;
    6.28 +
    6.29 +int main(int argc, char **argv)
    6.30  {
    6.31 -	printf("hi\n");
    6.32 -
    6.33  	if(!init_kinect(&kin_ctx, &kin_dev, &kin_params))
    6.34  		return 1;
    6.35  
    6.36 +	if(!init_kinect_frames(&kin_ctx, &kin_dev, &kin_params)) {
    6.37 +		stop_kinect(kin_ctx, kin_dev);
    6.38 +		return 1;
    6.39 +	}
    6.40 +
    6.41 +	glutInitWindowSize(800, 600);
    6.42 +	glutInit(&argc, argv);
    6.43 +	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    6.44 +	glutCreateWindow("Test Kinect");
    6.45 +
    6.46 +	glewInit();
    6.47 +
    6.48 +	glutDisplayFunc(display);
    6.49 +	glutReshapeFunc(reshape);
    6.50 +	glutKeyboardFunc(keyb);
    6.51 +	glutIdleFunc(idle);
    6.52 +
    6.53 +	glClearColor(1, 1, 1, 1);
    6.54 +
    6.55 +	atexit(cleanup);
    6.56 +	glutMainLoop();
    6.57 +}
    6.58 +
    6.59 +static void display()
    6.60 +{
    6.61 +	glClear(GL_COLOR_BUFFER_BIT);
    6.62 +
    6.63 +	glMatrixMode(GL_MODELVIEW);
    6.64 +	glLoadIdentity();
    6.65 +
    6.66 +	glClearColor(1, 0, 0, 1);
    6.67 +
    6.68 +	has_depth = false; has_video = false;
    6.69 +
    6.70 +	if(freenect_process_events(kin_ctx) != 0) {
    6.71 +		fprintf(stderr, "Failed to process events.\n");
    6.72 +		exit(0);
    6.73 +	}
    6.74 +	frame->process();
    6.75 +
    6.76 +	glutSwapBuffers();
    6.77 +}
    6.78 +
    6.79 +static void reshape(int w, int h)
    6.80 +{
    6.81 +	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    6.82 +	glMatrixMode(GL_PROJECTION);
    6.83 +	glLoadIdentity();
    6.84 +	gluPerspective(45, (float)w / (float)h, 1, 1000);
    6.85 +}
    6.86 +
    6.87 +static void keyb(unsigned char key, int x, int y)
    6.88 +{
    6.89 +	switch(key) {
    6.90 +	case 27:
    6.91 +		exit(0);
    6.92 +	default:
    6.93 +		break;
    6.94 +	}
    6.95 +}
    6.96 +
    6.97 +static void idle()
    6.98 +{
    6.99 +	glutPostRedisplay();
   6.100 +}
   6.101 +
   6.102 +static void cleanup()
   6.103 +{
   6.104 +	stop_kinect_frames(kin_dev);
   6.105  	stop_kinect(kin_ctx, kin_dev);
   6.106  }