Skip to main content
added 99 characters in body
Source Link
aybe
  • 815
  • 7
  • 23

I need to generate a 6464 grid with each cell being 3232 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.

I need to generate a 6464 grid with each cell being 3232 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 ?

I need to generate a 6464 grid with each cell being 3232 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.

Source Link
aybe
  • 815
  • 7
  • 23

Optimize a vertex buffer and generate its associated index buffer

I need to generate a 6464 grid with each cell being 3232 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 ?