天天看点

OSG读取Tif格式的高程数据

读取

使用OSG读取Tif格式的高程数据其实非常简单,使用osg::HeightField就行。

osg::ref_ptr<osg::HeightField> heightMap =
		osgDB::readHeightFieldFile("underWater.tif.gdal");//以.gdal结尾,调用gdal读取
           

着色

1)可以将高程数据生成图片(可以用QImage实现),直接然后对数据进行贴图显示,

2)也可以对顶点数据进行着色实现。

OSG读取Tif格式的高程数据
OSG读取Tif格式的高程数据

全部代码

//*********************
//测试OSG读取Tif高程数据进行着色
//BOO
//2021年8月28日
//*******************

#include <iostream>
#include<osgViewer/Viewer>
#include<osg/Node>
#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Texture2D>

int main() 
{
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	viewer->setUpViewInWindow(50, 50, 800, 600);

	osg::ref_ptr<osg::HeightField> heightMap =
		osgDB::readHeightFieldFile("underWater.tif.gdal");//以.gdal结尾,调用gdal读取
	std::cout << heightMap->getNumRows() << " " << heightMap->getNumColumns() << std::endl;
	osg::ref_
	ptr<osg::Group> root = new osg::Group;

	if (heightMap != nullptr)
	{
		osg::ref_ptr<osg::Geode> geode = new osg::Geode;
		//添加到叶子节点中
		geode->addDrawable(new osg::ShapeDrawable(heightMap));
		
		osg::ref_ptr<osg::Image> img = osgDB::readImageFile("test2.png");

		osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
		texture->setImage(img.get());
		texture->setDataVariance(osg::Object::DYNAMIC);

		osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
		stateset->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON);
		geode->setStateSet(stateset.get());

		//必须要,不知道为什么?
		osg::Material* material = new osg::Material;
		material->setAmbient(osg::Material::FRONT, osg::Vec4(0xCC / 255.0, 0x99 / 255.0, 0x33 / 255.0, 0.8f));
		geode->getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON);
		
		root->addChild(geode);
	}
	viewer->setSceneData(root);
	return viewer->run();
}