1

This is my first 3D application that I have created so sorry if this seems like such a simple question but I have searched the internet and these forums to try and find an answer.

I am attempting to draw a simple string to the screen using the spriteBatch.DrawString command.

The application is similar to Minecraft with a large quantity of cubes on the screen. To sort out lag issues all the cubes are created via vertices and hardware instancing has been implemented.

The issue is when ever I call spriteBatch.Begin() all the other cubes appear differently. I am aware that spritebatch changes some states so the following lines have been added

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;

Bellow is the code used to draw the player

public void Draw(Matrix view, Matrix projection)
    {
        effect.CurrentTechnique = effect.Techniques["TexturedNoShading"];
        effect.Parameters["xView"].SetValue(view);
        effect.Parameters["xProjection"].SetValue(projection);
        effect.Parameters["xWorld"].SetValue(world);
        effect.Parameters["xTexture"].SetValue(texture);

        device.SetVertexBuffer(myVertexBuffer);
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
        }
    }

This is the code used in the main class to draw everything

        RasterizerState rs = new RasterizerState();  
        rs.CullMode = CullMode.None;
        rs.FillMode = FillMode.Solid;
        device.RasterizerState = rs;

        GraphicsDevice.BlendState = BlendState.Opaque;
        GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        _map.Draw(_camera.GetCamera(), projection);
        _player.Draw(_camera.GetCamera(), projection);


        spriteBatch.Begin();
        spriteBatch.DrawString(Text, "test", new Vector2(100, 100), Color.White);
        spriteBatch.End();

I would like to post screenshots of the problem to show it more clearly but as this is my first post I do not have the rights to post images. I'm happy to email the images to people.

I'm happy to include any other information but it seems like such a simple problem.

Thanks for any help that you may be able to provide.

Sam Vickery

1 Answers1

0

I can see where you set your vertex buffer, but can't see you setting the index buffer anywhere?

it would normally be right around where you set your vertexbuffer. something like this:'

device.SetVertexBuffer(myVertexBuffer);
device.Indices = myIndexBuffer;
  • Yeah I set the data in a method which is exactly as you have it – user1668662 Feb 27 '13 at 14:53
  • It is called in the LoadContent method – user1668662 Feb 27 '13 at 16:19
  • Sorry I misunderstood what you were saying and you were correct. I didn't change the index buffer. Thanks so much for the help its been driving me crazy :) – user1668662 Feb 27 '13 at 21:17
  • No problem, remembering al lthe things you need to do before doing a draw-call is something everyone struggles with in the beginning :) I do it every time it has been a while since I have been programming anything game-related :P – Stig-Rune Skansgård Feb 28 '13 at 08:09