XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

RenderTargets in GameComponent

Last post 05-21-2008 10:29 AM by XTatic. 5 replies.
  • 05-16-2008 2:42 AM

    RenderTargets in GameComponent

    i have a game component that draws a 3d axis like this one

    now i want to render this component to a rendertarget and displayit in a sprite. so i have this

    ....

    RenderTarget2D renderTarget;

    ....

    renderTarget = new RenderTarget2D(Game.GraphicsDevice, Width, Heigth, 1, SurfaceFormat.Color);

    ....

    RenderTarget cTarget = Game.GraphicsDevice.GetRenderTarget(0);
    Game.GraphicsDevice.SetRenderTarget(1, renderTarget);

    ... all my draw code ...

    Game.GraphicsDevice.SetRenderTarget(0, cTarget as RenderTarget2D);

    but all i get is a purple screen.

    i`m not quite sure what i`m missing here.

  • 05-16-2008 4:00 AM In reply to

    Re: RenderTargets in GameComponent

    Rendertarget Changes in XNA Game Studio 2.0 by Shawn should solve this mystery.
    We are boki. The rest is known.
    The not so known part of the rest: It is Björn or Bjoern, but never Bjorn.
    Björn does not stalk John Sedlak
  • 05-16-2008 5:02 AM In reply to

    Re: RenderTargets in GameComponent

     {
    Game.GraphicsDevice.SetRenderTarget(0, renderTarget);

    Game.GraphicsDevice.VertexDeclaration = Game.GraphicsDevice.VertexDeclaration = new VertexDeclaration(Game.GraphicsDevice, VertexPositionColor.VertexElements);
    Game.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColor.SizeInBytes);

    lineEffect.Begin();
    foreach (EffectPass pass in lineEffect.CurrentTechnique.Passes)
    {
    pass.Begin();

    lineEffect.Parameters["worldViewProj"].SetValue(view * projection);

    renderTarget.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 11);

    pass.End();
    }
    lineEffect.End();

    Game.GraphicsDevice.SetRenderTarget(0, null);

    spriteBatch.Begin();

    spriteBatch.Draw(renderTarget.GetTexture(), new Vector2(50, 50), Color.White);

    spriteBatch.End();

    base.Draw(gameTime);

    }



    thats the draw method of my worldaxis component.

    i still getting a purple screen, the shawn post explains the diferences pretty well.

    i dont really see whats wrong in the code.

    them in the game.cs file i do Axis = new axis(this,64,64); and Components.Add(Axis); in the constructor.

  • 05-16-2008 7:39 AM In reply to

    Re: RenderTargets in GameComponent

    As soon as a RenderTarget is set on the device, it is cleared to purple (IIRC, at least), so you have to clear the device to your color of choice after the SetRenderTarget call. Or create your RT's with the Preserve flag.

    We are boki. The rest is known.
    The not so known part of the rest: It is Björn or Bjoern, but never Bjorn.
    Björn does not stalk John Sedlak
  • 05-19-2008 1:22 AM In reply to

    Re: RenderTargets in GameComponent

    This happens when you switch RenderTargets mid-Draw. As Bjorn said, the original render target (back buffer) gets cleared.

    If possible, draw to your render target before your Draw loop starts, otherwise when your game starts, you need to recreate the default RenderTarget to use RenderTargetUsage.Preserve.

    --
    Ruina et Stragos
    XNA SA
    --
  • 05-21-2008 10:29 AM In reply to

    Re: RenderTargets in GameComponent

    I think you might be over complicating this issue. You are drawing to render target index 1 there. Are you using MRT, is your shader outputting at least to COLOR0 and COLOR1? If not you probably want to set the render target at index 0, not 1.
    X-Tatic
    Blade3D Developer
Page 1 of 1 (6 items) Previous Next