rev |
line source |
eleni@5
|
1 #include <GL/gl.h>
|
eleni@4
|
2 #include <cxcore.h>
|
eleni@4
|
3 #include <highgui.h> //TODO remove
|
eleni@4
|
4
|
eleni@2
|
5 #include <pthread.h>
|
eleni@1
|
6 #include <stdio.h>
|
eleni@2
|
7 #include <string.h>
|
eleni@1
|
8
|
eleni@1
|
9 #include "frame.h"
|
eleni@2
|
10 #include "kinect.h"
|
eleni@1
|
11
|
eleni@2
|
12 extern Frame *frame;
|
eleni@2
|
13 extern KinectParams *kin_params;
|
eleni@1
|
14
|
eleni@2
|
15 extern bool has_video;
|
eleni@2
|
16 extern bool has_depth;
|
eleni@2
|
17
|
eleni@2
|
18 Frame::Frame()
|
eleni@1
|
19 {
|
eleni@5
|
20 video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
|
eleni@5
|
21 depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC1);
|
eleni@5
|
22
|
eleni@5
|
23 tex_setup();
|
eleni@5
|
24 }
|
eleni@5
|
25
|
eleni@5
|
26 void Frame::tex_setup()
|
eleni@5
|
27 {
|
eleni@5
|
28 glGenTextures(1, &video_tex);
|
eleni@5
|
29 glGenTextures(1, &depth_tex);
|
eleni@5
|
30
|
eleni@5
|
31 glBindTexture(GL_TEXTURE_2D, video_tex);
|
eleni@5
|
32 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
eleni@5
|
33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
eleni@5
|
34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
eleni@5
|
35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
eleni@5
|
36 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, video_buf.size().width, video_buf.size().height,
|
eleni@5
|
37 0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
|
eleni@5
|
38
|
eleni@5
|
39 glBindTexture(GL_TEXTURE_2D, depth_tex);
|
eleni@5
|
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
eleni@5
|
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
eleni@5
|
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
eleni@5
|
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
eleni@5
|
44 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, depth_buf.size().width, depth_buf.size().height,
|
eleni@5
|
45 0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
|
eleni@5
|
46
|
eleni@1
|
47 }
|
eleni@1
|
48
|
eleni@2
|
49 void Frame::process()
|
eleni@1
|
50 {
|
eleni@2
|
51 if(has_video && has_depth)
|
eleni@2
|
52 printf("Depth and Rgb\n");
|
eleni@2
|
53 }
|
eleni@2
|
54
|
eleni@2
|
55 void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
|
eleni@2
|
56 {
|
eleni@4
|
57 if(!video || !frame) {
|
eleni@2
|
58 has_video = false;
|
eleni@2
|
59 return;
|
eleni@2
|
60 }
|
eleni@2
|
61
|
eleni@5
|
62 /* freenect video data to cv mat: */
|
eleni@5
|
63 unsigned char* src = (unsigned char*)video;
|
eleni@5
|
64 unsigned char* dest = frame->video_buf.data;
|
eleni@5
|
65
|
eleni@5
|
66 for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
|
eleni@5
|
67 dest[0] = src[2];
|
eleni@5
|
68 dest[1] = src[1];
|
eleni@5
|
69 dest[2] = src[0];
|
eleni@5
|
70 dest += 3;
|
eleni@5
|
71 src += 3;
|
eleni@5
|
72 }
|
eleni@2
|
73 has_video = true;
|
eleni@1
|
74 }
|
eleni@1
|
75
|
eleni@1
|
76 void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
|
eleni@1
|
77 {
|
eleni@5
|
78 if(!depth || !frame) {
|
eleni@2
|
79 has_depth = false;
|
eleni@2
|
80 return;
|
eleni@2
|
81 }
|
eleni@2
|
82
|
eleni@5
|
83 /* freenect depth data to cv mat: */
|
eleni@5
|
84 unsigned char* src = (unsigned char*)depth;
|
eleni@5
|
85 unsigned char* dest = frame->depth_buf.data;
|
eleni@5
|
86
|
eleni@5
|
87 for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
|
eleni@5
|
88 *dest = *src;
|
eleni@5
|
89 dest++;
|
eleni@5
|
90 src++;
|
eleni@5
|
91 }
|
eleni@5
|
92
|
eleni@5
|
93 save_depth_ppm(depth, 640, 480);
|
eleni@2
|
94 has_depth = true;
|
eleni@1
|
95 }
|
eleni@5
|
96
|
eleni@5
|
97 bool save_video_ppm(void *video, int w, int h)
|
eleni@5
|
98 {
|
eleni@5
|
99 FILE *fp;
|
eleni@5
|
100 if(!(fp = fopen("test_video.ppm", "wb"))) {
|
eleni@5
|
101 fprintf(stderr, "Failed to open video file for writing.\n");
|
eleni@5
|
102 return false;
|
eleni@5
|
103 }
|
eleni@5
|
104 fprintf(fp, "P6\n640 480\n255\n");
|
eleni@5
|
105 /* unsigned char *ptr = (unsigned char*)video;
|
eleni@5
|
106 for(int i=0; i<w * h * 3; i++) {
|
eleni@5
|
107 fputc(*ptr, fp);
|
eleni@5
|
108 ptr++;
|
eleni@5
|
109 }
|
eleni@5
|
110 fclose(fp);
|
eleni@5
|
111 return true;
|
eleni@5
|
112 */
|
eleni@5
|
113 return false;
|
eleni@5
|
114 }
|
eleni@5
|
115
|
eleni@5
|
116 bool save_depth_ppm(void *depth, int w, int h)
|
eleni@5
|
117 {
|
eleni@5
|
118 FILE *fp;
|
eleni@5
|
119 if(!(fp = fopen("test_depth.ppm", "wb"))) {
|
eleni@5
|
120 fprintf(stderr, "Failed to open depth file for writing.\n");
|
eleni@5
|
121 return false;
|
eleni@5
|
122 }
|
eleni@5
|
123 fprintf(fp, "P6\n640 480\n255\n");
|
eleni@5
|
124 unsigned char *ptr = (unsigned char*)depth;
|
eleni@5
|
125 unsigned char *foo = (unsigned char*)depth;
|
eleni@5
|
126 for(int i=0; i<h; i++) {
|
eleni@5
|
127 for(int j=0; j<w; j++) {
|
eleni@5
|
128 *foo = *ptr;
|
eleni@5
|
129 fputc(foo[i * w + j], fp);
|
eleni@5
|
130 ptr++;
|
eleni@5
|
131 }
|
eleni@5
|
132 }
|
eleni@5
|
133 fclose(fp);
|
eleni@5
|
134 return true;
|
eleni@5
|
135 }
|