-
Notifications
You must be signed in to change notification settings - Fork 10
/
ch3_02.cpp
222 lines (195 loc) · 6.48 KB
/
ch3_02.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <GL/freeglut.h>
#include <MSHTML.h>
#include <NuiApi.h>
#include <iostream>
#include <sstream>
#include "common/GLUtilities.h"
INuiSensor* context = NULL;
HANDLE colorStreamHandle = NULL;
HANDLE depthStreamHandle = NULL;
TextureObject* colorTexture = NULL;
TextureObject* playerColorTexture = NULL;
bool initializeKinect()
{
int numKinects = 0;
HRESULT hr = NuiGetSensorCount( &numKinects );
if ( FAILED(hr) || numKinects<=0 )
{
std::cout << "No Kinect device found." << std::endl;
return false;
}
hr = NuiCreateSensorByIndex( 0, &context );
if ( FAILED(hr) )
{
std::cout << "Failed to connect to Kinect device." << std::endl;
return false;
}
DWORD nuiFlags = NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR |
NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX;
hr = context->NuiInitialize( nuiFlags );
if ( FAILED(hr) )
{
std::cout << "Failed to intialize Kinect: " << std::hex << (long)hr << std::dec << std::endl;
return false;
}
hr = context->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, NULL, &colorStreamHandle );
if ( FAILED(hr) )
{
std::cout << "Unable to create color stream: " << std::hex << (long)hr << std::dec << std::endl;
return false;
}
hr = context->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_640x480,
0, 2, NULL, &depthStreamHandle );
if ( FAILED(hr) )
{
std::cout << "Unable to create depth stream: " << std::hex << (long)hr << std::dec << std::endl;
return false;
}
hr = context->NuiSkeletonTrackingEnable( NULL, 0 );
if ( FAILED(hr) )
{
std::cout << "Unable to start tracking skeleton." << std::endl;
return false;
}
return true;
}
bool destroyKinect()
{
if ( context )
{
context->NuiShutdown();
return true;
}
return false;
}
bool setPlayerColorPixel( const USHORT depthValue, int x, int y, unsigned char alpha )
{
unsigned char* ptr = playerColorTexture->bits + 4 * (y * 640 + x);
if ( NuiDepthPixelToPlayerIndex(depthValue)>0 )
{
LONG colorX = 0, colorY = 0;
context->NuiImageGetColorPixelCoordinatesFromDepthPixelAtResolution(
NUI_IMAGE_RESOLUTION_640x480, NUI_IMAGE_RESOLUTION_640x480, NULL,
x, y, depthValue, &colorX, &colorY );
if ( colorX>=640 || colorY>=480 ) return false;
unsigned char* colorPtr = colorTexture->bits + 3 * (colorY * 640 + colorX);
*(ptr + 0) = *(colorPtr + 0);
*(ptr + 1) = *(colorPtr + 1);
*(ptr + 2) = *(colorPtr + 2);
*(ptr + 3) = alpha;
}
else
{
*(ptr + 0) = 0;
*(ptr + 1) = 0;
*(ptr + 2) = 0;
*(ptr + 3) = 0;
}
return true;
}
void updateImageFrame( NUI_IMAGE_FRAME& imageFrame, bool isDepthFrame )
{
INuiFrameTexture* nuiTexture = imageFrame.pFrameTexture;
NUI_LOCKED_RECT lockedRect;
nuiTexture->LockRect( 0, &lockedRect, NULL, 0 );
if ( lockedRect.Pitch!=NULL )
{
const BYTE* buffer = (const BYTE*)lockedRect.pBits;
for ( int i=0; i<480; ++i )
{
const BYTE* line = buffer + i * lockedRect.Pitch;
const USHORT* bufferWord = (const USHORT*)line;
for ( int j=0; j<640; ++j )
{
if ( !isDepthFrame )
{
unsigned char* ptr = colorTexture->bits + 3 * (i * 640 + j);
*(ptr + 0) = line[4 * j + 2];
*(ptr + 1) = line[4 * j + 1];
*(ptr + 2) = line[4 * j + 0];
}
else
setPlayerColorPixel( bufferWord[j], j, i, 255 );
}
}
TextureObject* tobj = (isDepthFrame ? playerColorTexture : colorTexture);
glBindTexture( GL_TEXTURE_2D, tobj->id );
glTexImage2D( GL_TEXTURE_2D, 0, tobj->internalFormat, tobj->width, tobj->height,
0, tobj->imageFormat, GL_UNSIGNED_BYTE, tobj->bits );
}
nuiTexture->UnlockRect( 0 );
}
void update()
{
NUI_IMAGE_FRAME colorFrame;
HRESULT hr = context->NuiImageStreamGetNextFrame( colorStreamHandle, 0, &colorFrame );
if ( SUCCEEDED(hr) )
{
updateImageFrame( colorFrame, false );
context->NuiImageStreamReleaseFrame( colorStreamHandle, &colorFrame );
}
NUI_IMAGE_FRAME depthFrame;
hr = context->NuiImageStreamGetNextFrame( depthStreamHandle, 0, &depthFrame );
if ( SUCCEEDED(hr) )
{
updateImageFrame( depthFrame, true );
context->NuiImageStreamReleaseFrame( depthStreamHandle, &depthFrame );
}
glutPostRedisplay();
}
void render()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
glEnable( GL_TEXTURE_2D );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// Draw background quad
GLfloat vertices[][3] = {
{ 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }
};
GLfloat texcoords[][2] = {
{0.0f, 1.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f}
};
VertexData meshData = { &(vertices[0][0]), NULL, NULL, &(texcoords[0][0]) };
glBindTexture( GL_TEXTURE_2D, playerColorTexture->id );
drawSimpleMesh( WITH_POSITION|WITH_TEXCOORD, 4, meshData, GL_QUADS );
glutSwapBuffers();
}
void reshape( int w, int h )
{
glViewport( 0, 0, w, h );
}
void keyEvents( unsigned char key, int x, int y )
{
switch ( key )
{
case 27: case 'Q': case 'q':
glutLeaveMainLoop();
return;
}
glutPostRedisplay();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH|GLUT_MULTISAMPLE );
glutCreateWindow( "ch3_02_Background_Subtraction" );
glutFullScreen();
glutIdleFunc( update );
glutDisplayFunc( render );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyEvents );
if ( !initializeKinect() ) return 1;
colorTexture = createTexture(640, 480, GL_RGB, 3);
playerColorTexture = createTexture(640, 480, GL_RGBA, 4);
glutMainLoop();
destroyTexture( colorTexture );
destroyTexture( playerColorTexture );
destroyKinect();
return 0;
}