MeshBatch.cpp 6.3 KB

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