Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to generate a 64*64 grid with each cell being 32*32 wide.

The following code works perfectly but I was wondering whether it can be further optimized when creating the index buffer for it.

I've been searching the web for an algorithm that would generate indices and optimize a buffer but without much success ...

var vector3s = new[]
    {
        new Vector3(0, 0, 0),
        new Vector3(1, 0, 0),
        new Vector3(0, 1, 0),
        new Vector3(1, 1, 0),
    };

List<VertexPositionTexture> positions = new List<VertexPositionTexture>();
for (int y = 0; y < 64; y++)
{
    for (int x = 0; x < 64; x++)
    {
        foreach (var vector3 in vector3s)
        {
            var translation = Matrix.CreateTranslation(new Vector3(x, y, 0));
            var transform = Vector3.Transform(vector3, translation);
            var multiply = Vector3.Multiply(transform, 32.0f);
            VertexPositionTexture vpt = new VertexPositionTexture(multiply, new Vector2(vector3.X, vector3.Y));
            positions.Add(vpt);
        }
    }
}

Output sample :

{Position:{X:0 Y:0 Z:0} TextureCoordinate:{X:0 Y:0}}
{Position:{X:32 Y:0 Z:0} TextureCoordinate:{X:1 Y:0}}
{Position:{X:0 Y:32 Z:0} TextureCoordinate:{X:0 Y:1}}
{Position:{X:32 Y:32 Z:0} TextureCoordinate:{X:1 Y:1}}
{Position:{X:32 Y:0 Z:0} TextureCoordinate:{X:0 Y:0}}
{Position:{X:64 Y:0 Z:0} TextureCoordinate:{X:1 Y:0}}
{Position:{X:32 Y:32 Z:0} TextureCoordinate:{X:0 Y:1}}
{Position:{X:64 Y:32 Z:0} TextureCoordinate:{X:1 Y:1}}

For instance, positions at 1 and 4 are identical but their texture coordinates are not.

What are the options for optimizing it and how does one generate the associated index buffer ?

Should positions be separated from texture coordinates and each have an index buffer ?

EDIT

Actually texture coordinates are wrong but it's not really relevant for the question.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Depending on how it's textured you could wrap the texture, and use UV coordinates outside of the 0-1 range. That might allow you to avoid duplicating vertices due to UV mismatches.

You definitely want an index buffer - hardware runs much quicker when you have one due to the post transform cache. It's rare that you want to automatically generate one by looking for duplicated vertices though - normally you know during generation what's going to be duplicated.

Other than that the main thing to do is optimize for the pre and post transform caches in the hardware. There's a good implementation of the post transform cache optimization process at: http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html

For the pre transform cache simply reorder the vertex buffer and renumber the index buffer so that vertices are roughly linear in memory (i.e. the index buffer starts from zero and is mostly sequential).

share|improve this answer
    
Thank you, it's probably overkill for my needs (only that 'track' grid for a mario kart clone) but good reading nonetheless :) –  Aybe Jul 19 '13 at 15:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.