In order to improve my 中文版 writing skills,i am going to write the blogs in 中文版 form now!
-------------------------------------------------------------------------------------------Luxuriant Line-----------------------------------------------------------------------
Today,we would learn how to initialize the Kinect and get RGB data form It,then Convert the data to a Texture,which would be Drawn to the Windows.
We have both real pieces of kinect-specific code. I'll go over these on some detail, and give a fairly hight level overview of the display code
Include the header files:
#include <Windows.h> #include <Ole2.h> #include <gl/GL.h> #include <gl/GLU.h> #include <gl/ glut.h> #include <NuiApi.h> #include <NuiImageCamera.h> #include <NuiSensor.h>
Constants and global variables:
#define WIDTH 640#define height 480//OpenGL variablesgluint textureid; ID of the texture to contain Kinect RGB dataglubyte data[width*height*4]; BGRA array containing the texture data//Kinect variableshandle rgbstream; The identifier of the Kinect ' s RGB camerainuisensor* sensor; The Kinect sensor
Kinect initialization:
BOOL Initkinect () { //Get A working Kinect sensor int numsensors; if (Nuigetsensorcount (&numsensors) < 0 | | Numsensors < 1) return false; if (Nuicreatesensorbyindex (0, &sensor) < 0) return false; Initialize sensor sensor->nuiinitialize (nui_initialize_flag_uses_depth | Nui_initialize_flag_uses_color); Sensor->nuiimagestreamopen ( Nui_image_type_color, //Depth camera or RGB camera? ) nui_image_resolution_640x480, //image RESOLUTION 0, //image stream flags, e.g. near Mode 2, //number of frames to buffer NULL, //Event handle &rgbstream); return sensor;}
Get an RGB frame from the Kinect:
void Getkinectdata (glubyte* dest) { nui_image_frame imageframe; Nui_locked_rect Lockedrect; if (Sensor->nuiimagestreamgetnextframe (rgbstream, 0, &imageframe) < 0) return; inuiframetexture* texture = imageframe.pframetexture; Texture->lockrect (0, &lockedrect, NULL, 0);
if (Lockedrect.pitch! = 0) { const byte* Curr = (const byte*) lockedrect.pbits; Const byte* Dataend = Curr + (width*height); while (Curr < dataend) { *dest++ = *curr++; } }
Texture->unlockrect (0); Sensor->nuiimagestreamreleaseframe (Rgbstream, &imageframe);}
Something about the window:
void Draw () { drawkinectdata (); Glutswapbuffers ();} void execute () { glutmainloop ();} BOOL Init (int argc, char* argv[]) { glutinit (&ARGC, argv); Glutinitdisplaymode (glut_depth | glut_double | GLUT_RGBA); Glutinitwindowsize (width,height); Glutcreatewindow ("Kinect SDK Tutorial"); Glutdisplayfunc (draw); Glutidlefunc (draw); return true;}
Display via OpenGL:
Initialize Textures glgentextures (1, &textureid); Glbindtexture (gl_texture_2d, Textureid); Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_nearest); Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_nearest); Glteximage2d (gl_texture_2d, 0, gl_rgba8, width, height, 0, Gl_bgra, Gl_unsigned_byte, (glvoid*) data); Glbindtexture (gl_texture_2d, 0); OpenGL setup glclearcolor (0,0,0,0); Glcleardepth (1.0f); Glenable (gl_texture_2d); Camera Setup glviewport (0, 0, width, height); Glmatrixmode (gl_projection); Glloadidentity (); Glortho (0, width, height, 0, 1,-1); Glmatrixmode (Gl_modelview); Glloadidentity ();
int main (int argc, char* argv[]) { if (!init (argc, argv)) return 1; if (!initkinect ()) return 1; /* ... OpenGL texture and camera initialization ... *// /Main Loop execute (); return 0;}
Draw a frame to the screen:
void Drawkinectdata () { glbindtexture (gl_texture_2d, Textureid); Getkinectdata (data); Gltexsubimage2d (gl_texture_2d, 0, 0, 0, width, height, gl_bgra, Gl_unsigned_byte, (glvoid*) data); Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); Glbegin (gl_quads); GLTEXCOORD2F (0.0f, 0.0f); glvertex3f (0, 0, 0); GLTEXCOORD2F (1.0f, 0.0f); glvertex3f (width, 0, 0); GLTEXCOORD2F (1.0f, 1.0f); glvertex3f (width, height, 0.0f); GLTEXCOORD2F (0.0f, 1.0f); glvertex3f (0, height, 0.0f); Glend ();}
The end! Build and run,making sure that your Kinect are plugged in. You should see a window containing a viseo stream of what your Kinect sees.
Draw the RGB data from Kinect C + + via OpenGL