Advertisement
Guest User

Untitled

a guest
Feb 13th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Terrain_Lighting
  13. {
  14. public class Game1 : Microsoft.Xna.Framework.Game
  15. {
  16. // Declarations **************************************************************************
  17.  
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20.  
  21. // Terrain
  22. Effect effect;
  23. VertexPositionNormalTexture[] vertices;
  24. short[] indices;
  25. private int terrainWidth = 4;
  26. private int terrainHeight = 4;
  27. private float[,] heightData;
  28. Texture2D grassTexture;
  29. private Matrix matrixWorld;
  30.  
  31. // Camera
  32. private Matrix view;
  33. private Matrix projection;
  34. private Matrix matrixCam;
  35. float camX = 2;
  36. float camY = 2;
  37. float camZ = 5;
  38.  
  39. // Sun
  40. Matrix matrixSun;
  41. Model sun;
  42. float sunX = -50;
  43. float sunY = 0;
  44. float sunZ = 20;
  45.  
  46.  
  47.  
  48. // Constructor ***************************************************************************
  49.  
  50. public Game1()
  51. {
  52. graphics = new GraphicsDeviceManager(this);
  53. // graphics.IsFullScreen = true;
  54.  
  55. Content.RootDirectory = "Content";
  56. }
  57.  
  58. protected override void Initialize()
  59. {
  60. base.Initialize();
  61.  
  62. matrixWorld = Matrix.CreateTranslation(new Vector3(0, 0, 0));
  63. matrixCam = Matrix.CreateTranslation(new Vector3(camX, camY, camZ));
  64. matrixSun = Matrix.CreateTranslation(new Vector3(sunX, sunY, sunZ));
  65.  
  66. IsMouseVisible = true;
  67. }
  68. protected override void LoadContent()
  69. {
  70. spriteBatch = new SpriteBatch(GraphicsDevice);
  71.  
  72. effect = Content.Load<Effect>("effects");
  73. sun = Content.Load<Model>("Sun");
  74. grassTexture = Content.Load<Texture2D>("grass");
  75.  
  76. LoadHeightData();
  77. SetUpVertices();
  78. SetUpIndices();
  79. CalculateNormals();
  80. }
  81. protected override void UnloadContent()
  82. {
  83. }
  84.  
  85.  
  86.  
  87. // Update ********************************************************************************
  88.  
  89. protected override void Update(GameTime gameTime)
  90. {
  91. // TODO: Add your update logic here
  92. KeyboardState newState = Keyboard.GetState(); // get the newest state
  93.  
  94. // Handle keypress continuously
  95. if (newState.IsKeyDown(Keys.Escape))
  96. {
  97. this.Exit();
  98. }
  99. if (newState.IsKeyDown(Keys.Left))
  100. {
  101. camX -= 0.1f;
  102. }
  103. if (newState.IsKeyDown(Keys.Right))
  104. {
  105. camX += 0.1f;
  106. }
  107. if (newState.IsKeyDown(Keys.Up))
  108. {
  109. camY += 0.1f;
  110. }
  111. if (newState.IsKeyDown(Keys.Down))
  112. {
  113. camY -= 0.1f;
  114. }
  115. if (newState.IsKeyDown(Keys.Space))
  116. {
  117. camZ += 0.1f;
  118. }
  119. if (newState.IsKeyDown(Keys.RightShift))
  120. {
  121. camZ -= 0.1f;
  122. }
  123.  
  124. matrixCam = Matrix.CreateTranslation(new Vector3(camX, camY, camZ));
  125. view = Matrix.CreateLookAt(new Vector3(camX, camY - 20, camZ + 30), new Vector3(camX, camY, camZ), Vector3.UnitY);
  126. projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.1f, 100f);
  127.  
  128. if (sunX < 50) sunX += 0.1f;
  129. else sunX = -50;
  130. matrixSun = Matrix.CreateTranslation(new Vector3(sunX, sunY, sunZ));
  131.  
  132. base.Update(gameTime);
  133. }
  134.  
  135.  
  136.  
  137. // Draw **********************************************************************************
  138.  
  139. protected override void Draw(GameTime gameTime)
  140. {
  141. GraphicsDevice.Clear(Color.CornflowerBlue);
  142.  
  143. RasterizerState rs = new RasterizerState();
  144. rs.CullMode = CullMode.None;
  145. rs.FillMode = FillMode.Solid;
  146. GraphicsDevice.RasterizerState = rs;
  147.  
  148. effect.CurrentTechnique = effect.Techniques["Textured"];
  149. effect.Parameters["xView"].SetValue(view);
  150. effect.Parameters["xProjection"].SetValue(projection);
  151. effect.Parameters["xWorld"].SetValue(matrixWorld);
  152.  
  153. effect.Parameters["xTexture"].SetValue(grassTexture);
  154.  
  155. Vector3 lightDirection = new Vector3(sunX, sunY, sunZ);
  156. lightDirection.Normalize();
  157. effect.Parameters["xLightDirection"].SetValue(lightDirection);
  158. effect.Parameters["xAmbient"].SetValue(0.1f);
  159. effect.Parameters["xEnableLighting"].SetValue(true);
  160.  
  161. foreach (EffectPass pass in effect.CurrentTechnique.Passes)
  162. {
  163. pass.Apply();
  164. GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionNormalTexture.VertexDeclaration);
  165. }
  166.  
  167. DrawModel(sun, matrixSun, view, projection);
  168.  
  169. base.Draw(gameTime);
  170. }
  171. private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
  172. {
  173. foreach (ModelMesh mesh in model.Meshes)
  174. {
  175. foreach (BasicEffect effect in mesh.Effects)
  176. {
  177. effect.World = world;
  178. effect.View = view;
  179. effect.Projection = projection;
  180. }
  181.  
  182. mesh.Draw();
  183. }
  184. }
  185.  
  186.  
  187.  
  188. // Generate Terrain **********************************************************************
  189.  
  190. private void LoadHeightData()
  191. {
  192. heightData = new float[terrainWidth, terrainHeight];
  193.  
  194. heightData[0, 0] = 0;
  195. heightData[1, 0] = 1;
  196. heightData[2, 0] = 1;
  197. heightData[3, 0] = 0;
  198.  
  199. heightData[0, 1] = 0;
  200. heightData[1, 1] = 1;
  201. heightData[2, 1] = 1;
  202. heightData[3, 1] = 0;
  203.  
  204. heightData[0, 2] = 0;
  205. heightData[1, 2] = 1;
  206. heightData[2, 2] = 1;
  207. heightData[3, 2] = 0;
  208.  
  209. heightData[0, 3] = 0;
  210. heightData[1, 3] = 0;
  211. heightData[2, 3] = 0;
  212. heightData[3, 3] = 0;
  213. }
  214. private void SetUpVertices()
  215. {
  216. vertices = new VertexPositionNormalTexture[terrainWidth * terrainHeight];
  217. for (int x = 0; x < terrainWidth; x++)
  218. {
  219. for (int y = 0; y < terrainHeight; y++)
  220. {
  221. vertices[x + y * terrainWidth].Position = new Vector3(x, -y, heightData[x, y]);
  222. vertices[x + y * terrainWidth].TextureCoordinate.X = 0;
  223. vertices[x + y * terrainWidth].TextureCoordinate.Y = 0;
  224. }
  225. }
  226. }
  227. private void SetUpIndices()
  228. {
  229. indices = new short[(terrainWidth - 1) * (terrainHeight - 1) * 6];
  230. int counter = 0;
  231. for (int y = 0; y < terrainHeight - 1; y++)
  232. {
  233. for (int x = 0; x < terrainWidth - 1; x++)
  234. {
  235. int lowerLeft = x + y * terrainWidth;
  236. int lowerRight = (x + 1) + y * terrainWidth;
  237. int topLeft = x + (y + 1) * terrainWidth;
  238. int topRight = (x + 1) + (y + 1) * terrainWidth;
  239.  
  240. indices[counter++] = (short)topLeft;
  241. indices[counter++] = (short)lowerRight;
  242. indices[counter++] = (short)lowerLeft;
  243.  
  244. indices[counter++] = (short)topLeft;
  245. indices[counter++] = (short)topRight;
  246. indices[counter++] = (short)lowerRight;
  247. }
  248. }
  249. }
  250. private void CalculateNormals()
  251. {
  252. for (int i = 0; i < vertices.Length; i++)
  253. vertices[i].Normal = new Vector3(0, 0, 0);
  254.  
  255. for (int i = 0; i < indices.Length / 3; i++)
  256. {
  257. int index1 = indices[i * 3];
  258. int index2 = indices[i * 3 + 1];
  259. int index3 = indices[i * 3 + 2];
  260.  
  261. Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
  262. Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
  263. Vector3 normal = Vector3.Cross(side1, side2);
  264.  
  265. vertices[index1].Normal += normal;
  266. vertices[index2].Normal += normal;
  267. vertices[index3].Normal += normal;
  268. }
  269.  
  270. for (int i = 0; i < vertices.Length; i++)
  271. vertices[i].Normal.Normalize();
  272. }
  273.  
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement