| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #region File Description
- //-----------------------------------------------------------------------------
- // CustomModelEffect.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- #endregion
- namespace CustomModelEffect
- {
- /// <summary>
- /// Sample shows how to render a model using a custom effect.
- /// </summary>
- public class CustomModelEffectGame : Microsoft.Xna.Framework.Game
- {
- #region Fields
- GraphicsDeviceManager graphics;
- Model model;
- #endregion
- #region Initialization
- public CustomModelEffectGame()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Load your graphics content.
- /// </summary>
- protected override void LoadContent()
- {
- model = Content.Load<Model>("saucer");
- }
- #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.CornflowerBlue);
- // Calculate the camera matrices.
- Viewport viewport = device.Viewport;
- float aspectRatio = (float)viewport.Width / (float)viewport.Height;
- float time = (float)gameTime.TotalGameTime.TotalSeconds;
- Matrix rotation = Matrix.CreateRotationX(time * 0.3f) *
- Matrix.CreateRotationY(time);
- Matrix view = Matrix.CreateLookAt(new Vector3(4000, 0, 0),
- Vector3.Zero,
- Vector3.Up);
- Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
- aspectRatio,
- 10, 10000);
- // Draw the model.
- Matrix[] transforms = new Matrix[model.Bones.Count];
- model.CopyAbsoluteBoneTransformsTo(transforms);
- foreach (ModelMesh mesh in model.Meshes)
- {
- foreach (Effect effect in mesh.Effects)
- {
- Matrix world = transforms[mesh.ParentBone.Index] * rotation;
- effect.Parameters["World"].SetValue(world);
- effect.Parameters["View"].SetValue(view);
- effect.Parameters["Projection"].SetValue(projection);
- }
- mesh.Draw();
- }
- base.Draw(gameTime);
- }
- #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 (CustomModelEffectGame game = new CustomModelEffectGame())
- {
- game.Run();
- }
- }
- }
- #endregion
- }
|