Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:33:11+00:00 2026-05-25T17:33:11+00:00

I’m new to using the vertex buffer in XNA. In my project I construct

  • 0

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: https://i.stack.imgur.com/RfagK.png (the intended look), and here’s how they look without: https://i.stack.imgur.com/WyGeI.png

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:33:12+00:00Added an answer on May 25, 2026 at 5:33 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.