main:
1. const char* deviceURI = openni::ANY_DEVICE;
2. rc = openni::OpenNI::initialize();3. rc = device.open(deviceURI);
4. rc = depth.create(device, openni::SENSOR_DEPTH);
5. rc = color.create(device, openni::SENSOR_COLOR);
6. rc = sampleViewer.init(argc, argv);
7. sampleViewer.run();
####################### 第 6 部分 ################################
openni::Status SampleViewer::init(int argc, char **argv)
{
openni::VideoMode depthVideoMode;
openni::VideoMode colorVideoMode;
if (m_depthStream.isValid() && m_colorStream.isValid())
{
depthVideoMode = m_depthStream.getVideoMode();
colorVideoMode = m_colorStream.getVideoMode();
int depthWidth = depthVideoMode.getResolutionX();
int depthHeight = depthVideoMode.getResolutionY();
int colorWidth = colorVideoMode.getResolutionX();
int colorHeight = colorVideoMode.getResolutionY();
if (depthWidth == colorWidth &&
depthHeight == colorHeight)
{
m_width = depthWidth;
m_height = depthHeight;
}
else
{
printf("Error - expect color and depth to be in same resolution: D: %dx%d, C: %dx%d\n",
depthWidth, depthHeight,
colorWidth, colorHeight);
return openni::STATUS_ERROR;
}
}
else if (m_depthStream.isValid())
{
depthVideoMode = m_depthStream.getVideoMode();
m_width = depthVideoMode.getResolutionX();
m_height = depthVideoMode.getResolutionY();
}
else if (m_colorStream.isValid())
{
colorVideoMode = m_colorStream.getVideoMode();
m_width = colorVideoMode.getResolutionX();
m_height = colorVideoMode.getResolutionY();
}
else
{
printf("Error - expects at least one of the streams to be valid...\n");
return openni::STATUS_ERROR;
}
m_streams = new openni::VideoStream*[2];
m_streams[0] = &m_depthStream;
m_streams[1] = &m_colorStream;
// Texture map init
m_nTexMapX = MIN_CHUNKS_SIZE(m_width, TEXTURE_SIZE);
m_nTexMapY = MIN_CHUNKS_SIZE(m_height, TEXTURE_SIZE);
m_pTexMap = new openni::RGB888Pixel[m_nTexMapX * m_nTexMapY];
return initOpenGL(argc, argv);
}
openni::Status SampleViewer::initOpenGL(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
glutCreateWindow (m_strSampleName);
// glutFullScreen();
glutSetCursor(GLUT_CURSOR_NONE);
initOpenGLHooks();
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
return openni::STATUS_OK;
}
######################### 第7部分 ##############################
/*
* Enters the freeglut processing loop.
* Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
*/
void FGAPIENTRY glutMainLoop( void )
{
int action;
FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
if (!fgStructure.Windows.First)
fgError(" ERROR: glutMainLoop called with no windows created.");
fgPlatformMainLoopPreliminaryWork ();
fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
{
SFG_Window *window;
glutMainLoopEvent( );
/*
* Step through the list of windows, seeing if there are any
* that are not menus
*/
for( window = ( SFG_Window * )fgStructure.Windows.First;
window;
window = ( SFG_Window * )window->Node.Next )
if ( ! ( window->IsMenu ) )
break;
if( ! window )
fgState.ExecState = GLUT_EXEC_STATE_STOP;
else
{
if( fgState.IdleCallback )
{
if( fgStructure.CurrentWindow &&
fgStructure.CurrentWindow->IsMenu )
/* fail safe */
fgSetWindow( window );
fgState.IdleCallback( );
}
else
fghSleepForEvents( );
}
}
/*
* When this loop terminates, destroy the display, state and structure
* of a freeglut session, so that another glutInit() call can happen
*
* Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
*/
action = fgState.ActionOnWindowClose;
fgDeinitialize( );
if( action == GLUT_ACTION_EXIT )
exit( 0 );
}