MeshManager.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "MeshManager.h"
  23. #include "Log.h"
  24. #include "MathUtils.h"
  25. namespace Crown
  26. {
  27. MeshManager::MeshManager()
  28. {
  29. }
  30. Mesh* MeshManager::Create(const char* name, bool& created)
  31. {
  32. Mesh* mesh = static_cast<Mesh*>(ResourceManager::Create(name, created));
  33. return mesh;
  34. }
  35. Mesh* MeshManager::Load(const char* /*name*/)
  36. {
  37. Log::E("MeshManager::Load: Not implemented yet.");
  38. return 0;
  39. }
  40. Mesh* MeshManager::LoadCube(const char* name, float size)
  41. {
  42. bool created;
  43. Mesh* mesh = Create(name, created);
  44. if (mesh != NULL && created)
  45. {
  46. MeshChunk* chunk = new MeshChunk();
  47. float actual = Math::Abs(size) * 0.5f;
  48. /**
  49. 19------18
  50. / /
  51. 16------17
  52. 15 6------7 10
  53. /| | | /|
  54. 14 | 3------2 | 11 |
  55. |12 | 5----|-4 | 9
  56. |/ | | |/
  57. 13 0------1 8
  58. 20------21
  59. / /
  60. 23------22
  61. */
  62. // 6 faces, with pos, normal and uv
  63. // Face ================================================================================================== Index
  64. // Front
  65. chunk->mVertexList.Append(VertexData(Vec3(-actual, -actual, actual), Vec3::ZAXIS, Vec2(0.0f, 0.0f))); // 0
  66. chunk->mVertexList.Append(VertexData(Vec3( actual, -actual, actual), Vec3::ZAXIS, Vec2(1.0f, 0.0f))); // 1
  67. chunk->mVertexList.Append(VertexData(Vec3( actual, actual, actual), Vec3::ZAXIS, Vec2(1.0f, 1.0f))); // 2
  68. chunk->mVertexList.Append(VertexData(Vec3(-actual, actual, actual), Vec3::ZAXIS, Vec2(0.0f, 1.0f))); // 3
  69. // Back
  70. chunk->mVertexList.Append(VertexData(Vec3( actual, -actual, -actual), -Vec3::ZAXIS, Vec2(0.0f, 0.0f))); // 4
  71. chunk->mVertexList.Append(VertexData(Vec3(-actual, -actual, -actual), -Vec3::ZAXIS, Vec2(1.0f, 0.0f))); // 5
  72. chunk->mVertexList.Append(VertexData(Vec3(-actual, actual, -actual), -Vec3::ZAXIS, Vec2(1.0f, 1.0f))); // 6
  73. chunk->mVertexList.Append(VertexData(Vec3( actual, actual, -actual), -Vec3::ZAXIS, Vec2(0.0f, 1.0f))); // 7
  74. // Right
  75. chunk->mVertexList.Append(VertexData(Vec3( actual, -actual, actual), Vec3::XAXIS, Vec2(0.0f, 0.0f))); // 8
  76. chunk->mVertexList.Append(VertexData(Vec3( actual, -actual, -actual), Vec3::XAXIS, Vec2(1.0f, 0.0f))); // 9
  77. chunk->mVertexList.Append(VertexData(Vec3( actual, actual, -actual), Vec3::XAXIS, Vec2(1.0f, 1.0f))); // 10
  78. chunk->mVertexList.Append(VertexData(Vec3( actual, actual, actual), Vec3::XAXIS, Vec2(0.0f, 1.0f))); // 11
  79. // Left
  80. chunk->mVertexList.Append(VertexData(Vec3(-actual, -actual, -actual), -Vec3::XAXIS, Vec2(0.0f, 0.0f))); // 12
  81. chunk->mVertexList.Append(VertexData(Vec3(-actual, -actual, actual), -Vec3::XAXIS, Vec2(1.0f, 0.0f))); // 13
  82. chunk->mVertexList.Append(VertexData(Vec3(-actual, actual, actual), -Vec3::XAXIS, Vec2(1.0f, 1.0f))); // 14
  83. chunk->mVertexList.Append(VertexData(Vec3(-actual, actual, -actual), -Vec3::XAXIS, Vec2(0.0f, 1.0f))); // 15
  84. // Top
  85. chunk->mVertexList.Append(VertexData(Vec3(-actual, actual, actual), Vec3::YAXIS, Vec2(0.0f, 0.0f))); // 16
  86. chunk->mVertexList.Append(VertexData(Vec3( actual, actual, actual), Vec3::YAXIS, Vec2(1.0f, 0.0f))); // 17
  87. chunk->mVertexList.Append(VertexData(Vec3( actual, actual, -actual), Vec3::YAXIS, Vec2(1.0f, 1.0f))); // 18
  88. chunk->mVertexList.Append(VertexData(Vec3(-actual, actual, -actual), Vec3::YAXIS, Vec2(0.0f, 1.0f))); // 19
  89. // Bottom
  90. chunk->mVertexList.Append(VertexData(Vec3(-actual, -actual, -actual), -Vec3::YAXIS, Vec2(0.0f, 0.0f))); // 20
  91. chunk->mVertexList.Append(VertexData(Vec3( actual, -actual, -actual), -Vec3::YAXIS, Vec2(1.0f, 0.0f))); // 21
  92. chunk->mVertexList.Append(VertexData(Vec3( actual, -actual, actual), -Vec3::YAXIS, Vec2(1.0f, 1.0f))); // 22
  93. chunk->mVertexList.Append(VertexData(Vec3(-actual, -actual, actual), -Vec3::YAXIS, Vec2(0.0f, 1.0f))); // 23
  94. // 12 triangles, CCW order
  95. chunk->mFaceList.Append(FaceData(0, 1, 2));
  96. chunk->mFaceList.Append(FaceData(0, 2, 3));
  97. chunk->mFaceList.Append(FaceData(4, 5, 6));
  98. chunk->mFaceList.Append(FaceData(4, 6, 7));
  99. chunk->mFaceList.Append(FaceData(8, 9, 10));
  100. chunk->mFaceList.Append(FaceData(8, 10, 11));
  101. chunk->mFaceList.Append(FaceData(12, 13, 14));
  102. chunk->mFaceList.Append(FaceData(12, 14, 15));
  103. chunk->mFaceList.Append(FaceData(16, 17, 18));
  104. chunk->mFaceList.Append(FaceData(16, 18, 19));
  105. chunk->mFaceList.Append(FaceData(20, 21, 22));
  106. chunk->mFaceList.Append(FaceData(20, 22, 23));
  107. mesh->AddMeshChunk(chunk);
  108. mesh->UpdateBoundingBox();
  109. mesh->RecompileMesh();
  110. }
  111. return mesh;
  112. }
  113. Mesh* MeshManager::LoadPlane(const char* name, float width, float height)
  114. {
  115. bool created;
  116. Mesh* mesh = Create(name, created);
  117. if (mesh != NULL && created)
  118. {
  119. MeshChunk* chunk = new MeshChunk();
  120. float actualWidth = Math::Abs(width) * 0.5f;
  121. float actualHeight = Math::Abs(height) * 0.5f;
  122. /**
  123. (-x; +y) (+x; +y)
  124. ______________
  125. / /
  126. / ^ (+z) /
  127. / | /
  128. / /
  129. /_____________/
  130. (-x; -y) (+x; -y)
  131. */
  132. // Only a face, with pos, normal and UVs
  133. chunk->mVertexList.Append(VertexData(Vec3(-actualWidth, -actualHeight, 0.0f), Vec3::ZAXIS, Vec2(0.0f, 0.0f)));
  134. chunk->mVertexList.Append(VertexData(Vec3( actualWidth, -actualHeight, 0.0f), Vec3::ZAXIS, Vec2(1.0f, 0.0f)));
  135. chunk->mVertexList.Append(VertexData(Vec3( actualWidth, actualHeight, 0.0f), Vec3::ZAXIS, Vec2(1.0f, 1.0f)));
  136. chunk->mVertexList.Append(VertexData(Vec3(-actualWidth, actualHeight, 0.0f), Vec3::ZAXIS, Vec2(0.0f, 1.0f)));
  137. // 2 triangles, CCW order
  138. chunk->mFaceList.Append(FaceData(0, 1, 2));
  139. chunk->mFaceList.Append(FaceData(0, 2, 3));
  140. mesh->AddMeshChunk(chunk);
  141. mesh->UpdateBoundingBox();
  142. mesh->RecompileMesh();
  143. }
  144. return mesh;
  145. }
  146. Mesh* MeshManager::LoadGrid(const char* name, uint size, float tileSize)
  147. {
  148. if (size < 1)
  149. {
  150. Log::E("MeshManager::LoadGrid: Size must be > 0");
  151. return 0;
  152. }
  153. bool created;
  154. Mesh* mesh = Create(name, created);
  155. if (mesh != NULL && created)
  156. {
  157. MeshChunk* chunk = new MeshChunk();
  158. float actual = ((float)size * tileSize * 0.5f);
  159. /**
  160. (-x; -z) (+x; -z)
  161. ____________
  162. /__/__/__/__/
  163. /__/__/__/__/ ^ (+y)
  164. /__/__/__/__/ |
  165. /__/__/__/__/
  166. (-x; +z) (+x; +z)
  167. */
  168. // Populate vertex list (generate a grid lying on the xz-plane and facing upwards)
  169. float vCoord = 0.0f;//(float)size;
  170. float zPos = actual;
  171. for (uint h = 0; h <= size; h++)
  172. {
  173. float uCoord = 0.0f;
  174. float xPos = -actual;
  175. for (uint w = 0; w <= size; w++)
  176. {
  177. chunk->mVertexList.Append(VertexData(Vec3(xPos, 0.0f, zPos), Vec3::YAXIS, Vec2(uCoord, vCoord)));
  178. xPos += tileSize;
  179. uCoord += 1.0f;// / ((float)size + 1.0f);
  180. }
  181. zPos -= tileSize;
  182. vCoord += 1.0f;// / ((float)size + 1.0f);
  183. }
  184. // Generate faces
  185. for (uint h = 0; h < size; h++)
  186. {
  187. for (uint w = 0; w < size; w++)
  188. {
  189. uint firstRow = (h * (size + 1)) + w;
  190. uint secondRow = ((h + 1) * (size + 1)) + w;
  191. chunk->mFaceList.Append(FaceData(firstRow, firstRow + 1, secondRow + 1));
  192. chunk->mFaceList.Append(FaceData(firstRow, secondRow + 1, secondRow));
  193. }
  194. }
  195. mesh->AddMeshChunk(chunk);
  196. mesh->UpdateBoundingBox();
  197. mesh->RecompileMesh();
  198. }
  199. return mesh;
  200. }
  201. Mesh* MeshManager::CreateSpecific(const char* name)
  202. {
  203. return new Mesh();
  204. }
  205. MeshManager meshMgr;
  206. MeshManager* GetMeshManager()
  207. {
  208. return &meshMgr;
  209. }
  210. } // namespace Crown