Example #1
0
        private void CalculateNormals(HeightMapComponent heightmap)
        {
            for (int i = 0; i < heightmap.Vertices.Length; i++)
            {
                heightmap.Vertices[i].Normal = new Vector3(0, 0, 0);
            }

            for (int i = 0; i < heightmap.Indices.Length / 3; i++)
            {
                int index1 = heightmap.Indices[i * 3];
                int index2 = heightmap.Indices[i * 3 + 1];
                int index3 = heightmap.Indices[i * 3 + 2];

                Vector3 side1  = heightmap.Vertices[index1].Position - heightmap.Vertices[index3].Position;
                Vector3 side2  = heightmap.Vertices[index1].Position - heightmap.Vertices[index2].Position;
                Vector3 normal = Vector3.Cross(side1, side2);

                heightmap.Vertices[index1].Normal += normal;
                heightmap.Vertices[index2].Normal += normal;
                heightmap.Vertices[index3].Normal += normal;
            }

            for (int i = 0; i < heightmap.Vertices.Length; i++)
            {
                heightmap.Vertices[i].Normal.Normalize();
            }
        }
        private int[] CreateIndicesAndUpdateColor(HeightMapComponent hmc, VertexPositionColor[] vertices)
        {
            List <int> indices = new List <int>();
            int        q1, q2, q3, q4;

            for (int z = 0; z < hmc.Height - 1; z++)
            {
                for (int x = 0; x < hmc.Width - 1; x++)
                {
                    // Calculate the four corners
                    q1 = z * hmc.Width + x;
                    q2 = z * hmc.Width + x + 1;
                    q3 = (z + 1) * hmc.Width + x;
                    q4 = (z + 1) * hmc.Width + x + 1;
                    // Add indices
                    indices.AddRange(new[]
                    {
                        q1, q2, q3, // First triangle
                        q2, q4, q3, // Second triangle
                    });

                    Color color = ToColor(vertices[q1], vertices[q2], vertices[q3]);
                    vertices[q1].Color = vertices[q2].Color = vertices[q3].Color = vertices[q4].Color = color;
                }
            }

            return(indices.ToArray());
        }
Example #3
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            wandererWorld = Matrix.Identity;

            wanderer = new WandererBody(this);
            heightMapTransformSystem_Wanderer = new HeightMapTransformSystem_Wanderer();

            heightMapComponent       = new HeightMapComponent();
            heightMapCameraComponent = new HeightMapCameraComponent();

            robotComponent = new RobotComponent();

            heightMapSystem         = new HeightmapSystem();
            heightMapTranformSystem = new HeightMapTranformSystem();
            heightMapRenderSystem   = new HeightMapRenderSystem(graphics.GraphicsDevice);

            robotSystem         = new RobotSystem();
            robotTranformSystem = new RobotTranformSystem();
            robotRenderSystem   = new RobotRenderSystem();

            collisionSystem = new CollisionSystem();
            houseSystem     = new HouseSystem(this);

            systemRenderer = new Renderer();

            base.Initialize();
        }
        public void Load(ContentManager content)
        {
            HeightMapComponent hmc = cm.GetComponentsOfType <HeightMapComponent>().First().Item2;

            foreach (var(k, bbc, mic) in cm.GetComponentsOfType <BoundingBoxComponent, ModelInstanceComponent>())
            {
                mic.Instance = PlaceVegetation(hmc);
            }
        }
        public static int CreateTerrain(GraphicsDevice gd, string heightMapFile, string heightMapTextureFile)
        {
            HeightMapComponent hmc = new HeightMapComponent(gd)
            {
                HeightMapFilePath = heightMapFile,
                TextureFilePath   = heightMapTextureFile,
            };

            return(cm.AddEntityWithComponents(hmc));
        }
 public void Render(GraphicsDevice gd, BasicEffect be)
 {
     foreach (var entity in cm.GetComponentsOfType <HeightMapComponent>())
     {
         HeightMapComponent hmc = (HeightMapComponent)entity.Value;
         be.CurrentTechnique.Passes[0].Apply();
         gd.SetVertexBuffer(hmc.VertexBuffer);
         gd.Indices = hmc.IndexBuffer;
         gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, hmc.IndexBuffer.IndexCount / 3);
     }
 }
 private int?GetIntersectingBoxIndex(HeightMapComponent heightmap, Ray ray)
 {
     for (int i = 0; i < heightmap.BoundingBoxes.Length; i++)
     {
         if (ray.Intersects(heightmap.BoundingBoxes[i]).HasValue)
         {
             return(i);
         }
     }
     return(null);
 }
Example #8
0
        private void SetChunks(HeightMapComponent heightmap)
        {
            ushort iCapacity = ushort.MaxValue;
            ushort vCapacity = (ushort)(iCapacity / 6);
            ushort height    = (ushort)(vCapacity / heightmap.Width);
            ushort width     = (ushort)(heightmap.Width);

            vCapacity = (ushort)(height * width);

            if (heightmap.Chunks == null)
            {
                heightmap.Chunks = new List <HeightMapChunk>();
            }

            int nChunks = (int)(heightmap.Height / height);

            for (int i = 0; i < nChunks; ++i)
            {
                HeightMapChunk hmc      = new HeightMapChunk();
                var            vertices = new List <VertexPositionNormalTexture>();
                for (int y = i * (height - 1); y < i * (height - 1) + height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        vertices.Add(heightmap.Vertices[y * heightmap.Width + x]);
                    }
                }
                hmc.Vertices = vertices.ToArray();

                var    indices = new ushort[6 * (width - 1) * (height - 1)];
                ushort n       = 0;
                for (int y = 0; y < height - 1; y++)
                {
                    for (int x = 0; x < width - 1; x++)
                    {
                        int botLeft  = y * width + x;
                        int botRight = botLeft + 1;
                        int topLeft  = botLeft + width;
                        int topRight = topLeft + 1;

                        indices[n++] = (ushort)topLeft;
                        indices[n++] = (ushort)botRight;
                        indices[n++] = (ushort)botLeft;

                        indices[n++] = (ushort)topLeft;
                        indices[n++] = (ushort)topRight;
                        indices[n++] = (ushort)botRight;
                    }
                }
                hmc.Indices = indices;

                heightmap.Chunks.Add(hmc);
            }
        }
Example #9
0
        public BoundingBox SetBoundingBoxHeightMap(HeightMapComponent heightMap, Matrix world)
        {
            VertexPositionNormalTexture[] verticesToNormal;
            verticesToNormal = heightMap.Vertices.ToArray();

            for (int i = 0; i < heightMap.Vertices.Length; i++)
            {
                verticesToNormal[i].Position = Vector3.Transform(heightMap.Vertices[i].Position, world);
            }

            return(BoundingBox.CreateFromPoints(verticesToNormal.Select(x => x.Position)));
        }
        public static int CreateHeightMap(GraphicsDevice gd, string heightMapFilePath)
        {
            ComponentManager cm = ComponentManager.GetInstance();

            HeightMapComponent hmComp = new HeightMapComponent(gd)
            {
                HeightMapFilePath = heightMapFilePath
            };
            int hm = cm.AddEntityWithComponents(hmComp);

            return(hm);
        }
Example #11
0
        public void SetVertices(HeightMapComponent heightmap)
        {
            heightmap.Vertices = new VertexPositionNormalTexture[heightmap.Width * heightmap.Height];

            for (int x = 0; x < heightmap.Width; x++)
            {
                for (int y = 0; y < heightmap.Height; y++)
                {
                    heightmap.TexturePosition = new Vector2((float)x / 150.5f, (float)y / 150.5f);
                    heightmap.Vertices[x + y * heightmap.Width] = new VertexPositionNormalTexture(new Vector3(x, heightmap.HeightMapData[x, y], -y), Vector3.Zero, heightmap.TexturePosition);
                }
            }
        }
Example #12
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Texture2D heightMapTexture2D    = Content.Load <Texture2D>("US_Canyon");
            Texture2D heightMapGrassTexture = Content.Load <Texture2D>("grass2");
            Texture2D heightMapFireTexture  = Content.Load <Texture2D>("fire");
            Texture2D BrickTexture          = Content.Load <Texture2D>("wall_texture");
            Model     robotModel            = Content.Load <Model>("Lab2Model");
            Texture2D robotTexture          = Content.Load <Texture2D>("robot_texture");
            Texture2D roofTexture           = Content.Load <Texture2D>("roof");
            Texture2D timberWallTexture     = Content.Load <Texture2D>("timber_wall");
            Texture2D timberRoofTexture     = Content.Load <Texture2D>("timber_roof");

            //This is new
            Effect shaderEffect = Content.Load <Effect>("TestShaders");

            shader = new Shader(shaderEffect);

            heightMapComponent = new HeightMapComponent
            {
                HeightMap        = heightMapTexture2D,
                HeightMapTexture = heightMapGrassTexture,
                GraphicsDevice   = graphics.GraphicsDevice,
                Width            = heightMapTexture2D.Width,
                Height           = heightMapTexture2D.Height,
                HeightMapData    = new float[heightMapTexture2D.Width, heightMapTexture2D.Height]
            };

            heightMapCameraComponent = new HeightMapCameraComponent()
            {
                ViewMatrix       = Matrix.CreateLookAt(new Vector3(-100, 0, 0), Vector3.Zero, Vector3.Up),
                ProjectionMatrix = Matrix.CreatePerspective(1.2f, 0.9f, 1.0f, 1000.0f),
                TerrainMatrix    = Matrix.CreateTranslation(new Vector3(0, -100, 256)),
                Position         = new Vector3(-100, 0, 0),
                Direction        = Vector3.Zero,
                Movement         = new Vector3(1, 1, 1),
                Rotation         = new Vector3(2, 2, 2) * 0.01f,
                TerrainPosition  = new Vector3(0, -100, 256),
            };
            heightMapCameraComponent.Frustum = new BoundingFrustum(heightMapCameraComponent.ViewMatrix * heightMapCameraComponent.ProjectionMatrix);

            int heightMapId = EntityComponentManager.GetManager().CreateNewEntityId();

            EntityComponentManager.GetManager().AddComponentToEntity(heightMapId, heightMapComponent);
            EntityComponentManager.GetManager().AddComponentToEntity(heightMapId, heightMapCameraComponent);

            CreateRandomHouses(10, BrickTexture, roofTexture, timberWallTexture, timberRoofTexture);

            heightMapSystem.CreateHeightMaps();
        }
Example #13
0
        public void StoreInGraphicsCard(HeightMapComponent heightmap)
        {
            var vertices      = from v in heightmap.Vertices select v;
            int verticesCount = vertices.Count();

            var indices      = from i in heightmap.Indices select i;
            int indicesCount = indices.Count();

            var vertexBuffer = new VertexBuffer(heightmap.GraphicsDevice, typeof(VertexPositionNormalTexture), verticesCount, BufferUsage.None);
            var indexBuffer  = new IndexBuffer(heightmap.GraphicsDevice, IndexElementSize.SixteenBits, indicesCount, BufferUsage.None);

            heightmap.GraphicsDevice.SetVertexBuffer(vertexBuffer);
            heightmap.GraphicsDevice.Indices = indexBuffer;
        }
Example #14
0
        public void SetHeights(HeightMapComponent heightmap)
        {
            Color[] greyValues = new Color[heightmap.Width * heightmap.Height];
            heightmap.HeightMap.GetData(greyValues);
            heightmap.HeightMapData = new float[heightmap.Width, heightmap.Height];

            for (int x = 0; x < heightmap.Width; x++)
            {
                for (int y = 0; y < heightmap.Height; y++)
                {
                    heightmap.HeightMapData[x, y] = greyValues[x + y * heightmap.Width].G / 3.1f;
                }
            }
        }
        private float?GetIntersectingVertexDistance(HeightMapComponent heightmap, int index, Ray ray)
        {
            Vector3[] vertices = heightmap.Vertices[index];
            int[]     indices  = heightmap.Indices[index];

            for (int i = 0; i < indices.Length; i += 3)
            {
                float?distance = ray.Intersects(vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]]);
                if (distance.HasValue)
                {
                    return(distance);
                }
            }
            return(null);
        }
        private static Matrix PlaceVegetation(HeightMapComponent hmc)
        {
            Random random = new Random();

            VertexPositionTexture[] vertices;
            int[] indices;

            float x = random.Next((int)Math.Ceiling(hmc.BoundingBox.Min.X), (int)hmc.BoundingBox.Max.X);
            float y = (float)Math.Ceiling(hmc.BoundingBox.Max.Y);
            float z = random.Next((int)Math.Ceiling(hmc.BoundingBox.Min.Z), (int)hmc.BoundingBox.Max.Z);

            float? distance;
            Matrix rotation = Matrix.Identity;

            Ray ray = new Ray(new Vector3(x, y, z), Vector3.Down);

            for (int i = 0; i < hmc.BoundingBoxes.Length; i++)
            {
                distance = ray.Intersects(hmc.BoundingBoxes[i]);
                if (distance != null)
                {
                    vertices = new VertexPositionTexture[hmc.VertexBuffers[i].VertexCount];
                    hmc.VertexBuffers[i].GetData(vertices);
                    indices = new int[hmc.IndexBuffers[i].IndexCount];
                    hmc.IndexBuffers[i].GetData(indices);

                    for (int j = 0; j < indices.Length; j += 3)
                    {
                        Vector3 vertex1 = vertices[indices[j]].Position;
                        Vector3 vertex2 = vertices[indices[j + 1]].Position;
                        Vector3 vertex3 = vertices[indices[j + 2]].Position;
                        distance = ray.Intersects(vertex1, vertex2, vertex3);

                        if (distance != null)
                        {
                            y = ray.Position.Y - distance.Value;
                            //rotation = GetRotation(Vector3.Cross(vertex2 - vertex1, vertex3 - vertex1));
                            break;
                        }
                    }

                    break;
                }
            }

            return(Matrix.CreateScale(0.05f) * rotation * Matrix.CreateTranslation(x, y, z));
        }
        private VertexPositionColor[] CreateVertices(HeightMapComponent hmc)
        {
            VertexPositionColor[] vertices = new VertexPositionColor[hmc.Width * hmc.Height];
            Color[] heightMapData          = new Color[hmc.Width * hmc.Height];
            hmc.HeightMap.GetData(heightMapData);

            for (int x = 0; x < hmc.Width; x++)
            {
                for (int z = 0; z < hmc.Height; z++)
                {
                    float y = heightMapData[z * hmc.Width + x].R;
                    vertices[z * hmc.Width + x] = new VertexPositionColor(new Vector3(x - 500, y, z - 500), Color.White);
                }
            }

            return(vertices);
        }
        public void Load(ContentManager content)
        {
            foreach (var entity in cm.GetComponentsOfType <HeightMapComponent>())
            {
                HeightMapComponent hmc = (HeightMapComponent)entity.Value;

                hmc.HeightMap = content.Load <Texture2D>(hmc.HeightMapFilePath);
                hmc.Width     = hmc.HeightMap.Width;
                hmc.Height    = hmc.HeightMap.Height;

                VertexPositionColor[] vertices = CreateVertices(hmc);
                int[] indices = CreateIndicesAndUpdateColor(hmc, vertices);

                hmc.VertexBuffer = new VertexBuffer(hmc.GraphicsDevice, VertexPositionColor.VertexDeclaration, hmc.Width * hmc.Height, BufferUsage.WriteOnly);
                hmc.VertexBuffer.SetData(vertices);
                hmc.IndexBuffer = new IndexBuffer(hmc.GraphicsDevice, IndexElementSize.ThirtyTwoBits, hmc.Width * hmc.Height * 6, BufferUsage.WriteOnly);
                hmc.IndexBuffer.SetData(indices);
            }
        }
Example #19
0
        public void SetIndices(HeightMapComponent heightmap)
        {
            // amount of triangles
            heightmap.Indices = new int[6 * (heightmap.Width - 1) * (heightmap.Height - 1)];
            int number = 0;

            // collect data for corners
            for (int y = 0; y < heightmap.Height - 1; y++)
            {
                for (int x = 0; x < heightmap.Width - 1; x++)
                {
                    // create double triangles
                    heightmap.Indices[number]     = x + (y + 1) * heightmap.Width;      // up left
                    heightmap.Indices[number + 1] = x + y * heightmap.Width + 1;        // down right
                    heightmap.Indices[number + 2] = x + y * heightmap.Width;            // down left
                    heightmap.Indices[number + 3] = x + (y + 1) * heightmap.Width;      // up left
                    heightmap.Indices[number + 4] = x + (y + 1) * heightmap.Width + 1;  // up right
                    heightmap.Indices[number + 5] = x + y * heightmap.Width + 1;        // down right
                    number += 6;
                }
            }
        }
        private Dictionary <Vector3, float[, ]> Split(HeightMapComponent hmc)
        {
            Dictionary <Vector3, float[, ]> cells = new Dictionary <Vector3, float[, ]>();

            Color[] heightMapData = new Color[hmc.Width * hmc.Height];
            hmc.HeightMap.GetData(heightMapData);

            int rows = hmc.Height / 256;
            int cols = hmc.Width / 256;

            for (int z = 0; z < rows; z++)
            {
                for (int x = 0; x < cols; x++)
                {
                    int x2 = MathHelper.Clamp((x + 1) * 256 + 1, 0, hmc.Width);
                    int z2 = MathHelper.Clamp((z + 1) * 256 + 1, 0, hmc.Height);
                    cells.Add(new Vector3(x * 256, 0, z * 256), getCellHeights(x * 256, x2, z * 256, z2));
                }
            }

            return(cells);

            float[,] getCellHeights(int x1, int x2, int y1, int y2)
            {
                float[,] heights = new float[x2 - x1, y2 - y1];

                for (int y = y1; y < y2; y++)
                {
                    for (int x = x1; x < x2; x++)
                    {
                        heights[x - x1, y - y1] = heightMapData[y * hmc.Width + x].R;
                    }
                }

                return(heights);
            }
        }
Example #21
0
 public void SetEffects(HeightMapComponent heightMap, HeightMapCameraComponent heightMapCamera)
 {
     heightMap.BasicEffect.View       = heightMapCamera.ViewMatrix;
     heightMap.BasicEffect.Projection = heightMapCamera.ProjectionMatrix;
     heightMap.BasicEffect.World      = heightMapCamera.TerrainMatrix;
 }
Example #22
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Texture2D heightMapTexture2D    = Content.Load <Texture2D>("US_Canyon");
            Texture2D heightMapGrassTexture = Content.Load <Texture2D>("grass2");
            Texture2D heightMapFireTexture  = Content.Load <Texture2D>("fire");
            Texture2D BrickTexture          = Content.Load <Texture2D>("wall_texture");
            Model     robotModel            = Content.Load <Model>("Lab2Model");
            Texture2D robotTexture          = Content.Load <Texture2D>("robot_texture");
            Texture2D roofTexture           = Content.Load <Texture2D>("roof");
            Texture2D timberWallTexture     = Content.Load <Texture2D>("timber_wall");
            Texture2D timberRoofTexture     = Content.Load <Texture2D>("timber_roof");

            heightMapComponent = new HeightMapComponent
            {
                HeightMap        = heightMapTexture2D,
                HeightMapTexture = heightMapGrassTexture,
                GraphicsDevice   = graphics.GraphicsDevice,
                Width            = heightMapTexture2D.Width,
                Height           = heightMapTexture2D.Height,
                HeightMapData    = new float[heightMapTexture2D.Width, heightMapTexture2D.Height]
            };

            heightMapCameraComponent = new HeightMapCameraComponent()
            {
                ViewMatrix       = Matrix.CreateLookAt(new Vector3(-100, 0, 0), Vector3.Zero, Vector3.Up),
                ProjectionMatrix = Matrix.CreatePerspective(1.2f, 0.9f, 1.0f, 1000.0f),
                TerrainMatrix    = Matrix.CreateTranslation(new Vector3(0, -100, 256)),
                Position         = new Vector3(-100, 0, 0),
                Direction        = Vector3.Zero,
                Movement         = new Vector3(1, 1, 1),
                Rotation         = new Vector3(2, 2, 2) * 0.01f,
                TerrainPosition  = new Vector3(0, -100, 256),
            };
            heightMapCameraComponent.Frustum = new BoundingFrustum(heightMapCameraComponent.ViewMatrix * heightMapCameraComponent.ProjectionMatrix);

            //var h1 = new HouseComponent(new Vector3(40, 100, 40), new Vector3(100, 50, -100), Matrix.Identity, BrickTexture, roofTexture);
            //var h2 = new HouseComponent(new Vector3(40, 100, 40), new Vector3(950, 50, -100), Matrix.Identity, BrickTexture, roofTexture);
            //var h3 = new HouseComponent(new Vector3(40, 100, 40), new Vector3(950, 50, -900), Matrix.Identity, BrickTexture, roofTexture);

            //int h1Id = EntityComponentManager.GetManager().CreateNewEntityId();
            //EntityComponentManager.GetManager().AddComponentToEntity(h1Id, h1);

            //int h2Id = EntityComponentManager.GetManager().CreateNewEntityId();
            //EntityComponentManager.GetManager().AddComponentToEntity(h2Id, h2);

            //int h3Id = EntityComponentManager.GetManager().CreateNewEntityId();
            //EntityComponentManager.GetManager().AddComponentToEntity(h3Id, h3);

            int heightMapId = EntityComponentManager.GetManager().CreateNewEntityId();

            EntityComponentManager.GetManager().AddComponentToEntity(heightMapId, heightMapComponent);
            EntityComponentManager.GetManager().AddComponentToEntity(heightMapId, heightMapCameraComponent);

            CreateRandomHouses(10, BrickTexture, roofTexture, timberWallTexture, timberRoofTexture);

            heightMapSystem.CreateHeightMaps();

            robotComponent = new RobotComponent
            {
                Speed             = 0,
                Texture           = robotTexture,
                PlaneObjectWorld  = Matrix.Identity,
                TransformMatrices = new Matrix[robotModel.Bones.Count],
                Effect            = new BasicEffect(graphics.GraphicsDevice),
                Scale             = Matrix.CreateScale(0.5f),
                Position          = new Vector3(0f, 1300f, 0f),
                RobotProjection   = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, 1280 / 720, 0.1f, 500f),
                RobotView         = Matrix.CreateLookAt(new Vector3(70, 50, 30), new Vector3(0, 0, 20), Vector3.Backward)
            };

            robotCameraComponent = new RobotCameraComponent
            {
                MaxRotation       = MathHelper.PiOver4,
                RotationSpeed     = 0.003f,
                ModelRotation     = 0,
                Model             = robotModel,
                Direction         = true,
                LeftArmMatrix     = robotModel.Bones["LeftArm"].Transform,
                RightArmMatrix    = robotModel.Bones["RightArm"].Transform,
                LeftLegMatrix     = robotModel.Bones["LeftLeg"].Transform,
                RightLegMatrix    = robotModel.Bones["RightLeg"].Transform,
                Rotation          = Quaternion.CreateFromAxisAngle(Vector3.Right, MathHelper.PiOver2) * Quaternion.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi),
                RotationInDegrees = 0
            };

            int robotId = EntityComponentManager.GetManager().CreateNewEntityId();

            EntityComponentManager.GetManager().AddComponentToEntity(robotId, robotComponent);
            EntityComponentManager.GetManager().AddComponentToEntity(robotId, robotCameraComponent);
            //robotSystem.CreateRobots();
        }
Example #23
0
 public void SetEffects(HeightMapComponent heightmap)
 {
     heightmap.BasicEffect = new BasicEffect(heightmap.GraphicsDevice);
     heightmap.BasicEffect.TextureEnabled = true;
     heightmap.BasicEffect.Texture        = heightmap.HeightMapTexture;
 }