Mesh.h 10 KB

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