Caelum + Terrain. Light trouble.

drboolean

14-06-2012 09:42:25

Hello!
I'm newbie in ogre3d and I have a trouble with terrain lighting. I'm using Caelum + Hydrax with terrain.
In generally after executing compiled project it looks like:

But sometimes I got this:

I don't know why I got this strange result.
There are no errors in my ogre.log and all the material scripts are parsed succesfully.

My Caelum code in createScene():

mCaelumSystem = new CaelumSystem (mRoot, mSceneMgr,CaelumSystem::CAELUM_COMPONENTS_DEFAULT);
mCaelumSystem->autoConfigure(CaelumSystem::CAELUM_COMPONENTS_DEFAULT);
mRoot->addFrameListener(mCaelumSystem);
mWindow->addListener(mCaelumSystem);
mCaelumSystem->getUniversalClock()->setTimeScale(100);
FlatCloudLayer* cloudLayer = mCaelumSystem->getCloudSystem()->createLayer();
mCaelumSystem->setManageSceneFog(Ogre::FOG_NONE);


My Terrain code in createScene():

mTerrainGlobals = OGRE_NEW TerrainGlobalOptions();

mTerrainGroup = OGRE_NEW TerrainGroup(mSceneMgr, Terrain::ALIGN_X_Z, 513, 12000.0f);
mTerrainGroup->setFilenameConvention(String("Example"), String("dat"));

mTerrainGroup->setOrigin(Vector3::ZERO);

configureTerrainDefaults(light);

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


mTerrainGroup->loadAllTerrains(true);

if (mTerrainsImported)
{
TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator();
while(ti.hasMoreElements())
{
Terrain* t = ti.getNext()->instance;
initBlendMaps(t);
}
}

mTerrainGroup->freeTemporaryResources();
terrainGroup = mTerrainGroup;


Terrain's addition functions:

void getTerrainImage(bool flipX, bool flipY, Image& img)
{
img.load("mapDepth.png", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
if (flipX)
img.flipAroundY();
if (flipY)
img.flipAroundX();

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

void initBlendMaps(Terrain* terrain)
{
TerrainLayerBlendMap* blendMap0 = terrain->getLayerBlendMap(1);
TerrainLayerBlendMap* blendMap1 = terrain->getLayerBlendMap(2);
Real minHeight0 = 70;
Real fadeDist0 = 40;
Real minHeight1 = 70;
Real fadeDist1 = 15;
float* pBlend1 = blendMap1->getBlendPointer();
for (uint16 y = 0; y < terrain->getLayerBlendMapSize(); ++y)
{
for (uint16 x = 0; x < terrain->getLayerBlendMapSize(); ++x)
{
Real tx, ty;

blendMap0->convertImageToTerrainSpace(x, y, &tx, &ty);
Real height = terrain->getHeightAtTerrainPosition(tx, ty);
Real val = (height - minHeight0) / fadeDist0;
val = Math::Clamp(val, (Real)0, (Real)1);

val = (height - minHeight1) / fadeDist1;
val = Math::Clamp(val, (Real)0, (Real)1);
*pBlend1++ = val;
}
}
blendMap0->dirty();
blendMap1->dirty();
blendMap0->update();
blendMap1->update();
}

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

// 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
Terrain::ImportData& defaultimp = mTerrainGroup->getDefaultImportSettings();
defaultimp.terrainSize = 513;
defaultimp.worldSize = 12000.0f;
defaultimp.inputScale = 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");
}


My frameEnded() function:

bool frameStarted(const Ogre::FrameEvent& evt)
{
Vector3 value = mCaelumSystem->getSun()->getSceneNode()->_getDerivedPosition();
ColourValue cval = mCaelumSystem->getSun()->getBodyColour();
mHydrax->setSunPosition(value);
mHydrax->setSunColor(Vector3(cval.r,cval.g,cval.b));

Caelum::LongReal mJulian = mCaelumSystem->getUniversalClock()->getJulianDay();
cval = mCaelumSystem->getSunLightColour(mJulian, mCaelumSystem->getSunDirection(mJulian));
mHydrax->setWaterColor(Vector3(cval.r - 0.3, cval.g - 0.2, cval.b));

Vector3 col = mHydrax->getWaterColor();
float height = mHydrax->getSunPosition().y / 10.0f;

Hydrax::HydraxComponent c = mHydrax->getComponents();
if(height < 0)
{
if(mHydrax->isComponent(Hydrax::HYDRAX_COMPONENT_CAUSTICS))
mHydrax->setComponents(Hydrax::HydraxComponent(c ^ Hydrax::HYDRAX_COMPONENT_CAUSTICS));
} else
{
if(!mHydrax->isComponent(Hydrax::HYDRAX_COMPONENT_CAUSTICS))
mHydrax->setComponents(Hydrax::HydraxComponent(c | Hydrax::HYDRAX_COMPONENT_CAUSTICS));
}

if(height < -99.0f)
{
col = mOriginalWaterColor * 0.1f;
height = 9999.0f;
}
else if(height < 1.0f)
{
col = mOriginalWaterColor * (0.1f + (0.009f * (height + 99.0f)));
height = 100.0f / (height + 99.001f);
}
else if(height < 2.0f)
{
col += mOriginalWaterColor;
col /= 2.0f;
float percent = (height - 1.0f);
col = (col * percent) + (mOriginalWaterColor * (1.0f - percent));
}
else
{
col += mOriginalWaterColor;
col /= 2.0f;
}
mHydrax->setWaterColor(col);
mHydrax->setSunArea(height);
mHydrax->update(evt.timeSinceLastFrame);

return true;
}

Could you help me? I'm using OGRE 1.7.4 and VC++ 2010 :(

drboolean

06-08-2012 10:20:54

The problem is solved! :D
I forgot to add this line:

mCaelumSystem->setEnsureSingleLightSource( true );

Sorry for my stupidness. :oops: