invisible
view src/frame.cc @ 8:bd1b0385a10b
fixed depth frame rendering
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Sun, 03 Nov 2013 22:07:00 +0200 |
parents | db8f1c036d0e |
children | 6f5f53d0d166 |
line source
1 #include <GL/gl.h>
2 #include <cxcore.h>
3 #include <highgui.h> //TODO remove
4 #include <cv.h> //TODO remove
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <string.h>
10 #include "frame.h"
11 #include "kinect.h"
13 extern Frame *frame;
14 extern KinectParams *kin_params;
16 extern bool has_video;
17 extern bool has_depth;
19 Frame::Frame()
20 {
21 video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
22 depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_16UC1);
24 tex_setup();
25 }
27 void Frame::tex_setup()
28 {
29 glGenTextures(1, &video_tex);
30 glGenTextures(1, &depth_tex);
32 glBindTexture(GL_TEXTURE_2D, video_tex);
33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
37 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, video_buf.size().width, video_buf.size().height,
38 0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
40 glBindTexture(GL_TEXTURE_2D, depth_tex);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
45 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, depth_buf.size().width, depth_buf.size().height,
46 0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
48 }
50 void Frame::process()
51 {
52 if(has_video && has_depth)
53 printf("Depth and Rgb\n");
54 }
56 void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
57 {
58 if(!video || !frame) {
59 has_video = false;
60 return;
61 }
63 /* freenect video data to cv mat: */
64 unsigned char* src = (unsigned char*)video;
65 unsigned char* dest = frame->video_buf.data;
67 for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
68 dest[0] = src[2];
69 dest[1] = src[1];
70 dest[2] = src[0];
71 dest += 3;
72 src += 3;
73 }
74 has_video = true;
75 }
77 void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
78 {
79 if(!depth || !frame) {
80 has_depth = false;
81 return;
82 }
84 /* freenect depth data to cv mat: */
85 uint16_t* src = (uint16_t*)depth;
86 uint16_t* dest = (uint16_t*)frame->depth_buf.data;
88 for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
89 uint16_t val = *src;
90 if(val >= 2047) {
91 val = 0;
92 }
93 val = val << 5;
94 *dest = val;
95 src++;
96 dest++;
97 }
100 cv::imshow("foo", frame->depth_buf);
101 cv::waitKey(100);
102 has_depth = true;
103 }
105 bool save_video_ppm(void *video, int w, int h)
106 {
107 FILE *fp;
108 if(!(fp = fopen("test_video.ppm", "wb"))) {
109 fprintf(stderr, "Failed to open video file for writing.\n");
110 return false;
111 }
112 fprintf(fp, "P6\n%d %d\n255\n", w, h);
113 unsigned char *ptr = (unsigned char*)video;
114 for(int i=0; i<w * h * 3; i++) {
115 fputc(*ptr, fp);
116 ptr++;
117 }
118 fclose(fp);
119 return true;
120 }