在osgEarth中添加模型的简单示例

一个在osgEarth中加载模型的小示例,方便在此基础上进行其它场景编辑,资源为osgEarth自带Data。

#include "common.h"
#include <Windows.h>

namespace
{
	void adjustWindow(osgViewer::Viewer*viewer,
		const unsigned int x, const unsigned int y,
		const unsigned int w, const unsigned int h)
	{
		osg::ref_ptr<osg::GraphicsContext::Traits>trait = new osg::GraphicsContext::Traits;
		trait->x = x;
		trait->y = y;
		trait->width = w;
		trait->height = h;
		trait->doubleBuffer = true;
		trait->windowDecoration = true;
		trait->sharedContext = 0;

		osg::ref_ptr<osg::GraphicsContext>gc = osg::GraphicsContext::createGraphicsContext(trait);
		viewer->getCamera()->setGraphicsContext(gc);
		viewer->getCamera()->setViewport(new osg::Viewport(0, 0, trait->width, trait->height));
		GLenum buffer = trait->doubleBuffer ? GL_BACK : GL_FRONT;
		viewer->getCamera()->setDrawBuffer(buffer);
		viewer->getCamera()->setReadBuffer(buffer);
	}
}

int main(int argc, char* argv[])
{
	osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer;
	//操作器
	osg::ref_ptr<osgEarth::Util::EarthManipulator> earthManipulator = new osgEarth::Util::EarthManipulator;
	viewer->setCameraManipulator(earthManipulator);
 	//根节点
	osg::ref_ptr<osg::Group>root = new osg::Group;

	//加载地球节点
	osg::Node* earthNode = osgDB::readNodeFile("world.earth");
	root->addChild(earthNode);

	osgEarth::MapNode* mapNode = osgEarth::MapNode::findMapNode(earthNode);
	if (!mapNode) return;			
	const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();
	
	//添加模型
	{
		osg::Node* model = osgDB::readNodeFile("cow.osg");
		//osg中光照只会对有法线的模型起作用,而模型经过缩放后法线是不会变得,
		//所以需要手动设置属性,让法线随着模型大小变化而变化。GL_NORMALIZE 或 GL_RESCALE_NORMAL
		model->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);

		osg::Matrix Lmatrix;
		geoSRS->getEllipsoid()->computeLocalToWorldTransformFromLatLongHeight(osg::DegreesToRadians(40.0), osg::DegreesToRadians(116.0), 100000.0, Lmatrix);
		//放大一些,方便看到
		Lmatrix.preMult(osg::Matrix::scale(osg::Vec3(10000, 10000, 10000)));

		osg::MatrixTransform* mt = new osg::MatrixTransform;
		mt->setMatrix(Lmatrix);
		mt->addChild(model);
		root->addChild(mt);
	}
	viewer->setSceneData(root);

	//配置窗口
	adjustWindow(viewer, 100, 100, 720, 576);
	
	//视点定位北京地区
	earthManipulator->setViewpoint(osgEarth::ViewPoint("", 116, 40, 0.0, -2.50, -90.0, 1.5e6));
	
	return viewer->run();
}

world.earth文件内容如下:

<map>
	<image name="bluemarble" driver="gdal">  
	<url>./OsgEarth2.8_SDK/data/world.tif</url>  
	</image>  	
</map>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值