Advertisement
Guest User

Terrain MonoGame C#

a guest
Jul 27th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using System.IO;
  9. namespace XNA3D_Applying.Core
  10. {
  11.     public class Terrain
  12.     {
  13.         private GraphicsDevice gDevice;
  14.         private Game1 gGame;
  15.  
  16.         private BasicEffect gEffect;
  17.  
  18.         private VertexPositionColorNormal[] gVertices;
  19.         private Int32[] gIndices;
  20.         private float[,] gHeights;
  21.        
  22.         public static int Width { get; set; }
  23.         public static int Height { get; set; }
  24.         public Texture2D TerrainTexture { get; private set; }
  25.  
  26.         public Terrain(Game1 g, Texture2D tex2d, int w, int h)
  27.         {
  28.             gGame = g;
  29.             gDevice = gGame.GraphicsDevice;
  30.             TerrainTexture = tex2d;
  31.             Width = w;
  32.             Height = h;
  33.         }
  34.  
  35.         public void Initialize()
  36.         {
  37.             gVertices = new VertexPositionColorNormal[Width * Height];
  38.             gIndices = new Int32[(Width - 1) * (Height - 1) * 6];
  39.             gHeights = new float[Width, Height];
  40.             gEffect = new BasicEffect(gDevice);
  41.  
  42.         }
  43.  
  44.         public void LoadContent(Texture2D tex)
  45.         {
  46.             LoadHeightMap(tex);
  47.             SetUpVertices();
  48.             SetUpIndices();
  49.             SetUpNormals();
  50.         }
  51.  
  52.  
  53.         private void SetUpVertices()
  54.         {
  55.             Random r = new Random();
  56.             float minHeight = float.MaxValue;
  57.             float maxHeight = float.MinValue;
  58.             for (int x = 0; x < Width; x++)
  59.             {
  60.                 for (int z = 0; z < Height; z++)
  61.                 {
  62.                     if (gHeights[x, z] < minHeight)
  63.                         minHeight = gHeights[x, z];
  64.                     if (gHeights[x, z] > maxHeight)
  65.                         maxHeight = gHeights[x, z];
  66.                     gVertices[x + z * Width].Position = new Vector3(x, gHeights[x, z], -z);
  67.  
  68.                     if (gHeights[x, z] < minHeight + (maxHeight - minHeight) / 4.0f)
  69.                         gVertices[x + z * Width].Color = Color.Blue;
  70.                     else if (gHeights[x, z] < minHeight + (maxHeight - minHeight) * 2.0f / 4.0f)
  71.                         gVertices[x + z * Width].Color = Color.Green;
  72.                     else if (gHeights[x, z] < minHeight + (maxHeight - minHeight) * 3.0f / 4.0f)
  73.                         gVertices[x + z * Width].Color = Color.SaddleBrown;
  74.                     else if (gHeights[x, z] < minHeight + (maxHeight - minHeight * 3.5f / 4.0f))
  75.                         gVertices[x + z * Width].Color = Color.Gray;
  76.                     else
  77.                         gVertices[x + z * Width].Color = Color.White;
  78.                 }
  79.             }
  80.         }
  81.  
  82.         public void LoadHeightMap(Texture2D heightmap)
  83.         {
  84.             Random r = new Random();
  85.             int hW = heightmap.Width;
  86.             int hH = heightmap.Height;
  87.             Color[] colormap = new Color[hW * hH];
  88.             heightmap.GetData<Color>(colormap);
  89.  
  90.             for (int x = 0; x < hW; x++)
  91.             {
  92.                 for (int z = 0; z < hH; z++)
  93.                 {
  94.                     gHeights[x, z] = (float)(colormap[x + z * hW].R / 5.0f);
  95.                 }
  96.             }
  97.  
  98.         }
  99.  
  100.         private void SetUpIndices()
  101.         {
  102.             int indicesCounter = 0;
  103.  
  104.             for (int x = 0; x < Width - 1; x++)
  105.             {
  106.                 for (int z = 0; z < Height - 1; z++)
  107.                 {
  108.                     int downLeft = x + z * Width;
  109.                     int upLeft = x + (z + 1) * Width;
  110.  
  111.                     int downRight = (x + 1) + z * Width;
  112.                     int upRight = (x + 1) + (z + 1) * Width;
  113.  
  114.                     gIndices[indicesCounter++] = downLeft;
  115.                     gIndices[indicesCounter++] = upLeft;
  116.                     gIndices[indicesCounter++] = downRight;
  117.  
  118.                     gIndices[indicesCounter++] = downRight;
  119.                     gIndices[indicesCounter++] = upLeft;
  120.                     gIndices[indicesCounter++] = upRight;
  121.                 }
  122.             }
  123.         }
  124.         private void SetUpNormals()
  125.         {
  126.             for (int i = 0; i < gVertices.Length; i++)
  127.             {
  128.                 gVertices[i].Normal = Vector3.Zero;
  129.             }
  130.             for (int i = 0; i < gIndices.Length / 3; i++)
  131.             {
  132.                 int index1 = gIndices[i * 3];
  133.                 int index2 = gIndices[i * 3 + 1];
  134.                 int index3 = gIndices[i * 3 + 2];
  135.  
  136.                 Vector3 side1 = gVertices[index1].Position - gVertices[index3].Position;
  137.                 Vector3 side2 = gVertices[index1].Position - gVertices[index2].Position;
  138.  
  139.                 Vector3 normal = Vector3.Cross(side1, side2);
  140.  
  141.                 gVertices[index1].Normal += normal;
  142.                 gVertices[index2].Normal += normal;
  143.                 gVertices[index3].Normal += normal;
  144.             }
  145.             for (int i = 0; i < gVertices.Length; i++)
  146.             {
  147.                 gVertices[i].Normal.Normalize();
  148.             }
  149.         }
  150.  
  151.         public void Render(Camera render_perspective)
  152.         {
  153.             gEffect.World = Matrix.CreateScale(150f);
  154.             gEffect.Projection = render_perspective.ProjectionsMatrix;
  155.             gEffect.View = render_perspective.ViewMatrix;
  156.             gEffect.EnableDefaultLighting();
  157.             gEffect.TextureEnabled = false;
  158.             gEffect.Texture = TerrainTexture;
  159.             gEffect.FogEnabled = false;
  160.             gEffect.FogColor = Color.CornflowerBlue.ToVector3();
  161.             gEffect.FogStart = 0.0f;
  162.             gEffect.FogEnd = 8000;
  163.             gEffect.VertexColorEnabled = true;
  164.             gEffect.CurrentTechnique.Passes[0].Apply();
  165.             gDevice.DrawUserIndexedPrimitives<VertexPositionColorNormal>(PrimitiveType.TriangleList, gVertices, 0, gVertices.Length, gIndices, 0, gIndices.Length / 3);
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement