MeshBatch.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "Base.h"
  2. #include "MeshBatch.h"
  3. namespace gameplay
  4. {
  5. MeshBatch::MeshBatch(const VertexFormat& vertexFormat, Mesh::PrimitiveType primitiveType, Material* material, bool indexed, unsigned int initialCapacity, unsigned int growSize)
  6. : _vertexFormat(vertexFormat), _primitiveType(primitiveType), _material(material), _indexed(indexed), _capacity(0), _growSize(growSize),
  7. _vertexCapacity(0), _indexCapacity(0), _vertexCount(0), _indexCount(0), _vertices(NULL), _verticesPtr(NULL), _indices(NULL), _indicesPtr(NULL)
  8. {
  9. resize(initialCapacity);
  10. }
  11. MeshBatch::MeshBatch(const MeshBatch& copy)
  12. : _vertexFormat(copy._vertexFormat)
  13. {
  14. // hidden
  15. }
  16. MeshBatch::~MeshBatch()
  17. {
  18. SAFE_RELEASE(_material);
  19. SAFE_DELETE_ARRAY(_vertices);
  20. SAFE_DELETE_ARRAY(_indices);
  21. }
  22. MeshBatch* MeshBatch::create(const VertexFormat& vertexFormat, Mesh::PrimitiveType primitiveType, const char* materialPath, bool indexed, unsigned int initialCapacity, unsigned int growSize)
  23. {
  24. Material* material = Material::create(materialPath);
  25. if (material == NULL)
  26. return NULL;
  27. MeshBatch* batch = create(vertexFormat, primitiveType, material, indexed, initialCapacity, growSize);
  28. SAFE_RELEASE(material); // batch now owns the material
  29. return batch;
  30. }
  31. MeshBatch* MeshBatch::create(const VertexFormat& vertexFormat, Mesh::PrimitiveType primitiveType, Material* material, bool indexed, unsigned int initialCapacity, unsigned int growSize)
  32. {
  33. GP_ASSERT(material);
  34. MeshBatch* batch = new MeshBatch(vertexFormat, primitiveType, material, indexed, initialCapacity, growSize);
  35. material->addRef();
  36. return batch;
  37. }
  38. void MeshBatch::updateVertexAttributeBinding()
  39. {
  40. // Update our vertex attribute bindings
  41. for (unsigned int i = 0, techniqueCount = _material->getTechniqueCount(); i < techniqueCount; ++i)
  42. {
  43. Technique* t = _material->getTechnique(i);
  44. for (unsigned int j = 0, passCount = t->getPassCount(); j < passCount; ++j)
  45. {
  46. Pass* p = t->getPass(j);
  47. VertexAttributeBinding* b = VertexAttributeBinding::create(_vertexFormat, _vertices, p->getEffect());
  48. p->setVertexAttributeBinding(b);
  49. SAFE_RELEASE(b);
  50. }
  51. }
  52. }
  53. unsigned int MeshBatch::getCapacity() const
  54. {
  55. return _capacity;
  56. }
  57. void MeshBatch::setCapacity(unsigned int capacity)
  58. {
  59. resize(capacity);
  60. }
  61. bool MeshBatch::resize(unsigned int capacity)
  62. {
  63. GP_ASSERT(capacity > 0);
  64. if (capacity == 0)
  65. return false;
  66. if (capacity == _capacity)
  67. return true;
  68. // Store old batch data
  69. unsigned char* oldVertices = _vertices;
  70. unsigned short* oldIndices = _indices;
  71. unsigned int vertexCapacity = 0;
  72. switch (_primitiveType)
  73. {
  74. case Mesh::LINES:
  75. vertexCapacity = capacity * 2;
  76. break;
  77. case Mesh::LINE_STRIP:
  78. vertexCapacity = capacity + 1;
  79. break;
  80. case Mesh::POINTS:
  81. vertexCapacity = capacity;
  82. break;
  83. case Mesh::TRIANGLES:
  84. vertexCapacity = capacity * 3;
  85. break;
  86. case Mesh::TRIANGLE_STRIP:
  87. vertexCapacity = capacity + 2;
  88. break;
  89. default:
  90. GP_ASSERT(0); // unexpected
  91. break;
  92. }
  93. // We have no way of knowing how many vertices will be stored in the batch
  94. // (we only know how many indices will be stored). Assume the worst case
  95. // for now, which is the same number of vertices as indices.
  96. unsigned int indexCapacity = vertexCapacity;
  97. GP_ASSERT(indexCapacity <= USHRT_MAX);
  98. if (indexCapacity > USHRT_MAX)
  99. return false;
  100. // Allocate new data and reset pointers
  101. unsigned int voffset = _verticesPtr - _vertices;
  102. unsigned int vBytes = vertexCapacity * _vertexFormat.getVertexSize();
  103. _vertices = new unsigned char[vBytes];
  104. if (voffset >= vBytes)
  105. voffset = vBytes - 1;
  106. _verticesPtr = _vertices + voffset;
  107. if (_indexed)
  108. {
  109. unsigned int ioffset = _indicesPtr - _indices;
  110. _indices = new unsigned short[indexCapacity];
  111. if (ioffset >= indexCapacity)
  112. ioffset = indexCapacity - 1;
  113. _indicesPtr = _indices + ioffset;
  114. }
  115. // Copy old data back in
  116. if (oldVertices)
  117. memcpy(_vertices, oldVertices, std::min(_vertexCapacity, vertexCapacity) * _vertexFormat.getVertexSize());
  118. SAFE_DELETE_ARRAY(oldVertices);
  119. if (oldIndices)
  120. memcpy(_indices, oldIndices, std::min(_indexCapacity, indexCapacity) * sizeof(unsigned short));
  121. SAFE_DELETE_ARRAY(oldIndices);
  122. // Assign new capacities
  123. _capacity = capacity;
  124. _vertexCapacity = vertexCapacity;
  125. _indexCapacity = indexCapacity;
  126. // Update our vertex attribute bindings now that our client array pointers have changed
  127. updateVertexAttributeBinding();
  128. return true;
  129. }
  130. void MeshBatch::begin()
  131. {
  132. _vertexCount = 0;
  133. _indexCount = 0;
  134. _verticesPtr = _vertices;
  135. _indicesPtr = _indices;
  136. }
  137. void MeshBatch::end()
  138. {
  139. }
  140. void MeshBatch::draw()
  141. {
  142. if (_vertexCount == 0 || (_indexed && _indexCount == 0))
  143. return; // nothing to draw
  144. // Not using VBOs, so unbind the element array buffer.
  145. // ARRAY_BUFFER will be unbound automatically during pass->bind().
  146. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0 ) );
  147. // Bind the material
  148. Technique* technique = _material->getTechnique();
  149. unsigned int passCount = technique->getPassCount();
  150. for (unsigned int i = 0; i < passCount; ++i)
  151. {
  152. Pass* pass = technique->getPass(i);
  153. pass->bind();
  154. if (_indexed)
  155. {
  156. GL_ASSERT( glDrawElements(_primitiveType, _indexCount, GL_UNSIGNED_SHORT, (GLvoid*)_indices) );
  157. }
  158. else
  159. {
  160. GL_ASSERT( glDrawArrays(_primitiveType, 0, _vertexCount) );
  161. }
  162. pass->unbind();
  163. }
  164. }
  165. }