Mesh.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "Base.h"
  2. #include "Mesh.h"
  3. #include "Model.h"
  4. namespace gameplay
  5. {
  6. Mesh::Mesh(void) : model(NULL)
  7. {
  8. }
  9. Mesh::~Mesh(void)
  10. {
  11. }
  12. unsigned int Mesh::getTypeId(void) const
  13. {
  14. return MESH_ID;
  15. }
  16. const char* Mesh::getElementName(void) const
  17. {
  18. return "Mesh";
  19. }
  20. void Mesh::writeBinary(FILE* file)
  21. {
  22. Object::writeBinary(file);
  23. // vertex formats
  24. write((unsigned int)_vertexFormat.size(), file);
  25. for (std::vector<VertexElement>::iterator i = _vertexFormat.begin(); i != _vertexFormat.end(); ++i)
  26. {
  27. i->writeBinary(file);
  28. }
  29. // vertices
  30. writeBinaryVertices(file);
  31. // parts
  32. writeBinaryObjects(parts, file);
  33. }
  34. void Mesh::writeBinaryVertices(FILE* file)
  35. {
  36. if (vertices.size() > 0)
  37. {
  38. // Assumes that all vertices are the same size.
  39. // Write the number of bytes for the vertex data
  40. const Vertex& vertex = vertices.front();
  41. write((unsigned int)(vertices.size() * vertex.byteSize()), file); // (vertex count) * (vertex size)
  42. // for each vertex
  43. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); ++i)
  44. {
  45. // Write this vertex
  46. i->writeBinary(file);
  47. }
  48. }
  49. else
  50. {
  51. // No vertex data
  52. writeZero(file);
  53. }
  54. // Write bounds
  55. write(&bounds.min.x, 3, file);
  56. write(&bounds.max.x, 3, file);
  57. write(&bounds.center.x, 3, file);
  58. write(bounds.radius, file);
  59. }
  60. void Mesh::writeText(FILE* file)
  61. {
  62. fprintElementStart(file);
  63. // for each VertexFormat
  64. if (vertices.size() > 0 )
  65. {
  66. for (std::vector<VertexElement>::iterator i = _vertexFormat.begin(); i != _vertexFormat.end(); ++i)
  67. {
  68. i->writeText(file);
  69. }
  70. }
  71. // for each Vertex
  72. fprintf(file, "<vertices count=\"%lu\">\n", vertices.size());
  73. for (std::vector<Vertex>::iterator i = vertices.begin(); i != vertices.end(); ++i)
  74. {
  75. i->writeText(file);
  76. }
  77. fprintf(file, "</vertices>\n");
  78. // write bounds
  79. fprintf(file, "<bounds>\n");
  80. fprintf(file, "<min>\n");
  81. writeVectorText(bounds.min, file);
  82. fprintf(file, "</min>\n");
  83. fprintf(file, "<max>\n");
  84. writeVectorText(bounds.max, file);
  85. fprintf(file, "</max>\n");
  86. fprintf(file, "<center>\n");
  87. writeVectorText(bounds.center, file);
  88. fprintf(file, "</center>\n");
  89. fprintf(file, "<radius>%f</radius>\n", bounds.radius);
  90. fprintf(file, "</bounds>\n");
  91. // for each MeshPart
  92. for (std::vector<MeshPart*>::iterator i = parts.begin(); i != parts.end(); ++i)
  93. {
  94. (*i)->writeText(file);
  95. }
  96. fprintElementEnd(file);
  97. }
  98. void Mesh::addMeshPart(MeshPart* part)
  99. {
  100. parts.push_back(part);
  101. }
  102. void Mesh::addMeshPart(Vertex* vertex)
  103. {
  104. vertices.push_back(*vertex);
  105. }
  106. void Mesh::addVetexAttribute(unsigned int usage, unsigned int count)
  107. {
  108. _vertexFormat.push_back(VertexElement(usage, count));
  109. }
  110. size_t Mesh::getVertexCount() const
  111. {
  112. return vertices.size();
  113. }
  114. const Vertex& Mesh::getVertex(unsigned int index) const
  115. {
  116. return vertices[index];
  117. }
  118. size_t Mesh::getVertexElementCount() const
  119. {
  120. return _vertexFormat.size();
  121. }
  122. const VertexElement& Mesh::getVertexElement(unsigned int index) const
  123. {
  124. return _vertexFormat[index];
  125. }
  126. bool Mesh::contains(const Vertex& vertex) const
  127. {
  128. return vertexLookupTable.count(vertex) > 0;
  129. }
  130. unsigned int Mesh::addVertex(const Vertex& vertex)
  131. {
  132. unsigned int index = getVertexCount();
  133. vertices.push_back(vertex);
  134. vertexLookupTable[vertex] = index;
  135. return index;
  136. }
  137. unsigned int Mesh::getVertexIndex(const Vertex& vertex)
  138. {
  139. std::map<Vertex,unsigned int>::iterator it;
  140. it = vertexLookupTable.find(vertex);
  141. return it->second;
  142. }
  143. bool Mesh::hasNormals() const
  144. {
  145. return !vertices.empty() && vertices[0].hasNormal;
  146. }
  147. bool Mesh::hasVertexColors() const
  148. {
  149. return !vertices.empty() && vertices[0].hasDiffuse;
  150. }
  151. void Mesh::computeBounds()
  152. {
  153. // If we have a Model with a MeshSkin associated with it,
  154. // compute the bounds from the skin - otherwise compute
  155. // it from the local mesh data.
  156. if (model && model->getSkin())
  157. {
  158. model->getSkin()->computeBounds();
  159. return;
  160. }
  161. LOG(2, "Computing bounds for mesh: %s\n", getId().c_str());
  162. bounds.min.x = bounds.min.y = bounds.min.z = FLT_MAX;
  163. bounds.max.x = bounds.max.y = bounds.max.z = -FLT_MAX;
  164. bounds.center.x = bounds.center.y = bounds.center.z = 0.0f;
  165. bounds.radius = 0.0f;
  166. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); ++i)
  167. {
  168. // Update min/max for this vertex
  169. if (i->position.x < bounds.min.x)
  170. bounds.min.x = i->position.x;
  171. if (i->position.y < bounds.min.y)
  172. bounds.min.y = i->position.y;
  173. if (i->position.z < bounds.min.z)
  174. bounds.min.z = i->position.z;
  175. if (i->position.x > bounds.max.x)
  176. bounds.max.x = i->position.x;
  177. if (i->position.y > bounds.max.y)
  178. bounds.max.y = i->position.y;
  179. if (i->position.z > bounds.max.z)
  180. bounds.max.z = i->position.z;
  181. }
  182. // Compute center point
  183. Vector3::add(bounds.min, bounds.max, &bounds.center);
  184. bounds.center.scale(0.5f);
  185. // Compute radius by looping through all points again and finding the max
  186. // distance between the center point and each vertex position
  187. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); ++i)
  188. {
  189. float d = bounds.center.distanceSquared(i->position);
  190. if (d > bounds.radius)
  191. {
  192. bounds.radius = d;
  193. }
  194. }
  195. // Convert squared distance to distance for radius
  196. bounds.radius = sqrt(bounds.radius);
  197. }
  198. }