public void CreateRobots() { var robots = EntityComponentManager.GetManager().GetComponentByType(typeof(RobotComponent)); var robotCams = EntityComponentManager.GetManager().GetComponentByType(typeof(RobotCameraComponent)); foreach (var c in robotCams) { foreach (var r in robots) { var robot = (RobotComponent)EntityComponentManager.GetManager().GetComponent(r.Key, typeof(RobotComponent)); var robotCam = (RobotCameraComponent)EntityComponentManager.GetManager().GetComponent(r.Key, typeof(RobotCameraComponent)); robot.Effect.Projection = robot.RobotProjection; robot.Effect.View = robot.RobotView; robot.Effect.VertexColorEnabled = false; robot.Effect.TextureEnabled = true; robot.Effect.EnableDefaultLighting(); robot.Effect.LightingEnabled = false; foreach (ModelMesh mesh in robotCam.Model.Meshes) { foreach (ModelMeshPart mp in mesh.MeshParts) { mp.Effect = robot.Effect; } } } } }
public void RenderSystem() { var heightMapCameras = EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)); var heightMaps = EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)); foreach (var heightMapCamera in heightMapCameras) { var cam = (HeightMapCameraComponent)heightMapCamera.Value; cam.Frustum = new BoundingFrustum(cam.ViewMatrix * cam.ProjectionMatrix); foreach (var heightMap in heightMaps) { var camera = (HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponent(heightMapCamera.Key, typeof(HeightMapCameraComponent)); var map = (HeightMapComponent)EntityComponentManager.GetManager().GetComponent(heightMapCamera.Key, typeof(HeightMapComponent)); foreach (EffectPass pass in map.BasicEffect.CurrentTechnique.Passes) { SetEffects(map, camera); //StoreInGraphicsCard(heightmap); pass.Apply(); map.GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalTexture>(PrimitiveType.TriangleList, map.Vertices, 0, map.Vertices.Length, map.Indices, 0, map.Indices.Length / 3); //heightmap.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, heightmap.Indices.Length / 3); } } } }
public void Update() { var cam = (HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)).Values.First(); var d = EntityComponentManager.GetManager().GetComponentByType(typeof(HouseComponent)); foreach (var i in d) { var bbox = (HouseBoundingBox)EntityComponentManager.GetManager().GetComponent(i.Key, typeof(HouseBoundingBox)); var house = (HouseComponent)i.Value; if (cam.Frustum.Intersects(bbox.BoundingBox)) { // Render the walls with one texture... var vertexBuffer = new VertexBuffer( game.GraphicsDevice, typeof(VertexPositionNormalTexture), house.WallVertices.Length, BufferUsage.None); vertexBuffer.SetData(house.WallVertices); var indexBuffer = new IndexBuffer(game.GraphicsDevice, typeof(short), house.WallIndices.Length, BufferUsage.None); indexBuffer.SetData(house.WallIndices); RenderHouses(house, vertexBuffer, indexBuffer, house.WallTexture); // ...Render the roof with another texture vertexBuffer = new VertexBuffer( game.GraphicsDevice, typeof(VertexPositionNormalTexture), house.RoofVertices.Length, BufferUsage.None); vertexBuffer.SetData(house.RoofVertices); indexBuffer = new IndexBuffer(game.GraphicsDevice, typeof(short), house.RoofIndices.Length, BufferUsage.None); indexBuffer.SetData(house.WallIndices); RenderHouses(house, vertexBuffer, indexBuffer, house.RoofTexture); } } }
public void RenderSystem() { var robots = EntityComponentManager.GetManager().GetComponentByType(typeof(RobotComponent)); var robotCams = EntityComponentManager.GetManager().GetComponentByType(typeof(RobotCameraComponent)); foreach (var c in robotCams) { foreach (var r in robots) { var robot = (RobotComponent)EntityComponentManager.GetManager().GetComponent(r.Key, typeof(RobotComponent)); var robotCam = (RobotCameraComponent)EntityComponentManager.GetManager().GetComponent(r.Key, typeof(RobotCameraComponent)); robot.Effect.TextureEnabled = true; robot.Effect.Texture = robot.Texture; robotCam.Model.CopyAbsoluteBoneTransformsTo(robot.TransformMatrices); robot.PlaneObjectWorld = Matrix.Identity * robot.Scale * Matrix.CreateFromQuaternion(robotCam.Rotation) * Matrix.CreateTranslation(robot.Position); foreach (ModelMesh mesh in robotCam.Model.Meshes) { robot.Effect.World = robot.TransformMatrices[mesh.ParentBone.Index] * robot.PlaneObjectWorld * Matrix.Identity; foreach (Effect e in mesh.Effects) { e.CurrentTechnique.Passes[0].Apply(); } mesh.Draw(); } } } }
private void CreateRandomHouses(int nHouses, Texture2D wall1, Texture2D roof1, Texture2D wall2, Texture2D roof2) { // Max and min values short maxScale = 110; short minScale = 70; short minX = 100; short maxX = 950; short minZ = -900; short maxZ = -100; for (int i = 0; i < nHouses; ++i) { var s = rnd.Next(minScale, maxScale); Vector3 scale = new Vector3(s += rnd.Next(0, 20), s + rnd.Next(0, 20), s + rnd.Next(0, 20)); Vector3 pos = new Vector3(rnd.Next(minX, maxX), scale.Y / 2 + 20, rnd.Next(minZ, maxZ)); var houses = (EntityComponentManager.GetManager().GetComponentByType(typeof(HouseComponent)).Values); bool ok = true; foreach (var h in houses) { var v = (HouseComponent)h; if (Vector3.Distance(v.Position, pos) < 150) { ok = false; break; } } if (ok) { var wall = wall1; var roof = roof1; if (rnd.NextDouble() < .5) { wall = wall2; roof = roof2; shader.RealisticSettings(); } HouseComponent house = new HouseComponent(scale, pos, Matrix.CreateRotationY((float)(rnd.NextDouble() * (Math.PI * 2))), wall, roof); int hid = EntityComponentManager.GetManager().CreateNewEntityId(); EntityComponentManager.GetManager().AddComponentToEntity(hid, house); HouseBoundingBox hBox = new HouseBoundingBox() { BoundingBox = new BoundingBox(pos, pos + scale) }; EntityComponentManager.GetManager().AddComponentToEntity(hid, hBox); } else { --i; } } }
/// <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(); }
public void CreateHeightMaps() { var heightMapsList = EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapComponent)); foreach (var heightMap in heightMapsList) { var hm = (HeightMapComponent)EntityComponentManager.GetManager().GetComponent(heightMap.Key, typeof(HeightMapComponent)); SetHeights(hm); SetVertices(hm); SetIndices(hm); CalculateNormals(hm); SetEffects(hm); } }
public void UpdateSystem(GameTime gameTime) { var heightMapCameras = EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)); var robotCameras = EntityComponentManager.GetManager().GetComponentByType(typeof(RobotCameraComponent)); foreach (var rc in robotCameras) { foreach (var hmc in heightMapCameras) { var heightMapCamera = (HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponent(hmc.Key, typeof(HeightMapCameraComponent)); var robotCamera = (RobotCameraComponent)EntityComponentManager.GetManager().GetComponent(rc.Key, typeof(RobotCameraComponent)); Vector3 tempMovement = Vector3.Zero; Vector3 tempRotation = Vector3.Zero; KeyboardState key = Keyboard.GetState(); //move backward if (robotCamera.Rotation.Z > robotCamera.RotationInDegrees && key.IsKeyDown(Keys.Up)) { tempMovement.Z = -heightMapCamera.Movement.Z; } //move forward if (robotCamera.Rotation.Z < robotCamera.RotationInDegrees && key.IsKeyDown(Keys.Up)) { tempMovement.Z = +heightMapCamera.Movement.Z; } //left rotation if (robotCamera.Rotation.X > robotCamera.RotationInDegrees && key.IsKeyDown(Keys.Up)) { tempRotation.Y = -heightMapCamera.Rotation.Y * 0.1f; } //right rotation if (robotCamera.Rotation.X < robotCamera.RotationInDegrees && key.IsKeyDown(Keys.Up)) { tempRotation.Y = +heightMapCamera.Rotation.Y * 0.1f; } //move camera to new position heightMapCamera.ViewMatrix = heightMapCamera.ViewMatrix * Matrix.CreateRotationX(tempRotation.X) * Matrix.CreateRotationY(tempRotation.Y) * Matrix.CreateTranslation(tempMovement); //update position heightMapCamera.Position += tempMovement; heightMapCamera.Direction += tempRotation; } } }
private void RenderHouses(HouseComponent h, VertexBuffer vb, IndexBuffer ib, Texture2D texture) { game.GraphicsDevice.SetVertexBuffer(vb); game.GraphicsDevice.Indices = ib; var effect = new BasicEffect(game.GraphicsDevice); effect.View = ((HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)).First().Value).ViewMatrix; effect.Projection = ((HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)).First().Value).ProjectionMatrix; effect.VertexColorEnabled = false; effect.TextureEnabled = true; effect.EnableDefaultLighting(); effect.LightingEnabled = false; var objectWorld = Matrix.CreateScale(h.Scale) * h.Rotation * Matrix.CreateTranslation(h.Position); effect.World = objectWorld * ((HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)).First().Value).TerrainMatrix; effect.Texture = texture; foreach (EffectPass ep in effect.CurrentTechnique.Passes) { ep.Apply(); game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 18); } }
/// <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(); }
public void UpdateSystem(GameTime gameTime) { float rotation = 0; float speed = 0; var robots = EntityComponentManager.GetManager().GetComponentByType(typeof(RobotCameraComponent)); foreach (var r in robots) { var robot = (RobotCameraComponent)EntityComponentManager.GetManager().GetComponent(r.Key, typeof(RobotCameraComponent)); if (Keyboard.GetState().IsKeyDown(Keys.Up)) { speed = 0.1f * (float)gameTime.ElapsedGameTime.Milliseconds; if (robot.ModelRotation < robot.MaxRotation) { if (robot.Direction) { robot.ModelRotation += speed * robot.RotationSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds; } else { robot.ModelRotation -= speed * robot.RotationSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds; } } else { if (!robot.Direction) { robot.ModelRotation -= speed * robot.RotationSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds; } else { robot.ModelRotation += speed * robot.RotationSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds; } } if (robot.ModelRotation > robot.MaxRotation || robot.ModelRotation < -robot.MaxRotation) { robot.Direction = !robot.Direction; } robot.Model.Bones["RightArm"].Transform = TransformPart(robot.RightArmMatrix, robot.ModelRotation); robot.Model.Bones["LeftArm"].Transform = TransformPart(robot.LeftArmMatrix, -robot.ModelRotation); robot.Model.Bones["RightLeg"].Transform = TransformPart(robot.RightLegMatrix, -robot.ModelRotation); robot.Model.Bones["LeftLeg"].Transform = TransformPart(robot.LeftLegMatrix, robot.ModelRotation); } if (Keyboard.GetState().IsKeyDown(Keys.Left)) { rotation = 0.01f * MathHelper.PiOver4 * (float)gameTime.ElapsedGameTime.Milliseconds; robot.RotationInDegrees = (int)(0.01f * MathHelper.PiOver4 * (float)gameTime.ElapsedGameTime.Milliseconds); } if (Keyboard.GetState().IsKeyDown(Keys.Right)) { rotation = -0.01f * MathHelper.PiOver4 * (float)gameTime.ElapsedGameTime.Milliseconds; robot.RotationInDegrees = (int)(-0.01f * MathHelper.PiOver4 * (float)gameTime.ElapsedGameTime.Milliseconds); } rotation = rotation % 360; robot.RotationInDegrees = robot.RotationInDegrees % 360; robot.Rotation *= Quaternion.CreateFromYawPitchRoll(rotation, 0, 0); //Debug.WriteLine(robot.RotationInDegrees); } }