XNA strange 3D graphics when drawing text

I am working on a 3D spaceship with XNA 3.1

I tried to separate my drawing code from the game logic (this was conceived by XNA, which makes it almost).

I use a special static class that draws my models on the screen ... the main class of the game uses this code when drawing:

protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.Black); // Draws the stage (the skybox and objects like rocks) stageManager.Draw(gameTime, GraphicsDevice, camera); // Draws each player and each Enemy on stage given the camera foreach (Player p in players) p.Draw(camera); foreach(Enemy e in enemies) e.Draw(camera); if(Configuration.Debug) col.renderColBoundings(GraphicsDevice, camera); GraphicHelper.drawOverlayText("50", "10"); // "Error" line... base.Draw(gameTime); } 

But when I draw the text, something strange arises ... Here is the image ( original ): j8p1V.jpg

As you can see, everything looks overlapping (but in place) ... Like the turbines of a spaceship.

The code inside GraphicHelper.drawOverlayText is as follows:

 public static void drawOverlayText(String p1Hp, String currEnemies) { string text1 = "Player1: " + p1Hp; string text2 = "Enemies: " + currEnemies + "/10"; string text3 = "Space Hogs Beta"; spriteBatch.Begin(); // Draw the string twice to create a drop shadow, first colored black // and offset one pixel to the bottom right, then again in white at the // intended position. This makes text easier to read over the background. spriteBatch.DrawString(font, text1, new Vector2(651, 11), Color.Gray); spriteBatch.DrawString(font, text1, new Vector2(650, 10), Color.White); spriteBatch.DrawString(font, text2, new Vector2(851, 11), Color.Gray); spriteBatch.DrawString(font, text2, new Vector2(850, 10), Color.White); spriteBatch.DrawString(font, text3, new Vector2(741, 611), Color.Gray); spriteBatch.DrawString(font, text3, new Vector2(740, 610), Color.White); spriteBatch.End(); } 

And this static class has the following attributes:

 static ContentManager content; static GraphicsDevice graphics; static Camera camera; static Dictionary<String, Model> models = new Dictionary<string, Model>(); static SpriteBatch spriteBatch; static SpriteFont font; public static void initHelper(ContentManager c, GraphicsDevice g, Camera cam) { content = c; graphics = g; camera = cam; spriteBatch = new SpriteBatch(g); font = c.Load<SpriteFont>("Fonts/main"); } 

I hope you help me :)

+4
source share
1 answer

The odd rendering you see is that the depth buffer is disabled. It turns off when using SpriteBatch. This is a known oddity of the XNA 3.1 API. XNA 4.0 at least makes it more obvious that this is happening.

The following is an explanation of which visualization states have been changed by SpriteBatch in XNA 3.1 . And the same goes for XNA 4.0 .

The solution is to basically configure the rendering states to what you want after using SpriteBatch. In this case, at least install:

 GraphicsDevice.RenderState.DepthBufferEnable = true; 

(There may be other states that you want to change back.)

+8
source

All Articles