实现过程
- 创建节点
- 读取模型
- 为模型添加变换矩阵
- 设置模型坐标
- 加载节点
osg::ref_ptr<osg::CoordinateSystemNode> csn;
osg::ref_ptr<osg::Node> airport;
osg::ref_ptr<QNodeMatrix> mtAirportself;
osg::ref_ptr<osg::MatrixTransform> mtAirport;
csn = new osg::CoordinateSystemNode;
csn->setEllipsoidModel(new osg::EllipsoidModel());//设置地球模型
//加载机场模型
airport = osgDB::readNodeFile("../DigtalEarth/builder/data/131651.osg");
mtAirport = new osg::MatrixTransform;
mtAirportself = new QNodeMatrix;
//缩放旋转
mtAirportself->setMatrix(osg::Matrixd::scale(0.4, 0.4, 0.4) * osg::Matrixd::rotate(osg::PI * (0), osg::Vec3(0, 0, 1)));
mtAirportself->_addChild(airport);
mtAirportself->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);
mtAirportself->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON);
//添加灯光
mtAirportself->getOrCreateStateSet()->setMode(GL_LIGHT0,osg::StateAttribute::ON);
mtAirportself->getOrCreateStateSet()->setMode(GL_LIGHT1,osg::StateAttribute::ON);
mtAirport->addChild(mtAirportself);
mRoot->addChild(mtAirport);
关于转换矩阵
上述代码添加了两个变换矩阵,mtAirportself
和 mtAirport
。
根节点mRoot
添加的孩子是 mtAirport
,对应的坐标系为地心坐标系即下图中蓝色坐标系。
mtAirport
添加孩子mtAirportself
直接绑定节点模型,其坐标系为下图中绿色的站心坐标系。
关于模型变换
模型的变换都是对模型相关的变换矩阵的操作,一下几种常见操作
- 缩放
- 平移
- 旋转
旋转
构造变换矩阵
osg::Matrix::rotate(osg::inDegrees(-90.0f), 0.0f, 0.0f, 1.0f);
实参中的后三个参数(0.0f, 0.0f, 1.0f)
实际指的是z轴的正半轴。
缩放
osg::Matrix::scale(size,size,size); //size表示物体缩放比例,如果size=2,则物体尺寸将放大2倍
平移
osg::Matrix::translate(v); //v表示移动量,是一个Vec3,比如(0, 0, 1)表示向z轴移动一个单位
【注】 所有的变换叠加都是矩阵乘法操作,需要先获取当前的状态矩阵再与变换矩阵相乘