I clearly need help with transparency :)

Grand Nagus

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

I'm working on a 2d tile-rendering engine based on Direct3D and quads, and so far it's pretty impressive! (I'm a noob to DirectX, so it doesn't 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 didn't mean to carry on like that, I'm just excited about getting something to actually work! Programming my own game from the ground-up beats the heck out of modding somebody else's work (Quake3, Elite Force)! I can see why you guys have so much fun in what you're doing. :)

Anyways, here's my question...

I'm trying to make the texture on a quad 'fade' out using alpha blending. First, here's some stuff that I'm 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 I'm 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 isn't 'alpha' at all; it's OPACITY! So all I'm doing is affecting the 'opacity' of the texture, not the alpha channel... D'oh!
< 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?):
start.jpg

color_1.jpg

color_3.jpg


What I want (done in PaintShop Pro)(from opaque to transparent):
start.jpg

fade_1.jpg

finish.jpg



I tried to find what I could to change the texture's 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 I'd just replace the above 'color fade' stuff to 'alpha fade'. Do any of you know where the 'alpha opacity' info is stored? I'm sure it's really simple, but I'm still quite the newbie so I'm not sure where to find it.

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

Well, wouldn't 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, it's 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... D'OH! )

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

Here's 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 there's a better way to do this please post it! I'd hate to be mis-informing anybody like I almost did with my botched 'color' information... :(

Well, back to work! :)
 
Back
Top