#include <Windows.h>
#include <iostream>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Texture2D>
#include <osg/ShapeDrawable>
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgd.lib")
osg::Node* createHeightField(std::string height_path, std::string texture_path)
{
osg::Image* height_image = osgDB::readImageFile(height_path);
osg::ref_ptr<osg::HeightField> height_field = new osg::HeightField();
height_field->allocate(height_image->s(), height_image->t());
height_field->setOrigin(osg::Vec3());
height_field->setXInterval(30);
height_field->setYInterval(30);
height_field->setSkirtHeight(10.0f);
for (int r = 0; r < height_field->getNumRows(); r++)
{
for (int c = 0; c < height_field->getNumColumns(); c++)
{
double h = *height_image->data(c, r)*5.0;
height_field->setHeight(c, r, h);
}
}
osg::Geode* geode = new osg::Geode();
osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable(height_field.get());
geode->addDrawable(shape);
osg::ref_ptr<osg::Image> texture_image = osgDB::readImageFile(texture_path);
osg::ref_ptr<osg::Texture2D> texure = new osg::Texture2D();
texure->setImage(texture_image);
texure->setDataVariance(osg::Object::DYNAMIC);
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texure.get(), osg::StateAttribute::ON);
return geode;
}
int main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> group = new osg::Group();
std::string height_path = "C:\\Qt\\height.png";
std::string texture_path = "C:\\Qt\\height.png";
group->addChild(createHeightField(height_path, texture_path));
viewer.setSceneData(group);
viewer.setUpViewInWindow(100, 100, 800, 600);
return viewer.run();
}