Game Development Reference
In-Depth Information
The Setup method sets up many things; we have omitted most of the
code that is not relevant to this chapter. With regard to blending, the
Setup method specifies the source that the alpha should be taken
from. In this example we instruct the alpha to be taken from the diffuse
component of the material. Notice that we set the teapot material's
diffuse alpha component to 0.5, indicating that the teapot should be ren-
dered at 50% transparency. We also set the blend factors here as well.
Take note that we do not enable alpha blending in this method. The rea-
son is that the alpha blending stage takes up additional processing and
should only be used on the geometry that needs it. For instance, in this
sample only the teapot needs to be rendered with alpha blending
enabled—the quad does not. Therefore, we enable alpha blending in the
Display function.
bool Setup()
{
TeapotMtrl = d3d::RED_MTRL;
TeapotMtrl.Diffuse.a = 0.5f; // set alpha to 50% opacity
BkGndMtrl = d3d::WHITE_MTRL;
D3DXCreateTeapot(Device, &Teapot, 0);
...// Create background quad snipped
...// Light and texture setup snipped
// use alpha in material's diffuse component for alpha
Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
Device->SetTextureStageState(0, D3DTSS_ALPHAOP,
D3DTOP_SELECTARG1);
// set blending factors so that alpha
// component determines transparency
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
...// view/projection matrix setup snipped
return true;
}
In the Display function, we check to see if the A or S key was pressed
and respond by increasing or decreasing the material's alpha value.
Notice that the method ensures that the alpha value does not go out-
side the interval [0, 1]. We then render the background quad. Finally,
we enable alpha blending, render the teapot with alpha blending
enabled, and then disable alpha blending.
bool Display(float timeDelta)
{
if( Device )
Search WWH ::




Custom Search