Skip to main content
gamedev.net
🔒 Locked

Texture Alpha

Started by marltoro 6 replies 850+ views
Original Post
marltoro
marltoro
How would I make a certain color in my texture invisible? For example, if I wanted the color 255 Red 0 Green and 255 Blue to be invisible in my program, how would I do that?
zerothrillz
zerothrillz

How would I make a certain color in my texture invisible? For example, if I wanted the color 255 Red 0 Green and 255 Blue to be invisible in my program, how would I do that?


What API(s) are you using?
marltoro
marltoro
DirectX 9
Buckeye
Buckeye
Take a look at D3DXLoadTextureFromFileEx. It provides for a transparent "black" color specified by a colorkey.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
marltoro
marltoro
Ok I found a D3DXCreateTextureFromFileEx function but It will only change the 255 Red, 0 Green and 255 Blue to black. How do I make it a transparent color instead?

D3DXCreateTextureFromFileEx(D3D9Dev, InactiveSprite, 512, 256, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &Inactive_Sprite);
Buckeye
Buckeye
How do I make it a transparent color instead?[/quote]
The documentation may not be very clear. Note that it says it replaces the colorkey with "transparent black" - that's ( 0,0,0,0 ). Black with a zero alpha.

Also, since you'll be rendering for transparency (alpha blending), you should probably use D3DFMT_A8R8G8B8.

EDIT: also, you may want to try a color key of D3DCOLOR_ARGB(255,255,0,255).

EDIT2: I'm not sure how you intend to use the texture, but you might want to consider D3DPOOL_MANAGED.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
marltoro
marltoro
Alright then, thanks for the help
marltoro
marltoro
For anyone else who has this problem, you have to use this code before you render the scene
// Alpha blending
D3D9Dev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); // turn on the color blending
D3D9Dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); // set source factor
D3D9Dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); // set dest factor
D3D9Dev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); // set the operation

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.