Memory is full with vertex buffer
- by Christian Frantz
I'm having a pretty strange problem that I didn't think I'd run into. I was able to store a 50x50 grid in one vertex buffer finally, in hopes of better performance. Before I had each cube have an individual vertex buffer and with 4 50x50 grids, this slowed down my game tremendously. But it still ran. With 4 50x50 grids with my new code, that's only 4 vertex buffers. With the 4 vertex buffers, I get a memory error. When I load the game with 1 grid, it takes forever to load and with my previous version, it started up right away. So I don't know if I'm storing chunks wrong or what but it stumped me -.-
for (int x = 0; x < 50; x++)
{
for (int z = 0; z < 50; z++)
{
for (int y = 0; y <= map[x, z]; y++)
{
SetUpVertices();
SetUpIndices();
cubes.Add(new Cube(device, new Vector3(x, map[x, z] - y, z), grass));
}
}
}
vertexBuffer = new VertexBuffer(device, typeof(VertexPositionTexture), vertices.Count(), BufferUsage.WriteOnly);
vertexBuffer.SetData<VertexPositionTexture>(vertices.ToArray());
indexBuffer = new IndexBuffer(device, typeof(short), indices.Count(), BufferUsage.WriteOnly);
indexBuffer.SetData(indices.ToArray());
Thats how theyre stored. The array I'm reading from is a byte array which defines the coordinates of my map. Now with my old version, I used the same loading from an array so that hasn't changed. The only difference is the one vertex buffer instead of 2500 for a 50x50 grid. cubes is just a normal list that holds all my cubes for the vertex buffer.
Another thing that just came to mind would be my draw calls. If I'm setting an effect for each cube in my cube list, that's probably going to take a lot of memory. How can I avoid doing this? I need the foreach method to set my cubes to the right position
foreach (Cube block in cube.cubes)
{
effect.VertexColorEnabled = false;
effect.TextureEnabled = true;
Matrix center = Matrix.CreateTranslation(new Vector3(-0.5f, -0.5f, -0.5f));
Matrix scale = Matrix.CreateScale(1f);
Matrix translate = Matrix.CreateTranslation(block.cubePosition);
effect.World = center * scale * translate;
effect.View = cam.view;
effect.Projection = cam.proj;
effect.FogEnabled = false;
effect.FogColor = Color.CornflowerBlue.ToVector3();
effect.FogStart = 1.0f;
effect.FogEnd = 50.0f;
cube.Draw(effect);
noc++;
}