sphereMesh.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SPHEREMESH_H_
  23. #define _SPHEREMESH_H_
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. //------------------------------------------------------------------------------
  31. // Class: SphereMesh
  32. //------------------------------------------------------------------------------
  33. // * ctor takes type of base polyhedron that is subdivided to create sphere
  34. // * getMesh(...) will subdivide the current mesh to the desired level where
  35. // (each level has 4 times the polys of the previous level)
  36. class SphereMesh
  37. {
  38. public:
  39. // regular polyhedra with triangle face polygons (num of faces)
  40. enum {
  41. Tetrahedron = 4,
  42. Octahedron = 8,
  43. Icosahedron = 20,
  44. MaxLevel = 5
  45. };
  46. struct Triangle {
  47. Triangle() {}
  48. Triangle(const Point3F &a, const Point3F &b, const Point3F &c)
  49. {
  50. pnt[0] = a;
  51. pnt[1] = b;
  52. pnt[2] = c;
  53. }
  54. Point3F pnt[3];
  55. Point3F normal;
  56. };
  57. struct TriangleMesh {
  58. U32 numPoly;
  59. Triangle * poly;
  60. };
  61. SphereMesh(U32 baseType = Octahedron);
  62. ~SphereMesh();
  63. const TriangleMesh * getMesh(U32 level = 0);
  64. private:
  65. TriangleMesh * createTetrahedron();
  66. TriangleMesh * createOctahedron();
  67. TriangleMesh * createIcosahedron();
  68. Vector<TriangleMesh*> mDetails;
  69. void calcNormals(TriangleMesh *);
  70. TriangleMesh * subdivideMesh(TriangleMesh*);
  71. };
  72. #endif