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

Terrain Rendering

Last post 8/27/2009 8:41 AM by jmitch18. 6 replies.
  • 8/26/2009 8:58 PM

    Terrain Rendering

    Hi, I've recently made the move from 2D XNA to 3D. I've been playing around trying to create a basic 3D world as practice; I just plan to have a camera, some terrain and an avatar.

    I took a look at the generated geometry sample found at http://creators.xna.com/en-GB/sample/generatedgeometry and have been attempting to use it along with my own camera I made.  I've been having two problems; the first is a strange kind of stretching of the texture (http://www.jason-mitchell.com/images/textureStretch.JPG) and the second is that some of my terrain is appearing see through and terrain that should be blocked from site is visible (http://www.jason-mitchell.com/images/renderError.JPG).

    The TerrainProcessor has been copied exactly from the sample and I have also tried a solution from a book which has the exact same problems which leads me to think that the problem lies elsewhere.

    Here is my drawing code:

            protected override void Draw(GameTime gameTime)  
            {  
                GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1, 0);  
     
                DrawTerrain(camera.ViewMatrix, camera.ProjectionMatrix);  
     
                base.Draw(gameTime);  
            }  
     
            private void DrawTerrain(Matrix view, Matrix projection)  
            {  
                foreach (ModelMesh mesh in terrain.Meshes)  
                {  
                    foreach (BasicEffect effect in mesh.Effects)  
                    {  
                        effect.View = view;  
                        effect.Projection = projection;  
     
                        effect.EnableDefaultLighting();  
                    }  
     
                    mesh.Draw();  
                }  
            } 

    As mentioned previously,  the terrain processor code is exactly the same as in the Generated Geometry sample.

    Does anyone know why this happens and how to solve it?

    Thanks
  • 8/26/2009 9:04 PM In reply to

    Re: Terrain Rendering

    Answer
    Reply Quote
    It looks like your texture wrap and depth buffer render states are being set to something other than what you want. Are you drawing using a SpriteBatch or setting render states manually? See this if you are.
  • 8/26/2009 9:13 PM In reply to

    Re: Terrain Rendering

    Ah, I had a game component that I was using to render debug information to the screen using a SpriteBatch, once I removed this it works perfectly!  From what I've read on your link, if I am combining the two, then I will need to set the render states manually?
  • 8/26/2009 10:33 PM In reply to

    Re: Terrain Rendering

    hi jmitch,

    yes.  As I gather, this is a pretty common issue when getting started with XNA.  All the changes you make to the renderstates are 'stored' on the graphics device and used when rendering, and they remain until you set them to something else (and I think they are 'reset' to defaults when present is called on the device?).  This means if you render an object to the screen after you have used the spritebatch to render something, then you will have to change all the renderstates back.

    You can do this in a few ways, such as:

    • Either manually change the renderstates back to the 'defaults' after using the sprite batch.
    • Use the RenderState class to store all the renderstates before you use the SpriteBatch, and then set them back on the device after you have finished using the SpriteBatch.
    • Or before you draw any objects with the GraphicsDevice, explicitly set the renderstates used when rendering the 'object'.


  • 8/26/2009 11:01 PM In reply to

    Re: Terrain Rendering

    I did quite a bit of searching and didn't really come up with this information anywhere else.  I couldn't find anywhere that told me straight up about it; all the tutorials and samples that served as an introduction to 3D XNA that I had looked at didn't mention this.

    I modified my draw code to the following so it would work along with the SpriteBatch in my game component:

            protected override void Draw(GameTime gameTime)  
            {  
                GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1, 0);  
     
                GraphicsDevice.RenderState.DepthBufferEnable = true;  
                GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;  
                GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;  
     
                DrawTerrain(camera.ViewMatrix, camera.ProjectionMatrix);  
     
                base.Draw(gameTime);  
            } 

    This works fine for the purposes of what I'm doing, but I would imagine the better way to go about this would be to use your second suggestion.
  • 8/26/2009 11:18 PM In reply to

    Re: Terrain Rendering

    hi jmitch,

    actually, although the second method will restore all renderstates to a previous state, this is overkill, and changing renderstates does impact performace.  

    As an exaggeration, suppose the SpriteBatch only changes 4 renderstates.  The second method would backup all renderstates, which is a waste.  I would only recommend the second option when you are trying to find out if you have a problem with your renderstates.  It sounds like you are doing it the correct way now.  I think it isn't mentioned in books etc because it isn't a problem useless you explicitly change them yourself (either manually or with other classes such as SpriteBatch).

    When I have a drawableGameComponent or something that draws to the screen, I usually just add a function SetRenderStates(GraphicsDevice device) which I call before rendering which makes sure that the main renderstates are set as I need them.

    -John 
  • 8/27/2009 8:41 AM In reply to

    Re: Terrain Rendering

    That's a very good point!  I will probably continue doing it as I am now then, or create a method to set the render states as you have done. Thanks a lot!
Page 1 of 1 (7 items) Previous Next