RenderState question

Started by
4 comments, last by GambitSnax 18 years, 4 months ago
Could someone please explain to me what D3DRS_SRCBLEND and D3DRS_DESTBLEND do and their relation to D3DRS_ALPHABLENDENABLE. I've searched the MSDN but they don't cover it in much detail and it seems like such a basic question that I can't find any details about it on the forum. Cheers
Advertisement
with my limited understanding I know this: Their settings are required to turn on transparency in textures. typical usage in c#:

[source lang=c#]            this.device.RenderState.SourceBlend = Blend.SourceAlpha; //set the source to be the source's alpha            this.device.RenderState.DestinationBlend = Blend.InvSourceAlpha; //set the destination to be the inverse of the source's alpha            this.device.RenderState.AlphaBlendEnable = true; //enable             this.texture = TextureLoader.FromFile(device, "tex.bmp");
Hi,

those parameters define how you will do alpha blending. Alpha blending is used to blend the color of a texture to what is currently on screen. For example, you draw the bottom of a lack with a sand texture, and then you render a semi-transparent water texture. The result of alpha blending allow you to see the sand throught the water texture.

Now, D3DRS_SRCBLEND tells the graphic card how to handle the input texture (in my example, the water texture) and D3DRS_DESTBLEND tells how to handle the pixels already on screen (the bottom of the lack which is already drawn on screen)

For example, is you use :

SetRenderstate(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRenderstate(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

The resulting color will be : (input texture color) * (input texture alpha) + (screen pixel color) * (1 - input texture alpha)

The thing to remember : SRC is the current texture, and DEST is the pixel into which is drawn. So SRCALPHA is the alpha of the water texture, and INVSRCALPHA is the inverse of this value.

I hope this helps a little ^^
Ahhh that makes a little more sense now. Does that mean though, that when I render the primary object (eg. a mirror) alpha blending doesn't need to be turned on as its not blending with anything when it is initially being drawn? I only need to turn on alpha blending when I am rendering the secondary objects (eg. anything reflected in the mirror). Or does it check to see if the primary object allows alpha blending?
Alpha blending apply only to the object currently being drawn. After something has been drawn on the screen, it's just a bunch of pixels, with RGB color, alpha, and a depth (simply put ^^)

In my lake example, the bottom of the lake doesn't need alpha blending since it's not transparent. Only the water needs alpha blending to let the user see through it.

Notice that the order in which you draw things is important. In this case, you must draw the bottom of the lake before the water.
Ahhh excellent, makes complete sense now. Cheers :)

This topic is closed to new replies.

Advertisement