Skip to content

Commit

Permalink
TopMenu/Water: Fixed hydrax crashing on nd start
Browse files Browse the repository at this point in the history
still very glitchy though, and crashes upon return to main menu (during rendering - invalid pointer to listener)
  • Loading branch information
ohlidalp committed Sep 18, 2024
1 parent d2f7ed1 commit 086a11a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
21 changes: 18 additions & 3 deletions source/main/gfx/HydraxWater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,38 @@ HydraxWater::HydraxWater(float water_height, Ogre::TerrainGroup* terrain_grp, Og
, waterHeight(water_height)
, waveHeight(water_height)
, CurrentConfigFile(conf_file)
, mTerrainGroup(terrain_grp)
{
App::GetCameraManager()->GetCamera()->setNearClipDistance(0.1f);

InitHydrax();

//Apply depth technique to the terrain
TerrainGroup::TerrainIterator ti = terrain_grp->getTerrainIterator();
TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator();
while (ti.hasMoreElements())
{
// getNext() advances the iterator
Ogre::Terrain* t = ti.getNext()->instance;
MaterialPtr ptr = t->getMaterial();
mHydrax->getMaterialManager()->addDepthTechnique(ptr->createTechnique());
MaterialPtr tMat = t->getMaterial();
mHydrax->getMaterialManager()->addDepthTechnique(tMat->createTechnique());
}
}

HydraxWater::~HydraxWater()
{
//Remove depth technique from the terrain
TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator();
while (ti.hasMoreElements())
{
// getNext() advances the iterator
Ogre::Terrain* t = ti.getNext()->instance;
MaterialPtr tMat = t->getMaterial();
for (Ogre::Technique* technique: tMat->getTechniques())
{
mHydrax->getMaterialManager()->removeDepthTechnique(technique);
}
}

mHydrax->remove();
mHydrax = nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions source/main/gfx/HydraxWater.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class HydraxWater : public IWater
Hydrax::Noise::Perlin* waternoise;
Hydrax::Module::ProjectedGrid* mModule;
Ogre::String CurrentConfigFile;
Ogre::TerrainGroup* mTerrainGroup; //!< For cleanup only
};

/// @} // addtogroup Gfx
Expand Down
20 changes: 20 additions & 0 deletions source/main/gfx/hydrax/MaterialManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3368,6 +3368,26 @@ namespace Hydrax
}
}

void MaterialManager::removeDepthTechnique(Ogre::Technique *Technique)
{
Ogre::Pass *DM_Technique_Pass0 = Technique->getPass(0);
ROR_ASSERT(DM_Technique_Pass0);
if (!DM_Technique_Pass0)
{
return;
}

if (DM_Technique_Pass0->getVertexProgramName() == _def_Depth_Shader_VP_Name)
{
DM_Technique_Pass0->setVertexProgram(""); // Remove vertex shader
}

if (DM_Technique_Pass0->getFragmentProgramName() == _def_Depth_Shader_FP_Name)
{
DM_Technique_Pass0->setFragmentProgram(""); // Remove fragment shader
}
}

void MaterialManager::addDepthTechnique(Ogre::Technique *Technique, const bool& AutoUpdate)
{
if (!Ogre::MaterialManager::getSingleton().resourceExists(_def_Depth_Material_Name))
Expand Down
4 changes: 4 additions & 0 deletions source/main/gfx/hydrax/MaterialManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ namespace Hydrax
return mOptions;
}

/** Undo `addDepthTechnique()`
*/
void removeDepthTechnique(Ogre::Technique *Technique);

/** Add depth technique to an especified material
@param Technique Technique where depth technique will be added
@param AutoUpdate The technique will be automatically updated when water parameters change
Expand Down

0 comments on commit 086a11a

Please sign in to comment.