## Posts Tagged ‘**game**’

## Abandoned: Terrain

I’m building a little game called Abandoned, and I’ve just taken the first step and built the terrain. I used Shawn’s excellent article about detail textures found here. It’s just a simple terrain, but the colors and the fog settings have a huge impact of the atmosphere in the game. Even though it’s flat, I’ve built the vertices and indices as a little grid, maybe I decide later to have a heightmap or something. I’ve encountered one problem so far, and that’s poor texture quality. Then I remembered something about mipmaps and turned on their generation on the content pipeline options, and the problem disappeared. The result:

I wish to share my terrain generation function that I’ve been using for a while now (I probably stole it from somewhere, I just can’t remember where). It’s good for heightmaps and flat terrain alike. The size is an int because its the number of rows and columns, size of CellSize in the terrain grid.

void Build(int Size, float CellSize)

{

Debug.WriteLine(“Building ground vertices and indices. Size: ” + Size + “, CellSize: ” + CellSize + “.”);

vertices = new VertexPositionTexture[(Size + 1) * (Size + 1)];

indices = new int[Size * Size * 6];

for (int i = 0; i < Size + 1; i++)
{
for (int j = 0; j < Size + 1; j++)
{
VertexPositionTexture vert = new VertexPositionTexture();
vert.Position = new Vector3((i - Size / 2.0f) * CellSize, 0, (j - Size / 2.0f) * CellSize);
vert.TextureCoordinate = new Vector2((float)i / Size, (float)j / Size);
vertices[i * (Size + 1) + j] = vert;
}
}
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
indices[6 * (i * Size + j)] = (i * (Size + 1) + j);
indices[6 * (i * Size + j) + 1] = (i * (Size + 1) + j + 1);
indices[6 * (i * Size + j) + 2] = ((i + 1) * (Size + 1) + j + 1);
indices[6 * (i * Size + j) + 3] = (i * (Size + 1) + j);
indices[6 * (i * Size + j) + 4] = ((i + 1) * (Size + 1) + j + 1);
indices[6 * (i * Size + j) + 5] = ((i + 1) * (Size + 1) + j);
}
}
vb = new VertexBuffer(GraphicsDevice, (Size + 1) * (Size + 1) * VertexPositionTexture.SizeInBytes, BufferUsage.None);
ib = new IndexBuffer(GraphicsDevice, 6 * Size * Size * sizeof(int), BufferUsage.None, IndexElementSize.ThirtyTwoBits);
vb.SetData(vertices);
ib.SetData(indices);
Debug.WriteLine("Finished building ground vertices and indices.");}
[/sourcecode]