osg-ex2.pro 文件:
CONFIG += c++11 console
CONFIG -= app_bundle
SOURCES += main.cpp
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# 添加库文件
LIBS += -LF:/osg3.6.0/OsgLearnExample/osg3.6.0/bin/ -lOpenThreads \
-losg \
-losgAnimation \
-losgDB \
-losgFX \
-losgGA \
-losgManipulator \
-losgParticle \
-losgPresentation \
-losgShadow \
-losgSim \
-losgTerrain \
-losgText \
-losgUI \
-losgUtil \
-losgViewer \
-losgVolume \
-losgWidget
# 引入头文件
INCLUDEPATH += F:/osg3.6.0/OsgLearnExample/osg3.6.0/include
main.c文件:
///** 移动/旋转/缩放模型
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/MatrixTransform>
int main(void)
{
osgViewer::Viewer viewer;
osg::ref_ptr < osg::Group> root = new osg::Group() ; // 申请一个跟节点
// 读取模型文件
osg::ref_ptr < osg::Node> osgcool = osgDB::readNodeFile("F:\\3D\\OSG3.6\\OpenSceneGraph-Data\\cow.osg") ;
// 申请一个矩阵使加入矩阵的东西沿Z 轴平移 2 个单位,在 OSG 中坐标轴是可以设置的
osg::ref_ptr < osg::MatrixTransform> trans = new osg::MatrixTransform ;
trans ->setMatrix(osg::Matrix::translate(0, 0, 6)) ;
trans ->addChild(osgcool.get()) ;
// 申请一个矩阵使加入该矩的模型向下移动两个单位,值得一提的是,移动和缩放以及旋转都是对矩阵进行操作
osg::ref_ptr < osg::MatrixTransform> scale = new osg::MatrixTransform ; // 申请一个矩阵
scale ->setMatrix(osg::Matrix::scale(0.5, 0.5, 0.5)*osg::Matrix::translate(0, 0, -2)); // 设置矩阵变换
scale ->addChild(osgcool.get()); // 把模型文件添加到矩阵中
// 申请一个矩阵使加入该矩阵的模型向下移动两个单位,向右移动四个单位,且绕 X 轴转 45 度
osg::ref_ptr < osg::MatrixTransform > rot = new osg::MatrixTransform ;
// 函数 DegreesToRadians 可以把角度转为弧度
rot ->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(45.0), 1, 0, 0)*osg::Matrix::scale(0.5, 0.5,0.5)*osg::Matrix::translate(4, 0, -2)) ;
rot ->addChild(osgcool.get()) ;
// 把这些矩阵都加入到 root 当中。
root ->addChild(osgcool.get()) ;
root ->addChild(trans.get()) ;
root ->addChild(scale.get()) ;
root ->addChild(rot.get()) ;
viewer.setSceneData(root.get());
viewer.realize();
// 修改窗体显示大小位置
osgViewer::GraphicsWindow *pWnd = dynamic_cast<osgViewer::GraphicsWindow*>(viewer.getCamera()->getGraphicsContext());
if (pWnd)
{
pWnd->setWindowRectangle(400, 400, 400,400);
pWnd->setWindowDecoration(true);
}
return viewer.run();
}
//*/
演示: