"Dynamic" transparency in DX?

Started by
12 comments, last by MasterWorks 19 years, 3 months ago
I've Googled my @ss off and can't find the answer to this. Making a 2D sprite-based game and been curious about this: So you can make something COMPLETELY transparent by setting a color key, but is there a way to make it PARTIALLY transparent at runtime? Like, could I take an opaque image and make it appear transparent in the game just by changing some property in code? [EDIT] typos ... [/EDIT]
Advertisement
We need more details. Are you using C++? Are you using Direct3D/D3DXSprite?
Lookup alpha blending.

Quote:Original post by jflanglois
We need more details. Are you using C++? Are you using Direct3D/D3DXSprite?


Sorry ... yes and yes.

Quote:Original post by Coder
Lookup alpha blending.


I did ... repeatedly ... All I find is info on complete transparency or mixing texture colors.

Quote:Original post by ldramire
Quote:Original post by Coder
Lookup alpha blending.


I did ... repeatedly ... All I find is info on complete transparency or mixing texture colors.

From the SDK docs, "Alpha blending" page:
Quote:Alpha blending is used to display an image that has transparent or semi-transparent pixels. In addition to a red, green, and blue color channel, each pixel in an alpha bitmap has a transparency component known as its alpha channel. The alpha channel typically contains as many bits as a color channel. For example, an 8-bit alpha channel can represent 256 levels of transparency, from 0 (the entire pixel is transparent) to 255 (the entire pixel is opaque)

You enable alphablending, set D3DRS_SRCBLEND and D3DRS_DESTBLEND (typically to D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA), set the alpha you want on the vertex or the material, and render. The pixel plotted to the frame-buffer is computed using (with the default D3DRS_BLENDOP of D3DBLEND_ADD):
p = source * a + dest * (1 - a)

Where 'dest' is the pixel already in the frame-buffer.

Quote:
From the SDK docs, "Alpha blending" page:
Quote:Alpha blending is used to display an image that has transparent or semi-transparent pixels. In addition to a red, green, and blue color channel, each pixel in an alpha bitmap has a transparency component known as its alpha channel. The alpha channel typically contains as many bits as a color channel. For example, an 8-bit alpha channel can represent 256 levels of transparency, from 0 (the entire pixel is transparent) to 255 (the entire pixel is opaque)


Yeah, I'd read through that section before and it honestly did not make much sense.

Quote:...set the alpha you want on the vertex or the material...


That's the part I'm trying to get at. How I actually set the alpha myself. There's an example there for texture alpha that I honestly don't quite follow (it'd be nice if they actually commented the sample code), but it looks like the basic idea is to loop through each pixel in the texture and set it's alpha value. Is that right? It looks like the sample is making a Texture in code, but will that work with CreateTextureFromFile?

Thanks alot.

[edit] I should also clarify that what I'm trying to get at is making textures go back and forth between levels of transparency, like a ghost fading in and out, or a house becoming almost completely transparent when a character goes behind it, then back to solid when they move back out. I hope that's clearer. [/edit]
Quote:That's the part I'm trying to get at. How I actually set the alpha myself. There's an example there for texture alpha that I honestly don't quite follow (it'd be nice if they actually commented the sample code), but it looks like the basic idea is to loop through each pixel in the texture and set it's alpha value. Is that right? It looks like the sample is making a Texture in code, but will that work with CreateTextureFromFile?

The code in "Texture alpha" documentation does what you describe - it locks the texture and writes alpha for every pixel. You'd need that if you wanted alpha to vary for different parts of the texture. If you just want alpha for the whole texture, you just use vertex or material alpha. For vertex alpha, check the page titled "Vertex Alpha" in the docs. For material alpha check "Material Alpha". In either case, you'll mostly want to select color from your texture, and alpha from your vertex/material. To do this, you can do a TSS setup like this:

// Select color from texture
SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

// Select alpha from vertex/material
SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);

Quote:[edit] I should also clarify that what I'm trying to get at is making textures go back and forth between levels of transparency, like a ghost fading in and out, or a house becoming almost completely transparent when a character goes behind it, then back to solid when they move back out. I hope that's clearer. [/edit]

For this kind of thing, go with material alpha. Locking your buffers to update alpha information won't be optimal.

Thank you very much for all the great info!!

There's no way I'm going to be able to figure all that out anytime soon, but at least now I know what I want is possible and where to start looking. I'll look further into vertexes, materials, and all those alpha states and start toying with it.

And here I was hoping there'd be one pre-built function that would do it for me. [smile]

Thanks again!
I recommend that you check the Forum FAQ, especially the further reading part. Go through drunken hyena's tutorials, and go on your own from there.

Well, if you use the D3DXSprite interface, then the last argument of the Draw method (in DX9 at least) is the modulation color, with 8 bits being for the alpha channel. Use D3DCOLOR_RBGA( 255, 255, 255, 200) -- or I do something like 0xCCFFFFFF because I'm lazy -- for instance to set the alpha lower.

[edit] You have to call Begin with D3DXSPRITE_ALPHABLEND to use alpha blending.


HTH

Regards,
jflanglois

This topic is closed to new replies.

Advertisement