/// <summary> /// Draw the loaded model using the provided effect /// </summary> /// <param name="effect"></param> protected virtual void DrawModel(BasicEffect effect) { Matrix initialWorld; Matrix[] boneTransforms; // Ensure we have a model to draw if (ObjectModel == null) { return; } // Store the initial world matrix initialWorld = effect.World; // Build an array of the absolute bone transformation matrices boneTransforms = new Matrix[ObjectModel.Bones.Count]; ObjectModel.CopyAbsoluteBoneTransformsTo(boneTransforms); // Loop for each mesh foreach (ModelMesh mesh in ObjectModel.Meshes) { // Update the world matrix to account for the position of this bone effect.World = boneTransforms[mesh.ParentBone.Index] * initialWorld; // Loop for each mesh part foreach (ModelMeshPart meshpart in mesh.MeshParts) { // Set the texture for this meshpart unless we have been explicitly // given a texture to use if (base.ObjectTexture != null) { SetEffectTexture(effect, base.ObjectTexture); } else { SetEffectTexture(effect, ((BasicEffect)meshpart.Effect).Texture); } // Set the vertex and index buffers effect.GraphicsDevice.SetVertexBuffer(meshpart.VertexBuffer); effect.GraphicsDevice.Indices = meshpart.IndexBuffer; // Draw the mesh part foreach (EffectPass pass in effect.CurrentTechnique.Passes) { // Apply the pass pass.Apply(); // Draw this meshpart effect.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, meshpart.VertexOffset, 0, meshpart.NumVertices, meshpart.StartIndex, meshpart.PrimitiveCount); } } } // Restore the initial world matrix effect.World = initialWorld; }