|
发表于 2012-6-27 16:24:34
|
显示全部楼层
本帖最后由 eway 于 2012-6-27 16:28 编辑
实现过程大概如下-
- class testCameraUpdateCallback:public osg::NodeCallback
- {
- public:
- virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
- {
- if(nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
- {
- osg::Camera* camera = dynamic_cast<osg::Camera*>(node);
- if (camera)
- {
- osg::View* view = camera->getView();
- if (view && view->getNumSlaves() > 0)
- {
- osg::View::Slave* slave = &view->getSlave(0);
- if(slave->_camera.get() == camera)
- {
- osg::Camera* masterCam = view->getCamera();
- osg::Vec3 eye, center, up;
- masterCam->getViewMatrixAsLookAt(eye, center, up, 30);
- osg::Matrixd matrix;
- matrix.makeLookAt(eye-center, osg::Vec3(0, 0, 0), up); // always look at (0, 0, 0)
- camera->setViewMatrix(matrix);
- }
- }
- }
- }
- traverse(node,nv);
- }
- };
- osg::Camera* DrawUnit::createHUD()
- {
- osg::Camera* camera = new osg::Camera;
- //======这个是位置,设置为左下==============
- camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 18, 0, 14));
- camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- camera->setViewMatrix(osg::Matrix::identity());
- camera->setClearMask(GL_DEPTH_BUFFER_BIT);
- camera->setRenderOrder(osg::Camera::POST_RENDER);
- camera->setUpdateCallback(new testCameraUpdateCallback);
- camera->setAllowEventFocus(false);
- //============防止被近端剪裁=============
- camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
- osg::Node* node = osgDB::ReadNodeFile("cow.osg");
- osg::StateSet *state = geode->getOrCreateStateSet();
- state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
- camera->addChild(node);
- return camera;
- }
复制代码 |
|