[Index page]
[Download source code]
Dream come true XNA (9)-3D Model
Author: webabcd
Introduction
XNA: 3D model. Rotate the 3D model using the "up/down" key on the keyboard
Example
Draw a 3D model and use the "up/down/left" key to control its rotation (press the keyboard R key to load this Demo)
3D/3 DModel/RotationDirection. cs
Namespace XNA. component. _ 3D. _ 3 DModel {// <summary> // orientation of rotation /// </summary> public enum RotationDirection {Up, Down, Left, Right }}
3D/3 DModel/MyCamera. cs
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 XNA. component. _ 3D. _ 3 DModel {// <summary> /// custom Camera class // </summary> public class MyCamera: Microsoft. xna. framework. gameComponent {// View Matrix, used to set the camera position and direction public Matrix View {get; protected set;} // Projection Matrix, used to project a 3D object to a 2D screen. public Matrix Projection {get; protected set;} public MyCamera (Game game, Vector3 pos, Vector3 target, Vector3 up): base (game) {/** Matrix CreateLookAt (Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector) -instantiate the View Matrix * Vector3 cameraPosition-camera position coordinate * Vector3 cameraTarget-orientation vector of the camera lens * Vector3 cameraUpVector-vector above the top of the camera body */View = Matrix. createLookAt (pos, target, up);/** CreatePerspectiveFieldOfView (float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance) -instantiate the angle radian of the projection matrix * float fieldOfView-Y axis, which is generally the aspect ratio of the 1/4 PI * float aspectRatio-visible area, generally, the width of the game window is divided by the height of the game window * float nearPlaneDistance-when the object is too close to the camera, it cannot be seen * float farPlaneDistance-when the object is too far away from the camera, it cannot be seen */Projection = matrix. createPerspectiveFieldOfView (MathHelper. piOver4, // 1/4 PI (there are many useful functions in MathHelper) (float) Game. window. clientBounds. width/(float) Game. window. clientBounds. height, 1, 3000);} public override void Initialize () {base. initialize ();} public override void Update (GameTime gameTime) {base. update (gameTime );}}}
3D/3 DModel/MyModel. cs
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 XNA. component. _ 3D. _ 3 DModel {// <summary> /// custom 3D model class /// </summa Ry> public class MyModel {// Model-represents a 3D Model. Texture2D private Model _ model is used to represent 2D objects. // The rotating Matrix private Matrix _ rotation = Matrix. identity; // initial Matrix private Matrix _ world = Matrix. identity; public MyModel (Model model, Matrix world) {_ model = model; _ world = world ;} /// <summary> /// Draw a 3D Model /// </summary> public void Draw (MyCamera camera) {/** a Model object contains multiple ModelMesh objects, each ModelMesh object contains the texture, color, and other information of the mesh */foreach (ModelMesh in _ model. meshes) {/** A ModelMesh object contains multiple Effect */foreach (BasicEffect effect in mesh) used to draw ModelMesh. effects) {// use the default light source effect. enabledefalightlighting (); effect. projection = camera. projection; effect. view = camera. view; effect. world = GetWorld ();} // draw the ModelMesh mesh. draw () ;}/// <summary> /// calculate the rotation matrix based on the direction (up and down) /// </summary> public void Rotate (RotationDirection direction) {switch (direction) {case RotationDirection. up: _ rotation * = Matrix. createRotationX (-MathHelper. pi/180); // radians that rotate around the X axis break; case RotationDirection. down: _ rotation * = Matrix. createRotationX (MathHelper. pi/180); break; case RotationDirection. left: _ rotation * = Matrix. createRotationY (-MathHelper. pi/180); // radians that rotate around the Y axis break; case RotationDirection. right: _ rotation * = Matrix. createRotationY (MathHelper. pi/180); break ;}/// <summary> // obtain the world Matrix, that is, multiply the initial Matrix by the rotation Matrix /// </summary> public Matrix GetWorld () {return _ world * _ rotation ;}}}
3D/3 DModel/Demo. cs
/** XNA only supports. x and. fbx format 3D model file * In this example, the 3D model is located in XNAContent/3 DModel/Spaceship. x ** in this example: rotate the 3D model using the "up/down" key on the keyboard */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 XNA. component. _ 3D. _ 3 DModel {public class Demo: Microsoft. xna. framework. drawableGameComponent {private MyCamera _ camera; private List <MyModel> _ models = new List <MyModel> (); public Demo (Game game): base (game) {} public override void Initialize () {// Add camera component _ camera = new MyCamera (Game, new Vector3 (0, 0, 50), Vector3.Zero, Vector3.Up); Game. components. add (_ camera); base. initialize ();} protected override void LoadContent () {// Add two 3D models _ models in different locations. add (new MyModel (Game. content. load <Model> (@ "3DModel \ Spaceship"), Matrix. identity * Matrix. createTranslation (-10,-5,-20); _ models. add (new MyModel (Game. content. load <Model> (@ "3DModel \ Spaceship"), Matrix. identity * Matrix. createTranslation (10, 5, 0); base. loadContent ();} public override void Update (GameTime gameTime) {// rotate the 3D model KeyboardState keyboardState = Keyboard based on the user's buttons (up, down, and left. getState (); foreach (MyModel in _ models) {if (keyboardState. isKeyDown (Keys. up) model. rotate (RotationDirection. up); else if (keyboardState. isKeyDown (Keys. down) model. rotate (RotationDirection. down); else if (keyboardState. isKeyDown (Keys. left) model. rotate (RotationDirection. left); else if (keyboardState. isKeyDown (Keys. right) model. rotate (RotationDirection. right);} base. update (gameTime);} public override void Draw (GameTime gameTime) {GraphicsDevice. clear (Color. cornflowerBlue); // draw a 3D model foreach (MyModel model in _ models) {model. draw (_ camera);} base. update (gameTime );}}}
OK
[Download source code]