Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System.IO;
- namespace XNA3D_Applying.Core
- {
- public class Terrain
- {
- private GraphicsDevice gDevice;
- private Game1 gGame;
- private BasicEffect gEffect;
- private VertexPositionColorNormal[] gVertices;
- private Int32[] gIndices;
- private float[,] gHeights;
- public static int Width { get; set; }
- public static int Height { get; set; }
- public Texture2D TerrainTexture { get; private set; }
- public Terrain(Game1 g, Texture2D tex2d, int w, int h)
- {
- gGame = g;
- gDevice = gGame.GraphicsDevice;
- TerrainTexture = tex2d;
- Width = w;
- Height = h;
- }
- public void Initialize()
- {
- gVertices = new VertexPositionColorNormal[Width * Height];
- gIndices = new Int32[(Width - 1) * (Height - 1) * 6];
- gHeights = new float[Width, Height];
- gEffect = new BasicEffect(gDevice);
- }
- public void LoadContent(Texture2D tex)
- {
- LoadHeightMap(tex);
- SetUpVertices();
- SetUpIndices();
- SetUpNormals();
- }
- private void SetUpVertices()
- {
- Random r = new Random();
- float minHeight = float.MaxValue;
- float maxHeight = float.MinValue;
- for (int x = 0; x < Width; x++)
- {
- for (int z = 0; z < Height; z++)
- {
- if (gHeights[x, z] < minHeight)
- minHeight = gHeights[x, z];
- if (gHeights[x, z] > maxHeight)
- maxHeight = gHeights[x, z];
- gVertices[x + z * Width].Position = new Vector3(x, gHeights[x, z], -z);
- if (gHeights[x, z] < minHeight + (maxHeight - minHeight) / 4.0f)
- gVertices[x + z * Width].Color = Color.Blue;
- else if (gHeights[x, z] < minHeight + (maxHeight - minHeight) * 2.0f / 4.0f)
- gVertices[x + z * Width].Color = Color.Green;
- else if (gHeights[x, z] < minHeight + (maxHeight - minHeight) * 3.0f / 4.0f)
- gVertices[x + z * Width].Color = Color.SaddleBrown;
- else if (gHeights[x, z] < minHeight + (maxHeight - minHeight * 3.5f / 4.0f))
- gVertices[x + z * Width].Color = Color.Gray;
- else
- gVertices[x + z * Width].Color = Color.White;
- }
- }
- }
- public void LoadHeightMap(Texture2D heightmap)
- {
- Random r = new Random();
- int hW = heightmap.Width;
- int hH = heightmap.Height;
- Color[] colormap = new Color[hW * hH];
- heightmap.GetData<Color>(colormap);
- for (int x = 0; x < hW; x++)
- {
- for (int z = 0; z < hH; z++)
- {
- gHeights[x, z] = (float)(colormap[x + z * hW].R / 5.0f);
- }
- }
- }
- private void SetUpIndices()
- {
- int indicesCounter = 0;
- for (int x = 0; x < Width - 1; x++)
- {
- for (int z = 0; z < Height - 1; z++)
- {
- int downLeft = x + z * Width;
- int upLeft = x + (z + 1) * Width;
- int downRight = (x + 1) + z * Width;
- int upRight = (x + 1) + (z + 1) * Width;
- gIndices[indicesCounter++] = downLeft;
- gIndices[indicesCounter++] = upLeft;
- gIndices[indicesCounter++] = downRight;
- gIndices[indicesCounter++] = downRight;
- gIndices[indicesCounter++] = upLeft;
- gIndices[indicesCounter++] = upRight;
- }
- }
- }
- private void SetUpNormals()
- {
- for (int i = 0; i < gVertices.Length; i++)
- {
- gVertices[i].Normal = Vector3.Zero;
- }
- for (int i = 0; i < gIndices.Length / 3; i++)
- {
- int index1 = gIndices[i * 3];
- int index2 = gIndices[i * 3 + 1];
- int index3 = gIndices[i * 3 + 2];
- Vector3 side1 = gVertices[index1].Position - gVertices[index3].Position;
- Vector3 side2 = gVertices[index1].Position - gVertices[index2].Position;
- Vector3 normal = Vector3.Cross(side1, side2);
- gVertices[index1].Normal += normal;
- gVertices[index2].Normal += normal;
- gVertices[index3].Normal += normal;
- }
- for (int i = 0; i < gVertices.Length; i++)
- {
- gVertices[i].Normal.Normalize();
- }
- }
- public void Render(Camera render_perspective)
- {
- gEffect.World = Matrix.CreateScale(150f);
- gEffect.Projection = render_perspective.ProjectionsMatrix;
- gEffect.View = render_perspective.ViewMatrix;
- gEffect.EnableDefaultLighting();
- gEffect.TextureEnabled = false;
- gEffect.Texture = TerrainTexture;
- gEffect.FogEnabled = false;
- gEffect.FogColor = Color.CornflowerBlue.ToVector3();
- gEffect.FogStart = 0.0f;
- gEffect.FogEnd = 8000;
- gEffect.VertexColorEnabled = true;
- gEffect.CurrentTechnique.Passes[0].Apply();
- gDevice.DrawUserIndexedPrimitives<VertexPositionColorNormal>(PrimitiveType.TriangleList, gVertices, 0, gVertices.Length, gIndices, 0, gIndices.Length / 3);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement