Sign in to follow this  
Odoacer

Transparency with PNGs

Recommended Posts

I'm using .PNGs with D3DXSPRITE because of the alpha channel. The problem is, the transparent background is showing up as a solid red color (which matches the foreground of my texture, an apple). I've already done things like pSprite->Draw(D3DXSPRITE_ALPHABLEND) and so forth. It works, but it changes the transparency of the ENTIRE image not just the background. I think the problem is it's not reading from the alpha channels correctly, although if I open it up in Photoshop, the background is transparent as it should be. So the bottom line is: how do I get the .PNG alpha channel to display correctly using D3DXSPRITE?

Share this post


Link to post
Share on other sites
Post your texture load code, and let's take a look. Most likely you are passing a format with no alpha, try using Format.Unknown

--pinz

Share this post


Link to post
Share on other sites
I've been switching between two loaders:


if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "images/apple.png", &g_pApples)))
return false;

if(FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice, "images/apple.png", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &g_pApples)))
return false;





Both show the same result.

Note that the default CreateTextureFromFile function is the same as what I've called D3DXCreateTextureFromFileEx with, except that the format defaults to D3DFMT_UNKNOWN.

Share this post


Link to post
Share on other sites
Do .PNGs even support alpha channel? If you only need transparency (No partial alpha) you can use the colorkey value in the D3DXCreateTextureFromFileEx function. Make the part of the image you want transparent some unusual color (purple for instance), set the color key to that color, and DX will make all the parts of the image that are that color transparent.

Share this post


Link to post
Share on other sites
Yeah, PNGs have an alpha channel for transparency. That's one of their main features, I would have used bitmaps otherwise.

I tried setting a colorkey to purple but it doesn't really work that well since there's a slight purple outline around my image for some reason and I don't want to have to fix that for every single sprite in my game.

I would still like to get the transparency to work though, I feel it's a more elegant solution. Any ideas?

Share this post


Link to post
Share on other sites
Never mind, I fixed my own problem. Apparently I was calling the Begin() and Draw() functions in different parts of the code, and DX doesn't like that. I moved those calls to the same function and it works fine now. *shrugs*

Thanks for all the help guys. +points!

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