snip code for XNA


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework;
  8.  
  9. namespace WindowsGame1
  10. {
  11. class Terrain
  12. {
  13. VertexPositionNormalTexture[] vertices;
  14. VertexBuffer vertexBuffer;
  15. int[] indices;
  16. IndexBuffer indexBuffer;
  17. float[,] heights;
  18. float height;
  19. float cellSize;
  20. public int width, length;
  21. int nVertices, nIndices;
  22. Effect effect;
  23. GraphicsDevice GraphicsDevice;
  24. Texture2D heightMap;
  25. Texture2D baseTexture;
  26. float textureTiling;
  27. Vector3 lightDirection;
  28.  
  29. public Terrain(Texture2D HeightMap, float CellSize, float Height, GraphicsDevice GraphicsDevice, ContentManager Content)
  30. {
  31. this.heightMap = HeightMap;
  32. this.width = HeightMap.Width;
  33. this.length = HeightMap.Height;
  34. this.cellSize = CellSize;
  35. this.height = Height;
  36. this.GraphicsDevice = GraphicsDevice;
  37. effect = Content.Load<Effect>("TerrainEffect");
  38. //1 vertex per pixel
  39. nVertices = width * length;
  40. nIndices = (width - 1) * (length - 1) * 6;
  41. vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture), nVertices, BufferUsage.WriteOnly);
  42. indexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, nIndices, BufferUsage.WriteOnly);
  43. getHeights();
  44. createVertices();
  45. createIndices();
  46. genNormals();
  47. vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
  48. indexBuffer.SetData<int>(indices);
  49. }
  50.  
  51. public Terrain(Texture2D HeightMap, float CellSize, float Height, Texture2D BaseTexture, float TextureTiling, Vector3 LightDirection, GraphicsDevice GraphicsDevice, ContentManager Content)
  52. {
  53. this.baseTexture = BaseTexture;
  54. this.textureTiling = TextureTiling;
  55. this.lightDirection = LightDirection;
  56. this.heightMap = HeightMap;
  57. this.width = HeightMap.Width;
  58. this.length = HeightMap.Height;
  59. this.cellSize = CellSize;
  60. this.height = Height;
  61. this.GraphicsDevice = GraphicsDevice;
  62. effect = Content.Load<Effect>("TerrainEffect2");
  63.  
  64. if(effect.Parameters["BaseTexture"] != null )
  65. effect.Parameters["BaseTexture"].SetValue(baseTexture);
  66. if (effect.Parameters["TextureTiling"] != null)
  67. effect.Parameters["TextureTiling"].SetValue(textureTiling);
  68. if (effect.Parameters["LightDirection"] != null)
  69. effect.Parameters["LightDirection"].SetValue(lightDirection);
  70.  
  71.  
  72.  
  73. //1 vertex per pixel
  74. nVertices = width * length;
  75. nIndices = (width - 1) * (length - 1) * 6;
  76. vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture), nVertices, BufferUsage.WriteOnly);
  77. indexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, nIndices, BufferUsage.WriteOnly);
  78. getHeights();
  79. createVertices();
  80. createIndices();
  81. genNormals();
  82. vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
  83. indexBuffer.SetData<int>(indices);
  84. }
  85.  
  86. private void getHeights()
  87. {
  88. Color[] heigthMapData = new Color[width * length];
  89. heightMap.GetData<Color>(heigthMapData);
  90. heights = new float[width,length];
  91. for(int y=0;y<length;y++)
  92. for(int x=0;x<width;x++)
  93. {
  94. float amt=heigthMapData[y * width + x].R;
  95. amt /=255.0f;
  96. heights[x,y]=amt * height;
  97. }
  98. }
  99.  
  100. private void createVertices()
  101. {
  102. vertices = new VertexPositionNormalTexture[nVertices];
  103. Vector3 offsetToCenter = - new Vector3(((float)width / 2.0f) * cellSize, 0 ,((float) length / 2.0f) * cellSize );
  104. //Vector3 offsetToCenter = Vector3.Zero;
  105. for(int z=0;z<length;z++)
  106. for(int x=0;x<width;x++)
  107. {
  108. Vector3 position = new Vector3(x*cellSize,heights[x,z],z * cellSize ) + offsetToCenter;
  109. Vector2 uv= new Vector2((float)x / width, (float) z / length );
  110. vertices[z*width + x] = new VertexPositionNormalTexture(position,Vector3.Zero,uv);
  111.  
  112.  
  113.  
  114. }
  115.  
  116. }
  117.  
  118. private void createIndices()
  119. {
  120. indices = new int[nIndices];
  121. int i=0;
  122. for(int x=0;x<width -1; x++)
  123. for(int z=0;z<length -1;z++)
  124. {
  125. int upperLeft = z * width + x;
  126. int upperRight = upperLeft +1;
  127. int lowerLeft = upperLeft + width;
  128. int lowerRight = lowerLeft +1;
  129.  
  130.  
  131. //upperTriangle
  132. indices[i++]= upperLeft;
  133. indices[i++]=upperRight;
  134. indices[i++] = lowerLeft;
  135.  
  136. //lowerTriangle
  137. indices[i++]=lowerLeft;
  138. indices[i++]=upperRight;
  139. indices[i++]=lowerRight;
  140.  
  141. }
  142.  
  143. }
  144.  
  145. private void genNormals()
  146. {
  147. for(int i=0;i<nIndices;i+=3)
  148. {
  149. Vector3 v1 = vertices[indices[i]].Position;
  150. Vector3 v2=vertices[indices[i+1]].Position;
  151. Vector3 v3=vertices[indices[i+2]].Position;
  152.  
  153. Vector3 normal= Vector3.Cross(v1-v2,v1-v3);
  154. normal.Normalize();
  155.  
  156. //add influence of the normal to each vertex in the triangle
  157. vertices[indices[i]].Normal += normal;
  158. vertices[indices[i+1]].Normal += normal;
  159. vertices[indices[i+2]].Normal += normal;
  160. }
  161. for(int i=0;i<nVertices;i++)
  162. vertices[i].Normal.Normalize();
  163.  
  164.  
  165.  
  166. }
  167.  
  168. public void Draw(Camera camera)
  169. {
  170.  
  171. effect.Parameters["World"].SetValue(Matrix.Identity * Matrix.CreateScale(10000));
  172. effect.Parameters["Projection"].SetValue(camera.projectionMatrix);
  173. effect.Parameters["View"].SetValue(camera.viewMatrix);
  174.  
  175. GraphicsDevice.SetVertexBuffer(vertexBuffer);
  176. GraphicsDevice.Indices = indexBuffer;
  177. foreach (EffectPass pass in effect.CurrentTechnique.Passes)
  178. {
  179. pass.Apply();
  180. GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,0, 0, vertexBuffer.VertexCount , 0, indexBuffer.IndexCount / 3);
  181.  
  182. }
  183. GraphicsDevice.SetVertexBuffer(null);
  184. GraphicsDevice.Indices = null;
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. }
  198. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.