public static TerainVertex[] GenerateVertexes(Device device, Bitmap heightMap) { heightMeasure = (tileWidth + tileHeight) * 0.25f * 0.2f; int width = heightMap.Width; int height = heightMap.Height; int primitiveCount = 2 * (width - 1) * (height - 1); int vertexCount = width * height; int indexCount = 3 * primitiveCount; Color[,] map = BitmapOperation.BitmapToColorArray(heightMap); TerainVertex[] vertexes = new TerainVertex[vertexCount]; int[] indices = new int[indexCount]; int[] adjency = new int[indexCount]; Terrain.minPosition = new Vector3(-(0.5f * (width - 1) * tileWidth), -127 * heightMeasure, -(0.5f * (height - 1) * tileHeight)); Terrain.maxPosition = new Vector3(tileWidth * (width - 1) * 0.5f, 127 * heightMeasure, tileHeight * (height - 1) * 0.5f); #region Generate Vertexes and Indexes int i = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float posX = x * tileWidth - (0.5f * (width - 1) * tileWidth); float posY = map[x, y].B * heightMeasure; float posZ = y * tileHeight - (0.5f * (height - 1) * tileHeight); Vector3 pos = new Vector3(posX, posY, posZ); Vector3 nor = new Vector3(); float u = (float)x; float v = (float)y; if (x + 1 < width) { if (y + 1 < height) { #region Generate Index Buffer indices[i] = (y * width + x); indices[i + 1] = ((y + 1) * width + x); indices[i + 2] = (y * width + x + 1); indices[i + 3] = (indices[i + 2]); indices[i + 4] = (indices[i + 1]); indices[i + 5] = (indices[i + 1] + 1); i += 6; #endregion } } vertexes[y * width + x] = new TerainVertex(pos, nor, u, v); } } #endregion return(vertexes); }
public Terrain(int level, Device device, Bitmap heightMap, Texture mask, Texture layer1, Texture layer2, Texture layer3) : base(level, GenerateVertexes(device, heightMap), GenerateIndexes(device, heightMap), Terrain.minPosition, Terrain.maxPosition, Matrix.Identity, new Texture[] { layer1 }, new Texture[] { layer2 }, new Texture[] { layer3 }, null) { this.heightMap = BitmapOperation.BitmapToColorArray(heightMap); blocked = new List <Point>(); base.SetIsEveryWhere(true); CameraDriver.SetAttachedTerain(this); }