# [MDX] Single Index Buffer with multiple Vertex Buffers

This topic is 3872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to create a fairly large (1000x1000 and above) grid. There's a limit on the size of a single vertexbuffer, so in order to get around it I've created a fixed array of vertexbuffers. I don't really understand indexbuffers so I kept the declaration of the indexbuffer the same as it was when I was using a single vertexbuffer. The problem is that when I render the grid, the edges of the seperate vertexbuffers aren't connected and instead trail off to the top left of the viewport. That's the best case scenario, in the worst case it'll crash and throw an access violation. Here's the declaration/render code (in C#):
private void vertexDeclaration()
{
vb = new VertexBuffer[bufferDivisions];
vertices = new CustomVertex.PositionColored[bufferDivisions][];

for (int bufferNum = 0; bufferNum < bufferDivisions; bufferNum++)
{
vb[bufferNum] = new VertexBuffer(typeof(CustomVertex.PositionColored), (gW * (gH / bufferDivisions)), D3DInterface.m_device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

vertices[bufferNum] = new CustomVertex.PositionColored[(gW * (gH / bufferDivisions))];

float gDW = gW / widthDivisor;
float gDH = gH / depthDivisor;

for (int x = 0; x < gW; x++) //
{
for (int z = 0; z < (gH / bufferDivisions); z++)
{
vertices[bufferNum][x + z * gW].Position = new Vector3((float)(gDW / 2) - gDW + (1 / widthDivisor) * x, 0, (float)(gDH / 2) - gDH + (1 / depthDivisor) * (((gH / bufferDivisions) * bufferNum) + z));
}
}

vb[bufferNum].SetData(vertices[bufferNum], 0, LockFlags.None);
}
}

private void IndicesDeclaration() //not changed since using a single vertexbuffer
{
ib = new IndexBuffer(typeof(int), (gW - 1) * (gH - 1) * 6, D3DInterface.m_device, Usage.WriteOnly, Pool.Default);
indices = new int[(gW - 1) * (gH - 1) * 6];

for (int x = 0; x < gW - 1; x++)
{

for (int z = 0; z < gH - 1; z++)
{
indices[(x + z * (gW - 1)) * 6] = (x + 1) + (z + 1) * gW;
indices[(x + z * (gW - 1)) * 6 + 1] = (x + 1) + z * gW;
indices[(x + z * (gW - 1)) * 6 + 2] = x + z * gW;

indices[(x + z * (gW - 1)) * 6 + 3] = (x + 1) + (z + 1) * gW;
indices[(x + z * (gW - 1)) * 6 + 4] = x + z * gW;
indices[(x + z * (gW - 1)) * 6 + 5] = x + (z + 1) * gW;
}
}

ib.SetData(indices, 0, LockFlags.None);
}

public override void Render(Device myDevice)
{
for (int bufferNum = 0; bufferNum < bufferDivisions; bufferNum++)
{
myDevice.SetStreamSource(0, (vb[bufferNum]), 0);
myDevice.Indices = ib;
myDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices[bufferNum].Length, 0, indices.Length / bufferDivisions / 3);
}
}


I'm guessing the problem lies in either/both the IndicesDeclaration() method / DrawIndexedPrimitives line, but I don't know enough to fix it. Any pointers would be much appreciated. [Edited by - chameleon_789 on June 15, 2007 9:45:17 AM]

##### Share on other sites
Ok, I fixed the artefacts mentioned previously by dividing the indexbuffer into arrays. However, there are gaps in the grid where one buffer ends and the next begins.. how would I go about connecting one buffer to the next? There's probably a simple solution but I've consumed way too much coffee to think rationally today :)

private void vertexDeclaration()        {            vb = new VertexBuffer[bufferDivisions];            vertices = new CustomVertex.PositionColored[bufferDivisions][];            for (int bufferNum = 0; bufferNum < bufferDivisions; bufferNum++)            {                vb[bufferNum] = new VertexBuffer(typeof(CustomVertex.PositionColored), (gW * (gH / bufferDivisions)), D3DInterface.m_device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);                vertices[bufferNum] = new CustomVertex.PositionColored[(gW * (gH / bufferDivisions))];                                float gDW = gW / widthDivisor;                float gDH = gH / depthDivisor;                for (int x = 0; x < gW; x++) //                 {                    for (int z = 0; z < (gH / bufferDivisions); z++)                    {                        vertices[bufferNum][x + z * gW].Position = new Vector3((float)(gDW / 2) - gDW + (1 / widthDivisor) * x, 0, (float)(gDH / 2) - gDH + (1 / depthDivisor) * (((gH / bufferDivisions) * bufferNum) + z));                    }                }                vb[bufferNum].SetData(vertices[bufferNum], 0, LockFlags.None);            }        }        private void IndicesDeclaration()        {            ib = new IndexBuffer[bufferDivisions];            indices = new int[bufferDivisions][];            for (int bufferNum = 0; bufferNum < bufferDivisions; bufferNum++)            {                ib[bufferNum] = new IndexBuffer(typeof(int), (gW - 1) * ((gH / bufferDivisions) - 1) * 6, D3DInterface.m_device, Usage.WriteOnly, Pool.Default);                indices[bufferNum] = new int[(gW - 1) * ((gH / bufferDivisions) - 1) * 6];                for (int x = 0; x < gW - 1; x++)                {                    for (int z = 0; z < (gH / bufferDivisions) - 1; z++)                    {                        indices[bufferNum][(x + z * (gW - 1)) * 6] = (x + 1) + (z + 1) * gW;                        indices[bufferNum][(x + z * (gW - 1)) * 6 + 1] = (x + 1) + z * gW;                        indices[bufferNum][(x + z * (gW - 1)) * 6 + 2] = x + z * gW;                        indices[bufferNum][(x + z * (gW - 1)) * 6 + 3] = (x + 1) + (z + 1) * gW;                        indices[bufferNum][(x + z * (gW - 1)) * 6 + 4] = x + z * gW;                        indices[bufferNum][(x + z * (gW - 1)) * 6 + 5] = x + (z + 1) * gW;                    }                }                ib[bufferNum].SetData(indices[bufferNum], 0, LockFlags.None);            }        }

[Edited by - chameleon_789 on June 15, 2007 10:29:13 AM]

##### Share on other sites
hey, I've got a similar problem, I'm gonna have a look at your code to see if it has my solution.

the edges of your vertex buffer have to match. a small example...

1,2,3,4,5,6
7,8,9,0,a,b
c,d,e,f,g,h

you have split it thus

1,2,3 4,5,6
7,8,9 0,a,b
c,d,e f,g,h

so triangle 3,4,9 doesnt work

split it thus

1,2,3 3,4,5 5,6
7,8,9 9,0,a a,b
c,d,e e,f,g g,h

it uses a bit more space, and is a bit more difficult to work out, but thats how come your edges dont render.

i think...

##### Share on other sites
hey, I've got a similar problem, I'm gonna have a look at your code to see if it has my solution.

the edges of your vertex buffer have to match. a small example...

1,2,3,4,5,6
7,8,9,0,a,b
c,d,e,f,g,h

you have split it thus

1,2,3 4,5,6
7,8,9 0,a,b
c,d,e f,g,h

so triangle 3,4,9 doesnt work

split it thus

1,2,3 3,4,5 5,6
7,8,9 9,0,a a,b
c,d,e e,f,g g,h

it uses a bit more space, and is a bit more difficult to work out, but thats how come your edges dont render.

i think...

##### Share on other sites
hey, I've got a similar problem, I'm gonna have a look at your code to see if it has my solution.

the edges of your vertex buffer have to match. a small example...

1,2,3,4,5,6
7,8,9,0,a,b
c,d,e,f,g,h

you have split it thus

1,2,3 4,5,6
7,8,9 0,a,b
c,d,e f,g,h

so triangle 3,4,9 doesnt work

split it thus

1,2,3 3,4,5 5,6
7,8,9 9,0,a a,b
c,d,e e,f,g g,h

it uses a bit more space, and is a bit more difficult to work out, but thats how come your edges dont render.

i think...