private List<Vector3> GenerateTreePositions(VertexMultitextured[] terrainVertices) { int[,] noiseData = new int[continentSize, continentSize]; for (int x = 0; x < continentSize; x++) for (int y = 0; y < continentSize; y++) noiseData[x, y] = (int)continent.treeMap[y, x]; List<Vector3> treeList = new List<Vector3>(); Random random = new Random(); for (int x = 0; x < terrainWidth; x++) { for (int y = 0; y < terrainLength; y++) { float terrainHeight = continent.heightMap[x, y]; if ((terrainHeight > 8) && (terrainHeight < 14)) { float flatness = Vector3.Dot(terrainVertices[x + y * terrainWidth].Normal, new Vector3(0, 1, 0)); float minFlatness = (float)Math.Cos(MathHelper.ToRadians(15)); //hard-coded until I can create "flatter" terrain surfaces if (true || flatness >= minFlatness) { float relx = (float)x / (float)terrainWidth; float rely = (float)y / (float)terrainLength; float noiseValueAtCurrentPosition = noiseData[(int)(relx * continentSize), (int)(rely * continentSize)]; float treeDensity; if (noiseValueAtCurrentPosition > 150) treeDensity = 0.2f; else if (noiseValueAtCurrentPosition > 100) treeDensity = 0.1f; else treeDensity = 0; for (int currDetail = 0; currDetail < treeDensity; currDetail++) { float rand1 = (float)random.Next(100) / 100.0f; float rand2 = (float)random.Next(100) / 100.0f; Vector3 treePos = new Vector3((float)x - rand1, 0, -(float)y - rand2); treePos.Y = continent.heightMap[x, y]; treeList.Add(treePos); } } } } } return treeList; }
private List<Vector3> GenerateGrassPositions(VertexMultitextured[] terrainVertices) { List<Vector3> grassList = new List<Vector3>(); Random random = new Random(); for (int x = 0; x < terrainWidth; x++) { for (int y = 0; y < terrainLength; y++) { float terrainHeight = continent.heightMap[x, y]; if ((terrainHeight > 5) && (terrainHeight < 14)) { int grassDensity = 10; for (int currDetail = 0; currDetail < grassDensity; currDetail++) { float rand1 = (float)random.Next(150) / 100.0f; float rand2 = (float)random.Next(150) / 100.0f; Vector3 grassPos = new Vector3((float)x - rand1, 0, -(float)y - rand2); grassPos.Y = continent.heightMap[x, y]; grassList.Add(grassPos); } } } } return grassList; }
private VertexMultitextured[] CalculateNormals(VertexMultitextured[] vertices, int[] indices) { for (int i = 0; i < vertices.Length; i++) vertices[i].Normal = new Vector3(0, 0, 0); for (int i = 0; i < indices.Length / 3; i++) { int index1 = indices[i * 3]; int index2 = indices[i * 3 + 1]; int index3 = indices[i * 3 + 2]; Vector3 side1 = vertices[index1].Position - vertices[index3].Position; Vector3 side2 = vertices[index1].Position - vertices[index2].Position; Vector3 normal = Vector3.Cross(side1, side2); vertices[index1].Normal += normal; vertices[index2].Normal += normal; vertices[index3].Normal += normal; } for (int i = 0; i < vertices.Length; i++) vertices[i].Normal.Normalize(); return vertices; }
private void CopyToTerrainBuffers(VertexMultitextured[] vertices, int[] indices) { terrainVertexBuffer = new VertexBuffer(device, vertices.Length * VertexMultitextured.SizeInBytes, BufferUsage.WriteOnly); terrainVertexBuffer.SetData(vertices); terrainIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly); terrainIndexBuffer.SetData(indices); }
private VertexMultitextured[] SetUpTerrainVertices() { VertexMultitextured[] terrainVertices = new VertexMultitextured[terrainWidth * terrainLength]; for (int x = 0; x < terrainWidth; x++) { for (int y = 0; y < terrainLength; y++) { terrainVertices[x + y * terrainWidth].Position = new Vector3(x, continent.heightMap[x, y], -y); terrainVertices[x + y * terrainWidth].TextureCoordinate.X = (float)x / 30.0f; terrainVertices[x + y * terrainWidth].TextureCoordinate.Y = (float)y / 30.0f; terrainVertices[x + y * terrainWidth].TexWeights.X = MathHelper.Clamp(1.0f - Math.Abs(continent.heightMap[x, y] - 0) / 8.0f, 0, 1); terrainVertices[x + y * terrainWidth].TexWeights.Y = MathHelper.Clamp(1.0f - Math.Abs(continent.heightMap[x, y] - 12) / 6.0f, 0, 1); terrainVertices[x + y * terrainWidth].TexWeights.Z = MathHelper.Clamp(1.0f - Math.Abs(continent.heightMap[x, y] - 20) / 6.0f, 0, 1); terrainVertices[x + y * terrainWidth].TexWeights.W = MathHelper.Clamp(1.0f - Math.Abs(continent.heightMap[x, y] - 30) / 6.0f, 0, 1); float total = terrainVertices[x + y * terrainWidth].TexWeights.X; total += terrainVertices[x + y * terrainWidth].TexWeights.Y; total += terrainVertices[x + y * terrainWidth].TexWeights.Z; total += terrainVertices[x + y * terrainWidth].TexWeights.W; terrainVertices[x + y * terrainWidth].TexWeights.X /= total; terrainVertices[x + y * terrainWidth].TexWeights.Y /= total; terrainVertices[x + y * terrainWidth].TexWeights.Z /= total; terrainVertices[x + y * terrainWidth].TexWeights.W /= total; } } return terrainVertices; }