MeshPart.cpp 3.5 KB

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