UW EXTENSION CERTIFICATE PROGRAM IN GAME DEVELOPMENT 2 ND QUARTER: ADVANCED GRAPHICS 2D Rendering
Goals 1. Learn the basic states used in 2D rendering 2. See how to render 2D images on the screen 3. Review the fixed function pixel pipeline
D3D Calls SetRenderStateSets a variety of states global to D3D (there are many) SetPixelShaderSelects how to express per-pixel calculations SetFVFSets the format of the vertices SetTextureSets a texture image (there can be more than one) SetSamplerStateSelects how the texture will be sampled from SetTextureStageStateSelects how the texture values will be used ClearFills a region of the screen to a single value DrawPrimitiveUPInitiates the actual drawing
Typical global configuration Disable Z-buffering and stencil SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); SetRenderState(D3DRS_ZWRITEENABLE, FALSE); SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); Disable pixel shader (will use the texture stage states) SetPixelShader(NULL); Normal shading and no culling SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
Typical global configuration (cont.) Enable alpha-blending SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); Disable alpha-testing SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
Basic primitive: the rectangle Create the vertices Vertex const vertices[4] = { { 20, 20, 0, 1, … }, { 100, 20, 0, 1, … }, { 20, 100, 0, 1, … }, { 100, 100, 0, 1, … }, }; Set up the vertex format (must match the data!) SetFVF(D3DFVF_XYZRHW | …); And render DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertices, sizeof(vertices[0]));
Textures Select the texture image: SetTexture(0, pTexture); Select the sampling settings: SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); SetSamplerState(0, D3DSAMP_ADDRESSU, D3DADDRESS_CLAMP); SetSamplerState(0, D3DSAMP_ADDRESSV, D3DADDRESS_CLAMP);
Texture calculations Perform custom calculations for every pixel. For instance: SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT); SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_CURRENT); SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0); Disable the first one that’s not needed SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);