NxOgreTerrainHeightfield        

You want to build a Heightfield from a Heightmap of your Ogre Terrain using NxOgre? Then you're at the right place.
I noticed that informations related to the different Physic-Engines are very rarely so I started to provide my experiences to you.

My Setup

  • Ogre3D Version 1.6.3
  • NxOgre Version 1.0.22T5 Bleeding
  • Nvidia PhysX SDK for Windows Version 2.8.1
  • Nvidia PhysX System Software Version 8.04.25

The Terrain Loader

void 
GamePhysicManager::createTerrainPhysic( Ogre::String const& terrainCFGFile, Ogre::String const& heightMapResGroup  ) {
/************************************************************************************
 * First we will read the Configfile terrainCFGFile to get the needed informations. *
 * Weneed:                                                                          *
 *     * the heightmap Image                                *
 *     * the terrain X, Y and Z dimensions                        *
 ***********************************************************************************/
    Ogre::ConfigFile terrInfo;
    terrInfo.load( terrainCFGFile ); // place this by your terrain file
    Ogre::String terrHeightMap = terrInfo.getSetting("Heightmap.image");
    Ogre::Real terrX = Ogre::StringConverter::parseReal(terrInfo.getSetting("PageWorldX"));
    Ogre::Real terrZ = Ogre::StringConverter::parseReal(terrInfo.getSetting("PageWorldZ"));
    Ogre::Real terrY = Ogre::StringConverter::parseReal(terrInfo.getSetting("MaxHeight"));
/********************************************
 * Now we can create the NxOgre heightfield *
 *******************************************/
    NxOgre::Resources::Heightfield* hf = createHeightfield( terrHeightMap, heightMapResGroup ); 
/******************************************************************************************
 * With the heightfield and the terrain informations we can now create the terrain shape. *
 *****************************************************************************************/
    NxOgre::Terrain* terr = new NxOgre::Terrain(hf, NxOgre::float3(terrX, terrY, terrZ), NxOgre::ShapeParams(), "centering: none, hole-material: 65535");
/************************************************************************
 * The final step is to create the actor with the created terrain shape *
 ***********************************************************************/
    mNxScene_->createActor("Terrain", terr, Ogre::Vector3::ZERO, "static: yes");
}

The function createHeightfield

NxOgre::Resources::Heightfield* 
GamePhysicManager::createHeightfield(const Ogre::String& heightmap, Ogre::String const& heightMapResGroup) {
    /***********************************************
     * Load the Image from the given resourcegroup *
     **********************************************/
    Ogre::Image img;
        img.load(heightmap, heightMapResGroup); // load from resourcegroup heightMapResGroup
        int w = img.getWidth();
        int h = img.getHeight();
    NxOgre::Resources::ManualHeightfield mh;
        mh.begin(w, h);

  // Tesselation direction
  bool tessltr = false;
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; ++x) {
    /******************************************************************
     * The NxOgre maxheight is not the same as the terrain maxHeight. *
     * In NxOgre it is from -32767 to 32767.              *
     *****************************************************************/
      short height = img.getColourAt(x, y, 0).r * 32767;
      mh.sample(height, tessltr);
    }
    tessltr = !tessltr;
  }
  return mh.end(true); 
}


This creates a Terrainfield similar to the one loaded in Ogre.
If you now place some Objects with a NxOgre::Body these Objects should hit the terrain when falling down.

Hope this helps somebody handling Terrain with Physic
Devildeath