|
楼主 |
发表于 2010-4-26 17:18:50
|
显示全部楼层
本帖最后由 lxz100 于 2010-4-26 17:19 编辑
//
//下述代码展示如何在opengl环境下(例如原有工程在MFC框架下的直接使用opengl代码的环境),使用OSG支持调入OSG支持的模型文件;
//第一种方法,使用osgUtil::SceneView,支持*.osg; *.flt;不支持*.txp
//第二种方法,使用osgViewer::Viewer ,支持*.osg; *.flt;和*.txp
//"lz.osg"在osg的数据目录下有,可从网站上获得;
//第一种方法,完全是照抄网站示例代码
osg::ref_ptr< osgUtil::SceneView > osgviewer;
osg::ref_ptr< osg::FrameStamp > frameStamp;
void UserOSGInit(void)
{
// load the scene.
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("lz.osg");
// create the view of the scene.
if(false) //如果用户发现调入的模型不可见,可能是位置不对;本段可实现将模型摆在坐标原点附近
{
const osg::BoundingSphere &bbox = loadedModel->getBound();
osg::ref_ptr<osg::MatrixTransform> mtf = new osg::MatrixTransform();
osg::Matrix mtx;
mtx.makeTranslate(-bbox.center());
mtf->setMatrix(mtx);
mtf->addChild(loadedModel.get());
root->addChild(mtf.get());
}
else
root->addChild(loadedModel.get());
//-----------------------------------------------------
frameNumber = 0;
timer;
start_tick = timer.tick();
osgviewer = new osgUtil::SceneView();
frameStamp = new osg::FrameStamp();
osgviewer->setDefaults();
osgviewer->setComputeNearFarMode( osgUtil::CullVisitor:O_NOT_COMPUTE_NEAR_FAR );
osgviewer->setSceneData(root.get());
//选择光照模式
osgviewer->setLightingMode( osgUtil::SceneView::NO_LIGHT );
//osgviewer->setLightingMode( osgUtil::SceneView::SKY_LIGHT );
//osgviewer->setLightingMode( osgUtil::SceneView::HEADLIGHT );
}
void UserOSGUpdate(void)
{
//保存opengl状态---------------------------------------------------------------
glPushAttrib( GL_ALL_ATTRIB_BITS );
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glMatrixMode( GL_TEXTURE );
glPushMatrix();
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glDisable(GL_LIGHTING);
//从opengl照搬各种矩阵-------------------------------------------------------
GLint viewParams[4];
glGetIntegerv( GL_VIEWPORT, viewParams );
osgviewer->setViewport( viewParams[0], viewParams[1], viewParams[2], viewParams[3]);
GLfloat glMat[16];
osg::Matrix osgMat;
glGetFloatv( GL_PROJECTION_MATRIX, glMat);
osgMat.set( glMat );
osgviewer->getProjectionMatrix().set( osgMat );
glGetFloatv( GL_MODELVIEW_MATRIX, glMat );
osgMat.set( glMat );
osgviewer->getViewMatrix().set( osgMat );
// update and render the scene graph
double time_since_start = timer.delta_s( start_tick, timer.tick() );
frameStamp->setReferenceTime( time_since_start);
frameStamp->setFrameNumber( frameNumber++ );
osgviewer->setFrameStamp( frameStamp.get());
//
// Lastly, you need to make the actual calls to the SceneView object:
//
osgviewer->update ();
osgviewer->cull ();
osgviewer->draw ();
//恢复opengl状态-----------------------------------------------------------------
glMatrixMode( GL_TEXTURE );
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
glPopAttrib();
}
//第二种方法,学习第一种方法,并从osgviewerGLUT例子中蒙了一个函数setUpViewerAsEmbeddedInWindow();
osg::ref_ptr< osgViewer::Viewer > osgviewer;
osg:bserver_ptr<osgViewer::GraphicsWindow> osgwindow;
void UserOSGInit(void)
{
// load the scene.
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("lz.osg");
// create the view of the scene.
if(false) //如果用户发现调入的模型不可见,可能是位置不对;本段可实现将模型摆在坐标原点附近
{
const osg::BoundingSphere &bbox = loadedModel->getBound();
osg::ref_ptr<osg::MatrixTransform> mtf = new osg::MatrixTransform();
osg::Matrix mtx;
mtx.makeTranslate(-bbox.center());
mtf->setMatrix(mtx);
mtf->addChild(loadedModel.get());
root->addChild(mtf.get());
}
else
root->addChild(loadedModel.get());
osgviewer = new osgViewer::Viewer();
//----------------------------------------------------------------
GLint viewParams[4];
glGetIntegerv( GL_VIEWPORT, viewParams );
osgwindow = osgviewer->setUpViewerAsEmbeddedInWindow(viewParams[0], viewParams[1], viewParams[2], viewParams[2]);
// Set the Scene Data
osgviewer->setSceneData(root.get());
// Realize the Viewer
osgviewer->realize();
osgviewer->setLightingMode( osgViewer::Viewer::NO_LIGHT );
//osgviewer->setLightingMode( osgViewer::Viewer::SKY_LIGHT );
//osgviewer->setLightingMode( osgViewer::Viewer::HEADLIGHT );
}
void UserOSGUpdate(void)
{
//保存opengl状态----------------------------------------------------------------
glPushAttrib( GL_ALL_ATTRIB_BITS );
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glMatrixMode( GL_TEXTURE );
glPushMatrix();
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glDisable(GL_LIGHTING);
//从opengl照搬各种矩阵------------------------------------------------------------
GLint viewParams[4];
glGetIntegerv( GL_VIEWPORT, viewParams );
osgviewer->getCamera()->setViewport( viewParams[0], viewParams[1], viewParams[2], viewParams[3]);
GLfloat glMat[16];
osg::Matrix osgMat;
glGetFloatv( GL_PROJECTION_MATRIX, glMat);
osgMat.set( glMat );
osgviewer->getCamera()->getProjectionMatrix().set( osgMat );
glGetFloatv( GL_MODELVIEW_MATRIX, glMat );
osgMat.set( glMat );
osgviewer->getCamera()->getViewMatrix().set( osgMat );
//
//调用单帧
osgviewer->frame();
//恢复opengl状态--------------------------------------------------------------
glMatrixMode( GL_TEXTURE );
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
glPopAttrib();
} |
|