Mesh.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include "Base.h"
  2. #include "Mesh.h"
  3. #include "MeshPart.h"
  4. #include "Effect.h"
  5. #include "Model.h"
  6. #include "Material.h"
  7. namespace gameplay
  8. {
  9. Mesh::Mesh(const VertexFormat& vertexFormat)
  10. : _vertexFormat(vertexFormat), _vertexCount(0), _vertexBuffer(0), _primitiveType(TRIANGLES),
  11. _partCount(0), _parts(NULL), _dynamic(false)
  12. {
  13. }
  14. Mesh::~Mesh()
  15. {
  16. if (_parts)
  17. {
  18. for (unsigned int i = 0; i < _partCount; ++i)
  19. {
  20. SAFE_DELETE(_parts[i]);
  21. }
  22. SAFE_DELETE_ARRAY(_parts);
  23. }
  24. if (_vertexBuffer)
  25. {
  26. glDeleteBuffers(1, &_vertexBuffer);
  27. _vertexBuffer = 0;
  28. }
  29. }
  30. Mesh* Mesh::createMesh(const VertexFormat& vertexFormat, unsigned int vertexCount, bool dynamic)
  31. {
  32. GLuint vbo;
  33. GL_ASSERT( glGenBuffers(1, &vbo) );
  34. GL_ASSERT( glBindBuffer(GL_ARRAY_BUFFER, vbo) );
  35. GL_ASSERT( glBufferData(GL_ARRAY_BUFFER, vertexFormat.getVertexSize() * vertexCount, NULL, dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW) );
  36. Mesh* mesh = new Mesh(vertexFormat);
  37. mesh->_vertexCount = vertexCount;
  38. mesh->_vertexBuffer = vbo;
  39. mesh->_dynamic = dynamic;
  40. return mesh;
  41. }
  42. Mesh* Mesh::createQuad(float x, float y, float width, float height, float s1, float t1, float s2, float t2)
  43. {
  44. float x2 = x + width;
  45. float y2 = y + height;
  46. float vertices[] =
  47. {
  48. x, y2, 0, 0, 0, 1, s1, t2,
  49. x, y, 0, 0, 0, 1, s1, t1,
  50. x2, y2, 0, 0, 0, 1, s2, t2,
  51. x2, y, 0, 0, 0, 1, s2, t1,
  52. };
  53. VertexFormat::Element elements[] =
  54. {
  55. VertexFormat::Element(VertexFormat::POSITION, 3),
  56. VertexFormat::Element(VertexFormat::NORMAL, 3),
  57. VertexFormat::Element(VertexFormat::TEXCOORD0, 2)
  58. };
  59. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 3), 4, false);
  60. if (mesh == NULL)
  61. {
  62. GP_ERROR("Failed to create mesh.");
  63. return NULL;
  64. }
  65. mesh->_primitiveType = TRIANGLE_STRIP;
  66. mesh->setVertexData(vertices, 0, 4);
  67. return mesh;
  68. }
  69. Mesh* Mesh::createQuadFullscreen()
  70. {
  71. float x = -1.0f;
  72. float y = -1.0f;
  73. float x2 = 1.0f;
  74. float y2 = 1.0f;
  75. float vertices[] =
  76. {
  77. x, y2, 0, 1,
  78. x, y, 0, 0,
  79. x2, y2, 1, 1,
  80. x2, y, 1, 0
  81. };
  82. VertexFormat::Element elements[] =
  83. {
  84. VertexFormat::Element(VertexFormat::POSITION, 2),
  85. VertexFormat::Element(VertexFormat::TEXCOORD0, 2)
  86. };
  87. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 2), 4, false);
  88. if (mesh == NULL)
  89. {
  90. GP_ERROR("Failed to create mesh.");
  91. return NULL;
  92. }
  93. mesh->_primitiveType = TRIANGLE_STRIP;
  94. mesh->setVertexData(vertices, 0, 4);
  95. return mesh;
  96. }
  97. Mesh* Mesh::createQuad(const Vector3& p1, const Vector3& p2, const Vector3& p3, const Vector3& p4)
  98. {
  99. // Calculate the normal vector of the plane.
  100. Vector3 v1, v2, n;
  101. Vector3::subtract(p2, p1, &v1);
  102. Vector3::subtract(p3, p2, &v2);
  103. Vector3::cross(v1, v2, &n);
  104. n.normalize();
  105. float vertices[] =
  106. {
  107. p1.x, p1.y, p1.z, n.x, n.y, n.z, 0, 1,
  108. p2.x, p2.y, p2.z, n.x, n.y, n.z, 0, 0,
  109. p3.x, p3.y, p3.z, n.x, n.y, n.z, 1, 1,
  110. p4.x, p4.y, p4.z, n.x, n.y, n.z, 1, 0
  111. };
  112. VertexFormat::Element elements[] =
  113. {
  114. VertexFormat::Element(VertexFormat::POSITION, 3),
  115. VertexFormat::Element(VertexFormat::NORMAL, 3),
  116. VertexFormat::Element(VertexFormat::TEXCOORD0, 2)
  117. };
  118. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 3), 4, false);
  119. if (mesh == NULL)
  120. {
  121. GP_ERROR("Failed to create mesh.");
  122. return NULL;
  123. }
  124. mesh->_primitiveType = TRIANGLE_STRIP;
  125. mesh->setVertexData(vertices, 0, 4);
  126. return mesh;
  127. }
  128. Mesh* Mesh::createLines(Vector3* points, unsigned int pointCount)
  129. {
  130. GP_ASSERT(points);
  131. GP_ASSERT(pointCount);
  132. float* vertices = new float[pointCount*3];
  133. memcpy(vertices, points, pointCount*3*sizeof(float));
  134. VertexFormat::Element elements[] =
  135. {
  136. VertexFormat::Element(VertexFormat::POSITION, 3)
  137. };
  138. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 1), pointCount, false);
  139. if (mesh == NULL)
  140. {
  141. GP_ERROR("Failed to create mesh.");
  142. SAFE_DELETE_ARRAY(vertices);
  143. return NULL;
  144. }
  145. mesh->_primitiveType = LINE_STRIP;
  146. mesh->setVertexData(vertices, 0, pointCount);
  147. SAFE_DELETE_ARRAY(vertices);
  148. return mesh;
  149. }
  150. Mesh* Mesh::createBoundingBox(const BoundingBox& box)
  151. {
  152. Vector3 corners[8];
  153. box.getCorners(corners);
  154. float vertices[] =
  155. {
  156. corners[7].x, corners[7].y, corners[7].z,
  157. corners[6].x, corners[6].y, corners[6].z,
  158. corners[1].x, corners[1].y, corners[1].z,
  159. corners[0].x, corners[0].y, corners[0].z,
  160. corners[7].x, corners[7].y, corners[7].z,
  161. corners[4].x, corners[4].y, corners[4].z,
  162. corners[3].x, corners[3].y, corners[3].z,
  163. corners[0].x, corners[0].y, corners[0].z,
  164. corners[0].x, corners[0].y, corners[0].z,
  165. corners[1].x, corners[1].y, corners[1].z,
  166. corners[2].x, corners[2].y, corners[2].z,
  167. corners[3].x, corners[3].y, corners[3].z,
  168. corners[4].x, corners[4].y, corners[4].z,
  169. corners[5].x, corners[5].y, corners[5].z,
  170. corners[2].x, corners[2].y, corners[2].z,
  171. corners[1].x, corners[1].y, corners[1].z,
  172. corners[6].x, corners[6].y, corners[6].z,
  173. corners[5].x, corners[5].y, corners[5].z
  174. };
  175. VertexFormat::Element elements[] =
  176. {
  177. VertexFormat::Element(VertexFormat::POSITION, 3)
  178. };
  179. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 1), 18, false);
  180. if (mesh == NULL)
  181. {
  182. GP_ERROR("Failed to create mesh.");
  183. return NULL;
  184. }
  185. mesh->_primitiveType = LINE_STRIP;
  186. mesh->setVertexData(vertices, 0, 18);
  187. return mesh;
  188. }
  189. const char* Mesh::getUrl() const
  190. {
  191. return _url.c_str();
  192. }
  193. const VertexFormat& Mesh::getVertexFormat() const
  194. {
  195. return _vertexFormat;
  196. }
  197. unsigned int Mesh::getVertexCount() const
  198. {
  199. return _vertexCount;
  200. }
  201. unsigned int Mesh::getVertexSize() const
  202. {
  203. return _vertexFormat.getVertexSize();
  204. }
  205. VertexBufferHandle Mesh::getVertexBuffer() const
  206. {
  207. return _vertexBuffer;
  208. }
  209. bool Mesh::isDynamic() const
  210. {
  211. return _dynamic;
  212. }
  213. Mesh::PrimitiveType Mesh::getPrimitiveType() const
  214. {
  215. return _primitiveType;
  216. }
  217. void Mesh::setPrimitiveType(PrimitiveType type)
  218. {
  219. _primitiveType = type;
  220. }
  221. void Mesh::setVertexData(const float* vertexData, unsigned int vertexStart, unsigned int vertexCount)
  222. {
  223. GL_ASSERT( glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer) );
  224. if (vertexStart == 0 && vertexCount == 0)
  225. {
  226. GL_ASSERT( glBufferData(GL_ARRAY_BUFFER, _vertexFormat.getVertexSize() * _vertexCount, vertexData, _dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW) );
  227. }
  228. else
  229. {
  230. if (vertexCount == 0)
  231. {
  232. vertexCount = _vertexCount - vertexStart;
  233. }
  234. GL_ASSERT( glBufferSubData(GL_ARRAY_BUFFER, vertexStart * _vertexFormat.getVertexSize(), vertexCount * _vertexFormat.getVertexSize(), vertexData) );
  235. }
  236. }
  237. MeshPart* Mesh::addPart(PrimitiveType primitiveType, IndexFormat indexFormat, unsigned int indexCount, bool dynamic)
  238. {
  239. MeshPart* part = MeshPart::create(this, _partCount, primitiveType, indexFormat, indexCount, dynamic);
  240. if (part)
  241. {
  242. // Increase size of part array and copy old subets into it.
  243. MeshPart** oldParts = _parts;
  244. _parts = new MeshPart*[_partCount + 1];
  245. for (unsigned int i = 0; i < _partCount; ++i)
  246. {
  247. _parts[i] = oldParts[i];
  248. }
  249. // Add new part to array.
  250. _parts[_partCount++] = part;
  251. // Delete old part array.
  252. SAFE_DELETE_ARRAY(oldParts);
  253. }
  254. return part;
  255. }
  256. unsigned int Mesh::getPartCount() const
  257. {
  258. return _partCount;
  259. }
  260. MeshPart* Mesh::getPart(unsigned int index)
  261. {
  262. GP_ASSERT(_parts);
  263. return _parts[index];
  264. }
  265. const BoundingBox& Mesh::getBoundingBox() const
  266. {
  267. return _boundingBox;
  268. }
  269. void Mesh::setBoundingBox(const BoundingBox& box)
  270. {
  271. _boundingBox = box;
  272. }
  273. const BoundingSphere& Mesh::getBoundingSphere() const
  274. {
  275. return _boundingSphere;
  276. }
  277. void Mesh::setBoundingSphere(const BoundingSphere& sphere)
  278. {
  279. _boundingSphere = sphere;
  280. }
  281. }