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

Rendertarget Ignorance

Last post 04-26-2008 3:38 PM by Byron Nelson. 4 replies.
  • 04-26-2008 1:49 PM

    Rendertarget Ignorance

    I am trying to put some text on a texture.   This is my first venture into render targets, and I would think from what I've seen in the help files this little class should work. 

    The put_text function and text_on_texture functions are called in the main Draw().  Put_text works fine.   The text_on_texture function gives my game a purple screen.   Any reference to the correct reading material would be much appreciated.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    public class text_handling
    {
    SpriteFont the_font;
    SpriteBatch batch_o_font;
    RenderTarget2D texture_for_text;
    Texture2D texture_with_text;

    public void load_and_assign(ContentManager content, GraphicsDevice gd)
    {
    texture_for_text = new RenderTarget2D(gd, 300, 300, 1, gd.PresentationParameters.BackBufferFormat);

    texture_with_text = new Texture2D(gd, 300, 300, 2, TextureUsage.None, gd.PresentationParameters.BackBufferFormat);

    batch_o_font = new SpriteBatch(gd);

    the_font = content.Load<SpriteFont>("SpriteFont1");
    }

    public void put_text(string text_to_put, Vector2 position, Color color, GraphicsDevice gd)
    {

    batch_o_font.Begin();
    batch_o_font.DrawString(the_font,text_to_put,position,color);
    batch_o_font.End();

    gd.RenderState.DepthBufferEnable = true;
    }

    public void text_on_texture(string text, Vector2 position, Color color, GraphicsDevice gd)
    {
    gd.SetRenderTarget(0, texture_for_text);

    gd.Clear(Color.Black);

    put_text(text, Vector2.Zero, Color.Yellow,gd);

    gd.SetRenderTarget(0, null);

    texture_with_text = texture_for_text.GetTexture();
    }

    }
    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
  • 04-26-2008 2:22 PM In reply to

    Re: Rendertarget Ignorance

    How are you using texture_with_text after rendering to it?
    New Microsoft DirectX/XNA MVP  (*evil laugh*)
  • 04-26-2008 2:39 PM In reply to

    Re: Rendertarget Ignorance

    I'm doing nothing with it at this point.  I was just trying to compile and run to see if I had everything snytactially sound.

    The main draw loop draws my space game.  Put_text puts up my framerate info. 

    If I drop in the routing to put text on a texture, I get a purple screen.  

    If I comment out everything in that function and just put

    gd.SetRenderTarget(0, texture_for_text);

    //gd.Clear(Color.Black);

    // put_text(text, Vector2.Zero, Color.Yellow,gd);

    gd.SetRenderTarget(0, null);

    // texture_with_text = texture_for_text.GetTexture();

    I get a purple screen when the game runs.

    The code below is my draw function.  Commenting and uncommenting line 28 switches between a nice looking game and a static purple screen.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    General_lighting.Light_effects_light_settings.SetValue(General_lighting.light_parameters_array);

    Warship.draw(camera.view_matrix,camera.projection_matrix,General_lighting.Light_effects,sky.stars);

    graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;


    if (lambda_ray.on)
    lambda_ray.draw(camera.view_matrix, camera.projection_matrix, view_frustrum.corners[3], General_lighting.Light_effects,graphics.GraphicsDevice);

    Laserbolts.draw(camera.view_matrix, camera.projection_matrix, graphics.GraphicsDevice, General_lighting.Light_effects);

    plasma.draw(camera.view_matrix, camera.projection_matrix, General_lighting.Light_effects, graphics.GraphicsDevice);

    if (Warship_shield.impacted)
    Warship_shield.draw(camera.view_matrix, camera.projection_matrix, General_lighting.Light_effects, graphics.GraphicsDevice);

    torpedos.Draw(camera.view_matrix, camera.projection_matrix, General_lighting.Light_effects, graphics.GraphicsDevice);



    Text_handler.put_text(Text_handler.framerateinfo, Vector2.Zero, Color.Yellow, graphics.GraphicsDevice);

    // Text_handler.text_on_texture("what the hell", Vector2.Zero, Color.Yellow, graphics.GraphicsDevice);

    graphics.GraphicsDevice.RenderState.AlphaBlendEnable = false;

    base.Draw(gameTime);

     

     

    Best,

    Byron

    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
  • 04-26-2008 3:22 PM In reply to

    Re: Rendertarget Ignorance

    Answer
    By default, XNA GS is configured to clear the contents of render targets when they are bound, to make render targets consistent across Windows and Xbox.  That's why you get a purple screen when you use your render target.  In general, what you want to do is render all of your render targets, then render the back buffer.

    You can override this behavior to keep render target contents instead of clearing.  You will need to attach an event handler to the PreparingDeviceSettings event in your game's GraphicsDeviceManager instance.  In your event handler, you'll have a PreparingDeviceSettingsEventArgs parameter that you need to modify.

    public MyGame()
    {
    this.graphics.PreparingDeviceSettings += OnPreparingDeviceSettings;
    }

    private void OnPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs args)
    {
    args.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;

    // or....

    args.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PlatformContents;
    }


    PlatformContents is faster, but it won't work right on Xbox.  You'll still get a cleared render target.  If you only care about Windows, then it's fine.
    New Microsoft DirectX/XNA MVP  (*evil laugh*)
  • 04-26-2008 3:38 PM In reply to

    Re: Rendertarget Ignorance

    If the draw is being called over and over, I'm afraid I don't follow. 

    Draw()

    .. starts on backbuffer

    ..draws stuff fine,

    set new rendertarget (what gets cleared at this point?)

    draw to new target

    set back to backbuffer (what gets cleared at this point?)

    repeat..  regardless of the clearing, I would think when draw gets called the next time I'd get my screen.  At worst, I'd expect flickering between purple & my normal screen.   I tried to set the rendertarget to preservecontents when I initialized it, but I get the purple screen.  I'll plug in your code & see what happens.

    texture_for_text = new RenderTarget2D(gd, 300, 300, 1, gd.PresentationParameters.BackBufferFormat,RenderTargetUsage.PreserveContents)

    [EDIT]  I get it now.  When I change from backbuffer to new target, backbuffer gets cleared in purple.  Since that is the last part of my draw function, thats all I see.    If I move the rendertarget rendering to the front of the draw function, then switch to the backbuffer & render the scene normally, it works fine.   Thanks again Shawmishrak, you are always helpful,

    Best,

    Byron

     

    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
Page 1 of 1 (5 items) Previous Next