4

All I want is to clear my renderTarget2D once so that it starts off compeltely transparent, and then preserve it's contents between frames.

I draw the renderTarget texture after I draw the background texture, so I don't want it to paint over. However Clear(Color.Transparent) it shows up as an opaque purple, which as I understand is the default clear color...

What am I doing wrong? I tried changing the SurfaceFormat parameter in the constructor but that had no effect. what am I doing wrong?

// instantiate renderTarget to preserve contents
renderTarget = new RenderTarget2D(GraphicsDevice,
                                   GraphicsDevice.PresentationParameters.BackBufferWidth,
                                   GraphicsDevice.PresentationParameters.BackBufferHeight,
                                   false,
                                   GraphicsDevice.PresentationParameters.BackBufferFormat,
                                   DepthFormat.Depth24,
                                   0,
                                   RenderTargetUsage.PreserveContents);
// clear with transparent color
GraphicsDevice.SetRenderTarget(Globals.renderTarget);
GraphicsDevice.Clear(Color.Transparent);
GraphicsDevice.SetRenderTarget(null);
kbirk
  • 3,906
  • 7
  • 48
  • 72
  • 2
    Silly question but is `renderTarget` and `renderTarget1` a typo? – George Duckett Mar 29 '12 at 15:58
  • yes that was a typo when converting the code here! oops – kbirk Mar 29 '12 at 16:05
  • 1
    Why do you use RenderTargetUsage.PreserveContents if the first thing you are going to do is clear it? PreserveContents is by far the slowest RenderTargetUsage. – ClassicThunder Mar 29 '12 at 16:13
  • I'm using it for trail effects, so I want the texture to start off completely transparent and preserve its contents as they are rendered to it. Is there another way to instantiate it so that it starts completely transparent? – kbirk Mar 29 '12 at 16:17
  • 2
    The GraphicsDevice.Clear(Color.Transparent); does clear the RenderTarget as you are expecting. However, PreserveContents only preserves the contents when the rendertarget is changed such as i GraphicsDevice.SetRenderTarget(null). In all RenderTargetUsages the RenderTarget remain unaffected until its spawwed out. – ClassicThunder Mar 29 '12 at 16:38
  • 1
    PS. If you want to talk about how render targets work I'm over on the GameDev Chat http://chat.stackexchange.com/rooms/19/game-development – ClassicThunder Mar 29 '12 at 16:38

1 Answers1

3

RenderTarget2D is used to draw things off screen which brings up the fact that your code sample is drawing nothing to the screen. Use the sprite batch to actually draw the RenderTarget2D to the back buffer which will effect the display.

Another problem being that by drawing a fully transparent RenderTarget2D is you are not going to change anything on screen so even if you code was working creating a rendertarget, clearing it with transparency, and drawing it effects nothing on the screen.

Below is an example of using a render target then drawing that rendertarget on screen. You usually don't want to use rendertargets unless you have very costly drawing operations that are static so you can render them once to the rendertarget and reuse them. Or bacause you wan't to isolate a scene to run a shader on.

        _renderTarget = new RenderTarget2D(
            GraphicsDevice, 
            (int)size.X, 
            (int)size.Y);

        GraphicsDevice.SetRenderTarget(_renderTarget);
        GraphicsDevice.Clear(Color.Transparent);

        SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);

        //draw some stuff.

        SpriteBatch.End()

        GraphicsDevice.SetRenderTarget(null);

        GraphicsDevice.Clear(Color.Blue);

        SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);

        SpriteBatch.Draw(_renderTarget, Vector2.Zero, Color.white);

        SpriteBatch.End()
ClassicThunder
  • 1,896
  • 16
  • 25