Model.h 5.7 KB

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