osg中实现HUDAxis功能
生活随笔
收集整理的這篇文章主要介紹了
osg中实现HUDAxis功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在場景左下角顯示一個表示當前場景坐標系,它只對相機旋轉有反應,對場景縮放和移動不起效果,采取方法:將一個坐標軸模型節點放在一個Projection下或放在Matrixtransform下設置為絕對坐標模式,然后再回調剔除移動變換。
使用這個類代碼
?
#include<osg/Geometry> #include<osg/Geode> #include<osgViewer/Viewer> #include<osgViewer/ViewerEventHandlers> #include <osg/Node> #include <osg/Geode> #include <osg/Group> #include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include<osg/DrawPixels> #include<osg/PositionAttitudeTransform> #include<osg/computeBoundsVisitor> #include<osg/MatrixTransform> #include<osg/ShapeDrawable> #include<osg/Shape> #include<osg/PolygonMode> #include<osg/NodeVisitor> class HUDAxis :public osg::Camera { public:HUDAxis();HUDAxis( const HUDAxis& copy, osg::CopyOp copyOp = osg::CopyOp::SHALLOW_COPY);META_Node(osg, HUDAxis);inline void setMainCamera(Camera* camera){ _mainCamera = camera; }virtual void traverse(osg::NodeVisitor& nv); protected:virtual ~HUDAxis();osg::observer_ptr<Camera> _mainCamera; };HUDAxis::HUDAxis() {//可以在這直接讀取axes.osgt;// this->addChild(osgDB::readNodeFile("axes.osgt")); }HUDAxis::HUDAxis( const HUDAxis& copy, osg::CopyOp copyOp /* = CopyOp::SHALLOW_COPY */) :Camera(copy, copyOp), _mainCamera(copy._mainCamera) {}// 每次回調時,該函數就會被執行 void HUDAxis::traverse(osg::NodeVisitor& nv) {double fovy, aspectRatio, vNear, vFar;_mainCamera->getProjectionMatrixAsPerspective(fovy, aspectRatio, vNear, vFar);// 改為正投影,正投影不會隨相機的拉近拉遠而放大、縮小,這樣就剔除了縮放效果this->setProjectionMatrixAsOrtho(-10.0*aspectRatio, 10.0*aspectRatio, -10.0, 10.0, 2.0, 4.0); //設置投影矩陣,使縮放不起效果// 讓坐標軸模型位于窗體右下角。osg::Vec3 trans(8.5*aspectRatio, -8.5, -8.0); if (_mainCamera.valid()){osg::Matrix matrix = _mainCamera->getViewMatrix();//改變視圖矩陣,讓移動位置固定// 讓移動固定,否則鼠標左鍵按住模型可以拖動或按空格鍵時模型會動matrix.setTrans(trans); this->setViewMatrix(matrix);}osg::Camera::traverse(nv); }HUDAxis::~HUDAxis() {}void main() {osg::ref_ptr<osgViewer::Viewer> spViewer = new osgViewer::Viewer;//使用這個類代碼 /osg::ref_ptr<HUDAxis> hudAxes = new HUDAxis;// 如果沒有坐標軸模型的osg文件,用cow.osg代替驗證也行,都一樣的,只是模型不同而已。osg::ref_ptr<osg::Node> _axes = osgDB::readNodeFile("cow.osg");hudAxes->addChild(_axes);osg::ref_ptr<osg::Camera> spCamera = spViewer->getCamera();hudAxes->setMainCamera(spCamera);hudAxes->setRenderOrder(osg::Camera::POST_RENDER);hudAxes->setClearMask(GL_DEPTH_BUFFER_BIT);hudAxes->setAllowEventFocus(false);hudAxes->setReferenceFrame(osg::Transform::ABSOLUTE_RF);spViewer->setSceneData(hudAxes);spViewer->run(); }需要此相機的視圖矩陣和主相機的一樣。
總結
以上是生活随笔為你收集整理的osg中实现HUDAxis功能的全部內容,希望文章能夠幫你解決所遇到的問題。