Posted By

vorp on 04/06/10


Tagged

open osg height field graph scene


Versions (?)

OSG height field example


 / Published in: C++
 

Texturing and generating a height field

  1. osg::Node* createHeightField(std::string heightFile, std::string texFile) {
  2.  
  3. osg::Image* heightMap = osgDB::readImageFile(heightFile);
  4.  
  5. osg::HeightField* heightField = new osg::HeightField();
  6. heightField->allocate(heightMap->s(), heightMap->t());
  7. heightField->setOrigin(osg::Vec3(-heightMap->s() / 2, -heightMap->t() / 2, 0));
  8. heightField->setXInterval(1.0f);
  9. heightField->setYInterval(1.0f);
  10. heightField->setSkirtHeight(1.0f);
  11.  
  12. for (int r = 0; r < heightField->getNumRows(); r++) {
  13. for (int c = 0; c < heightField->getNumColumns(); c++) {
  14. heightField->setHeight(c, r, ((*heightMap->data(c, r)) / 255.0f) * 80.0f);
  15. }
  16. }
  17.  
  18. osg::Geode* geode = new osg::Geode();
  19. geode->addDrawable(new osg::ShapeDrawable(heightField));
  20.  
  21. osg::Texture2D* tex = new osg::Texture2D(osgDB::readImageFile(texFile));
  22. tex->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_LINEAR);
  23. tex->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
  24. tex->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
  25. tex->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
  26. geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex);
  27.  
  28. return geode;
  29. }

Report this snippet  

You need to login to post a comment.