|
Visualizzazione di un modello 3D in rotazione su 3 assi
Per caricare e animare il modello 3D importato in modo tale da ruotarlo in real-time intorno ai 3 assi, dobbiamo modificare alcune istruzioni viste nel capitolo precedente. Modifichiamo il metodo Draw come riportato di seguito: protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); //Copy any parent transforms Matrix[] transforms = new Matrix[myModel.Bones.Count]; myModel.CopyAbsoluteBoneTransformsTo(transforms); //Draw the model, a model can have multiple meshes, so loop foreach (ModelMesh mesh in myModel.Meshes) { //This is where the mesh orientation is set, as well as our camera and projection foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateRotationZ(modelRotationZ) * Matrix.CreateRotationX(modelRotationX) * Matrix.CreateTranslation(modelPosition); effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } Draw the mesh, will use the effects set above. mesh.Draw(); } } Immediatamente sopra al metodo Draw inseriamo le seguenti stringhe: float modelRotationX = 0.0f; float modelRotationZ = 0.0f; Modifichiamo il metodo Update come riportato di seguito: protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f); modelRotation += 0.001f; modelRotationX += 0.01f; modelRotationZ += 0.1f; base.Update(gameTime); }
In sostanza, mentre nel precedente progetto il modello ruotava solamente intorno all'asse Y, in questo caso sono state introdotte le altre 2 componenti di rotazione intorno a X e Z indipendenti l'una dall'altra. Il risultato è il seguente: 
VIDEO
|