Sign in to follow this  
Psychopathetica

Alphablending - Darken Background

Recommended Posts

Im trying to use alphablending to be able to darken whatevers behind my polygon. So far I only know how to make things lighter behind it using D3DBLEND_ONE as the source blend, and D3DBLEND_SRCALPHA as the destination blend. I played around with the values with no luck. Is there a way to alphablend a polygon to darken whatevers behind it? A prime example would be in this image here:

[EDIT: ...if the image uploads o.o]

Share this post


Link to post
Share on other sites
I'm guessing, given that you're only making things brighter so far that you're not setting the alpha colour of the source pixel and just using 1.0? Use D3DBLEND_SRCALPHA as the source and D3DBLEND _INVSRCALPHA for the destination. This lets you control the transparency with the source pixel's alpha colour, so to make things darker you'd use a black source, with an alpha colour of, say, 0.5 (which would be half-transparent). You can also make things lighter without changing the blending state by using a white source.

Share this post


Link to post
Share on other sites
Thanks for the reply. I tried changing the values of the polygons alpha to whatever I wanted using D3DColorRGBA(), and using D3DBLEND_SRCALPHA as the source and D3DBLEND_INVSRCALPHA as the destination but it didnt blend at all no matter what values I used. I even had the Alphatestenable set to false. Is there something else I needed to do?

Share this post


Link to post
Share on other sites
Ok. I made a sample program with all the possible D3DBLEND methods and even made the arrow keys control the alpha on one 2D polygon, and the A and Z control the alpha on the other 2D polygon behind it. The alpha values do nothing no matter what setting i have it on. And I would like the alpha changes to show something. However the blending methods for either polygon work depending on what i have it set on. Im not an expert at this and I use to have working code before my harddrive got wiped couple years ago and I lost it. But i think I have to add extra setting if my memory serves me correct to change the alphas. After doing some research there was a number of possibilities but for some reason none of em worked. Like some of the solutions given off this post:

http://www.gamedev.net/topic/280712-how-do-you-control-how-much-your-polygons-are-see-through-in-directx8/

None worked. I even tried adding this in:

Direct3D_Device.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE

Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE


And the alphas still wont do nothing to the image. In a sense I am trying to control how see through it is, but darkening behind it. However it would be nice to have things fade away as well, or fade in from invisible to visible. If anyone has a sample program of some sort that might help bump me in the right direction or ideas that would be nice.

Share this post


Link to post
Share on other sites
kauna    2922
Can you show the whole code? Does your drawing code draw anything actually? Did you enable (alpha)blending?

[color=#1C2837][size=2]D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA are correct blending modes.[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]Alphatest should be disabled as your did.[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[size="2"][color="#1c2837"]The desired alpha value should be placed in the vertices diffuse color value.[/color][/size]
[size="2"][color="#1c2837"]
[/color][/size]
[size="2"][color="#1c2837"]Cheers![/color][/size]
[size="2"][color="#1c2837"]
[/color][/size]

Share this post


Link to post
Share on other sites
Holy cow I figured it out. It was a code issue in my code and I found out this works!!! Problem solved :)

Direct3D_Device.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE

'//Set the alpha to come completely from the diffuse
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG2, D3DTA_TEXTURE

Direct3D_Device.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
Direct3D_Device.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
Direct3D_Device.SetRenderState D3DRS_BLENDOP, D3DBLENDOP_ADD

Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, True

Share this post


Link to post
Share on other sites
Ran into one itty bitty problem with it though. I cant use these to remove the backgrounds of sprites anymore:

Direct3D_Device.SetRenderState D3DRS_ALPHAREF, 255
Direct3D_Device.SetRenderState D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL
Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE, True

How can I combine what I have to change the alphas, and and the same time be able to remove the backgrounds of the sprites?

[EDIT]

Nvm found that this works better lol cause it allows both background removal and alphablending and fading from invisible to opaque however I want. Funny how theres not much documentation on this fading stuff. Problem solved again!

Direct3D_Device.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE

Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE

Direct3D_Device.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
Direct3D_Device.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
Direct3D_Device.SetRenderState D3DRS_BLENDOP, D3DBLENDOP_ADD

Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, True



For that code to work DO NOT use these

Direct3D_Device.SetRenderState D3DRS_ALPHAREF, 255
Direct3D_Device.SetRenderState D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL
Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE, True

Be sure Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE is set to False

Share this post


Link to post
Share on other sites
kauna    2922
[quote name='Psychopathetica' timestamp='1306688148' post='4817154']
Ran into one itty bitty problem with it though. I cant use these to remove the backgrounds of sprites anymore:
[/quote]

What do you mean? It is certain that there isn't one blending combination that will work for all your different rendering requirements.

You can and you should use different blending operations for your sprites and different for drawing UI elements etc.

So, when you start drawing sprites, you may use alphatesting, you just need to disable it for drawing your UI elements.

Cheers!


Share this post


Link to post
Share on other sites
Dont worry I solved the problem and shown it in the [EDIT] shown above. Also on top of me solving my problem, with darkening backgrounds and going from invisible to opaque, this chunk of code also managed to improve and sharpened up my graphics and text, which is also what it needed:

Direct3D_Device.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE

Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this