MeshPart.cpp 2.1 KB

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