#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 { /// /// Sample shows how to render a model using a custom effect. /// 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"; } /// /// Load your graphics content. /// protected override void LoadContent() { model = Content.Load("saucer"); } #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.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 /// /// 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 (CustomModelEffectGame game = new CustomModelEffectGame()) { game.Run(); } } } #endregion }