Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

Render target with alpha channel?

This topic is 3680 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've been trying to draw to a render target (texture) but I can't seem to maintain the alpha transparency in the background. Is it even possible to use a render target with alpha or do you have to clear it? Here is my method for drawing the objects onto the render target. It works okay but the background should be alpha not black. Removing the clear doesn't help either.
public void RenderObjects(Device device, float elapsedTime)
{
	using (Surface backbuffer = device.GetRenderTarget(0))
	{
		device.Transform.View = Global.ViewMatrix;
		device.SetRenderTarget(0, m_surface);
		device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, Color.Black, 1.0f, 0);
		if (OnRenderFrame != null)
			OnRenderFrame(device, elapsedTime);
		device.SetRenderTarget(0, backbuffer);
	}
}

Share this post


Link to post
Share on other sites
Advertisement
I'm a little confused by your terminology here...what do you mean when you say "the background should be alpha"? Do you mean it should have an alpha value of less than 1.0? A render-target texture works just like the back-buffer: you clear it to a certain color and and alpha value and then the pixels that you render to will have their values changed based on the blending mode used. You're clearing the texture to a value of <0, 0, 0, 1>, so that's the value pixels will have by default.

Share this post


Link to post
Share on other sites
I was hoping I could use a texture as a render target for some text that needs to have per pixel scrolling but stay inside a rectangle. So using a render target I could render to a texture and it will clip the graphics outside the rectangle.

The problem is behind the text is a black rectangle which I can't have, it needs to be transparent.

So I'm guessing that render targets have to have a background color so I can't have the render target texture appear over the other graphics rendered behind it. If that's the case what method could I use to clip quads that outside a rectangle?

Share this post


Link to post
Share on other sites
I had a similar issue recently and it took me a while to figure it out.

In DX9 the renderstate for D3DRS_COLORWRITEENABLE doesn't write the alpha unless you tell it to.


m_pd3dDevice->SetRenderState ( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA );



hope this helps

-programmer_tom

Share this post


Link to post
Share on other sites
What format do you create your render target texture in?
How are you drawing it to the backbuffer? StretchRect won't use alpha.
Try saving your render target texture with a D3DXSaveSurfaceToFile call using a format that supports alpha. Does it have alpha values? You should then know which half of the rendering isn't working.

While it would be nice to figure out where your problem lies, just to avoid it in the future, I'd then ask myself if you're approaching the problem the right way. If you're just trying to limit yourself to a rectangle, perhaps you can just SetViewport instead. If you don't want the full scene stretched to the viewport, perhaps SetScissorRect would do what you want.

Share this post


Link to post
Share on other sites
I tried adding the render state:

device.SetRenderState(RenderStates.ColorWriteEnable, (int)(ColorWriteEnable.Alpha | ColorWriteEnable.Red | ColorWriteEnable.Green | ColorWriteEnable.Blue));



But it doesn't seem to make any difference. Still black background for the render target.

Namethatnobodyelsetook: I must have made that post at the same time as yours. I was using the default device render format, so I changed it to A8R8G8B8 and I think I'm getting on the right track now although it's still a black background, I can see through the edges around the text where it's aliased into the background. So I can see there must be a way to do this then, like I have to clear the background with an alpha color or something?

Okay just tried Device.Clear() using Color.FromArgb(0, 0, 0, 0) and it works! Thanks! :)

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!