Blending Outline • The Blending Equation • Blend Factors • Alpha Test WKTai/GAME Lab. Blending 2 Introduction • Blending – A technique to blend (combine) pixels that we are currently rasterizing with pixels that have been previously rasterized to the same pixel locations • Demo TeapotDemo.exe WKTai/GAME Lab. Blending 3 The Blending Equation • Variables are a 4D color vector (r, g, b, a) • denotes component-wise multiplication • OutputPixel: OutputPixel The resulting blended pixel • SourcePixel – The pixel currently being computed that is to be blended with the pixel on the back buffer • SourceBlendFactor – A value in the interval [0, 1] that specifies the percent of the source pixel to use in the blend • DestPixel: DestPixel The pixel currently on the back buffer • DestBlendFactor – A value in the interval [0, 1] that specifies the percent of the destination pixel to use in the blend WKTai/GAME Lab. Blending 4 “+” Operator in the Blending Function • “+” operator can be changed by setting the D3DRS_BLENDOP gd3dDevice->SetRenderState(D3DRS_BLENDOP, ???); ??? • D3DBLENDOP – Defines the supported blend operations typedef enum D3DBLENDOP { D3DBLENDOP_ADD = 1, D3DBLENDOP_SUBTRACT = 2, D3DBLENDOP_REVSUBTRACT = 3, D3DBLENDOP_MIN = 4, D3DBLENDOP_MAX = 5, D3DBLENDOP_FORCE_DWORD = 0x7fffffff, } D3DBLENDOP, *LPD3DBLENDOP; WKTai/GAME Lab. Blending 5 D3DBLENDOP • D3DBLENDOP_ADD – OutputPixel = SourcePixel⊗SourceBlendFactor + DestPixel⊗DestBlendFactor • D3DBLENDOP_SUBTRACT – OutputPixel = SourcePixel⊗SourceBlendFactor DestPixel⊗DestBlendFactor • D3DBLENDOP_REVSUBTRACT – OutputPixel = DestPixel⊗DestBlendFactor SourcePixel⊗SourceBlendFactor • D3DBLENDOP_MIN – OutputPixel = MIN(SourcePixel⊗SourceBlendFactor, MIN DestPixel⊗DestBlendFactor) • D3DBLENDOP_MAX – OutputPixel = MAX(SourcePixel⊗SourceBlendFactor, MAX DestPixel⊗DestBlendFactor) WKTai/GAME Lab. Blending 6 Outline • • • The Blending Equation Blend Factors Alpha Test WKTai/GAME Lab. Blending 7 Blend Factors • Setting different combinations of source and destination blend factors – Device->SetRenderState(D3DRS_SRCBLEND, SRC Source); – Device->SetRenderState(D3DRS_DESTBLEND, DEST Destination); • Source and Destination can be one of the following blend factors: – D3DBLEND_ZERO—blendFactor=(0, 0, 0, 0) – D3DBLEND_ONE—blendFactor=(1, 1, 1, 1) – D3DBLEND_SRCCOLOR—blendFactor=(r SRC s, gs, bs, as) WKTai/GAME Lab. Blending 8 Blend Factors (Contd.) • D3DBLEND_INVSRCCOLOR – blendFactor=(1 – rs, 1 – gs, 1 – bs,1 – as) • D3DBLEND_SRCALPHA // default – blendFactor=(αs, αs, αs, αs) • D3DBLEND_INVSRCALPHA // default – blendFactor=(1 – αs, 1 – αs, 1 – αs,1 – αs) • D3DBLEND_DESTALPHA – blendFactor=(αd, αd, αd, αd) • D3DBLEND_INVDESTALPHA – blendFactor=(1 – αd, 1 – αd, 1 – αd,1 – αd) WKTai/GAME Lab. Blending 9 Blend Factors (Contd.) • D3DBLEND_DESTCOLOR DEST – blendFactor=(rd, gd, bd, αd) • D3DBLEND_INVDESTCOLOR DEST – blendFactor=(1 – rd, 1 – gd, 1 – bd,1 – αd) • D3DBLEND_SRCALPHASAT SRC – blendFactor=(f, f, f, 1), where f=min(αs, 1 – αd) • D3DBLEND_BOTHINVSRCALPHA SRC – Sets the source blend factor to (1–αs, 1–αs, 1–αs, 1–αs) and – The destination blend factor to (αs, αs, αs, αs) – This blend mode is only valid for D3DRS_SRCBLEND SRC WKTai/GAME Lab. Blending 10 Blend Factors (Contd.) • Set the source and destination blend factors in an effect file inside a pass block Pass p0 { vertexShader= compile vs_2_0 VX(); pixelShader= compile ps_2_0 PS(); AlphaBlendEnable= AlphaBlendEnable true; SrcBlend= One; DestBlend= One; BlendOp= RevSubtract; } WKTai/GAME Lab. Blending 11 Blend Factors (Contd.) Pass p0 { vertexShader= compile vs_2_0 VX(); pixelShader= compile ps_2_0 PS(); // use these states to blend RGB components AlphaBlendEnable= AlphaBlendEnable true; SrcBlend= One; DestBlend= One; BlendOp= RevSubtract; // Blend the alpha component SeparateAlphaBlendEnable = ture; Alpha SrcBlendAlpha = ScrAlpha; DestBlendAlpha = InvScrAlpha; Dest BlendOpAlpha = Add; } WKTai/GAME Lab. Blending 12 Blend Factor Example • Keep the orignial destination pixel exactly as it is – Not overwrite or blend it with the source pixel currently being rasterized • Set the source pixel blend factor to D3DBLEND_ZERO and the destination pixel blend factor to D3DBLEND_ONE OutputPixel= SourcePixel ⊗ SourceBlendFactor + DestPixel ⊗ DestBlendFactor = SourcePixel ⊗ (0,0,0,0) + DestPixel ⊗(1,1,1,1) = (0,0,0,0) + DestPixel = DestPixel WKTai/GAME Lab. Blending 13 Code Snippet gd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD); ADD Device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ZERO); D3DBLEND_ZERO Device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE); ONE WKTai/GAME Lab. Blending 14 Blend Factor Example II • Blending by adding two images OutputPixel= SourcePixel ⊗ SourceBlendFactor + DestPixel ⊗ DestBlendFactor = SourcePixel ⊗ (1,1,1,1) + DestPixel ⊗(1,1,1,1) = SourcePixel + DestPixel • Code snippet gd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD); ADD Device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE); D3DBLEND_ONE Device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE); ONE WKTai/GAME Lab. Blending 15 Blend Factor Example III • Multiply a source pixel with its corresponding destination pixel OutputPixel= SourcePixel ⊗ SourceBlendFactor + DestPixel ⊗ DestBlendFactor = SourcePixel ⊗ (0,0,0,0) + DestPixel ⊗ SourcePixel = DestPixel ⊗ SourcePixel • Code snippet gd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD); ADD Device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ZERO); D3DBLEND_ZERO Device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR); SRCCOLOR WKTai/GAME Lab. Blending 16 Blend Factor Example III • Similar to texture modulation for lightmap WKTai/GAME Lab. Blending 17 Blend Factor Example IV • Blend the source and destination pixels based on the transparency percent of the source pixel – Let the source alpha component as be thought of as α percent that controls the transparency of the source pixel OutputPixel= SourcePixel ⊗ SourceBlendFactor + DestPixel ⊗ DestBlendFactor = SourcePixel ⊗ (sα, sα, sα, sα) + DestPixel⊗(1sα, 1-sα, 1-sα, 1-sα) = SourcePixel sα ⊗DestPixel (1-sα ) WKTai/GAME Lab. Blending 18 Code snippet gd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD); ADD Device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); D3DBLEND_SRCALPHA Device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); SRCALPHA WKTai/GAME Lab. Blending 19 Enable/Disable Blending Operation • Enable by setting the D3DRS_ALPHABLENDENABLE render state to true – Blending is disabled by default Device->SetRenderState( RenderState D3DRS_ALPHABLENDENABLE, true); • Disable blending Device->SetRenderState( RenderState D3DRS_ALPHABLENDENABLE, false); WKTai/GAME Lab. Blending 20 Note on Render State • To control which color components the graphics device writes to – Set D3DPMISCCAPS_COLORWRITEENABLE bit in WRITE D3DCAPS9::PrimitiveMiscCaps • Example – To write only to the green and blue channels – IDirect3DDevice9::SetRenderState( D3DRS_COLORWRITEENABLE, WRITE D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN ); WKTai/GAME Lab. Blending 21 Note on Render State (Contd.) • Default setting – Writes to all four color channels – IDirect3DDevice9::SetRenderState( D3DRS_COLORWRITEENABLE, WRITE D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); RED WKTai/GAME Lab. Blending 22 Rules When Using Blending 1. Draw objects that do not use blending first 2. Then sort the objects that use blending by their distance from the camera 3. Draw the objects that use blending in a back-tofront order WKTai/GAME Lab. Blending 23 Demo Teapot Demo.exe • Demo TeapotDemo.exe – Use the source pixel’s alpha component to control its transparency WKTai/GAME Lab. Blending 24 TeapotDemo.exe void TeapotDemo::drawTeapot() { // Cylindrically interpolate texture coordinates. HR(gd3dDevice->SetRenderState( D3DRS_WRAP0, D3DWRAPCOORD_0)); // Enable alpha blending. HR(gd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true)); ALPHA true HR(gd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)); SRC SRCALPHA HR(gd3dDevice->SetRenderState( D3DRS_DESTBLEND, BLEND D3DBLEND_INVSRCALPHA)); INV … // Disable alpha blending. HR(gd3dDevice>SetRenderState(D3DRS_ALPHABLENDENABLE, false)); ALPHA false } WKTai/GAME Lab. Blending 25 TeapotDemo.exe • Setting the diffuse material’s alpha component TeapotDemo::TeapotDemo( { … mTeapotMtrl.ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); 1.0f mTeapotMtrl.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 0.5f); 0.5f mTeapotMtrl.spec = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); 1.0f mTeapotMtrl.specPower = 16.0f; … } WKTai/GAME Lab. Blending 26 TeapotDemo.exe • In vertex shader, shader just pass along the alpha component to the pixel shader uniform extern float4 gDiffuseMtrl; gDiffuseMtrl struct OutputVS … { float4 posH : POSITION0; float4 diffuse : COLOR0; COLOR0 float4 spec : COLOR1; float2 tex0 : TEXCOORD0; OutputVS DirLightTexVS(…) { … }; outVS.diffuse. a = gDiffuseMtrl. outVS gDiffuseMtrl a; … return outVS; outVS } WKTai/GAME Lab. Blending 27 TeapotDemo.exe • In pixel shader, shader set the pixel’s alpha value to the interpolated color’s alpha component float4 DirLightTexPS(float4 c : COLOR0, COLOR0 float4 spec : COLOR1, float2 tex0 : TEXCOORD0) : COLOR { float3 texColor = tex2D(TexS, tex0).rgb; float3 diffuse = c.rgb * texColor; // modulation return float4(diffuse + spec.rgb, c.a); } WKTai/GAME Lab. Blending 28 Control Transparency by Texture Alpha Channel • Using material’s alph, the level of transparency will be uniformly set for the entire triangles • Using texture alpha channel, control the transparency at the pixel level WKTai/GAME Lab. Blending 29 Creating Textures with Alpha Channel • Create the alpha channel by – Adobe Photoshop – Using DXTex utility program • Steps – DirectX Texture Tool -> open -> brick.jpg (24-bit RGB texture, D3DFMT_R8G8B8) *Change format to support an alpha channel • 32-bit ARGB texture format D3DFMT_A8R8G8B8 • Or a compressed format, like D3DFMT_DXT3 – Format -> Change Surface Format -> DXT3 – Load the 8-bit grayscale – File-> Open Onto Alpha Channel Of This Texture -> alpha.jpg – # WKTai/GAME Lab. Blending 30 Brick Texture with Alpha Channel RGB Textrue Alpha grayscale WKTai/GAME Lab. Blending 31 Transparent Teapot Demo • Demo Teapot with TexAlpha.exe WKTai/GAME Lab. Blending 32 Code Snippet for Pixel Shader float4 DirLightTexPS(float4 c : COLOR0, float4 spec : COLOR1, float2 tex0 : TEXCOORD0) : COLOR { float4 texColor = tex2D(TexS, tex0); float3 diffuse = c.rgb * texColor.rgb; return float4(diffuse + spec.rgb, texColor.a*c.a); texColor.a } • When using an alpha channel, usually set the diffuse material’s alpha component to 1.0 WKTai/GAME Lab. Blending 33 Outline • The Blending Equation • Blend Factors • Alpha Test WKTai/GAME Lab. Blending 34 Alpha Test • To discard a pixel (not render it) based on its alpha value in comparison to some reference value if αs ⊙ ref == true then accept pixel else reject pixel αs :Source alpha component ref :Application-defined reference value ⊙ :Is an operation by D3DCMPFUNC enumerated type WKTai/GAME Lab. Blending 35 D3DCMPFUNC typedef enum D3DCMPFUNC { D3DCMP_NEVER = 1, // Always fail the test D3DCMP_LESS = 2, D3DCMP_EQUAL = 3, D3DCMP_LESSEQUAL = 4, D3DCMP_GREATER = 5, D3DCMP_NOTEQUAL = 6, D3DCMP_GREATEREQUAL = 7, D3DCMP_ALWAYS = 8, // Always pass the test D3DCMP_FORCE_DWORD = 0x7fffffff, } D3DCMPFUNC, *LPD3DCMPFUNC; WKTai/GAME Lab. Blending 36 Example Code • Enable the alpha test, set the ref= 100, and set the comparison operation to D3DCMP_GREATERQUAL – Only pixels with alpha values >= 100 are draw HR(gd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, true)); true HR(gd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DRS_ALPHAFUNC D3DCMP_GREATEREQUAL)); HR(gd3dDevice->SetRenderState( D3DRS_ALPHAREF, REF 100)); 100 WKTai/GAME Lab. Blending 37 Alpha Test in Effect File Pass P0 { vertexShader = compile vs_2_0 VS(); pixelShader = compile ps_2_0 PS(); AlphaTestEnable = true; AlphaFunc = GreaterEqual; AlphaRef = 220; } WKTai/GAME Lab. Blending 38 Application of Alpha Test • A quad with texture and alpha test for saving polygons • Demo GateDemo.exe • Any other applications? WKTai/GAME Lab. Blending 39 Remarks • The Blending Equation • Blend Factors • Alpha Test WKTai/GAME Lab. Blending 40
© Copyright 2024 ExpyDoc