Mesh.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "Mesh.h"
  2. #include <cmath>
  3. namespace gameplay
  4. {
  5. Mesh::Mesh(void)
  6. {
  7. }
  8. Mesh::~Mesh(void)
  9. {
  10. }
  11. unsigned int Mesh::getTypeId(void) const
  12. {
  13. return MESH_ID;
  14. }
  15. const char* Mesh::getElementName(void) const
  16. {
  17. return "Mesh";
  18. }
  19. void Mesh::writeBinary(FILE* file)
  20. {
  21. Object::writeBinary(file);
  22. // vertex formats
  23. write(_vertexFormats.size(), file);
  24. for (std::vector<VertexElement>::iterator i = _vertexFormats.begin(); i != _vertexFormats.end(); i++)
  25. {
  26. i->writeBinary(file);
  27. }
  28. // vertices
  29. writeBinaryVertices(file);
  30. // parts
  31. writeBinaryObjects(parts, file);
  32. }
  33. void Mesh::writeBinaryVertices(FILE* file)
  34. {
  35. if (vertices.size() > 0)
  36. {
  37. // Assumes that all vertices are the same size.
  38. // Write the number of bytes for the vertex data
  39. const Vertex& vertex = vertices.front();
  40. write(vertices.size() * vertex.byteSize(), file); // (vertex count) * (vertex size)
  41. // for each vertex
  42. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); i++)
  43. {
  44. // Write this vertex
  45. i->writeBinary(file);
  46. }
  47. }
  48. else
  49. {
  50. // No vertex data
  51. write((unsigned int)0, file);
  52. }
  53. // Write bounds
  54. computeBounds();
  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 = _vertexFormats.begin(); i != _vertexFormats.end(); i++)
  67. {
  68. i->writeText(file);
  69. }
  70. }
  71. // for each Vertex
  72. fprintf(file, "<vertices count=\"%u\">\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. computeBounds();
  80. fprintf(file, "<bounds>\n");
  81. fprintf(file, "<min>\n");
  82. bounds.min.writeText(file);
  83. fprintf(file, "</min>\n");
  84. fprintf(file, "<max>\n");
  85. bounds.max.writeText(file);
  86. fprintf(file, "</max>\n");
  87. fprintf(file, "<center>\n");
  88. bounds.center.writeText(file);
  89. fprintf(file, "</center>\n");
  90. fprintf(file, "<radius>%f</radius>\n", bounds.radius);
  91. fprintf(file, "</bounds>\n");
  92. // for each MeshPart
  93. for (std::vector<MeshPart*>::iterator i = parts.begin(); i != parts.end(); i++)
  94. {
  95. (*i)->writeText(file);
  96. }
  97. fprintElementEnd(file);
  98. }
  99. void Mesh::addMeshPart(MeshPart* part)
  100. {
  101. parts.push_back(part);
  102. }
  103. void Mesh::addMeshPart(Vertex* vertex)
  104. {
  105. vertices.push_back(*vertex);
  106. }
  107. void Mesh::addVetexAttribute(unsigned int usage, unsigned int count)
  108. {
  109. _vertexFormats.push_back(VertexElement(usage, count));
  110. }
  111. size_t Mesh::getVertexCount() const
  112. {
  113. return vertices.size();
  114. }
  115. bool Mesh::contains(const Vertex& vertex) const
  116. {
  117. return vertexLookupTable.count(vertex) > 0;
  118. }
  119. unsigned int Mesh::addVertex(const Vertex& vertex)
  120. {
  121. unsigned int index = getVertexCount();
  122. vertices.push_back(vertex);
  123. vertexLookupTable[vertex] = index;
  124. return index;
  125. }
  126. unsigned int Mesh::getVertexIndex(const Vertex& vertex)
  127. {
  128. std::map<Vertex,unsigned int>::iterator it;
  129. it = vertexLookupTable.find(vertex);
  130. // TODO: Remove it from the map because we are going to delete it
  131. return it->second;
  132. }
  133. void Mesh::computeBounds()
  134. {
  135. bounds.min.x = bounds.min.y = bounds.min.z = FLT_MAX;
  136. bounds.max.x = bounds.max.y = bounds.max.z = FLT_MIN;
  137. bounds.center.x = bounds.center.y = bounds.center.z = 0.0f;
  138. bounds.radius = 0.0f;
  139. // for each vertex
  140. Vector3 avgPos;
  141. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); i++)
  142. {
  143. // Update min/max for this vertex
  144. if (i->position.x < bounds.min.x)
  145. bounds.min.x = i->position.x;
  146. if (i->position.y < bounds.min.y)
  147. bounds.min.y = i->position.y;
  148. if (i->position.z < bounds.min.z)
  149. bounds.min.z = i->position.z;
  150. if (i->position.x > bounds.max.x)
  151. bounds.max.x = i->position.x;
  152. if (i->position.y > bounds.max.y)
  153. bounds.max.y = i->position.y;
  154. if (i->position.z > bounds.max.z)
  155. bounds.max.z = i->position.z;
  156. avgPos.x += i->position.x;
  157. avgPos.y += i->position.y;
  158. avgPos.z += i->position.z;
  159. }
  160. // Compute center point
  161. bounds.center.x = avgPos.x / (float)vertices.size();
  162. bounds.center.y = avgPos.y / (float)vertices.size();
  163. bounds.center.z = avgPos.z / (float)vertices.size();
  164. // Compute radius by looping through all points again and finding the max
  165. // distance between the center point and each vertex position
  166. for (std::vector<Vertex>::const_iterator i = vertices.begin(); i != vertices.end(); i++)
  167. {
  168. float d = Vector3::distanceSquared(bounds.center, i->position);
  169. if (d > bounds.radius)
  170. {
  171. bounds.radius = d;
  172. }
  173. }
  174. // Convert squared distance to distance for radius
  175. bounds.radius = std::sqrtf(bounds.radius);
  176. }
  177. }