Model.h 6.3 KB

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