GetHeightAtPosition() public method

public GetHeightAtPosition ( float X, float Z ) : float
X float
Z float
return float
Ejemplo n.º 1
0
        //public float GetHeightAtPosition2(float X, float Z)
        //{
        //    if (X > -512 && X < 0 && Z > -512 && Z < 0)
        //        return clamp(terrain[3].GetHeightAtPosition(X, Z));
        //    else if (X > -512 && X < 0 && Z > 0 && Z < 512)
        //        return clamp(terrain[1].GetHeightAtPosition(X, Z));
        //    else if (X > -0 && X < 512 && Z > 0 && Z < 512)
        //        return clamp(terrain[0].GetHeightAtPosition(X, Z));
        //    else
        //        return clamp(terrain[2].GetHeightAtPosition(X, Z));
        //}

        public float GetHeightAtPosition(float X, float Z)
        {
            float steepness;

            //if (Constants.NUM_OF_TERRAINS == 1)
            return(clamp(terrain.GetHeightAtPosition(X, Z, out steepness)));
            //if (X > -512 * Constants.TERRAIN_CELL_SIZE && X < 0 &&
            //    Z > -512 * Constants.TERRAIN_CELL_SIZE && Z < 0)
            //    return clamp(terrain[3].GetHeightAtPosition(X, Z, out steepness));
            //else if (X > -512 * Constants.TERRAIN_CELL_SIZE && X < 0 &&
            //         Z > 0 && Z < 512 * Constants.TERRAIN_CELL_SIZE)
            //    return clamp(terrain[1].GetHeightAtPosition(X, Z, out steepness));
            //else if (X >= 0 && X < 512 * Constants.TERRAIN_CELL_SIZE &&
            //         Z >= 0 && Z < 512 * Constants.TERRAIN_CELL_SIZE)
            //    return clamp(terrain[0].GetHeightAtPosition(X, Z, out steepness));
            //else if (X >= 0 && X < 512 * Constants.TERRAIN_CELL_SIZE &&
            //         Z > -512 * Constants.TERRAIN_CELL_SIZE && Z < 0)
            //    return clamp(terrain[2].GetHeightAtPosition(X, Z, out steepness));
            //else
            //    return 0;
            //return clamp(terrain[0].GetHeightAtPosition(X, Z, out steepness)) ;
        }
Ejemplo n.º 2
0
        // Called when the game should load its content
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            camera = new FreeCamera(new Vector3(8000, 6000, 8000),
                                    MathHelper.ToRadians(45),
                                    MathHelper.ToRadians(-30),
                                    GraphicsDevice);

            terrain = new Terrain(Content.Load <Texture2D>("terrain"), 30, 4800,
                                  Content.Load <Texture2D>("grass"), 6, new Vector3(1, -1, 0),
                                  GraphicsDevice, Content);

            terrain.WeightMap     = Content.Load <Texture2D>("weightMap");
            terrain.RTexture      = Content.Load <Texture2D>("sand");
            terrain.GTexture      = Content.Load <Texture2D>("rock");
            terrain.BTexture      = Content.Load <Texture2D>("snow");
            terrain.DetailTexture = Content.Load <Texture2D>("noise_texture");

            // Positions where trees should be drawn
            List <Vector3> treePositions = new List <Vector3>();

            // Continue until we get 500 trees on the terrain
            for (int i = 0; i < 500; i++) // 500
            {
                // Get X and Z coordinates from the random generator, between
                // [-(terrain width) / 2 * (cell size), (terrain width) / 2 * (cell size)]
                float x = r.Next(-256 * 30, 256 * 30);
                float z = r.Next(-256 * 30, 256 * 30);

                // Get the height and steepness of this position on the terrain,
                // taking the height of the billboard into account
                float steepness;
                float y = terrain.GetHeightAtPosition(x, z, out steepness) + 100;

                // Reject this position if it is too low, high, or steep. Otherwise
                // add it to the list
                if (steepness < MathHelper.ToRadians(15) && y > 2300 && y < 3200)
                {
                    treePositions.Add(new Vector3(x, y, z));
                }
                else
                {
                    i--;
                }
            }

            trees = new BillboardSystem(GraphicsDevice, Content,
                                        Content.Load <Texture2D>("tree_billboard"), new Vector2(200),
                                        treePositions.ToArray());

            trees.Mode            = BillboardSystem.BillboardMode.Cylindrical;
            trees.EnsureOcclusion = true;


            // List of positions to place grass billboards
            List <Vector3> grassPositions = new List <Vector3>();

            // Retrieve pixel grid from grass map
            Texture2D grassMap = Content.Load <Texture2D>("grass_map");

            Color[] grassPixels = new Color[grassMap.Width * grassMap.Height];
            grassMap.GetData <Color>(grassPixels);

            // Loop until 1000 billboards have been placed
            for (int i = 0; i < 300; i++)
            {
                // Get X and Z coordinates from the random generator, between
                // [-(terrain width) / 2 * (cell size), (terrain width) / 2 * (cell size)]
                float x = r.Next(-256 * 30, 256 * 30);
                float z = r.Next(-256 * 30, 256 * 30);

                // Get corresponding coordinates in grass map
                int xCoord = (int)(x / 30) + 256;
                int zCoord = (int)(z / 30) + 256;

                // Get value between 0 and 1 from grass map
                float texVal = grassPixels[zCoord * 512 + xCoord].R / 255f;

                // Retrieve height
                float steepness;
                float y = terrain.GetHeightAtPosition(x, z, out steepness) + 50;

                // Randomly place a billboard here based on pixel color in grass
                // map
                if ((int)((float)r.NextDouble() * texVal * 10) == 1)
                {
                    grassPositions.Add(new Vector3(x, y, z));
                }
                else
                {
                    i--;
                }
            }

            // Create grass billboard system
            grass = new BillboardSystem(GraphicsDevice, Content,
                                        Content.Load <Texture2D>("grass_billboard"), new Vector2(100),
                                        grassPositions.ToArray());

            grass.Mode            = BillboardSystem.BillboardMode.Cylindrical;
            grass.EnsureOcclusion = false;


            List <Vector3> cloudPositions = new List <Vector3>();

            // Create 20 "clusters" of clouds
            for (int i = 0; i < 20; i++)
            {
                Vector3 cloudLoc = new Vector3(
                    r.Next(-8000, 8000),
                    r.Next(4000, 6000),
                    r.Next(-8000, 8000));

                // Add 10 cloud billboards around each cluster point
                for (int j = 0; j < 10; j++)
                {
                    cloudPositions.Add(cloudLoc +
                                       new Vector3(
                                           r.Next(-3000, 3000),
                                           r.Next(-300, 900),
                                           r.Next(-1500, 1500)));
                }
            }

            clouds = new BillboardSystem(GraphicsDevice, Content,
                                         Content.Load <Texture2D>("cloud2"), new Vector2(2000),
                                         cloudPositions.ToArray());

            clouds.Mode            = BillboardSystem.BillboardMode.Spherical;
            clouds.EnsureOcclusion = false;

            sky = new SkySphere(Content, GraphicsDevice,
                                Content.Load <TextureCube>("clouds"));

            water = new Water(Content, GraphicsDevice,
                              new Vector3(0, 1600, 0), new Vector2(256 * 30));

            water.Objects.Add(sky);
            water.Objects.Add(terrain);

            lastMouseState = Mouse.GetState();
        }