2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > osg实现物体绕自身轴旋转及地球自转且绕太阳公转

osg实现物体绕自身轴旋转及地球自转且绕太阳公转

时间:2023-08-28 20:31:50

相关推荐

osg实现物体绕自身轴旋转及地球自转且绕太阳公转

目录

1. 太阳、地球模型

2. 思路说明

3. 代码实现

1. 太阳、地球模型

我们知道,地球在自转的同时绕太阳公转。这就涉及到地球绕自身轴旋转及绕太阳轴公转,如何用osg实现呢?即效果如下(这里我们假定太阳是静止的):

2. 思路说明

如果一个模型不在场景的中心点,这时候使用 osg::Matrix::rotate旋转的话,这个对象会围绕场景的中心点进行旋转,会转一个大圈,那么怎么做才能让他在任何位置的时候,围绕自己的轴心进行旋转?解决思路如下:

先保存物体在世界坐标系下的坐标,即物体在世界坐标系下的中心点坐标再将物体移动到世界坐标系的原点。在世界坐标系的原点旋转好后,再移动回原来的位置,即步骤1中的提到的坐标。

const osg::BoundingSphere& loaded_bs = m_spTrans0->getBound(); osg::Vec3d center = m_spTrans0->getBound().center(); // 先保存物体中心点坐标float fX = m_spTrans0->getBound().center().x(); float fY = m_spTrans0->getBound().center().y(); float fZ = m_spTrans0->getBound().center().z(); osg::Matrix curMatrix = m_spTrans0->getMatrix();curMatrix *= osg::Matrix::translate(-center);// 再将物体移动到世界坐标系原点curMatrix *= osg::Matrix::rotate(osg::inDegrees(1.0), osg::Vec3d(0, 0, 1)); // 旋转curMatrix *= osg::Matrix::translate(center); // 再移回物体原来的位置m_spTrans0->setMatrix(curMatrix);

3. 代码实现

代码实现如下:

#include<osgViewer/Viewer>#include<osg/ArgumentParser>#include<osg/ShapeDrawable>#include<osgDB/readFile>#include<osg/Texture2D>#include<osg/matrixTransform>// 创建太阳叶节点osg::ref_ptr<osg::Geode> createSun(float fSunRadius = 1.0){osg::ref_ptr<osg::Geode>spSunGeode = new osg::Geode;osg::ref_ptr<osg::ShapeDrawable >spSun = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3d(0.0, 0.0, 0.0), 10.0f));spSunGeode->addChild(spSun);// 给太阳加上纹理/*注意:这里需要读取png,故请保证png插件存在,否则读取png会失败。关于怎么编译png插件到osg,请参见:/danshiming/article/details/115412956*/spSun->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile("sun.png")));return spSunGeode;}// 创建地球节点osg::ref_ptr<osg::ShapeDrawable> createEarth(float fEarthRadius = 1.0){osg::ref_ptr<osg::ShapeDrawable >spEarth = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3d(50.0, 0.0, 0.0), 5.0f));// 给地球加上纹理/*注意:这里需要读取jpg,故请保证jpg插件存在,否则读取jpg会失败。关于怎么编译jpg插件到osg,请参见:/danshiming/article/details/115412956*/spEarth->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile(R"(E:\osg\OpenSceneGraph-Data\Images\land_shallow_topo_2048.jpg)")));return spEarth;}class selfRotateCallback : public osg::Drawable::DrawCallback{public:selfRotateCallback(){}private:virtual void drawImplementation(osg::RenderInfo& renderInfo, const osg::Drawable* drawable) const{osg::ref_ptr<osg::ShapeDrawable>spEarth = (osg::ShapeDrawable*) (drawable);auto earthBoundCenter = spEarth->getBound().center();auto spSelfRotateEarthMt = (osg::MatrixTransform*)spEarth->getParent(0)->asTransform();if (nullptr == spSelfRotateEarthMt){OSG_WARN << "spSelfRotateEarthMt is nullptr\r\n";return;}// 自转auto curMatrix = spSelfRotateEarthMt->getMatrix();curMatrix *= osg::Matrix::translate(-earthBoundCenter);curMatrix *= osg::Matrix::rotate(osg::inDegrees(2.0f), osg::Vec3(0, 0, 1));curMatrix *= osg::Matrix::translate(earthBoundCenter);spSelfRotateEarthMt->setMatrix(curMatrix);// 公转auto spRevolRotateEarthMt = (osg::MatrixTransform*)spSelfRotateEarthMt->getParent(0)->asTransform();if (nullptr == spRevolRotateEarthMt){OSG_WARN << "spRevolRotateEarthMt is nullptr\r\n";return;}curMatrix = spRevolRotateEarthMt->getMatrix();curMatrix *= osg::Matrix::rotate(osg::inDegrees(1.0f), osg::Vec3(0, 0, 1));spRevolRotateEarthMt->setMatrix(curMatrix);spEarth->drawImplementation(renderInfo);}};int main(int argc, char *argv[]){osg::ref_ptr<osg::Group> spRoot = new osg::Group;// 创建太阳叶节点auto spSunGeode = createSun();spRoot->addChild(spSunGeode);// 自转osg::ref_ptr<osg::MatrixTransform> spSelfRotateEarthMt = new osg::MatrixTransform();// 公转osg::ref_ptr<osg::MatrixTransform>spRevolRotateEarthMt = new osg::MatrixTransform();spRevolRotateEarthMt->addChild(spSelfRotateEarthMt);// 创建地球节点auto spEarth = createEarth();spSelfRotateEarthMt->addChild(spEarth);spEarth->setUseDisplayList(false); // 这个要设置为false,否则drawImplementation函数只会调用一次spEarth->setDrawCallback(new selfRotateCallback);spRoot->addChild(spRevolRotateEarthMt);osgViewer::Viewer viewer;viewer.setSceneData(spRoot);viewer.run();}

注意:

spEarth->setUseDisplayList(false);

这个要设置为false,否则drawImplementation函数只会调用一次。上面用到了可绘制几何体的回调函数技术,相关技术的详细描述,请参见

《osg::Drawable类通过setDrawCallback函数设置回调函数的说明》博文。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。