|
- //第二种方法,学习第一种方法,并从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();
- }
复制代码
这种方式下能用设置漫游器的方式来实现场景的移动吗? |
|