1
\$\begingroup\$

Let's say I have a background and a set of entities.
I have managed to draw the background into a Texture2D using a RenderTarget
I have also managed to draw all my entities into another RenderTarget

Now I want to take these two Textures and render them on the screen as so:

// Top-level rendering
entitiesSystem.Process();
background.Process();
Texture2D entitiesTexture = entitiesSystem.EntitiesTexture;
Texture2D backgroundTexture = background.BackgroundTexture;

graphicsDevice.SetRenderTarget(null);
graphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(
    backgroundTexture,
    LevelManager.LEVEL_RECTANGLE,
    null,
    Color.White,
    0.0f,
    Vector2.Zero,
    SpriteEffects.None,
    1
);
spriteBatch.Draw(
    entitiesTexture,
    LevelManager.LEVEL_RECTANGLE,
    null,
    Color.White,
    0.0f,
    Vector2.Zero,
    SpriteEffects.None,
    0
);
spriteBatch.End();

Here is how the background system renders the background:

graphicsDevice.SetRenderTarget(BackgroundTexture);
spriteBatch.Begin();
for (int i = 0; i < 2; i++)
    spriteBatch.Draw(textures[i], positions[i], Color.White);
spriteBatch.End();

Almost the same thing happens for the entities: I set the render target to the relevant one (not the same as the one for the background), start the spritebatch (again, not the same one), draw the entities and end the spritebatch.

If I set BlendMode to additive, the result is correct (they blend together). However, if I set to to AlphaBlend, then I can only render one of them (depending on the SpriteSortMode).

I have come as far as understanding that the problem lies in not preserving... something, but I am not sure how to preserve it :P

By the way, I read that multiple render targets are not supported for the XBOX... Soooooooo... what is the alternative of doing what I am doing now?

\$\endgroup\$
1
  • \$\begingroup\$ OK, multiple render targets are supported for XBOX, so forget about that part of the question (source: msdn.microsoft.com/en-us/library/… ) \$\endgroup\$
    – pek
    Aug 19, 2011 at 20:41

3 Answers 3

1
\$\begingroup\$

Is your entities texture created with a format that has an alpha channel, and cleared to transparent before use?

\$\endgroup\$
3
  • \$\begingroup\$ My texture images are PNG files. So I guess they do have alpha channels. As for clearing the transparency, I don't know what you mean. I don't think I do something explicitly; I just draw the textures. \$\endgroup\$
    – pek
    Aug 21, 2011 at 1:47
  • \$\begingroup\$ @pek Sorry, when I wrote 'Texture', what I meant to say was 'Render Target' \$\endgroup\$
    – Adam
    Aug 27, 2011 at 1:37
  • \$\begingroup\$ The Format attribute of the RenderTarget was Color, but I wasn't clearing to Color.Transparent (I didn't even know there was such a color!). So once I did that it worked like a charm! \$\endgroup\$
    – pek
    Aug 27, 2011 at 6:02
0
\$\begingroup\$

In my 2D XNA-based game, I have multiple layers like this too. I just draw the background, then draw the foreground. Using multiple render targets is often reserved for special effects and taking screenshots. Here is some doc on multiple render targets: http://msdn.microsoft.com/en-us/library/bb975911%28XNAGameStudio.31%29.aspx#ID4E4F

\$\endgroup\$
0
\$\begingroup\$

Setting a render target will clear it by default, as this is the only fast path on Xbox 360.

You can adjust this by setting the appropriate RenderTargetUsage.

See this blog post for details.

\$\endgroup\$
1
  • \$\begingroup\$ I added a method for the DeviceCreated event and tested setting all the different options of RenderTargetUsage with no luck. :/ \$\endgroup\$
    – pek
    Aug 20, 2011 at 2:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .