private void CalculateNormals(HeightmapComponent component) { for (int i = 0; i < component.vertices.Length; i++) { component.vertices[i].Normal = new Vector3(0, 0, 0); } for (int i = 0; i < component.indices.Length / 3; i++) { int index1 = component.indices[i * 3]; int index2 = component.indices[i * 3 + 1]; int index3 = component.indices[i * 3 + 2]; Vector3 side1 = component.vertices[index1].Position - component.vertices[index3].Position; Vector3 side2 = component.vertices[index1].Position - component.vertices[index2].Position; Vector3 normal = Vector3.Cross(side1, side2); component.vertices[index1].Normal += normal; component.vertices[index2].Normal += normal; component.vertices[index3].Normal += normal; } for (int i = 0; i < component.vertices.Length; i++) { component.vertices[i].Normal.Normalize(); } }
public void SetUpBuffers(HeightmapComponent component) { if (component.vertexBuffer == null) { component.vertexBuffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, component.vertices.Length, BufferUsage.WriteOnly); component.vertexBuffer.SetData(component.vertices); } component.indexBuffer = new IndexBuffer(device, typeof(int), component.indices.Length, BufferUsage.WriteOnly); component.indexBuffer.SetData(component.indices); }
private void SetUpVertices(HeightmapComponent component) { component.vertices = new VertexPositionNormalTexture[component.terrainWidth * component.terrainHeight]; for (int x = 0; x < component.terrainWidth; x++) { for (int y = 0; y < component.terrainHeight; y++) { component.vertices[x + y * component.terrainWidth].Position = new Vector3(x, component.heightMapData[x, y], -y); component.vertices[x + y * component.terrainWidth].TextureCoordinate = new Vector2(x / 22.5f, y / 22.5f); } } }
public void LoadContent() { foreach (Entity ent in ComponentManager.Instance.GetAllEntitiesWithCertainComp <HeightmapComponent>()) { HeightmapComponent hmComp = ComponentManager.Instance.GetEntityComponent <HeightmapComponent>(ent); TransformComponent transComp = ComponentManager.Instance.GetEntityComponent <TransformComponent>(ent); hmComp.World = Matrix.CreateTranslation(transComp.Position); SetHeights(hmComp); SetVertices(hmComp); SetIndices(hmComp); } }
//method for getting out textures private void SetEffects(HeightmapComponent hmComp) { foreach (Entity ent in ComponentManager.Instance.GetAllEntitiesWithCertainComp <CameraComponent>()) { CameraComponent camComp = ComponentManager.Instance.GetEntityComponent <CameraComponent>(ent); hmComp.Effect.View = camComp.View; hmComp.Effect.Projection = camComp.Proj; } hmComp.Effect.World = hmComp.World; hmComp.Effect.FogEnabled = false; hmComp.Effect.TextureEnabled = true; }
public void SetHeights(HeightmapComponent hmComp) { Color[] greyValues = new Color[hmComp.Width * hmComp.Height]; hmComp.HeightMap.GetData(greyValues); hmComp.heightMapData = new float[hmComp.Width, hmComp.Height]; for (int x = 0; x < hmComp.Width; x++) { for (int y = 0; y < hmComp.Height; y++) { hmComp.heightMapData[x, y] = greyValues[x + y * hmComp.Width].G / 3.1f; } } }
private void SetVertices(HeightmapComponent hmComp) { hmComp.Vertices = new VertexPositionTexture[hmComp.Width * hmComp.Height]; Vector2 texturePosition; for (int x = 0; x < hmComp.Width; x++) { for (int y = 0; y < hmComp.Height; y++) { texturePosition = new Vector2((float)x / 25.5f, (float)y / 25.5f); hmComp.Vertices[x + y * hmComp.Width] = new VertexPositionTexture(new Vector3(x, hmComp.heightMapData[x, y], -y), texturePosition); } } }
public void Draw(SpriteBatch spriteBatch, GameTime gameTime) { foreach (Entity ent in ComponentManager.Instance.GetAllEntitiesWithCertainComp <HeightmapComponent>()) { HeightmapComponent hmComp = ComponentManager.Instance.GetEntityComponent <HeightmapComponent>(ent); hmComp.Effect.CurrentTechnique.Passes[0].Apply(); SetEffects(hmComp); foreach (EffectPass pass in hmComp.Effect.CurrentTechnique.Passes) { pass.Apply(); spriteBatch.GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionTexture>(PrimitiveType.TriangleList, hmComp.Vertices, 0, hmComp.Vertices.Length, hmComp.Indices, 0, hmComp.Indices.Length / 3); } } }
private void LoadHeightMapData(Texture2D heightMap, HeightmapComponent component) { component.terrainWidth = heightMap.Width; component.terrainHeight = heightMap.Height; Color[] heightMapColors = new Color[component.terrainWidth * component.terrainHeight]; heightMap.GetData(heightMapColors); component.heightMapData = new float[component.terrainWidth, component.terrainHeight]; for (int x = 0; x < component.terrainWidth; x++) { for (int y = 0; y < component.terrainHeight; y++) { component.heightMapData[x, y] = heightMapColors[x + y * component.terrainWidth].R; } } }
public void LoadContent() { Random ran = new Random(); HeightmapComponent hc = ComponentManager.Instance.GetEntityComponent <HeightmapComponent> (ComponentManager.Instance.GetEntityWithTag("heightmap", SceneManager.Instance.GetActiveSceneEntities())); foreach (Entity ent in ComponentManager.Instance.GetAllEntitiesWithCertainComp <ModelComponent>()) { TagComponent tagc = ComponentManager.Instance.GetEntityComponent <TagComponent>(ent); if (tagc.ID.Contains("tree") || tagc.ID.Contains("house") || tagc.ID.Contains("stone")) { ModelComponent mc = ComponentManager.Instance.GetEntityComponent <ModelComponent>(ent); TransformComponent tc = ComponentManager.Instance.GetEntityComponent <TransformComponent>(ent); tc.Position = Vector3.Transform(hc.Vertices[ran.Next(hc.Vertices.Length)].Position, hc.World); } } }
private void SetUpHeightMapData(HeightmapComponent component) { component.heightMapData = new float[4, 3]; component.heightMapData[0, 0] = 0; component.heightMapData[1, 0] = 0; component.heightMapData[2, 0] = 0; component.heightMapData[3, 0] = 0; component.heightMapData[0, 1] = 0.5f; component.heightMapData[1, 1] = 0; component.heightMapData[2, 1] = -1.0f; component.heightMapData[3, 1] = 0.2f; component.heightMapData[0, 2] = 1.0f; component.heightMapData[1, 2] = 1.2f; component.heightMapData[2, 2] = 0.8f; component.heightMapData[3, 2] = 0; }
private void SetIndices(HeightmapComponent hmComp) { // amount of triangles hmComp.Indices = new int[6 * (hmComp.Width - 1) * (hmComp.Height - 1)]; int number = 0; // collect data for corners for (int y = 0; y < hmComp.Height - 1; y++) { for (int x = 0; x < hmComp.Width - 1; x++) { // create double triangles hmComp.Indices[number] = x + (y + 1) * hmComp.Width; // up left hmComp.Indices[number + 1] = x + y * hmComp.Width + 1; // down right hmComp.Indices[number + 2] = x + y * hmComp.Width; // down left hmComp.Indices[number + 3] = x + (y + 1) * hmComp.Width; // up left hmComp.Indices[number + 4] = x + (y + 1) * hmComp.Width + 1; // up right hmComp.Indices[number + 5] = x + y * hmComp.Width + 1; // down right number += 6; } } }
private void SetUpIndices(HeightmapComponent component) { component.indices = new int[(component.terrainWidth - 1) * (component.terrainHeight - 1) * 6]; int counter = 0; for (int y = 0; y < component.terrainHeight - 1; y++) { for (int x = 0; x < component.terrainWidth - 1; x++) { int lowerLeft = x + y * component.terrainWidth; int lowerRight = (x + 1) + y * component.terrainWidth; int topLeft = x + (y + 1) * component.terrainWidth; int topRight = (x + 1) + (y + 1) * component.terrainWidth; component.indices[counter++] = topLeft; component.indices[counter++] = lowerRight; component.indices[counter++] = lowerLeft; component.indices[counter++] = topLeft; component.indices[counter++] = topRight; component.indices[counter++] = lowerRight; } } }