[C++ DirectX 9] Rendering to texture

Started by
5 comments, last by r_bewick 13 years, 6 months ago
Hi, I am able to render colours to a texture, from black to white and then draw it. But is it possible to clear a texture so it is completely transparent, and to draw pixels with 0 alpha on it? so that when i draw this texture I can have parts that are completely transparent? like in GIMP where you go colour to alpha or something.

thanks,
r_bewick
Advertisement
Quote:But is it possible to clear a texture so it is completely transparent
Sure thing, just use device->Clear with a transparent color. You can then change the alpha, just make sure your alpha channel will be written to (D3DRS_COLORWRITEENABLE, default is probably on), and make sure you got the blending states right (both for your render target and your final texture draw).
thanks unbird, I tried to clear it a transparent colour, eg. 0x00FFFFFF, but it just cleared it white. So like you said I must have some of my render states wrong. I will try to fix them.
Thanks

EDIT: checked everything and they seemed to be right, so looked at my texture again. The format for my renderable texture was wrong. Changed to D3DFMT_A8R8G8B8 and it worked. Thanks

[Edited by - r_bewick on October 25, 2010 4:56:40 PM]
Your mistake is probably when you're using it, not when you're clearing it. To check if it has the right values you can use GetRenderTargetData and lock and check the memory copy (or save it to disk as a TGA using D3DXSaveTextureToFile and check it in GIMP or whatever).
Thanks for the reply ET3D.


I now cleared my texture so it is completely transparent, and then drew another texture on it. So it is some ground with transparent sky.
I want my player that walks on the ground to be able to dig, so can I also somehow draw over some parts of the image with 0 alpha? by drawing squares that are coloured 0x00FFFFFF in my current state obviously doesn't work because it doesn't draw anything. Unbird, do you mean I can set D3DRS_COLORWRITEENABLE to just alpha before I draw, and then I will be "erase" parts of the image with a transparent colour?

(Ah, yes, sorry, forgot to mention the target's format)

Not sure if I understand you correctly, the phrase "transparent color" is a bit confusing in this context.

The reason you did not get any change with 00FFFFFF is probably you had your alpha blending on something like:

D3DRS_SRCBLEND = D3DBLEND_SRCALPHA
D3DRS_DESTBLEND = D3DBLEND_INVSRCALPHA

A fully transparent (alpha == 0) won't get written, even if writing to alpha is on. You probably want to draw full opaque, i.e. replace the "color" (in this case the alpha) entirely, i.e.

D3DRS_SRCBLEND = D3DBLEND_ONE,
D3DRS_DESTBLEND = D3DBLEND_ZERO

So this will "cut out" a transparent rectangle, if drawing with the above color (alpha = 0).

You can have it even more complex with the state D3DRS_SEPARATEALPHABLENDENABLE, if device caps allow. Never used myself, but it might be of use to you.

I suggest examining the blend modes and do some math to find out if it's even possible what you want to do. If not, you have to resort to shaders.

If you want specific help, post example images (PNG!, they have alpha), or derive a formula.
Quote:
A fully transparent (alpha == 0) won't get written, even if writing to alpha is on. You probably want to draw full opaque, i.e. replace the "color" (in this case the alpha) entirely, i.e.

D3DRS_SRCBLEND = D3DBLEND_ONE,
D3DRS_DESTBLEND = D3DBLEND_ZERO

So this will "cut out" a transparent rectangle, if drawing with the above color (alpha = 0).

yes: this is what I want to do unbird. Thankyou for the reply.

I set my render states to the above, and unfortunately using 0x00FFFFFF does not "cut out" a transparent rectangle, but indeed does nothing. I am able to cut out a black rectangle or any other colour, but I am unable to cut out a transparent one. Please reply. Thanks


EDIT: My bad. This does work, I had alpha testing on by accident :O. I turned it off and that worked fine. Thankyou for all your help, unbird!

[Edited by - r_bewick on October 25, 2010 8:53:14 PM]

This topic is closed to new replies.

Advertisement