Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

How to make red part of drawn image transparent?

This topic is 5031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm using d3d9, I'm trying to draw a mouse cursor from a bitmap image, I load the image, using 255 red as the transparent part of the image. I then setup some vertices to draw the image using DrawPrimitiveUP. It draws fine but when I put the drawn image over a white square, it shows black pixels where it should be transparent. I was wondering if I am supposed to set any other renderstates aswell to make it transparent? Thanks

Share this post


Link to post
Share on other sites
Advertisement
D3d does not support color keying, hence you cannot set a color to be transparent. However, if you do want to use a color as transparent instead of a alpha channel (maybe the format dosnt support a alpha channel, or you already have a large graphics library that uses colors for transparncy) you can do the following:

After you have loaded the image to a d3d texture or surface, lock the surface. Loop trough each pixel and check its color, if it is the color that i supposed to be transparent, write 100% transparency to the alphachannel of the surface on that location. Then you just turn on alpha blending and render it.

Share this post


Link to post
Share on other sites
Are you sure you can't do it simply?

I'm using a load texture function from d3d9, that has the field colourKey.

The program is on another computer, but I'll post it tomorrow.

Share this post


Link to post
Share on other sites
Well i found that by simply making a texture in photoshop as a PNG and having it exactly as i want it, ie no key for transparancy, it was fine. I don't think i set any render settings.

ace

Share this post


Link to post
Share on other sites
Peter, I don't think that is strictly true. If you load the mouse pointer using D3DXCreateTextureFromFileEx you can specify a colorkey to be transparent, I have used this and it works fine for me.

You will have to set the correct renderstates to take into account the alpha'd texture.

edit: Ace's method will also work if you create the texture with A8R8G8B8, although I think you will need to set some renderstates and texturestages. I sure one of our resident experts will fill you in on exactly what these are before too long!

Kind regards.

Mark Coleman

Share this post


Link to post
Share on other sites
Try setting the colourkey field to 0xFFFF0000. Then call the following:

D3DDevice->SetRenderState(D3DRS_ALPHAFUNC, 5);
D3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, 1);

This might work. It would help if you could post your image loading code.

Share this post


Link to post
Share on other sites
Don't forget to render the cursor after everything else, so what's behind it is also drawn...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!