osgEarth 局部坐标系用法

8 篇文章 0 订阅
5 篇文章 0 订阅
//节点
osg::ref_ptr<osg::Node> nd = new osg::Node;
//全局geometry
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
//矩阵相关
osg::Matrix _BasePointMat;
osg::Matrixd _BasePointInverseMat;


osg::ref_ptr<osg::MatrixTransform> matTrans1 = new osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> matTrans2 = new osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> matTrans3 = new osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> matTrans4 = new osg::MatrixTransform;


osg::ref_ptr<osg::Geode> create4Geo()
{
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	//设置顶点数组
	osg::ref_ptr<osg::Vec3Array> vex = new osg::Vec3Array;
	geometry->setVertexArray(vex);
	//设置颜色数组
	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
	colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 1.0));
	geometry->setColorArray(colors);
	geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
	//添加图元
	geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, 0, 0));
	//设置线宽
	osg::ref_ptr<osg::LineWidth> lw = new osg::LineWidth(1.0);
	geometry->getOrCreateStateSet()->setAttribute(lw, osg::StateAttribute::ON);
	//添加
	geode->addDrawable(geometry);
	//混合 深度测试
	geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
	geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
	geode->getOrCreateStateSet()->setRenderBinDetails(10, "RenderBin");
	return geode;
}


void UpdateTrackLine(double cameraHeight)
{
	//每个位置 生成四个顶点 
	matTrans1->setMatrix(Planemat->getMatrix() * osg::Matrix::translate(1, 0, 0));
	matTrans2->setMatrix(Planemat->getMatrix() * osg::Matrix::translate(1, 0, 4));
	matTrans3->setMatrix(Planemat->getMatrix() * osg::Matrix::translate(-1, 0, 4));
	matTrans4->setMatrix(Planemat->getMatrix() * osg::Matrix::translate(-1, 0, 0));
	
	osg::Vec3d pos = getModelPosition(matTrans1);
	osg::Vec3d pos2 = getModelPosition(matTrans2);
	osg::Vec3d pos3 = getModelPosition(matTrans3);
	osg::Vec3d pos4 = getModelPosition(matTrans4);


	osg::Vec3d worldpos1 = LonLatAltToWorld(pos);
	osg::Vec3d worldpos2 = LonLatAltToWorld(pos2);
	osg::Vec3d worldpos3 = LonLatAltToWorld(pos3);
	osg::Vec3d worldpos4 = LonLatAltToWorld(pos4);


	//顶点
	osg::ref_ptr<osg::Vec3Array> vertexArray;
	if (vertexArray = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()))
	{
		if (vertexArray->size() >8000)
		{
			vertexArray->erase(vertexArray->begin());
			vertexArray->erase(vertexArray->begin());
			vertexArray->erase(vertexArray->begin());
			vertexArray->erase(vertexArray->begin());
		}


		if (vertexArray->size() == 0)
  		{
            //第一次 初始化基准点矩阵。 并将此节点加入根节点
			//以第一个点为基准 建立局部坐标系
			osg::EllipsoidModel em;
			em.computeLocalToWorldTransformFromXYZ(worldpos1.x(), worldpos1.y(), worldpos1.z(), _BasePointMat);
			_BasePointInverseMat.invert(_BasePointMat);


			osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
			mt->setMatrix(_BasePointMat);
			mt->addChild(nd);
			pSkyNode->addChild(mt);
		}
		osg::Vec3d newPos  = _BasePointInverseMat.preMult(worldpos1);
		osg::Vec3d newPos2 = _BasePointInverseMat.preMult(worldpos2);
		osg::Vec3d newPos3 = _BasePointInverseMat.preMult(worldpos3);
		osg::Vec3d newPos4 = _BasePointInverseMat.preMult(worldpos4);
		vertexArray->push_back(newPos);
		vertexArray->push_back(newPos2);
		vertexArray->push_back(newPos3);
		vertexArray->push_back(newPos4);


		vertexArray->dirty();
	}
	
	//颜色
	if (osg::ref_ptr<osg::Vec4Array> colorArray = dynamic_cast<osg::Vec4Array*>(geometry->getColorArray()))
	{
		colorArray->clear();
		if (cameraHeight<pos.z())
		{
			colorArray->push_back(osg::Vec4(1, 0, 0, 0.1));
		}
		else
		{
			colorArray->push_back(osg::Vec4(0, 0, 1, 0.1));
		}
		colorArray->dirty();
	}
	
	//图元
	if (osg::ref_ptr<osg::DrawArrays> drawArrays = dynamic_cast<osg::DrawArrays*>(geometry->getPrimitiveSet(0)))
	{
		if (vertexArray->size()<1000)
		{
			return;
		}
		drawArrays->setFirst(0);
		drawArrays->setCount(vertexArray->size()-1000);
		drawArrays->dirty();
	}
	
	geometry->dirtyBound();
	geometry->dirtyDisplayList();
}



nd = create4Geo()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值