Mesh.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #ifndef MESH_H_
  2. #define MESH_H_
  3. #include "Ref.h"
  4. #include "VertexFormat.h"
  5. #include "Vector3.h"
  6. #include "BoundingBox.h"
  7. #include "BoundingSphere.h"
  8. namespace gameplay
  9. {
  10. class MeshPart;
  11. class Material;
  12. class Model;
  13. /**
  14. * Defines a mesh supporting various vertex formats and 1 or more
  15. * MeshPart(s) to define how the vertices are connected.
  16. */
  17. class Mesh : public Ref
  18. {
  19. friend class Model;
  20. friend class Bundle;
  21. public:
  22. /**
  23. * Defines supported index formats.
  24. */
  25. enum IndexFormat
  26. {
  27. INDEX8 = GL_UNSIGNED_BYTE,
  28. INDEX16 = GL_UNSIGNED_SHORT,
  29. INDEX32 = GL_UNSIGNED_INT
  30. };
  31. /**
  32. * Defines supported primitive types.
  33. */
  34. enum PrimitiveType
  35. {
  36. TRIANGLES = GL_TRIANGLES,
  37. TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
  38. LINES = GL_LINES,
  39. LINE_STRIP = GL_LINE_STRIP,
  40. POINTS = GL_POINTS
  41. };
  42. /**
  43. * Constructs a new mesh with the specified vertex format.
  44. *
  45. * @param vertexFormat The vertex format.
  46. * @param vertexCount The number of vertices.
  47. * @param dynamic true if the mesh is dynamic; false otherwise.
  48. *
  49. * @return The created mesh.
  50. */
  51. static Mesh* createMesh(const VertexFormat& vertexFormat, unsigned int vertexCount, bool dynamic = false);
  52. /**
  53. * Creates a new textured 3D quad.
  54. *
  55. * The specified points should describe a triangle strip with the first 3 points
  56. * forming a triangle wound in counter-clockwise direction, with the second triangle
  57. * formed from the last three points in clockwise direction.
  58. *
  59. * @param p1 The first point.
  60. * @param p2 The second point.
  61. * @param p3 The third point.
  62. * @param p4 The fourth point.
  63. *
  64. * @return The created mesh.
  65. */
  66. static Mesh* createQuad(const Vector3& p1, const Vector3& p2, const Vector3& p3, const Vector3& p4);
  67. /**
  68. * Constructs a new textured 2D quad.
  69. *
  70. * @param x The x coordinate.
  71. * @param y The y coordinate.
  72. * @param width The width of the quad.
  73. * @param height The height of the quad.
  74. *
  75. * @return The newly created mesh.
  76. */
  77. static Mesh* createQuad(float x, float y, float width, float height);
  78. /**
  79. * Creates a new full-screen 2D quad.
  80. *
  81. * The returned mesh's vertex format includes a 2-element (x,y) position
  82. * and a 2-element texture coordinate.
  83. *
  84. * This method returns a mesh describing a fullscreen quad using
  85. * normalized device coordinates for vertex positions.
  86. *
  87. * @return The newly created mesh.
  88. */
  89. static Mesh* createQuadFullscreen();
  90. /**
  91. * Creates lines between 2 or more points passed in as a Vector3 array.
  92. *
  93. * The mesh contains only position data using lines to connect the vertices.
  94. * This is useful for drawing basic color elements into a scene.
  95. *
  96. * @param points The array of points.
  97. * @param pointCount The number of points.
  98. *
  99. * @return The newly created mesh.
  100. */
  101. static Mesh* createLines(Vector3* points, unsigned int pointCount);
  102. /**
  103. * Creates a bounding box mesh when passed a BoundingBox.
  104. *
  105. * The mesh contains only position data using lines to connect the vertices.
  106. *
  107. * @param box The BoundingBox that will be used to create the mesh.
  108. *
  109. * @return The newly created bounding box mesh.
  110. */
  111. static Mesh* createBoundingBox(const BoundingBox& box);
  112. /**
  113. * Returns a URL from which the mesh was loaded from.
  114. *
  115. * For meshes loaded from a Bundle, this URL will point
  116. * to the file and ID of the mesh within the bundle. For
  117. * all other meshes, an empty string will be returned.
  118. */
  119. const char* getUrl() const;
  120. /**
  121. * Gets the vertex format for the mesh.
  122. *
  123. * @return The vertex format.
  124. */
  125. const VertexFormat& getVertexFormat() const;
  126. /**
  127. * Gets the number of vertices in the mesh.
  128. *
  129. * @return The number of vertices in the mesh.
  130. */
  131. unsigned int getVertexCount() const;
  132. /**
  133. * Gets the size of a single vertex in the mesh.
  134. *
  135. * @return The size of 1 vertex in the mesh.
  136. */
  137. unsigned int getVertexSize() const;
  138. /**
  139. * Returns a handle to the vertex buffer for the mesh.
  140. *
  141. * @return The vertex buffer object handle.
  142. */
  143. VertexBufferHandle getVertexBuffer() const;
  144. /**
  145. * Determines if the mesh is dynamic.
  146. *
  147. * @return true if the mesh is dynamic; false otherwise.
  148. */
  149. bool isDynamic() const;
  150. /**
  151. * Returns the primitive type of the vertices in the mesh.
  152. *
  153. * The default primitive type for a Mesh is TRIANGLES.
  154. *
  155. * @return The primitive type.
  156. *
  157. * @see setPrimitiveType(PrimitiveType)
  158. */
  159. PrimitiveType getPrimitiveType() const;
  160. /**
  161. * Sets the primitive type for the vertices in the mesh.
  162. *
  163. * The primitive type for a Mesh is only meaningful for meshes that do not
  164. * have any MeshParts. When there are no MeshParts associated with a mesh,
  165. * the Mesh is drawn as non-indexed geometry and the PrimitiveType of the Mesh
  166. * determines how the vertices are interpreted when drawn.
  167. *
  168. * @param type The new primitive type.
  169. */
  170. void setPrimitiveType(Mesh::PrimitiveType type);
  171. /**
  172. * Sets the specified vertex data into the mapped vertex buffer.
  173. *
  174. * @param vertexData The vertex data to be set.
  175. * @param vertexStart The index of the starting vertex (0 by default).
  176. * @param vertexCount The number of vertices to be set (default is 0, for all vertices).
  177. */
  178. void setVertexData(void* vertexData, unsigned int vertexStart = 0, unsigned int vertexCount = 0);
  179. /**
  180. * Creates and adds a new part of primitive data defining how the vertices are connected.
  181. *
  182. * @param primitiveType The type of primitive data to connect the indices as.
  183. * @param indexFormat The format of the indices. SHORT or INT.
  184. * @param indexCount The number of indices to be contained in the part.
  185. * @param dynamic true if the index data is dynamic; false otherwise.
  186. *
  187. * @return The newly created/added mesh part.
  188. */
  189. MeshPart* addPart(PrimitiveType primitiveType, Mesh::IndexFormat indexFormat, unsigned int indexCount, bool dynamic = false);
  190. /**
  191. * Gets the number of mesh parts contained within the mesh.
  192. *
  193. * @return The number of mesh parts contained within the mesh.
  194. */
  195. unsigned int getPartCount() const;
  196. /**
  197. * Gets a MeshPart by index.
  198. *
  199. * @param index The index of the MeshPart to get.
  200. *
  201. * @return The MeshPart at the specified index.
  202. */
  203. MeshPart* getPart(unsigned int index);
  204. /**
  205. * Returns the bounding box for the points in this mesh.
  206. *
  207. * Only meshes loaded from bundle files are imported with valid
  208. * bounding volumes. Programmatically created meshes will contain
  209. * empty bounding volumes until the setBoundingBox and/or
  210. * setBoundingSphere methods are called to specify the mesh's
  211. * local bounds.
  212. *
  213. * Meshes that are attached to a Model with a MeshSkin will have
  214. * a bounding volume that is not neccessarily tight fighting on the
  215. * Mesh vertices. Instead, the bounding volume will be an approximation
  216. * that contains all possible vertex positions in all possible poses after
  217. * skinning is applied. This is neccessary since skinning vertices
  218. * result in vertex positions that lie outside the original mesh bounds
  219. * and could otherwise result in a bounding volume that does not fully
  220. * contain an animated/skinned mesh.
  221. *
  222. * @return The bounding box for the mesh.
  223. */
  224. const BoundingBox& getBoundingBox() const;
  225. /**
  226. * Sets the bounding box for this mesh.
  227. *
  228. * @param box The new bounding box for the mesh.
  229. */
  230. void setBoundingBox(const BoundingBox& box);
  231. /**
  232. * Returns the bounding sphere for the points in the mesh.
  233. *
  234. * Only meshes loaded from bundle files are imported with valid
  235. * bounding volumes. Programmatically created meshes will contain
  236. * empty bounding volumes until the setBoundingBox and/or
  237. * setBoundingSphere methods are called to specify the mesh's
  238. * local bounds.
  239. *
  240. * Meshes that are attached to a Model with a MeshSkin will have
  241. * a bounding volume that is not neccessarily tight fighting on the
  242. * Mesh vertices. Instead, the bounding volume will be an approximation
  243. * that contains all possible vertex positions in all possible poses after
  244. * skinning is applied. This is neccessary since skinning vertices
  245. * result in vertex positions that lie outside the original mesh bounds
  246. * and could otherwise result in a bounding volume that does not fully
  247. * contain an animated/skinned mesh.
  248. *
  249. * @return The bounding sphere for the mesh.
  250. */
  251. const BoundingSphere& getBoundingSphere() const;
  252. /**
  253. * Sets the bounding sphere for this mesh.
  254. *
  255. * @param sphere The new bounding sphere for the mesh.
  256. */
  257. void setBoundingSphere(const BoundingSphere& sphere);
  258. /**
  259. * Destructor.
  260. */
  261. virtual ~Mesh();
  262. private:
  263. /**
  264. * Constructor.
  265. */
  266. Mesh(const VertexFormat& vertexFormat);
  267. /**
  268. * Constructor.
  269. */
  270. Mesh(const Mesh& copy);
  271. std::string _url;
  272. const VertexFormat _vertexFormat;
  273. unsigned int _vertexCount;
  274. VertexBufferHandle _vertexBuffer;
  275. PrimitiveType _primitiveType;
  276. unsigned int _partCount;
  277. MeshPart** _parts;
  278. bool _dynamic;
  279. BoundingBox _boundingBox;
  280. BoundingSphere _boundingSphere;
  281. };
  282. }
  283. #endif