Mesh.cpp 5.3 KB

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