Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace Terrain_Lighting
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- // Declarations **************************************************************************
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- // Terrain
- Effect effect;
- VertexPositionNormalTexture[] vertices;
- short[] indices;
- private int terrainWidth = 4;
- private int terrainHeight = 4;
- private float[,] heightData;
- Texture2D grassTexture;
- private Matrix matrixWorld;
- // Camera
- private Matrix view;
- private Matrix projection;
- private Matrix matrixCam;
- float camX = 2;
- float camY = 2;
- float camZ = 5;
- // Sun
- Matrix matrixSun;
- Model sun;
- float sunX = -50;
- float sunY = 0;
- float sunZ = 20;
- // Constructor ***************************************************************************
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- // graphics.IsFullScreen = true;
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- base.Initialize();
- matrixWorld = Matrix.CreateTranslation(new Vector3(0, 0, 0));
- matrixCam = Matrix.CreateTranslation(new Vector3(camX, camY, camZ));
- matrixSun = Matrix.CreateTranslation(new Vector3(sunX, sunY, sunZ));
- IsMouseVisible = true;
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- effect = Content.Load<Effect>("effects");
- sun = Content.Load<Model>("Sun");
- grassTexture = Content.Load<Texture2D>("grass");
- LoadHeightData();
- SetUpVertices();
- SetUpIndices();
- CalculateNormals();
- }
- protected override void UnloadContent()
- {
- }
- // Update ********************************************************************************
- protected override void Update(GameTime gameTime)
- {
- // TODO: Add your update logic here
- KeyboardState newState = Keyboard.GetState(); // get the newest state
- // Handle keypress continuously
- if (newState.IsKeyDown(Keys.Escape))
- {
- this.Exit();
- }
- if (newState.IsKeyDown(Keys.Left))
- {
- camX -= 0.1f;
- }
- if (newState.IsKeyDown(Keys.Right))
- {
- camX += 0.1f;
- }
- if (newState.IsKeyDown(Keys.Up))
- {
- camY += 0.1f;
- }
- if (newState.IsKeyDown(Keys.Down))
- {
- camY -= 0.1f;
- }
- if (newState.IsKeyDown(Keys.Space))
- {
- camZ += 0.1f;
- }
- if (newState.IsKeyDown(Keys.RightShift))
- {
- camZ -= 0.1f;
- }
- matrixCam = Matrix.CreateTranslation(new Vector3(camX, camY, camZ));
- view = Matrix.CreateLookAt(new Vector3(camX, camY - 20, camZ + 30), new Vector3(camX, camY, camZ), Vector3.UnitY);
- projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.1f, 100f);
- if (sunX < 50) sunX += 0.1f;
- else sunX = -50;
- matrixSun = Matrix.CreateTranslation(new Vector3(sunX, sunY, sunZ));
- base.Update(gameTime);
- }
- // Draw **********************************************************************************
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- RasterizerState rs = new RasterizerState();
- rs.CullMode = CullMode.None;
- rs.FillMode = FillMode.Solid;
- GraphicsDevice.RasterizerState = rs;
- effect.CurrentTechnique = effect.Techniques["Textured"];
- effect.Parameters["xView"].SetValue(view);
- effect.Parameters["xProjection"].SetValue(projection);
- effect.Parameters["xWorld"].SetValue(matrixWorld);
- effect.Parameters["xTexture"].SetValue(grassTexture);
- Vector3 lightDirection = new Vector3(sunX, sunY, sunZ);
- lightDirection.Normalize();
- effect.Parameters["xLightDirection"].SetValue(lightDirection);
- effect.Parameters["xAmbient"].SetValue(0.1f);
- effect.Parameters["xEnableLighting"].SetValue(true);
- foreach (EffectPass pass in effect.CurrentTechnique.Passes)
- {
- pass.Apply();
- GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionNormalTexture.VertexDeclaration);
- }
- DrawModel(sun, matrixSun, view, projection);
- base.Draw(gameTime);
- }
- private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
- {
- foreach (ModelMesh mesh in model.Meshes)
- {
- foreach (BasicEffect effect in mesh.Effects)
- {
- effect.World = world;
- effect.View = view;
- effect.Projection = projection;
- }
- mesh.Draw();
- }
- }
- // Generate Terrain **********************************************************************
- private void LoadHeightData()
- {
- heightData = new float[terrainWidth, terrainHeight];
- heightData[0, 0] = 0;
- heightData[1, 0] = 1;
- heightData[2, 0] = 1;
- heightData[3, 0] = 0;
- heightData[0, 1] = 0;
- heightData[1, 1] = 1;
- heightData[2, 1] = 1;
- heightData[3, 1] = 0;
- heightData[0, 2] = 0;
- heightData[1, 2] = 1;
- heightData[2, 2] = 1;
- heightData[3, 2] = 0;
- heightData[0, 3] = 0;
- heightData[1, 3] = 0;
- heightData[2, 3] = 0;
- heightData[3, 3] = 0;
- }
- private void SetUpVertices()
- {
- vertices = new VertexPositionNormalTexture[terrainWidth * terrainHeight];
- for (int x = 0; x < terrainWidth; x++)
- {
- for (int y = 0; y < terrainHeight; y++)
- {
- vertices[x + y * terrainWidth].Position = new Vector3(x, -y, heightData[x, y]);
- vertices[x + y * terrainWidth].TextureCoordinate.X = 0;
- vertices[x + y * terrainWidth].TextureCoordinate.Y = 0;
- }
- }
- }
- private void SetUpIndices()
- {
- indices = new short[(terrainWidth - 1) * (terrainHeight - 1) * 6];
- int counter = 0;
- for (int y = 0; y < terrainHeight - 1; y++)
- {
- for (int x = 0; x < terrainWidth - 1; x++)
- {
- int lowerLeft = x + y * terrainWidth;
- int lowerRight = (x + 1) + y * terrainWidth;
- int topLeft = x + (y + 1) * terrainWidth;
- int topRight = (x + 1) + (y + 1) * terrainWidth;
- indices[counter++] = (short)topLeft;
- indices[counter++] = (short)lowerRight;
- indices[counter++] = (short)lowerLeft;
- indices[counter++] = (short)topLeft;
- indices[counter++] = (short)topRight;
- indices[counter++] = (short)lowerRight;
- }
- }
- }
- private void CalculateNormals()
- {
- for (int i = 0; i < vertices.Length; i++)
- vertices[i].Normal = new Vector3(0, 0, 0);
- for (int i = 0; i < indices.Length / 3; i++)
- {
- int index1 = indices[i * 3];
- int index2 = indices[i * 3 + 1];
- int index3 = indices[i * 3 + 2];
- Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
- Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
- Vector3 normal = Vector3.Cross(side1, side2);
- vertices[index1].Normal += normal;
- vertices[index2].Normal += normal;
- vertices[index3].Normal += normal;
- }
- for (int i = 0; i < vertices.Length; i++)
- vertices[i].Normal.Normalize();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement