Cubic fog

Blitz3D Forums/Blitz3D Programming/Cubic fog

Volumetric fog is easy.

Make a polygon volume for yuor fog. For example, a sphere.

Cast a ray from the camera through each vertex touching a front facing tri. Determine where that ray hits the back of the volume.

The distance between the vertex and the point the ray exits the fog is the thickness of the fog.

Use an exponential formula to then choose the value to set that vertex's alpha to.

For example,
Alpha# = (Distance#/FogFar#)^2

Where FogFar# is the thickness of the fog where alpha reaches 1, and below which where it falls off exponentially.

In other words, if FogFar# is 2.0 and the fog is 1.0 thick, if the equation were linear alpha would be 0.5, but it's exponential so alpha will be much less than 0.5.

Exponential is how real fog works. But you might want linear fog if you don't want your fog volumes to be too big.

You can also change the eponentiality of the equation by reducing the exponent. For example, instead of ^2, you can ^1.5. That will give you a more linear curve.

There's functions in the code archives which will tell you if a ray or line segment instersects a polygon.

Btw you need to update this whenever the camera moves. So you won't want your fog volume to have too many vertices.

Another less accurate way you can make a fog volume is by using spherical environment maps. Just make a map that goes from black in the middle to white at the edges. Apply this to your fog shape on one surface. Then on a second surface, apply the inverse of this map, and set the surface to add. The color of the second surface will be the color of the fog. The spherical environment map acts as an alpha channel.

Neither of these methods take into account objects within the fog though. If you want an object within fog to appear fogged the right amount, if that object is an animated object in Blitz you may be out of luck, unless Blitz's linepicks work properly with animated objects. Cause you can't do your own linepicks on animated objects. There's no way to know where the vertcies are as they change during animation.

Then again, it might still be possible. It is possile to generate a pseudo zbuffer like map by using regular fog on an entity so that parts of it farther from the camera are darker. If you can generate that then you might be able to use it as an alpha map to control how much fog the entity recieves on each part. Of course, it would be a linear change, but that would probably not be too noticeable.

Of course, it occurs to me that you might be able to get away with simply finding how deep inside the fog volume the entity is realtive to the camera, and then just use a single fog value for it to kinda fake it. But that would mean if the entity sticks it head up out of the fog it would not be fogged. You'd change the amount of fog on the entity by giving it a second surface with alpha blending.

Well, that's enought o get you started. I guess it's not such an easy effect after all when you don't have access to all the data. :-)