CustomModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CustomModel.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System.Collections.Generic;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace CustomModelSample
  16. {
  17. /// <summary>
  18. /// Custom class that can be used as a replacement for the built-in Model type.
  19. /// This provides functionality roughly similar to Model, but simplified as far
  20. /// as possible while still being able to correctly render data from arbitrary
  21. /// X or FBX files. This can be used as a starting point for building up your
  22. /// own more sophisticated Model replacements.
  23. /// </summary>
  24. public class CustomModel
  25. {
  26. #region Fields
  27. // Disable compiler warning that we never initialize these fields.
  28. // That's ok, because the XNB deserializer initialises them for us!
  29. #pragma warning disable 649
  30. // Internally our custom model is made up from a list of model parts.
  31. [ContentSerializer]
  32. List<ModelPart> modelParts;
  33. // Each model part represents a piece of geometry that uses one
  34. // single effect. Multiple parts are needed for models that use
  35. // more than one effect.
  36. class ModelPart
  37. {
  38. public int TriangleCount;
  39. public int VertexCount;
  40. public VertexBuffer VertexBuffer;
  41. public IndexBuffer IndexBuffer;
  42. [ContentSerializer(SharedResource = true)]
  43. public Effect Effect;
  44. }
  45. #pragma warning restore 649
  46. #endregion
  47. /// <summary>
  48. /// Private constructor, for use by the XNB deserializer.
  49. /// </summary>
  50. private CustomModel()
  51. {
  52. }
  53. /// <summary>
  54. /// Draws the model using the specified camera matrices.
  55. /// </summary>
  56. public void Draw(Matrix world, Matrix view, Matrix projection)
  57. {
  58. foreach (ModelPart modelPart in modelParts)
  59. {
  60. // Look up the effect, and set effect parameters on it. This sample
  61. // assumes the model will only be using BasicEffect, but a more robust
  62. // implementation would probably want to handle custom effects as well.
  63. BasicEffect effect = (BasicEffect)modelPart.Effect;
  64. effect.EnableDefaultLighting();
  65. effect.World = world;
  66. effect.View = view;
  67. effect.Projection = projection;
  68. // Set the graphics device to use our vertex declaration,
  69. // vertex buffer, and index buffer.
  70. GraphicsDevice device = effect.GraphicsDevice;
  71. device.SetVertexBuffer(modelPart.VertexBuffer);
  72. device.Indices = modelPart.IndexBuffer;
  73. // Loop over all the effect passes.
  74. foreach (EffectPass pass in effect.CurrentTechnique.Passes)
  75. {
  76. pass.Apply();
  77. // Draw the geometry.
  78. device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
  79. 0, 0, modelPart.VertexCount,
  80. 0, modelPart.TriangleCount);
  81. }
  82. }
  83. }
  84. }
  85. }