Saturday, 19. July 2008, 01:46:18
In a
previous article, I showed how you can draw from front to back using OpenGL. Here is the final set of equations that we used for blending. S is the source and D is the destination.
D[B] += S[B] * D[A]
D[G] += S[G] * D[A]
D[R] += S[R] * D[A]
D[A] *= S[A]
Source and destination are in precomputed format. A precomputed format is where you apply the following transformation.
B *= A
G *= A
R *= A
A = 1-A
A on the right hand side is the original Alpha value. All values are relative to 255. So 128 for alpha would actually be 128/255 in all the above equations. D3D takes care of all this for you automatically.
You do not need to convert all your images beforehand to precomputed format. Instead, you can use the fixed function pipeline to precompute your image on the fly.
// Set texture for texture stage 0
pD3DDevice->SetTexture(0,pMyTexture);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE|D3DTA_ALPHAREPLICATE);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE|D3DTA_COMPLEMENT);
pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE);
This should work on ALL video cards no matter how old. Then we would apply the alpha blending equations.
Read more...