Sign in to follow this  

Render to transparent texture

This topic is 4836 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

I'm currently rendering some text and other things to another off-screen texture which was created using CreateTexture (i.e., NOT loaded from a file). Everything renders to the texture fine and displays fine, except that the black background (the result of Clear()-ing it to black, not any texture or anything) is not transparent, creating a black square that overlaps the other objects on the screen. I would like everything black in the rendered texture to be completely transparent, but haven't been able to figure it out. Basically, I just want a color key for this off-screen texture. As far as I know I can't use any kind of alpha channel setting because, as stated, the texture is not being loaded from any file, and so there isn't anything to add an alpha channel to externally. And CreateTexture() doesn't have a color key parameter the way that CreateTextureFromFileEx() does. Diffused vertex transparency doesn't give the effect I want (makes everything transparent, whereas I just want black to be transparent). This has been asked about before, but (being rather inexperienecd in this area) I didn't fully understand the response. This was the response given by Dexio in a thread from last year (http://www.gamedev.net/community/forums/topic.asp?topic_id=165517):
Quote:
You can create render target in a format that contains alpha channel like D3DFMT_A8R8G8B8, and then, when drawing to that render target you can mask given pixel by setting alpha value to 0 (opaque) or 255 (transparent). Then when the texture is being rendered you can use the alpha test to cut out pixels with alpha != 0 or whatever.
I think I understand the principle here but I don't know how to implement it (which, I suppose, means I probably haven't understood the principle at all). I've set what I think are the relevant render states (ALPHATESTENABLE, ALPHAREF, etc.) and texture stage states, but I get the impression that more is required to manually set the alpha for each pixel (this would seem to be an alpha testing issue). In any case, how do I go about making this non-file loaded texture's black pixels fully transparent? (in C++). Code samples would be very helpful. Thanks for any help. Sayfadeen [Edited by - sayfadeen on September 18, 2004 9:40:11 PM]

Share this post


Link to post
Share on other sites
Using a D3DFMT_A8R8G8B8 will allow you to achieve the effect you want. No need to load the alpha channel from a file, you will write to the alpha channel while rendering on the texture. So as long as you also clear the alpha channel to 0 before rendering, it will be fine.

- Clear the texture to black (0x00000000).
- Render the text (probably using SrcAlpha | InvSrcAlpha).
- Once this is done, render the off-screen texture to the back buffer using SrcAlpha | InvSrcAlpha, using the alpha from the off-screen texture.

Share this post


Link to post
Share on other sites
Quote:
I was stupidly using the D3DCOLOR_XRGB macro which doesn't clear the alpha channel.


actually it does clear the alpha channel, but clears it to 255 (opaque).

Share this post


Link to post
Share on other sites

This topic is 4836 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.

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