Model.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef MODEL_H_
  2. #define MODEL_H_
  3. #include "Mesh.h"
  4. #include "MeshSkin.h"
  5. #include "Material.h"
  6. namespace gameplay
  7. {
  8. class Bundle;
  9. class MeshSkin;
  10. class Node;
  11. class NodeCloneContext;
  12. /**
  13. * Defines a Model which is an instance of a Mesh that can be drawn
  14. * with the specified Materials.
  15. */
  16. class Model : public Ref
  17. {
  18. friend class Node;
  19. friend class Mesh;
  20. friend class Bundle;
  21. public:
  22. /**
  23. * Creates a new Model.
  24. */
  25. static Model* create(Mesh* mesh);
  26. /**
  27. * Returns the Mesh for this Model.
  28. *
  29. * @return The Mesh for this Model.
  30. */
  31. Mesh* getMesh() const;
  32. /**
  33. * Returns the number of parts in the Mesh for this Model.
  34. *
  35. * @return The number of parts in the Mesh for this Model.
  36. */
  37. unsigned int getMeshPartCount() const;
  38. /**
  39. * Returns the Material currently bound to the specified mesh part.
  40. *
  41. * If partIndex is >= 0 and no Material is directly bound to the specified
  42. * mesh part, the shared Material will be returned.
  43. *
  44. * @param partIndex The index of the mesh part whose Material to return (-1 for shared material).
  45. *
  46. * @return The requested Material, or NULL if no Material is set.
  47. */
  48. Material* getMaterial(int partIndex = -1);
  49. /**
  50. * Sets a material to be used for drawing this Model.
  51. *
  52. * The specified Material is applied for the MeshPart at the given index in
  53. * this Model's Mesh. A partIndex of -1 sets a shared Material for
  54. * all mesh parts, whereas a value of 0 or greater sets the Material for the
  55. * specified mesh part only.
  56. *
  57. * Mesh parts will use an explicitly set part material, if set; otherwise they
  58. * will use the globally set material.
  59. *
  60. * @param material The new material.
  61. * @param partIndex The index of the mesh part to set the material for (-1 for shared material).
  62. */
  63. void setMaterial(Material* material, int partIndex = -1);
  64. /**
  65. * Sets a material to be used for drawing this Model.
  66. *
  67. * A Material is created from the given vertex and fragment shader source files.
  68. * The Material is applied for the MeshPart at the given index in this Model's
  69. * Mesh. A partIndex of -1 sets a shared Material for all mesh parts, whereas a
  70. * value of 0 or greater sets the Material for the specified mesh part only.
  71. *
  72. * Mesh parts will use an explicitly set part material, if set; otherwise they
  73. * will use the globally set material.
  74. *
  75. * @param vshPath The path to the vertex shader file.
  76. * @param fshPath The path to the fragment shader file.
  77. * @param defines A new-line delimited list of preprocessor defines. May be NULL.
  78. * @param partIndex The index of the mesh part to set the material for (-1 for shared material).
  79. *
  80. * @return The newly created and bound Material, or NULL if the Material could not be created.
  81. */
  82. Material* setMaterial(const char* vshPath, const char* fshPath, const char* defines = NULL, int partIndex = -1);
  83. /**
  84. * Sets a material to be used for drawing this Model.
  85. *
  86. * A Material is created from the specified material file.
  87. * The Material is applied for the MeshPart at the given index in this Model's
  88. * Mesh. A partIndex of -1 sets a shared Material for all mesh parts, whereas a
  89. * value of 0 or greater sets the Material for the specified mesh part only.
  90. *
  91. * Mesh parts will use an explicitly set part material, if set; otherwise they
  92. * will use the globally set material.
  93. *
  94. * @param materialPath The path to the material file.
  95. * @param partIndex The index of the mesh part to set the material for (-1 for shared material).
  96. *
  97. * @return The newly created and bound Material, or NULL if the Material could not be created.
  98. */
  99. Material* setMaterial(const char* materialPath, int partIndex = -1);
  100. /**
  101. * Determines if a custom (non-shared) material is set for the specified part index.
  102. *
  103. * @param partIndex MeshPart index.
  104. *
  105. * @return True if a custom MeshPart material is set for the specified index, false otherwise.
  106. */
  107. bool hasMaterial(unsigned int partIndex) const;
  108. /**
  109. * Returns the MeshSkin.
  110. *
  111. * @return The MeshSkin, or NULL if one is not set.
  112. */
  113. MeshSkin* getSkin() const;
  114. /**
  115. * Returns the node that is associated with this model.
  116. *
  117. * @return The node that is associated with this model.
  118. */
  119. Node* getNode() const;
  120. /**
  121. * Draws this mesh instance.
  122. *
  123. * This method binds the vertex buffer and index buffers for the Mesh and
  124. * all of its MeshParts and draws the mesh geometry. Any other state
  125. * neccessary to render the Mesh, such as rendering states, shader state,
  126. * and so on, should be set up before calling this method.
  127. *
  128. * @param wireframe If true, draw the model in wireframe mode.
  129. */
  130. void draw(bool wireframe = false);
  131. private:
  132. /**
  133. * Constructor.
  134. */
  135. Model(Mesh* mesh);
  136. /**
  137. * Destructor. Hidden use release() instead.
  138. */
  139. ~Model();
  140. /**
  141. * Sets the MeshSkin for this model.
  142. *
  143. * @param skin The MeshSkin for this model.
  144. */
  145. void setSkin(MeshSkin* skin);
  146. /**
  147. * Sets the node that is associated with this model.
  148. *
  149. * @param node The node that is associated with this model.
  150. */
  151. void setNode(Node* node);
  152. /**
  153. * Sets the specified materia's node binding to this model's node.
  154. */
  155. void setMaterialNodeBinding(Material *m);
  156. void validatePartCount();
  157. /**
  158. * Clones the model and returns a new model.
  159. *
  160. * @param context The clone context.
  161. * @return The new cloned model.
  162. */
  163. Model* clone(NodeCloneContext &context);
  164. Mesh* _mesh;
  165. Material* _material;
  166. unsigned int _partCount;
  167. Material** _partMaterials;
  168. Node* _node;
  169. MeshSkin* _skin;
  170. };
  171. }
  172. #endif