Squadron Z HD Terrain Generator

Terrain Generation Code:

  • This code is used to generate the next line of randomized terrain.
  • The GenTerrain() function(highlighted in green) does the actual generation of the line recursively.
  • The Update() function(highlighted in red) does the initial setup for each line, passes the line information to GenTerrain(), and then sets up the new vertices, triangles, texture coordinates, normals, etc.
private void GenTerrain(int y, int left, int right, float displace)
{
int mid = (left + right) / 2;
if (mid == left)
return;
float average = map.ElementAt(y)[left] + map.ElementAt(y)[right] + map.ElementAt(y + 1)[mid] + map.ElementAt(y+1)[mid+1] + map.ElementAt(y+1)[mid – 1];
average /= 5;
average += ((float)r.NextDouble() – 0.5f) * displace;
if(average > 2)
average =2;
if(average < -6)
average = -6;
map.ElementAt(y)[mid] = average;
GenTerrain(y, left, mid, displace * (float)Math.Pow(2, -roughness));
GenTerrain(y, mid, right, displace * (float)Math.Pow(2, -roughness));
}

public void Update(GameTime gs)
{
if (vertices.Count == 6 * (numvtiles – 1) * (numhtiles – 1))
{
map.RemoveLast();
for (int x = 0; x < 6 * (numhtiles – 1); x++)
{
vertices.RemoveLast();
}
}
map.AddFirst(new float[numhtiles]);
map.ElementAt(0)[0] = map.ElementAt(1)[0] + ((float)r.NextDouble() – 0.5f);
if (map.ElementAt(0)[0] > 2)
{
map.ElementAt(0)[0] = 2;
}
if (map.ElementAt(0)[0] < -6)
{
map.ElementAt(0)[0] = -6;
}
map.ElementAt(0)[numhtiles – 1] = map.ElementAt(1)[numhtiles – 1] + ((float)r.NextDouble() – 0.5f);
if (map.ElementAt(0)[numhtiles – 1] > 2)
{
map.ElementAt(0)[numhtiles – 1] = 2;
}
if (map.ElementAt(0)[numhtiles – 1] < -6)
{
map.ElementAt(0)[numhtiles – 1] = -6;
}
GenTerrain(0, 0, numhtiles – 1, 0.5f * (float)Math.Pow(2, -roughness));
int k = 0;
float[] f = map.ElementAt(k);
position -= (2f * Game1.max.Y) / numvtiles;
for (int c = 0; c < f.Length – 1; c++)
{
vertices.AddFirst(new VertexPositionNormalTexture(new Vector3(Game1.max.X * 2.2f * ((c + 1) / (float)numhtiles) – Game1.max.X * 1.1f, -2 + Math.Max(map.ElementAt(k + 1)[c + 1], -4), position + (Game1.max.Y * 5f * ((k + 1)) / (float)numvtiles) – Game1.max.Y * 1f), CalcNormal(c + 1, k + 1), new Vector2(map.ElementAt(k + 1)[c + 1] < -3.75 ? 0 : .2f + .8f * ((map.ElementAt(k + 1)[c + 1] + 6) / 8 ) + ((float)r.NextDouble() / 10) – 0.1f, 0)));

vertices.AddFirst(new VertexPositionNormalTexture(new Vector3(Game1.max.X * 2.2f * ((c + 1) / (float)numhtiles) – Game1.max.X * 1.1f, -2 + Math.Max(map.ElementAt(k)[c + 1], -4), position + (Game1.max.Y * 5f * k) / (float)numvtiles – Game1.max.Y * 1f), CalcNormal(c + 1, k), new Vector2(map.ElementAt(k)[c + 1] < -3.75 ? 0 : .2f + .8f * (map.ElementAt(k)[c + 1] + 6) / 8 + ((float)r.NextDouble() / 10) – 0.1f, 0)));

vertices.AddFirst(new VertexPositionNormalTexture(new Vector3(Game1.max.X * 2.2f * (c / (float)numhtiles) – Game1.max.X * 1.1f, -2 + Math.Max(map.ElementAt(k)[c], -4), position + (Game1.max.Y * 5f * k) / (float)numvtiles – Game1.max.Y * 1f), CalcNormal(c, k), new Vector2(.2f + .8f * (map.ElementAt(k)[c] < -3.75 ? 0 : map.ElementAt(k)[c] + 6) / 8 + ((float)r.NextDouble() / 10) – 0.1f, 0)));

vertices.AddFirst(new VertexPositionNormalTexture(new Vector3(Game1.max.X * 2.2f * (c / (float)numhtiles) – Game1.max.X * 1.1f, -2 + Math.Max(map.ElementAt(k + 1)[c], -4), position + (Game1.max.Y * 5f * (k + 1)) / (float)numvtiles – Game1.max.Y * 1f), CalcNormal(c, k + 1), new Vector2(map.ElementAt(k + 1)[c] < -3.75 ? 0 : .2f + .8f * (map.ElementAt(k + 1)[c] + 6) / 8 + ((float)r.NextDouble() / 10) – 0.1f, 0)));

vertices.AddFirst(new VertexPositionNormalTexture(new Vector3(Game1.max.X * 2.2f * ((c + 1) / (float)numhtiles) – Game1.max.X * 1.1f, -2 + Math.Max(map.ElementAt(k + 1)[c + 1], -4), position + (Game1.max.Y * 5f * (k + 1)) / (float)numvtiles – Game1.max.Y * 1f), CalcNormal(c + 1, k + 1), new Vector2(map.ElementAt(k + 1)[c + 1] < -3.75 ? 0 : .2f + .8f * (map.ElementAt(k + 1)[c + 1] + 6) / 8 + ((float)r.NextDouble() / 10) – 0.1f, 0)));

vertices.AddFirst(new VertexPositionNormalTexture(new Vector3(Game1.max.X * 2.2f * (c / (float)numhtiles) – Game1.max.X * 1.1f, -2 + Math.Max(map.ElementAt(k)[c], -4), position + (Game1.max.Y * 5f * k) / (float)numvtiles – Game1.max.Y * 1f), CalcNormal(c, k), new Vector2(map.ElementAt(k)[c] < -3.75 ? 0 : .2f + .8f * (map.ElementAt(k)[c] + 6) / 8 + ((float)r.NextDouble() / 10) – 0.1f, 0)));
}
}
%d bloggers like this: