MeshPart.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "Base.h"
  2. #include "MeshPart.h"
  3. namespace gameplay
  4. {
  5. MeshPart::MeshPart() :
  6. _mesh(NULL), _meshIndex(0), _primitiveType(Mesh::TRIANGLES), _indexCount(0), _indexBuffer(0), _dynamic(false)
  7. {
  8. }
  9. MeshPart::~MeshPart()
  10. {
  11. if (_indexBuffer)
  12. {
  13. glDeleteBuffers(1, &_indexBuffer);
  14. }
  15. }
  16. MeshPart* MeshPart::create(Mesh* mesh, unsigned int meshIndex, Mesh::PrimitiveType primitiveType,
  17. Mesh::IndexFormat indexFormat, unsigned int indexCount, bool dynamic)
  18. {
  19. // Create a VBO for our index buffer.
  20. GLuint vbo;
  21. GL_ASSERT( glGenBuffers(1, &vbo) );
  22. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo) );
  23. unsigned int indexSize = 0;
  24. switch (indexFormat)
  25. {
  26. case Mesh::INDEX8:
  27. indexSize = 1;
  28. break;
  29. case Mesh::INDEX16:
  30. indexSize = 2;
  31. break;
  32. case Mesh::INDEX32:
  33. indexSize = 4;
  34. break;
  35. default:
  36. GP_ERROR("Unsupported index format (%d).", indexFormat);
  37. glDeleteBuffers(1, &vbo);
  38. return NULL;
  39. }
  40. GL_ASSERT( glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * indexCount, NULL, dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW) );
  41. MeshPart* part = new MeshPart();
  42. part->_mesh = mesh;
  43. part->_meshIndex = meshIndex;
  44. part->_primitiveType = primitiveType;
  45. part->_indexFormat = indexFormat;
  46. part->_indexCount = indexCount;
  47. part->_indexBuffer = vbo;
  48. part->_dynamic = dynamic;
  49. return part;
  50. }
  51. unsigned int MeshPart::getMeshIndex() const
  52. {
  53. return _meshIndex;
  54. }
  55. Mesh::PrimitiveType MeshPart::getPrimitiveType() const
  56. {
  57. return _primitiveType;
  58. }
  59. unsigned int MeshPart::getIndexCount() const
  60. {
  61. return _indexCount;
  62. }
  63. Mesh::IndexFormat MeshPart::getIndexFormat() const
  64. {
  65. return _indexFormat;
  66. }
  67. IndexBufferHandle MeshPart::getIndexBuffer() const
  68. {
  69. return _indexBuffer;
  70. }
  71. bool MeshPart::isDynamic() const
  72. {
  73. return _dynamic;
  74. }
  75. void MeshPart::setIndexData(const void* indexData, unsigned int indexStart, unsigned int indexCount)
  76. {
  77. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer) );
  78. unsigned int indexSize = 0;
  79. switch (_indexFormat)
  80. {
  81. case Mesh::INDEX8:
  82. indexSize = 1;
  83. break;
  84. case Mesh::INDEX16:
  85. indexSize = 2;
  86. break;
  87. case Mesh::INDEX32:
  88. indexSize = 4;
  89. break;
  90. default:
  91. GP_ERROR("Unsupported index format (%d).", _indexFormat);
  92. return;
  93. }
  94. if (indexStart == 0 && indexCount == 0)
  95. {
  96. GL_ASSERT( glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * _indexCount, indexData, _dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW) );
  97. }
  98. else
  99. {
  100. if (indexCount == 0)
  101. {
  102. indexCount = _indexCount - indexStart;
  103. }
  104. GL_ASSERT( glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexStart * indexSize, indexCount * indexSize, indexData) );
  105. }
  106. }
  107. }