Exemplo n.º 1
0
        public Sun(Game game, Landscape landscape)
        {
            this.landscape = landscape;
            this.terrain = landscape.Terrain;
            float sunsize = terrain.max / 4;
            numberUpdates = 80f;
            frontBottomLeftNormal = new Vector3(-0.333f, -0.333f,-0.333f);
            frontTopLeftNormal = new Vector3(-0.333f, 0.333f, -0.333f);
            frontTopRightNormal = new Vector3(0.333f, 0.333f, -0.333f);
            frontBottomRightNormal = new Vector3(0.333f,-0.333f, -0.333f);
            backBottomLeftNormal = new Vector3(-0.333f, -0.333f, 0.333f);
            backBottomRightNormal = new Vector3(0.333f, -0.333f, 0.333f);
            backTopLeftNormal = new Vector3(-0.333f,0.333f,0.333f);
            backTopRightNormal = new Vector3(0.333f, 0.333f , 0.333f);

            float sunX, sunY, sunZ, sunOffset;
            sunX = terrain.size; sunY = terrain.maxHeight + 10f; sunZ = terrain.size/2; sunOffset = 40f;
            frontBottomLeft = new Vector3(sunX-sunOffset, sunY - sunOffset, sunZ-sunOffset);
            frontTopLeft = new Vector3(sunX-sunOffset, sunY + sunOffset, sunZ-sunOffset);
            frontTopRight = new Vector3(sunX + sunOffset, sunY + sunOffset, sunZ-sunOffset);
            frontBottomRight = new Vector3(sunX + sunOffset, sunY - sunOffset, sunZ-sunOffset);
            backBottomLeft = new Vector3(sunX-sunOffset, sunY - sunOffset, sunZ + sunOffset);
            backBottomRight = new Vector3(sunX + sunOffset, sunY - sunOffset, sunZ + sunOffset);
            backTopLeft = new Vector3(sunX-sunOffset, sunY + sunOffset, sunZ + sunOffset);
            backTopRight = new Vector3(sunX + sunOffset, sunY + sunOffset, sunZ + sunOffset);

            vertices = Buffer.Vertex.New(
                game.GraphicsDevice,
                new[]
                    {
                    new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, Color.Yellow), // Front
                    new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, Color.Yellow), // BACK
                    new VertexPositionNormalColor(backTopRight, backTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(backTopRight, backTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, Color.Yellow), // Top
                    new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(backTopRight, backTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(backTopRight, backTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, Color.Yellow), // Bottom
                    new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, Color.Yellow), // Left
                    new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, Color.Yellow), // Right
                    new VertexPositionNormalColor(backTopRight, backTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, Color.Yellow),
                    new VertexPositionNormalColor(backTopRight, backTopRightNormal, Color.Yellow),
            });

            basicEffect = new BasicEffect(game.GraphicsDevice)
            {
                VertexColorEnabled = true,
                View = Matrix.LookAtLH(landscape.currentPosition, landscape.currentTarget, landscape.currentUp),
                Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)game.GraphicsDevice.BackBuffer.Width / game.GraphicsDevice.BackBuffer.Height, -10.0f, (float)terrain.size * 2),
                World = Matrix.Identity,
            };

            inputLayout = VertexInputLayout.FromBuffer(0, vertices);
            this.game = game;
        }
Exemplo n.º 2
0
        public Landscape(Game game)
        {
            Terrain = new HeightMap(10);

            terrain3D = new VertexPositionNormalColor[Terrain.max * Terrain.max * 6  + 12];
            int index = 0;
            for (int z = 0; z < Terrain.max; z++)
            {
                for (int x = 0; x < Terrain.max; x++)
                {
                    // Front left.
                    terrain3D[index] = new VertexPositionNormalColor(new Vector3(x, Terrain.get(x,z), z), Terrain.getVertexNormal(x,z) , Terrain.getColor(x,z));
                    index++;
                    // Back left.
                    terrain3D[index] = new VertexPositionNormalColor(new Vector3(x, Terrain.get(x, z + 1), z + 1), Terrain.getVertexNormal(x, z + 1), Terrain.getColor(x, z+1));
                    index++;

                    // Back right.
                    terrain3D[index] = new VertexPositionNormalColor(new Vector3(x + 1, Terrain.get(x + 1, z + 1), z + 1), Terrain.getVertexNormal(x + 1, z + 1), Terrain.getColor(x+1, z+1));
                    index++;

                    // Front left.
                    terrain3D[index] = new VertexPositionNormalColor(new Vector3(x, Terrain.get(x, z), z), Terrain.getVertexNormal(x, z), Terrain.getColor(x, z));
                    index++;

                    // Back right.
                    terrain3D[index] = new VertexPositionNormalColor(new Vector3(x + 1, Terrain.get(x + 1, z + 1), z + 1), Terrain.getVertexNormal(x + 1, z + 1), Terrain.getColor(x+1, z+1));
                    index++;

                    // Front right.
                    terrain3D[index] = new VertexPositionNormalColor(new Vector3(x + 1, Terrain.get(x + 1, z), z), Terrain.getVertexNormal(x + 1, z), Terrain.getColor(x+1, z));
                    index++;
                }
            }

            Color blue = Color.MidnightBlue;
            blue.A = 0xC0;

            waterHeight = 0.695f * Terrain.maxHeight;

            //Set Water Polygons
            // Front left.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(0.0f, waterHeight, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Back left.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(0.0f, waterHeight, Terrain.max), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Back right.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(Terrain.max, waterHeight, Terrain.max), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Front left.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(0.0f, waterHeight, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Back right.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(Terrain.max, waterHeight, Terrain.max), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Front right.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(Terrain.max, waterHeight, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            //water from the bottom
            // Front left.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(0.0f, waterHeight, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Back right.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(Terrain.max, waterHeight, Terrain.max), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Back left.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(0.0f, waterHeight, Terrain.max), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Front left.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(0.0f, waterHeight, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Front right.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(Terrain.max, waterHeight, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            // Back right.
            terrain3D[index] = new VertexPositionNormalColor(new Vector3(Terrain.max, waterHeight, Terrain.max), new Vector3(0.0f, 1.0f, 0.0f), blue);
            index++;

            //initialized here because I wanted the terrain details to place the initial position/target.
            currentPosition = new Vector3(0.0f, Terrain.maxHeight, 0.0f); //start on corner of map at highest point of terrain
            currentTarget = new Vector3(Terrain.max, Terrain.maxHeight, Terrain.max); //looking across to other corner (same height)
            currentUp = Vector3.UnitY;
            prevMouseX = 0.5f;
            prevMouseY = 0.5f;

            vertices = Buffer.Vertex.New(game.GraphicsDevice, terrain3D);

            basicEffect = new BasicEffect(game.GraphicsDevice)
            {
                VertexColorEnabled = true,
                View = Matrix.LookAtLH(currentPosition, currentTarget, currentUp),
                Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)game.GraphicsDevice.BackBuffer.Width / game.GraphicsDevice.BackBuffer.Height, -10.0f, (float)Terrain.size * 2),
                World = Matrix.Identity,
                LightingEnabled = true
            };

            inputLayout = VertexInputLayout.FromBuffer(0, vertices);
            basicEffect.EnableDefaultLighting();

            basicEffect.AmbientLightColor = new Vector3(.1f * 255/255, .1f * 244/255, .1f * 229/255);

            this.game = game;
        }