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

目录

1. 太阳、地球模型

2. 思路说明

3. 代码实现


1. 太阳、地球模型

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

2. 思路说明

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

  1. 先保存物体在世界坐标系下的坐标,即物体在世界坐标系下的中心点坐标
  2. 再将物体移动到世界坐标系的原点。
  3. 在世界坐标系的原点旋转好后,再移动回原来的位置,即步骤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
#include
#include
#include
#include
#include// 创建太阳叶节点
osg::ref_ptr createSun(float fSunRadius = 1.0)
{osg::ref_ptrspSunGeode = new osg::Geode;osg::ref_ptrspSun = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3d(0.0, 0.0, 0.0), 10.0f));spSunGeode->addChild(spSun);// 给太阳加上纹理/*注意:这里需要读取png,故请保证png插件存在,否则读取png会失败。关于怎么编译png插件到osg,请参见:https://blog.csdn.net/danshiming/article/details/115412956*/spSun->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile("sun.png")));return spSunGeode;
}// 创建地球节点
osg::ref_ptr createEarth(float fEarthRadius = 1.0)
{osg::ref_ptrspEarth = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3d(50.0, 0.0, 0.0), 5.0f));// 给地球加上纹理/*注意:这里需要读取jpg,故请保证jpg插件存在,否则读取jpg会失败。关于怎么编译jpg插件到osg,请参见:https://blog.csdn.net/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_ptrspEarth = (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 spRoot = new osg::Group;// 创建太阳叶节点auto spSunGeode = createSun();spRoot->addChild(spSunGeode);// 自转osg::ref_ptr spSelfRotateEarthMt = new osg::MatrixTransform();// 公转osg::ref_ptrspRevolRotateEarthMt = 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函数设置回调函数的说明》博文。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部