1
votes

I'm new to using the vertex buffer in XNA. In my project I construct one using a very large amount of vertices. I have been drawing the primitives heretofore with DrawUserIndexedPrimitives(), but I have grown out of that and need more efficiency, so I am trying to figure out the basics of buffers.

I think I've successfully implemented a buffer (and have been very satisfied with the performance improvements), except that all the faces look wrong. Here's how the primitives looked without the buffer: http://i.imgur.com/ygsnB.jpg (the intended look), and here's how they look without: http://i.imgur.com/rQN1p.jpg

Here's my code for loading a vertex buffer and index buffer:

private void RefreshVertexBuffer()
    {
        List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
        List<int> indices = new List<int>();
        //rebuild the vertex list and index list
        foreach(var entry in blocks)
        {
            Block block = entry.Value;
            for (int q = 0; q < block.Quads.Count; q++)
            {
                vertices.AddRange(block.Quads[q].Corners);
                int offset = vertices.Count;
                foreach (Triangle tri in block.Quads[q].Triangles)
                {
                    indices.Add(tri.Indices[0] + offset);
                    indices.Add(tri.Indices[1] + offset);
                    indices.Add(tri.Indices[2] + offset);
                }
            }
        }

        vertexBuffer = new DynamicVertexBuffer(graphics, typeof(VertexPositionNormalTexture), vertices.Count, BufferUsage.None);
        indexBuffer = new DynamicIndexBuffer(graphics, IndexElementSize.ThirtyTwoBits, indices.Count, BufferUsage.None);
        vertexBuffer.SetData<VertexPositionNormalTexture>(vertices.ToArray(), 0, vertices.Count);
        indexBuffer.SetData<int>(indices.ToArray(), 0, indices.Count);

    }

and here is the draw call for plain rendering:

public void Render(Planet planet, Camera camera)
    {
        effect.View = camera.View;
        effect.Projection = camera.Projection;

        effect.World = planet.World;

        foreach (KeyValuePair<Vector3, Block> entry in planet.Geometry.Blocks)
        {
            int blockID = planet.BlockMap.Blocks[entry.Value.U][entry.Value.V][entry.Value.W];
            if (blockID == 1)
                effect.Texture = dirt;
            else if (blockID == 2)
                effect.Texture = rock;
            effect.CurrentTechnique.Passes[0].Apply();
            foreach (Quad quad in entry.Value.Quads)
            {
                foreach (Triangle tri in quad.Triangles)
                {
                    graphics.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList,
                        quad.Corners, 0, quad.Corners.Length, tri.Indices, 0, 1);
                }
            }
        }
    }

...and for the vertex buffer rendering:

public void RenderFromBuffer(Planet planet, Camera camera)
    {
        effect.View = camera.View;
        effect.Projection = camera.Projection;
        effect.World = planet.World;

        graphics.SetVertexBuffer(planet.Geometry.VertexBuffer);
        graphics.Indices = planet.Geometry.IndexBuffer;

        effect.CurrentTechnique.Passes[0].Apply();
        graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, planet.Geometry.VertexBuffer.VertexCount, 0, planet.Geometry.IndexBuffer.IndexCount / 6);
    }

My indices may be off? Or is this due to some quirks with how the graphics device acts with a buffer vs. user indexed?

Edit: It may also have something to do with the way I stitch triangle indices. When I built this project with DrawUserIndexedPrimitives, I ran into some issues similar to this where triangles facing a certain direction would draw on the wrong 'side' (or, the wrong face would be culled). So I came up with this solution:

Triangle[] tris;
        if (faceDirection == ADJACENT_FACE_NAMES.BOTTOM | faceDirection == ADJACENT_FACE_NAMES.OUTER | faceDirection == ADJACENT_FACE_NAMES.LEFT)
        {
            //create the triangles for the quad
            tris = new Triangle[2]{
                new Triangle( //the bottom triangle
                    new int[3] {
                        0, //the bottom left corner
                        1, //the bottom right corner
                        2 //the top left corner
                    }),
                new Triangle( //the top triangle
                    new int[3] {
                        1, //the bottom right corner
                        3, //the top right corner
                        2 //the top left corner
                    })};
        }
        else
        {
            tris = new Triangle[2]{
                            new Triangle(
                                new int[3] {
                                    2, //the top left corner
                                    1, 
                                    0
                                }),
                                new Triangle(
                                    new int[3] {
                                        2,
                                        3,
                                        1
                                    })
                        };
        }
1

1 Answers

1
votes

The problem is that the triangles are being culled. So you have two options to fix this:

  1. You can change the order of the triangle indices.

            foreach (Triangle tri in block.Quads[q].Triangles)
            {
                indices.Add(tri.Indices[1] + offset);
                indices.Add(tri.Indices[0] + offset);
                indices.Add(tri.Indices[2] + offset);
            }
    
  2. You can change your RasterizerState...

    Default rasterizer state is RasterizerState.CullClockwise...

    You can change it to RasterizerState.CullCounterClockwise

EDIT:

this line has a bug:

 graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, planet.Geometry.VertexBuffer.VertexCount, 0, planet.Geometry.IndexBuffer.IndexCount / 6)

should be:

 graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, planet.Geometry.VertexBuffer.VertexCount, 0, planet.Geometry.IndexBuffer.IndexCount / 3)