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

Rendertarget issue

Last post 8/3/2009 4:34 PM by Catalin Zima. 5 replies.
  • 8/3/2009 12:36 AM

    Rendertarget issue

    Hi,

    EDIT: Scratch what I had before.

    I've a scene being rendered from one angle to a rendertarget, and from another to the actual window.However, when I draw to the rendertarget it renders both to the window *and* the rendertarget. All my code uses:

    graphicsdevice.setrendertarget(0, [relevantrendertarget])

    to change things at the correct places.

    Any ideas what's causing this?

    Thanks,

    Austin K
  • 8/3/2009 5:49 AM In reply to

    Re: Rendertarget issue

    You'll need to provide some more code, so we can look at it, and see what's wrong.
  • 8/3/2009 12:00 PM In reply to

    Re: Rendertarget issue

    Okay! Problem is that there's a *lot* of it at the moment; this is only a small part of it, but I'll try to post the relevant parts:


    Draw code in Game class
                graphics.GraphicsDevice.RenderState.DepthBufferWriteEnable = true
                graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Pink, 1.0f, 0); 
                graphics.GraphicsDevice.SetRenderTarget(0, null); 
    [call to draw the terrain with view matrices, camera info, whether to render water and light direction]
                base.Draw(gameTime); 
     

    Draw code in Terrain class
               Matrix worldMatrix = Matrix.Identity;
    [sets values for the effect file]
     
                effect.Begin(); 
                foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
                { 
                    pass.Begin(); 
     
                    device.Vertices[0].SetSource(terrVB, 0, VertexFourTextures.SizeInBytes); 
                    device.Indices = terrIB; 
                    device.VertexDeclaration = terrVD; 
     
                    int noVertices = terrVB.SizeInBytes / VertexFourTextures.SizeInBytes; 
                    int noTriangles = terrIB.SizeInBytes / sizeof(int) / 3; 
                    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, noVertices, 0, noTriangles); 
     
                    pass.End(); 
                } 
                effect.End(); 
     
                if (drawWater) 
     water.draw(cameraposition, cameraright, cameratarget, Matrix.Identity, viewMatrix, projMatrix, lightdirection); 
     
                roads.draw(sand, device, viewMatrix, projMatrix, lightdirection); 
     
    Initialisation of Rendertarget
                reflectionrendertarget = new RenderTarget2D(device, device.PresentationParameters.BackBufferWidth, device.PresentationParameters.BackBufferHeight, 1, device.DisplayMode.Format); 
     

    Drawing of water
            public void draw( Vector3 cameraposition, Vector3 cameraright, Vector3 cameratarget, Matrix camerarotation, Matrix viewmatrix, Matrix projmatrix, Vector3 lightdirection) 
            { 
    [deals with camera positions etc] 
     
                updatereflection(); 
                #region Actually draw the water 
                Matrix worldMatrix = Matrix.Identity; 
    [effect parameters] 
     
                device.SetRenderTarget(0, null); 
                effect.Begin(); 
                foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
                { 
                    pass.Begin(); 
                     
                    device.Vertices[0].SetSource(waterVB, 0, VertexPositionTexture.SizeInBytes); 
                    device.VertexDeclaration = waterVD; 
                    int noVertices = waterVB.SizeInBytes / VertexPositionTexture.SizeInBytes; 
                    device.DrawPrimitives(PrimitiveType.TriangleList, 0, noVertices / 3); 
     
                    pass.End(); 
                } 
                effect.End(); 
                #endregion 
     
            } 


    Then the part that's actually causing the issue: the rendertargets. These routines are called from the water routine above:
            private void updatereflection() 
            { 
                device.SetRenderTarget(0, reflectionrendertarget); 
                device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0); 
                device.RenderState.DepthBufferWriteEnable = true
     
     
                terrain.drawTerrain(false, cameraright, cameraposition, cameratarget + cameraposition, viewmatrix, projmatrix, lightdirection); 
     
     
                device.SetRenderTarget(0, null); 
            } 


    I initially thought it was something to do with a spritebatch I was using elsewhere, but I've completely disabled that and it's still
    causing the issue.

    The weird thing is that if I put a "device.clear(colorx)" call in the "draw terrain" method, it still draws the terrain to the screen and the rendertarget, but the screen's version will all "colorx" (but the meshes etc are still there)!!!

  • 8/3/2009 2:58 PM In reply to
    • (2342)
    • premium membership MVP
    • Posts 1,226

    Re: Rendertarget issue

    I'm not really understanding your problem here...what exactly is it that you're drawing to your render target that shouldn't be there?  Do you have a screenshot you could show?
    Matt Pettineo | DirectX/XNA MVP


    Ride into The Danger Zone | PIX With XNA Tutorial
  • 8/3/2009 3:56 PM In reply to

    Re: Rendertarget issue

    Yeah, sure... I didn't realise quite how unhelpful my second post was until I reread it just now!

    I'm creating a city; at the moment it renders a terrain (which is mostly flat, except for the hills outside the city and groves for what will be rivers), a network of roads and some bridges which will eventually go over the rivers.

    I'm currently trying to create the rivers, with half-decent looking water. I've found a tutorial on it, done it in a separate project and it all worked fine; so I'm trying to implement it in my city.

    At the moment, the game renders the entire city just to the normal screen output (no rendertargets etc). To get the rivers to reflect, I'm also rendering the city from the "reflected" camera point to a rendertarget (with the different viewmatrix for this different camera). The reflecting part works fine; what's drawn to the reflection rendertarget is perfect.

    *However*, when I draw the city to this rendertarget for the reflection (from this different viewpoint), it *also* appears on the screen! (screenshot 1 below). If i clear the screen, the geometry remains there but it's all purple (screenshot 2)! The rendertargets are fine and only have the things they're meant to have on them.

    Screenshot 1:


    Screenshot 2:


    This is really bugging me now! I don't know what could possibly be causing it anymore; I've tried everything I can think of!
  • 8/3/2009 4:34 PM In reply to

    Re: Rendertarget issue

    you should call updatereflections before any other drawing.

    What you do now is the following:

    Clear
    Draw Terrain
    Set a different RenderTarget ***
    DrawTerrain
    Set to null RenderTarget ***
    Draw Roads


    The problem happens when you set a different RenderTarget (***). This has the effect of clearing the screen to Purple, and discarding the content of the backbuffer.




Page 1 of 1 (6 items) Previous Next