[DIY] Adding Range-Fog

BlitzMax Forums/MiniB3D Module/[DIY] Adding Range-Fog

Kryzon(Posted 2010) [#1]
Hello,

Range or Radial fog is a method of computing the fog distance based on the actual distance from the camera to the vertices.
It contrasts the default method of Eye-plane fog because of that.

You can notice that, in some applications that use Eye-Plane fog, the objects closer to the edges of the screen are less occluded than those on the center, even if their distance to the eye point is equal. You can notice this especially if you Yaw the camera around. An object that is partially occluded in the center of the view will become more visible when it's on the edge of the screen. It's not very nice.

To enable Range-Fog in MiniB3D, you'll need to search the 'fog setup code in your TCamera.Update method:
'fog
If fog_mode>0
       glEnable(GL_FOG)
       glFogi(GL_FOG_MODE,GL_LINEAR)

       If fog_mode=2 Then 
	glFogi(GL_FOG_DISTANCE_MODE_NV,GL_EYE_RADIAL_NV) 
       Else
	glFogi(GL_FOG_DISTANCE_MODE_NV,GL_EYE_PLANE_ABSOLUTE_NV)
       EndIf 

       glFogf(GL_FOG_START,fog_range_near#)
       glFogf(GL_FOG_END,fog_range_far#)
       Local rgb#[]=[fog_r#,fog_g#,fog_b#]
       glFogfv(GL_FOG_COLOR,rgb#)
Else
       glDisable(GL_FOG)
EndIf

The part separated by blank lines is the one to be added to this part of the TCamera.BMX. Nothing else is changed.

Notice the "If fog_mode = 2" condition. This adds a new fog mode for you to use.
You can use any number you like, really, but since Blitz3D only has 2 fog modes (0 for off and and 1 for linear), it makes sense.

After pasting that IF, make sure to save the TCamera.BMX and go to Program -> Build Modules. It should rebuild the MiniB3D module.

To test this new mode, simply go to the Fog example from the MiniB3D "examples" folder, and change the line...
CameraFogMode cam,1
to
CameraFogMode cam,2
That's it.

NOTE: Radial-fog is computationally more expensive than Eye-Plane. It is up to you to decide if the [possibly negligible] cost is acceptable.

NOTE2: It's advisable to check if the user's machine supports this type of Extension before using it. More info on this, check the THardwareSupport.BMX file with MiniB3D Extended - by Klepto2.

Further reading:

http://www.opengl.org/registry/specs/NV/fog_distance.txt

http://developer.download.nvidia.com/SDK/9.5/Samples/samples.html#fog_compare


Banshee(Posted 2010) [#2]
Very cool work :)


Kryzon(Posted 2010) [#3]
Thanks, glad you liked it.