Пример #1
0
        public Terrain()
        {
            float[,] height = null;

            // Load the height map
            using (Bitmap bmp = (Bitmap)Bitmap.FromFile(FileResource.Fsp("heightfield.bmp")))
            {

                // compute scale
                gridWidth = bmp.Width;										// scale is determined by the size of the
                gridHeight = bmp.Height;									// heightfield & the size of the map
                tileWidth = Map.MapWidth / bmp.Width;
                tileHeight = Map.MapHeight / bmp.Height;
                gridWidth = bmp.Height-1;									// the grid of tiles will be 1 smaller than
                gridHeight = bmp.Height-1;									// the grid of heights (last height = RH side of last tile)

                // Create the blank tiles
                tile = new Tile[gridWidth,gridHeight];

                // get heightmap into a temp array, for faster access
                height = new float[bmp.Width,bmp.Height];
                for (int y = 0; y<bmp.Height; y++)
                {
                    for (int x=0; x<bmp.Width; x++)
                    {
            ///////////////////////						height[x,y] = (float)bmp.GetPixel(x,y).R / 256.0f * MAXHEIGHT;
                        height[x,y] = 0;
                    }
                }
            }																// dispose of the bitmap

            // Create the tiles and define their extents and heights
            for (int y = 0; y<gridHeight; y++)
            {
                for (int x=0; x<gridWidth; x++)
                {
                    tile[x,y] = new Tile(x,y,tileWidth,tileHeight,ref height);
                }
            }

            // Now that the triangles exist, define the vertex normals, by averaging the
            // surface normals of surrounding triangles
            for (int y = 1; y<gridHeight-1; y++)
            {
                for (int x=1; x<gridWidth-1; x++)
                {
                    Tile.SetVertexNormal(tile,x,y);
                }
            }

            /// TODO: Load the texture map here & create the texture library,
            /// then set the tile textures

            // Reload resources on reset of device
            Engine.Device.DeviceReset += new System.EventHandler(this.OnReset);
            // Load them for the first time now
            OnReset(null, null);
        }
Пример #2
0
        /// <summary>
        /// Set the vertex normals for the triangles in this tile.
        /// Each vertex normal is the average of the face normals for surrounding triangles
        /// (some of which are in different tiles)
        /// </summary>
        /// <param name="tile">the array of tiles</param>
        /// <param name="x">our location in the array</param>
        /// <param name="y"></param>
        public static void SetVertexNormal(Tile[,] tile, int x, int y)
        {
            // NW vertex
            tile[x,y].mesh[0].Normal = Vector3.Normalize(
                tile[x-1,y-1].FaceNormal2
                + tile[x,y-1].FaceNormal1
                + tile[x,y-1].FaceNormal2
                + tile[x,y].FaceNormal1
                + tile[x-1,y].FaceNormal2
                + tile[x-1,y].FaceNormal1
                );

            // NE vertex
            tile[x,y].mesh[1].Normal = Vector3.Normalize(
                tile[x,y-1].FaceNormal2
                + tile[x+1,y-1].FaceNormal1
                + tile[x+1,y-1].FaceNormal2
                + tile[x+1,y].FaceNormal1
                + tile[x,y].FaceNormal2
                + tile[x,y].FaceNormal1
                );

            // SW vertex
            tile[x,y].mesh[2].Normal = Vector3.Normalize(
                tile[x-1,y].FaceNormal2
                + tile[x,y].FaceNormal1
                + tile[x,y].FaceNormal2
                + tile[x,y+1].FaceNormal1
                + tile[x-1,y+1].FaceNormal2
                + tile[x-1,y+1].FaceNormal1
                );

            // SE vertex
            tile[x,y].mesh[3].Normal = Vector3.Normalize(
                tile[x,y].FaceNormal2
                + tile[x+1,y].FaceNormal1
                + tile[x+1,y].FaceNormal2
                + tile[x+1,y+1].FaceNormal1
                + tile[x,y+1].FaceNormal2
                + tile[x,y+1].FaceNormal1
                );
        }