MeshPart.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if (GL_LAST_ERROR())
  23. {
  24. GP_ERROR("Failed to create VBO for index buffer with OpenGL error %d.", GL_LAST_ERROR());
  25. return NULL;
  26. }
  27. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo) );
  28. if (GL_LAST_ERROR())
  29. {
  30. GP_ERROR("Failed to bind VBO for index buffer with OpenGL error %d.", GL_LAST_ERROR());
  31. glDeleteBuffers(1, &vbo);
  32. return NULL;
  33. }
  34. unsigned int indexSize = 0;
  35. switch (indexFormat)
  36. {
  37. case Mesh::INDEX8:
  38. indexSize = 1;
  39. break;
  40. case Mesh::INDEX16:
  41. indexSize = 2;
  42. break;
  43. case Mesh::INDEX32:
  44. indexSize = 4;
  45. break;
  46. default:
  47. GP_ERROR("Unsupported index format (%d).", indexFormat);
  48. glDeleteBuffers(1, &vbo);
  49. return NULL;
  50. }
  51. GL_CHECK( glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * indexCount, NULL, dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW) );
  52. if (GL_LAST_ERROR())
  53. {
  54. GP_ERROR("Failed to load VBO with index data with OpenGL error %d.", GL_LAST_ERROR());
  55. glDeleteBuffers(1, &vbo);
  56. return NULL;
  57. }
  58. MeshPart* part = new MeshPart();
  59. part->_mesh = mesh;
  60. part->_meshIndex = meshIndex;
  61. part->_primitiveType = primitiveType;
  62. part->_indexFormat = indexFormat;
  63. part->_indexCount = indexCount;
  64. part->_indexBuffer = vbo;
  65. part->_dynamic = dynamic;
  66. return part;
  67. }
  68. unsigned int MeshPart::getMeshIndex() const
  69. {
  70. return _meshIndex;
  71. }
  72. Mesh::PrimitiveType MeshPart::getPrimitiveType() const
  73. {
  74. return _primitiveType;
  75. }
  76. unsigned int MeshPart::getIndexCount() const
  77. {
  78. return _indexCount;
  79. }
  80. Mesh::IndexFormat MeshPart::getIndexFormat() const
  81. {
  82. return _indexFormat;
  83. }
  84. IndexBufferHandle MeshPart::getIndexBuffer() const
  85. {
  86. return _indexBuffer;
  87. }
  88. bool MeshPart::isDynamic() const
  89. {
  90. return _dynamic;
  91. }
  92. void MeshPart::setIndexData(void* indexData, unsigned int indexStart, unsigned int indexCount)
  93. {
  94. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer) );
  95. unsigned int indexSize = 0;
  96. switch (_indexFormat)
  97. {
  98. case Mesh::INDEX8:
  99. indexSize = 1;
  100. break;
  101. case Mesh::INDEX16:
  102. indexSize = 2;
  103. break;
  104. case Mesh::INDEX32:
  105. indexSize = 4;
  106. break;
  107. default:
  108. GP_ERROR("Unsupported index format (%d).", _indexFormat);
  109. return;
  110. }
  111. if (indexStart == 0 && indexCount == 0)
  112. {
  113. GL_ASSERT( glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * _indexCount, indexData, _dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW) );
  114. }
  115. else
  116. {
  117. if (indexCount == 0)
  118. {
  119. indexCount = _indexCount - indexStart;
  120. }
  121. GL_ASSERT( glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexStart * indexSize, indexCount * indexSize, indexData) );
  122. }
  123. }
  124. }