Special Effects

[Previous] [Next]

Alpha blending lets you create many special effects in your applications, including emissive objects in a foggy atmosphere (such as flames and lasers), lens flare, light maps, and tinted glass. In the following subsections, I'll give you a brief tour of these effects.

Emissive Objects in a Foggy Atmosphere

To simulate emissive objects, such as flames or lasers, in a foggy or dusty atmosphere, you need to set the source and destination blend states to D3DBLEND_ONE. The following code sets up this type of blending:

 // // Draw a light as it would appear in a slightly misty room. // m_pd3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE); m_pd3dDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_ONE); m_pd3dDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_ONE); m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX,                              g_avLightVertices, 4, NULL ); 

Objects such as flames are often generated using billboarding. In this process, a 2D object is placed into a 3D scene and rotated so that it always faces the viewer. Although for most objects this effect is fairly unrealistic, for objects such as flames, you can often insert an animated texture into a scene with billboarding and produce a very realistic look.

Lens Flare

Lens flare is used in a variety of applications to simulate the effect of light reflecting off the camera lens. The Flare demo from Microsoft, which this book's companion CD installs in the \mssdk\samples\Multimedia\d3dim\src\Flare directory, illustrates how to generate this lens flare effect. Figure 10-1 shows the resulting animated scene with the lens flare applied.

Figure 10-1 Microsoft's Flare program demonstrating a lens flare effect

Figure 10-2 shows four of the ten textures (Shine0.ppm through Shine9.ppm) used to generate the animated lens flare effect.

click to view at full size.

Figure 10-2 Textures for animated lens flare effect

Each image in Figure 10-2 is displayed cyclically, producing the final animated flare effect. Strategic use of this effect can make a scene appear more realistic, by simulating the appearance of light reflection. To create this effect, set both the source and destination blend factors to D3DBLEND_ONE.

Light Maps

As you'll see in Chapter 11 when we cover this topic in detail, light mapping allows you to use one texture as a light map to affect the appearance of the other textures applied to the scene. Using D3DBLEND_ZERO as the source blend state and D3DBLEND_SRCALPHA as the destination blend state, the source primitive is used as a light map to scale the contents of the frame buffer to darken the scene when appropriate.

Keep in mind that several accelerators support alpha blending only with ARGB:4444 , ARGB:1555, or ARGB:8888 format textures. Because of this, you might need to change any code you add alpha blending to so that it uses these texture formats. To guarantee that one of these formats is supported, you need to use the EnumTextureFormats method to verify that a format containing 4 (or more) bits of alpha exists:

 void ChooseTextureFormat(IDirect3DDevice7 *Device, DWORD bpp,                           DDPIXELFORMAT *pddpf) {     FindTextureData FindData;     ZeroMemory(&FindData, sizeof(FindData));     FindData.bpp = bpp;     Device->EnumTextureformats(FindTextureCallback,                                (LPVOID)&FindData);     *pddpf = FindData.ddpf;     § } 

Tinted Glass

By using a linear blend of the source color and the destination color, you can create the appearance of tinted glass, in which some of destination object's color appears to flow through the source glass and the rest of it is absorbed by the glass. For glass that is tinted evenly across the surface, it is most efficient to use vertex alpha (and vertex color, too, if the color is uniform). If you want the transparency of the glass to vary nonlinearly across the polygon, use texture alpha instead of vertex alpha.



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net