Platform SDK: DirectX |
Adding fog to a 3-D scene can enhance realism, provide ambiance or set a mood, and obscure artifacts sometimes caused when distant geometry comes into view. Direct3D Immediate Mode supports two fog models, pixel fog and vertex fog, each with its own features and programming interface.
Essentially, fog is implemented by blending the color of objects in a scene with a chosen fog color based on the depth of an object in a scene, or its distance from the viewpoint. As objects grow more distant, their original color blends more and more into the chosen fog color, creating the illusion that the object is being increasingly obscured by tiny particles floating in the scene—fog. The following screen capture shows a scene rendered without fog, and a similar scene rendered with fog enabled.
In the preceding figure, the scene on the left has a clear horizon, beyond which no scenery is visible, even though it would be visible in the real world. The scene on the right obscures the horizon by using a fog color identical to the background color, making polygons appear to fade into the distance. By combining discrete fog effects with creative scene design you can add mood and soften the color of objects in a scene.
Direct3D provides applications with two ways to add fog to a scene, pixel fog and vertex fog, named according to how the fog effects are applied. For details, see Pixel Fog and Vertex Fog. In short, pixel fog (also called "table fog") is implemented in the device driver, and vertex fog is implemented in the Direct3D lighting engine.
Note Regardless of which type of fog—pixel or vertex—you use, your application must provide a compliant projection matrix to ensure that fog effects will be properly applied. This restriction applies even to applications that do not use the Direct3D transformation and lighting engine. For additional details about how you can provide an appropriate matrix, see A W-Friendly Projection Matrix.