MeshPart.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "Base.h"
  2. #include "MeshPart.h"
  3. namespace gameplay
  4. {
  5. MeshPart::MeshPart(void) :
  6. _primitiveType(TRIANGLES),
  7. _indexFormat(INDEX16)
  8. {
  9. }
  10. MeshPart::~MeshPart(void)
  11. {
  12. }
  13. unsigned int MeshPart::getTypeId(void) const
  14. {
  15. return MESHPART_ID;
  16. }
  17. const char* MeshPart::getElementName(void) const
  18. {
  19. return "MeshPart";
  20. }
  21. void MeshPart::writeBinary(FILE* file)
  22. {
  23. Object::writeBinary(file);
  24. write(_primitiveType, file);
  25. write((unsigned int)_indexFormat, file);
  26. // write the number of bytes
  27. write(indicesByteSize(), file);
  28. // for each index
  29. for (std::vector<unsigned int>::const_iterator i = _indices.begin(); i != _indices.end(); ++i)
  30. {
  31. writeBinaryIndex(*i, file);
  32. }
  33. }
  34. void MeshPart::writeText(FILE* file)
  35. {
  36. fprintElementStart(file);
  37. fprintfElement(file, "primitiveType", _primitiveType);
  38. fprintfElement(file, "indexFormat", (unsigned int)_indexFormat);
  39. fprintfElement(file, "%d ", "indices", _indices);
  40. fprintElementEnd(file);
  41. }
  42. void MeshPart::addIndex(unsigned int index)
  43. {
  44. updateIndexFormat(index);
  45. _indices.push_back(index);
  46. }
  47. size_t MeshPart::getIndicesCount() const
  48. {
  49. return _indices.size();
  50. }
  51. unsigned int MeshPart::indicesByteSize() const
  52. {
  53. return _indices.size() * indexFormatSize();
  54. }
  55. unsigned int MeshPart::indexFormatSize() const
  56. {
  57. switch (_indexFormat)
  58. {
  59. case INDEX32:
  60. return 4;
  61. default: // INDEX16
  62. return 2;
  63. }
  64. }
  65. MeshPart::IndexFormat MeshPart::getIndexFormat() const
  66. {
  67. return _indexFormat;
  68. }
  69. unsigned int MeshPart::getIndex(unsigned int i) const
  70. {
  71. return _indices[i];
  72. }
  73. void MeshPart::writeBinaryIndex(unsigned int index, FILE* file)
  74. {
  75. switch (_indexFormat)
  76. {
  77. case INDEX32:
  78. write(index, file);
  79. break;
  80. default: // INDEX16
  81. write((unsigned short)index, file);
  82. break;
  83. }
  84. }
  85. void MeshPart::updateIndexFormat(unsigned int newIndex)
  86. {
  87. if (newIndex >= 65536)
  88. {
  89. _indexFormat = INDEX32;
  90. }
  91. else if (newIndex >= 256 && _indexFormat != INDEX32)
  92. {
  93. _indexFormat = INDEX16;
  94. }
  95. }
  96. }