private void AddTrisFromHeightmap(float xPos, float yPos, ref List<Tri> triangles, ref float zLower, ref float zUpper)
{
int xInt = (int)xPos;
int yInt = (int)yPos;
// Corner 1 of 1x1 rectangle
int x = Util.Clamp<int>(xInt+1, 0, World.Heightmap.Width - 1);
int y = Util.Clamp<int>(yInt+1, 0, World.Heightmap.Height - 1);
Vector3 pos1 = new Vector3(x, y, (float)World.Heightmap[x, y]);
// Adjust bounding box
zLower = Math.Min(zLower, pos1.Z);
zUpper = Math.Max(zUpper, pos1.Z);
// Corner 2 of 1x1 rectangle
x = Util.Clamp<int>(xInt, 0, World.Heightmap.Width - 1);
y = Util.Clamp<int>(yInt+1, 0, World.Heightmap.Height - 1);
Vector3 pos2 = new Vector3(x, y, (float)World.Heightmap[x, y]);
// Adjust bounding box
zLower = Math.Min(zLower, pos2.Z);
zUpper = Math.Max(zUpper, pos2.Z);
// Corner 3 of 1x1 rectangle
x = Util.Clamp<int>(xInt, 0, World.Heightmap.Width - 1);
y = Util.Clamp<int>(yInt, 0, World.Heightmap.Height - 1);
Vector3 pos3 = new Vector3(x, y, (float)World.Heightmap[x, y]);
// Adjust bounding box
zLower = Math.Min(zLower, pos3.Z);
zUpper = Math.Max(zUpper, pos3.Z);
// Corner 4 of 1x1 rectangle
x = Util.Clamp<int>(xInt+1, 0, World.Heightmap.Width - 1);
y = Util.Clamp<int>(yInt, 0, World.Heightmap.Height - 1);
Vector3 pos4 = new Vector3(x, y, (float)World.Heightmap[x, y]);
// Adjust bounding box
zLower = Math.Min(zLower, pos4.Z);
zUpper = Math.Max(zUpper, pos4.Z);
// Add triangle 1
Tri triangle1 = new Tri();
triangle1.p1 = pos1;
triangle1.p2 = pos2;
triangle1.p3 = pos3;
triangles.Add(triangle1);
// Add triangle 2
Tri triangle2 = new Tri();
triangle2.p1 = pos3;
triangle2.p2 = pos4;
triangle2.p3 = pos1;
triangles.Add(triangle2);
}