Game Development Community

SceneState::getHazeAndFog()

by asmaloney (Andy) · in Torque Game Engine · 01/18/2007 (9:13 pm) · 0 replies

I took a look at SceneState::getHazeAndFog() in preparation for re-examining the fog calculator and noticed a couple of things right away.

Here's a version of SceneState::getHazeAndFog() which - for my profiling runs on gcc/PPC - reduced the time from 0.6% to 0.3%. Simple changes - mostly using floats instead of doubles [0.0f vs. 0.0] in calculations and some const-ifying. I'd be interested to see if it makes any difference with VC++ [Clint?].

Note that you'll have to change the prototype in the header file [sceneState.h] because I made it a const function.

F32 SceneState::getHazeAndFog(float dist, float deltaZ) const
{
   float haze = 0.0f;

   if(dist > mFogDistance) {
      if (dist > mVisibleDistance)
         return 1.0f;

      const float distFactor = (dist - mFogDistance) * mFogScale - 1.0f;
      haze = 1.0f - distFactor * distFactor;
   }

   const Vector<FogBand> *band;
   if(deltaZ < 0.0f)
   {
      deltaZ = -deltaZ;
      band = &mNegFogBands;
   }
   else
      band = &mPosFogBands;

   float ht = deltaZ;
   for(int i = 0; i < band->size(); ++i)
   {
      const FogBand &bnd = (*band)[ i ];

      if(ht < bnd.cap)
      {
         if(bnd.isFog)
            haze += dist * bnd.factor;
         break;
      }
      const float subDist = dist * bnd.cap / ht;
      if(bnd.isFog)
         haze += subDist * bnd.factor;
      dist -= subDist;
      ht -= bnd.cap;
   }

   if ( haze > 1.0f )
      return( 1.0f );

   return( haze );
}