Hydrax Demo under 1.8 : solved

Chojuro

21-06-2012 18:58:28

I am trying to redo the Hydrax Demo using the new terrain system since the TerrainSceneManager has been removed in 1.8.
I now have it working pretty darn well. So this is basically just a post on adding the 1.8 terrain demo as the Island in Hydrax Demo
to get something visual in 1.8.
I have the following code placed into the Hydrax_v0.5.1 Demo1 code:


void chooseSceneManager()
{ // Create the SceneManager, in this case a generic one
// mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "ExampleSMInstance");
}

// New OGRE Terrain system
void Configure_Terrain ()
{
Ogre::Light* light = CreateDirectionalLight(); // Make a directional light for terrain

mTerrainGlobals = OGRE_NEW Ogre::TerrainGlobalOptions();
mTerrainGroup = OGRE_NEW Ogre::TerrainGroup(mSceneMgr, Ogre::Terrain::ALIGN_X_Z, 129, 3000.0f);
mTerrainGroup->setFilenameConvention(Ogre::String("ExileDemoTerrain"), Ogre::String("dat"));
mTerrainGroup->setOrigin(Ogre::Vector3::ZERO);

configureTerrainDefaults(light);

for (long x = 0; x <= 0; ++x)
for (long y = 0; y <= 0; ++y)
defineTerrain(x, y);

// sync load since we want everything in place when we start
mTerrainGroup->loadAllTerrains(true);
if (mTerrainsImported)
{
Ogre::TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator();
while(ti.hasMoreElements())
{
Ogre::Terrain* t = ti.getNext()->instance;
initBlendMaps(t);
}
}

mTerrainGroup->freeTemporaryResources();
}

void defineTerrain(long x, long y)
{
Ogre::String filename = mTerrainGroup->generateFilename(x, y);
if (Ogre::ResourceGroupManager::getSingleton().resourceExists(mTerrainGroup->getResourceGroup(), filename))
{
mTerrainGroup->defineTerrain(x, y);
}
else
{
Ogre::Image img;
getTerrainImage(x % 2 != 0, y % 2 != 0, img);
mTerrainGroup->defineTerrain(x, y, &img);
mTerrainsImported = true;
}
}

// should be a static local function
void getTerrainImage(bool flipX, bool flipY, Ogre::Image& img)
{
img.load("Island.png", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
if (flipX) img.flipAroundY();
if (flipY) img.flipAroundX();
}

void configureTerrainDefaults(Ogre::Light* light)
{
// Configure global
mTerrainGlobals->setMaxPixelError(8);
mTerrainGlobals->setCompositeMapDistance(3000); // testing composite map

// Important to set these so that the terrain knows what to use for derived (non-realtime) data
mTerrainGlobals->setLightMapDirection(light->getDerivedDirection());
mTerrainGlobals->setCompositeMapAmbient(mSceneMgr->getAmbientLight());
mTerrainGlobals->setCompositeMapDiffuse(light->getDiffuseColour());

// Configure default import settings for if we use imported image
Ogre::Terrain::ImportData& defaultimp = mTerrainGroup->getDefaultImportSettings();
defaultimp.terrainSize = 129; // 513;
defaultimp.worldSize = 3000.0f; // Set up for island.cfg
defaultimp.inputScale = 200; //600; //
defaultimp.minBatchSize = 33;
defaultimp.maxBatchSize = 65;

// textures
defaultimp.layerList.resize(3);
defaultimp.layerList[0].worldSize = 100;
defaultimp.layerList[0].textureNames.push_back("dirt_grayrocky_diffusespecular.dds");
defaultimp.layerList[0].textureNames.push_back("dirt_grayrocky_normalheight.dds");
defaultimp.layerList[1].worldSize = 30;
defaultimp.layerList[1].textureNames.push_back("grass_green-01_diffusespecular.dds");
defaultimp.layerList[1].textureNames.push_back("grass_green-01_normalheight.dds");
defaultimp.layerList[2].worldSize = 200;
defaultimp.layerList[2].textureNames.push_back("growth_weirdfungus-03_diffusespecular.dds");
defaultimp.layerList[2].textureNames.push_back("growth_weirdfungus-03_normalheight.dds");
}

Ogre::Light* CreateDirectionalLight()
{
Ogre::Vector3 lightdir(0.55, -0.3, 0.75);
lightdir.normalise();

Ogre::Light* light = mSceneMgr->createLight("tstLight");
light->setType(Ogre::Light::LT_DIRECTIONAL);
light->setDirection(lightdir);
light->setDiffuseColour(Ogre::ColourValue::White);
light->setSpecularColour(Ogre::ColourValue(0.4, 0.4, 0.4));

mSceneMgr->setAmbientLight(Ogre::ColourValue(0.4, 0.4, 0.4));
return light;
}

void destroyScene(void)
{
OGRE_DELETE mTerrainGroup;
OGRE_DELETE mTerrainGlobals;
}

And in createScene(), just replace the setWorldGeometry() call with:

// Load island
// mSceneMgr->setWorldGeometry("Island.cfg");
Configure_Terrain(); // For 1.8 I put this in instead of LoadWorldGeometry

The sample constructor needed to be updated, and some variables added:

public:
// Basic constructor
SampleApp() : mTerrainGlobals(0),mTerrainGroup(0),mTerrainsImported(false)
{}
protected:
Ogre::TerrainGlobalOptions *mTerrainGlobals;
Ogre::TerrainGroup *mTerrainGroup;
bool mTerrainsImported;

Also, createpalms would go into an endless loop, not freeing the raySceneQuery, so I simple muted it:

void createPalms(Ogre::SceneManager *mSceneMgr)
{
const int NumberOfPalms = 12;

Ogre::SceneNode* mPalmsSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

for (int k = 0; k < NumberOfPalms; k++)
{
Ogre::Vector3 RandomPos;
#if 1
RandomPos = Ogre::Vector3(rnd_(500,2500), rnd_(20,105), rnd_(500,2500));
#else
while (1) {
RandomPos = Ogre::Vector3(rnd_(500,2500), 0, rnd_(500,2500));
Ogre::RaySceneQuery * raySceneQuery = mSceneMgr->createRayQuery(Ogre::Ray(RandomPos + Ogre::Vector3(0,1000000,0), Ogre::Vector3::NEGATIVE_UNIT_Y));
Ogre::RaySceneQueryResult& qryResult = raySceneQuery->execute();
Ogre::RaySceneQueryResult::iterator i = qryResult.begin();

qryResult
if (i != qryResult.end() && i->worldFragment)
{
int y = i->worldFragment->singleIntersection.y;
if (y > 19 && y < 106)
{
RandomPos.y = y;
break;
}
}
OGRE_DELETE raySceneQuery;
}
#endif
Ogre::Entity *mPalmEnt = mSceneMgr->createEntity("Palm"+Ogre::StringConverter::toString(k), "Palm.mesh");
Ogre::SceneNode *mPalmSN = mPalmsSceneNode->createChildSceneNode();

mPalmSN->rotate(Ogre::Vector3(-1,0,rnd_(-0.3,0.3)), Ogre::Degree(90));
mPalmSN->attachObject(mPalmEnt);
Ogre::Real Scale = rnd_(50,75);
mPalmSN->scale(Scale,Scale,Scale);
mPalmSN->setPosition(RandomPos);
}
}

Ok, so it all builds (using the Hydrax built for 1.8 ). Side note: I am using MinGW.

When it runs, I get the complete scene, the island and palms are a little off location, but otherwise it works! (I had posted this with a problem, but now I have edited the code and it is fixed, so I figure maybe of some value).
The underwater terrain is still a little too bright, so turning down the CausticsPower option in the .HDX file helps a lot.

Thanks for reading..

Bill

MikeCyber

11-07-2012 10:18:29

Great, would you have the complete 0.5.4 demo for download?
I've managed to compile on Linux/Eclipse, but the water looks not very good from below. Also the palms are not
moving and sinking into terrain.
Many thanks
Michael