Mesh.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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(_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(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. write((unsigned int)0, file);
  53. }
  54. // Write bounds
  55. computeBounds();
  56. write(&bounds.min.x, 3, file);
  57. write(&bounds.max.x, 3, file);
  58. write(&bounds.center.x, 3, file);
  59. write(bounds.radius, file);
  60. }
  61. void Mesh::writeText(FILE* file)
  62. {
  63. fprintElementStart(file);
  64. // for each VertexFormat
  65. if (vertices.size() > 0 )
  66. {
  67. for (std::vector<VertexElement>::iterator i = _vertexFormat.begin(); i != _vertexFormat.end(); i++)
  68. {
  69. i->writeText(file);
  70. }
  71. }
  72. // for each Vertex
  73. fprintf(file, "<vertices count=\"%lu\">\n", vertices.size());
  74. for (std::vector<Vertex>::iterator i = vertices.begin(); i != vertices.end(); ++i)
  75. {
  76. i->writeText(file);
  77. }
  78. fprintf(file, "</vertices>\n");
  79. // write bounds
  80. computeBounds();
  81. fprintf(file, "<bounds>\n");
  82. fprintf(file, "<min>\n");
  83. writeVectorText(bounds.min, file);
  84. fprintf(file, "</min>\n");
  85. fprintf(file, "<max>\n");
  86. writeVectorText(bounds.max, file);
  87. fprintf(file, "</max>\n");
  88. fprintf(file, "<center>\n");
  89. writeVectorText(bounds.center, file);
  90. fprintf(file, "</center>\n");
  91. fprintf(file, "<radius>%f</radius>\n", bounds.radius);
  92. fprintf(file, "</bounds>\n");
  93. // for each MeshPart
  94. for (std::vector<MeshPart*>::iterator i = parts.begin(); i != parts.end(); ++i)
  95. {
  96. (*i)->writeText(file);
  97. }
  98. fprintElementEnd(file);
  99. }
  100. void Mesh::addMeshPart(MeshPart* part)
  101. {
  102. parts.push_back(part);
  103. }
  104. void Mesh::addMeshPart(Vertex* vertex)
  105. {
  106. vertices.push_back(*vertex);
  107. }
  108. void Mesh::addVetexAttribute(unsigned int usage, unsigned int count)
  109. {
  110. _vertexFormat.push_back(VertexElement(usage, count));
  111. }
  112. size_t Mesh::getVertexCount() const
  113. {
  114. return vertices.size();
  115. }
  116. const Vertex& Mesh::getVertex(unsigned int index) const
  117. {
  118. return vertices[index];
  119. }
  120. size_t Mesh::getVertexElementCount() const
  121. {
  122. return _vertexFormat.size();
  123. }
  124. const VertexElement& Mesh::getVertexElement(unsigned int index) const
  125. {
  126. return _vertexFormat[index];
  127. }
  128. bool Mesh::contains(const Vertex& vertex) const
  129. {
  130. return vertexLookupTable.count(vertex) > 0;
  131. }
  132. unsigned int Mesh::addVertex(const Vertex& vertex)
  133. {
  134. unsigned int index = getVertexCount();
  135. vertices.push_back(vertex);
  136. vertexLookupTable[vertex] = index;
  137. return index;
  138. }
  139. unsigned int Mesh::getVertexIndex(const Vertex& vertex)
  140. {
  141. std::map<Vertex,unsigned int>::iterator it;
  142. it = vertexLookupTable.find(vertex);
  143. return it->second;
  144. }
  145. void Mesh::computeBounds()
  146. {
  147. // If we have a Model with a MeshSkin associated with it,
  148. // compute the bounds from the skin - otherwise compute
  149. // it from the local mesh data.
  150. if (model && model->getSkin())
  151. {
  152. model->getSkin()->computeBounds();
  153. return;
  154. }
  155. bounds.min.x = bounds.min.y = bounds.min.z = FLT_MAX;
  156. bounds.max.x = bounds.max.y = bounds.max.z = FLT_MIN;
  157. bounds.center.x = bounds.center.y = bounds.center.z = 0.0f;
  158. bounds.radius = 0.0f;
  159. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); ++i)
  160. {
  161. // Update min/max for this vertex
  162. if (i->position.x < bounds.min.x)
  163. bounds.min.x = i->position.x;
  164. if (i->position.y < bounds.min.y)
  165. bounds.min.y = i->position.y;
  166. if (i->position.z < bounds.min.z)
  167. bounds.min.z = i->position.z;
  168. if (i->position.x > bounds.max.x)
  169. bounds.max.x = i->position.x;
  170. if (i->position.y > bounds.max.y)
  171. bounds.max.y = i->position.y;
  172. if (i->position.z > bounds.max.z)
  173. bounds.max.z = i->position.z;
  174. }
  175. // Compute center point
  176. Vector3::add(bounds.min, bounds.max, &bounds.center);
  177. bounds.center.scale(0.5f);
  178. // Compute radius by looping through all points again and finding the max
  179. // distance between the center point and each vertex position
  180. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); ++i)
  181. {
  182. float d = bounds.center.distanceSquared(i->position);
  183. if (d > bounds.radius)
  184. {
  185. bounds.radius = d;
  186. }
  187. }
  188. // Convert squared distance to distance for radius
  189. bounds.radius = sqrt(bounds.radius);
  190. }
  191. }