| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #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
- {
- /// <summary>
- /// Sample showing how to use geometry that is programatically
- /// generated as part of the content pipeline build process.
- /// </summary>
- 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
- }
- /// <summary>
- /// Load your graphics content.
- /// </summary>
- protected override void LoadContent()
- {
- terrain = Content.Load<Model>("terrain");
- sky = Content.Load<Sky>("sky");
- }
- #endregion
- #region Update and Draw
- /// <summary>
- /// Allows the game to run logic.
- /// </summary>
- protected override void Update(GameTime gameTime)
- {
- HandleInput();
-
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- 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);
- }
- /// <summary>
- /// Helper for drawing the terrain model.
- /// </summary>
- 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
- /// <summary>
- /// Handles input for quitting the game.
- /// </summary>
- 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
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static class Program
- {
- static void Main()
- {
- using (GeneratedGeometryGame game = new GeneratedGeometryGame())
- {
- game.Run();
- }
- }
- }
- #endregion
- }
|