#region File Description //----------------------------------------------------------------------------- // GeneratedGeometry.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; #endregion namespace GeneratedGeometry { /// /// Sample showing how to use geometry that is programatically /// generated as part of the content pipeline build process. /// public class GeneratedGeometryGame : Microsoft.Xna.Framework.Game { #region Fields GraphicsDeviceManager graphics; Model terrain; Sky sky; #endregion #region Initialization public GeneratedGeometryGame() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; #if WINDOWS_PHONE // Frame rate is 30 fps by default for Windows Phone. TargetElapsedTime = TimeSpan.FromTicks(333333); graphics.IsFullScreen = true; #endif } /// /// Load your graphics content. /// protected override void LoadContent() { terrain = Content.Load("terrain"); sky = Content.Load("sky"); } #endregion #region Update and Draw /// /// Allows the game to run logic. /// protected override void Update(GameTime gameTime) { HandleInput(); base.Update(gameTime); } /// /// This is called when the game should draw itself. /// protected override void Draw(GameTime gameTime) { GraphicsDevice device = graphics.GraphicsDevice; device.Clear(Color.Black); // Calculate the projection matrix. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1, 10000); // Calculate a view matrix, moving the camera around a circle. float time = (float)gameTime.TotalGameTime.TotalSeconds * 0.333f; float cameraX = (float)Math.Cos(time); float cameraY = (float)Math.Sin(time); Vector3 cameraPosition = new Vector3(cameraX, 0, cameraY) * 64; Vector3 cameraFront = new Vector3(-cameraY, 0, cameraX); Matrix view = Matrix.CreateLookAt(cameraPosition, cameraPosition + cameraFront, Vector3.Up); // Draw the terrain first, then the sky. This is faster than // drawing the sky first, because the depth buffer can skip // bothering to draw sky pixels that are covered up by the // terrain. This trick works because the code used to draw // the sky forces all the sky vertices to be as far away as // possible, and turns depth testing on but depth writes off. DrawTerrain(view, projection); sky.Draw(view, projection); // If there was any alpha blended translucent geometry in // the scene, that would be drawn here, after the sky. base.Draw(gameTime); } /// /// Helper for drawing the terrain model. /// void DrawTerrain(Matrix view, Matrix projection) { foreach (ModelMesh mesh in terrain.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.View = view; effect.Projection = projection; effect.EnableDefaultLighting(); // Set the specular lighting to match the sky color. effect.SpecularColor = new Vector3(0.6f, 0.4f, 0.2f); effect.SpecularPower = 8; // Set the fog to match the distant mountains // that are drawn into the sky texture. effect.FogEnabled = true; effect.FogColor = new Vector3(0.15f); effect.FogStart = 100; effect.FogEnd = 320; } mesh.Draw(); } } #endregion #region Handle Input /// /// Handles input for quitting the game. /// private void HandleInput() { KeyboardState currentKeyboardState = Keyboard.GetState(); GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One); // Check for exit. if (currentKeyboardState.IsKeyDown(Keys.Escape) || currentGamePadState.Buttons.Back == ButtonState.Pressed) { Exit(); } } #endregion } #region Entry Point /// /// The main entry point for the application. /// static class Program { static void Main() { using (GeneratedGeometryGame game = new GeneratedGeometryGame()) { game.Run(); } } } #endregion }