Search code examples
c#3drendermonogamespritebatch

MonoGame: problem with rendering 3d model and spritebatch


I want to draw some debug info on screen, but this cause problem with rendering model. I found some solutions in the internet, but it dosen't work. Maybe i'm too stupid, but i don't know what's wrong. Here's Draw method:

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.DarkCyan);

        spriteBatch.Begin();
        player.Draw(dt);
        spriteBatch.DrawString(gamefont, "render Time :" + gameTime.ElapsedGameTime.TotalSeconds, new Vector2(0, 85), Color.White);
        spriteBatch.End();

        effect.View = player.view;
        effect.Projection = player.projection;
        effect.Texture = dirtTexture;

        GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
        GraphicsDevice.BlendState = BlendState.Opaque;
        GraphicsDevice.DepthStencilState = DepthStencilState.Default;

        foreach (EffectPass p in effect.CurrentTechnique.Passes)
        {
            p.Apply();
            effect.World = player.world;
            GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, buffer.VertexCount / 3);
        }



        base.Draw(gameTime);
    }

Here how it look like


Solution

  • The Triangles normals (the forward face) are being drawn backwards. This is defined by the order of the vertices in the vertexbuffer.

    Luckily, we can specify the order in the GraphicsDevice. Just add the line:

    GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
    

    If that gives the same result use:

    GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;