Model.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "Base.h"
  2. #include "Model.h"
  3. namespace gameplay
  4. {
  5. Model::Model(void) :
  6. _mesh(NULL),
  7. _meshSkin(NULL),
  8. _material(NULL)
  9. {
  10. }
  11. Model::~Model(void)
  12. {
  13. }
  14. unsigned int Model::getTypeId(void) const
  15. {
  16. return MODEL_ID;
  17. }
  18. const char* Model::getElementName(void) const
  19. {
  20. return "Model";
  21. }
  22. void Model::writeBinary(FILE* file)
  23. {
  24. Object::writeBinary(file);
  25. // xref:Mesh
  26. if (_mesh != NULL)
  27. {
  28. _mesh->writeBinaryXref(file);
  29. }
  30. else
  31. {
  32. writeZero(file);
  33. }
  34. // _meshSkin
  35. // Write one unsigned char to indicate if this model has a skin
  36. if (_meshSkin != NULL)
  37. {
  38. write((bool)true, file); // has a skin
  39. _meshSkin->writeBinary(file);
  40. }
  41. else
  42. {
  43. write((bool)false, file); // doesn't have a skin
  44. }
  45. // Write the list of materials or zero if there are no materials
  46. if (_material && _materials.empty())
  47. {
  48. write((unsigned int)1, file);
  49. write(_material->getId(), file);
  50. }
  51. else
  52. {
  53. write((unsigned int)_materials.size(), file);
  54. if (_materials.size() > 0)
  55. {
  56. // Write the material names for each mesh part
  57. for (unsigned int i = 0; i < _materials.size(); ++i)
  58. {
  59. if (Material* mat = _materials[i])
  60. {
  61. write(mat->getId(), file);
  62. }
  63. else
  64. {
  65. writeZero(file);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. void Model::writeText(FILE* file)
  72. {
  73. // Compute mesh bounds before writing
  74. fprintElementStart(file);
  75. if (_mesh != NULL)
  76. {
  77. fprintfElement(file, "ref", _mesh->getId());
  78. }
  79. if (_meshSkin != NULL)
  80. {
  81. _meshSkin->writeText(file);
  82. }
  83. if (_material)
  84. {
  85. fprintfElement(file, "material", _material->getId().c_str());
  86. }
  87. for (unsigned int i = 0; i < _materials.size(); ++i)
  88. {
  89. if (Material* mat = _materials[i])
  90. {
  91. fprintfElement(file, "material", mat->getId().c_str());
  92. }
  93. }
  94. fprintElementEnd(file);
  95. }
  96. MeshSkin* Model::getSkin()
  97. {
  98. return _meshSkin;
  99. }
  100. Mesh* Model::getMesh()
  101. {
  102. return _mesh;
  103. }
  104. void Model::setMesh(Mesh* mesh)
  105. {
  106. _mesh = mesh;
  107. if (mesh)
  108. {
  109. mesh->model = this;
  110. }
  111. if (_mesh && _meshSkin)
  112. {
  113. _meshSkin->_mesh = _mesh;
  114. }
  115. }
  116. void Model::setSkin(MeshSkin* skin)
  117. {
  118. _meshSkin = skin;
  119. if (_meshSkin)
  120. {
  121. _meshSkin->_mesh = _mesh;
  122. }
  123. }
  124. void Model::setMaterial(Material* material, int partIndex)
  125. {
  126. if (partIndex < 0)
  127. {
  128. _material = material;
  129. }
  130. else
  131. {
  132. if ((int)_materials.size() < partIndex + 1)
  133. {
  134. _materials.resize(partIndex + 1, (Material*)NULL);
  135. }
  136. _materials[partIndex] = material;
  137. }
  138. }
  139. }