
osgEarth地球表面放置模型不依赖三方,计算姿态
不依赖osgEarth的内置放置模型的接口,自己实现模型放置姿态的计算。
·
一、效果演示
二、逻辑分析
不依赖osgEarth的内置放置模型的接口,自己实现模型放置姿态的计算。
三、整体代码实现
#include <iostream>
#include <osgEarth/MapNode>
#include <osgViewer/Viewer>
#include <osgEarthUtil/EarthManipulator>
#include <osgGA/GUIEventHandler>
#include <osg/Point>
#include <osg/LineWidth>
#include <osg/MatrixTransform>
#include <osg/Switch>
using namespace std;
osgEarth::MapNode *pMapNode;
osg::Group *pLineGroup = new osg::Group;
osg::ref_ptr<osg::Node> CreateRect()
{
//初始化相关对象
osg::ref_ptr<osg::Vec3Array> pVertexArray = new osg::Vec3Array;
osg::ref_ptr<osg::Vec4Array> pColorArray = new osg::Vec4Array;
osg::ref_ptr<osg::Geometry> pGeom = new osg::Geometry;
pGeom->setVertexArray(pVertexArray.get());
pGeom->setColorArray(pColorArray.get());
pGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
pGeom->setDataVariance(osg::Object::STATIC);
// pGeom->setUseVertexBufferObjects(true);
pVertexArray->push_back(osg::Vec3(-1.0, 1.0, 0.0));
pVertexArray->push_back(osg::Vec3( 1.0, 1.0, 0.0));
pVertexArray->push_back(osg::Vec3( 1.0, -1.0, 0.0));
pVertexArray->push_back(osg::Vec3(-1.0, -1.0, 0.0));
pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
pGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
pGeode->addDrawable(pGeom);
//关闭光照,每个面看起来都一样
osg::StateSet* stateSet = pGeode->getOrCreateStateSet();
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
return pGeode.get();
}
class CCustomEvent : public osgGA::GUIEventHandler
{
public:
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override
{
if(ea.getEventType() == osgGA::GUIEventAdapter::PUSH
&& ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)
{
//根据窗口视图位置和地图地形标准获取当前鼠标与地球交点的位置的世界坐标
osg::Vec3d w;
pMapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), w);
if(w.x() == 0)
{
return false;
}
//计算当前点相对地球地面高度为h的点
// double h = 2000000;//相对高度,可以自由设置
double h = 0;//相对高度,可以自由设置
double r = sqrt(pow(w.x(), 2) + pow(w.y(), 2) + pow(w.z(), 2));
double x = (r+h)*w.x()/r;
double y = (r+h)*w.y()/r;
double z = (r+h)*w.z()/r;
osg::Vec3d calV(x, y, z);
//进行姿态位置变换
osg::Matrix mat;
mat.translate(calV);
osg::MatrixTransform* pTrans = new osg::MatrixTransform;
//沿X轴转动的夹角
double acosX = y > 0 ? (M_PI*2 - acos(z/sqrt(y*y+y*y+z*z))) : acos(z/sqrt(y*y+y*y+z*z));
/***
* 沿Y轴转动的夹角,原点O到当前点P的向量到yoz平面的投影向量长除以OP向量的长,
* 然后利用反三角函数求出夹角:acos(sqrt(y*y+z*z)/sqrt(x*x+y*y+z*z))
*/
double acosY = x > 0 ? acos(sqrt(y*y+z*z)/sqrt(x*x+y*y+z*z)) : (M_PI*2 - acos(sqrt(y*y+z*z)/sqrt(x*x+y*y+z*z)));
pTrans->setMatrix(osg::Matrix::rotate(acosY, 0, 1, 0)*
osg::Matrix::rotate(acosX, 1, 0, 0)*
osg::Matrix::scale(1000000, 1000000, 1000000)*
osg::Matrix::translate(calV));
// osg::Node* pNode1 = osgDB::readNodeFile("cessna.osg");
osg::Node* pNode1 = osgDB::readNodeFile("axes.osgt");
osg::StateSet* stateSet = pNode1->getOrCreateStateSet();
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
pTrans->addChild(pNode1);
pTrans->addChild(CreateRect());
pLineGroup->addChild(pTrans);
}
return false;
}
};
int main()
{
osg::Group* root = new osg::Group();
osg::Node* earthNode = osgDB::readNodeFile("D:\\QtCode\\GGD_PRO_20240105\\GGD_YF\\data\\globel3d.earth");
// osg::Node* earthNode = osgDB::readNodeFile("cessna.osg");
pMapNode = osgEarth::MapNode::findMapNode( earthNode );
root->addChild(earthNode);
root->addChild(pLineGroup);
osg::Node* pNode1 = osgDB::readNodeFile("axes.osgt");
osg::StateSet* stateSet = pNode1->getOrCreateStateSet();
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
osg::MatrixTransform* pTrans = new osg::MatrixTransform;
pTrans->setMatrix(osg::Matrix::scale(10000000, 10000000, 10000000));
pTrans->addChild(pNode1);
root->addChild(pTrans);
osgViewer::Viewer viewer;
viewer.setUpViewInWindow(100, 100, 800, 600);
viewer.addEventHandler(new CCustomEvent);
viewer.setSceneData( root );
viewer.run();
return 0;
}
更多推荐
所有评论(0)