I clearly need help with transparency :)

Grand Nagus

Member
Joined
Jan 7, 2004
Messages
10
Hey guys. I knew Id be back! :) Maybe I have a more challenging question than my last one...

Im working on a 2d tile-rendering engine based on Direct3D and quads, and so far its pretty impressive! (Im a noob to DirectX, so it doesnt take much to impress me. ;) ) It has a particle system that works (something blows up and it throws sparks - REALLY cool effect!), and you can assign a blendfunc to each individual tile instead of the whole shebang so it renders how you want it to look. WOW! ...Sorry, I didnt mean to carry on like that, Im just excited about getting something to actually work! Programming my own game from the ground-up beats the heck out of modding somebody elses work (Quake3, Elite Force)! I can see why you guys have so much fun in what youre doing. :)

Anyways, heres my question...

Im trying to make the texture on a quad fade out using alpha blending. First, heres some stuff that Im gonna reference later in my explanation of my problem:

Code:
// this is my customvertex struct...
struct CUSTOMVERTEX2
{
    float x, y, z, rhw;	// The transformed position for the vertex
    DWORD color;		// The vertex color
	float tu, tv;		// The texture coordinates
};

... 
// LOTS of stuff to create an array of quads
...

// this is a pointer to my struct...
CUSTOMVERTEX2	*pVertices;

...
// locked this vert buffer and actually changing and re-assigning the new color value...
oldColor = pVertices[0].color;
newColor = oldColor + 0x00000001;

if (newColor > 0xFFFFFFFF)
	newColor = 0xFFFFFF00;

pVertices[0].color = newColor;
pVertices[1].color = newColor;
pVertices[2].color = newColor;
pVertices[3].color = newColor;
...


You can see from that last part that Im gradually adding the last value in color from 0x00 (transparent) to 0xFF (opaque) in an effort to fade in this quad. But after several days of research and tinkering, I found out that the last value isnt alpha at all; its OPACITY! So all Im doing is affecting the opacity of the texture, not the alpha channel... Doh!
< color is a dword: 0x00(red) 00(green) 00(blue) 00(opacity) >
When the program with the above code is executed, a pic of a little ghost appears over a background, where it fades from black to ghost texture and repeats... But I want it to fade from totally transparent to totally opaque. :(

What the above code does (from opaque to... black?):
[Broken External Image]:http://www.nagusmaps.com/hotlinx/d3d_probs/start.jpg
[Broken External Image]:http://www.nagusmaps.com/hotlinx/d3d_probs/color_1.jpg
[Broken External Image]:http://www.nagusmaps.com/hotlinx/d3d_probs/color_3.jpg

What I want (done in PaintShop Pro)(from opaque to transparent):
[Broken External Image]:http://www.nagusmaps.com/hotlinx/d3d_probs/start.jpg
[Broken External Image]:http://www.nagusmaps.com/hotlinx/d3d_probs/fade_1.jpg
[Broken External Image]:http://www.nagusmaps.com/hotlinx/d3d_probs/finish.jpg


I tried to find what I could to change the textures alpha opacity but had absolutely no luck... I think my above function would work if I knew where to find and change the alpha info, cause Id just replace the above color fade stuff to alpha fade. Do any of you know where the alpha opacity info is stored? Im sure its really simple, but Im still quite the newbie so Im not sure where to find it.

Thanks in advance for the help! :)
 
Got it fixed!

Well, wouldnt ya know it... Waste 3 days, post for help, and then figure it out within a couple of hours. Sheesh!

Turns out that .color value really DOES have an alpha value... Somebody told me that it was an opacity value, but he was prolly thinking of a paint program as opposed to DirectX. Plus, its an argb value, NOT an rgba value! (I was playing with the last value instead of the first value, which is why the alphafade never happened... DOH! )

Anyways, it goes like this:
< color is a dword: 0x00(alpha) 00(red) 00(green) 00(blue) >

Heres how I got my texture to render progressively more or less transparent:

In your alphachange function, set your new alpha value for this quad:
newColor = oldColor + 0x01000000; // <argb>

In your render function, set your render states like so:
<enable alpha blending>
SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
<texturestate>
SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
<alphastate>
SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTOP_SELECTARG1 );

and set your blendfuncs for this quad to this:
SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

Hope this info helps somebody else, cause it gave me hassles for way too long! I found out how to do this completely by trial and error, so if theres a better way to do this please post it! Id hate to be mis-informing anybody like I almost did with my botched color information... :(

Well, back to work! :)
 
Back
Top