0

I try to draw an image on the screen with render target. I used this code:

 _renderTarget = new RenderTarget2D(
                 this._graphicsDevice,
                 this._graphicsDevice.PresentationParameters.BackBufferWidth,
                 this._graphicsDevice.PresentationParameters.BackBufferHeight,
                 false,
                 this._graphicsDevice.PresentationParameters.BackBufferFormat,
                 DepthFormat.None, 0, RenderTargetUsage.PreserveContents);

  _graphicsDevice.SetRenderTarget(_renderTarget);
                _spriteBatch.Begin();

   _spriteBatch.Draw(texture, drawPoint, null, Color.Red, 0.0f
                , new Vector2(texture.Width / 2, texture.Height / 2), 0.5f, SpriteEffects.None, 0                         .0f);
                _spriteBatch.End();
      _graphicsDevice.SetRenderTarget(null);

But, the result image is always black! Could you help me to change the color of this image. Thanks.

hmimo
  • 21
  • 1
  • 4
  • Note that you are rendering to that texture (_renderTarget), but that is not actually being presented on the screen as it is not being rendered to the backbuffer. You are essentially drawing your sprite on a texture that simply has the same properties as the backbuffer. Try setting your render target to null, and don't use _render target. Do you see anything then? – selkathguy Mar 12 '14 at 20:58
  • What exactly are you trying to do here? Render to the screen or render to a texture? And if you are rendering to a texture what do you want to do with it after its ready? If you can answer those you may realise what you're doing wrong. – craftworkgames Mar 12 '14 at 21:16
  • Did you solve your problem? There are several comments and even an answer, and you haven't said anything... – davidsbro Mar 18 '14 at 16:16

1 Answers1

0

From the code shown, _spriteBatch.Draw is only rendering content to _renderTarget.

Next you need to render the resulting RenderTarget2D to your screen so you can see it.

You already have _graphicsDevice.SetRenderTarget(null) in place. You then just need to make a separate SpriteBatch.Draw call passing in your _renderTarget.

You can do this because RenderTarget2D extends Texture2D.

fooser
  • 822
  • 6
  • 14