MeshPart.cpp 3.0 KB

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