Model.h 6.2 KB

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