|
用osg绘制地形,读取tif文件形成的地形全是针状体。
using namespace osg;
ref_ptr createHeight(std::string heightFile,std::string texFile)
{
ref_ptr geode = new Geode();
ref_ptr heightMap = osgDB::readImageFile(heightFile);
ref_ptr heightFied = new HeightField();
//创建高度场景
heightFied->allocate(heightMap->s(),heightMap->t());
heightFied->setOrigin(Vec3(0,0,0));
//设置X间隔
heightFied->setXInterval(1.0f);
//设置Y间隔
heightFied->setYInterval(1.0f);
for (int r = 0; r < heightFied->getNumRows(); r++)
{
for (int c = 0; c < heightFied->getNumColumns(); c++)
{
heightFied->setHeight(c,r,((*heightMap->data(c,r))/255.0f)*60.0f);
}
}
geode->addDrawable(new ShapeDrawable(heightFied));
ref_ptr tex = new Texture2D(osgDB::readImageFile(texFile));
tex->setFilter(Texture2D::MIN_FILTER,Texture2D:INEAR_MIPMAP_LINEAR);
tex->setFilter(Texture2D::MAG_FILTER,Texture2D::LINEAR);
tex->setWrap(Texture::WRAP_S,Texture::REPEAT);
tex->setWrap(Texture::WRAP_T,Texture::REPEAT);
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,tex);
return geode;
}
int main()
{
ref_ptr group = new Group();
group->addChild(createHeight('terrain//Heightmap.jpg','terrain//fungus.dds'));
osgViewer::Viewer view;
view.setSceneData(group);
view.run();
return 0;
} |
|