2
0

IMeshManipulator.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "IReferenceCounted.h"
  6. #include "vector3d.h"
  7. #include "aabbox3d.h"
  8. #include "IAnimatedMesh.h"
  9. #include "IMeshBuffer.h"
  10. #include "SVertexManipulator.h"
  11. namespace irr
  12. {
  13. namespace scene
  14. {
  15. struct SMesh;
  16. //! An interface for easy manipulation of meshes.
  17. /** Scale, set alpha value, flip surfaces, and so on. This exists for
  18. fixing problems with wrong imported or exported meshes quickly after
  19. loading. It is not intended for doing mesh modifications and/or
  20. animations during runtime.
  21. */
  22. class IMeshManipulator : public virtual IReferenceCounted
  23. {
  24. public:
  25. //! Recalculates all normals of the mesh.
  26. /** \param mesh: Mesh on which the operation is performed.
  27. \param smooth: If the normals shall be smoothed.
  28. \param angleWeighted: If the normals shall be smoothed in relation to their angles. More expensive, but also higher precision. */
  29. virtual void recalculateNormals(IMesh *mesh, bool smooth = false,
  30. bool angleWeighted = false) const = 0;
  31. //! Recalculates all normals of the mesh buffer.
  32. /** \param buffer: Mesh buffer on which the operation is performed.
  33. \param smooth: If the normals shall be smoothed.
  34. \param angleWeighted: If the normals shall be smoothed in relation to their angles. More expensive, but also higher precision. */
  35. virtual void recalculateNormals(IMeshBuffer *buffer,
  36. bool smooth = false, bool angleWeighted = false) const = 0;
  37. //! Scales the actual mesh, not a scene node.
  38. /** \param mesh Mesh on which the operation is performed.
  39. \param factor Scale factor for each axis. */
  40. void scale(IMesh *mesh, const core::vector3df &factor) const
  41. {
  42. apply(SVertexPositionScaleManipulator(factor), mesh, true);
  43. }
  44. //! Scales the actual meshbuffer, not a scene node.
  45. /** \param buffer Meshbuffer on which the operation is performed.
  46. \param factor Scale factor for each axis. */
  47. void scale(IMeshBuffer *buffer, const core::vector3df &factor) const
  48. {
  49. apply(SVertexPositionScaleManipulator(factor), buffer, true);
  50. }
  51. //! Clones a static IMesh into a modifiable SMesh.
  52. /** All meshbuffers in the returned SMesh
  53. are of type SMeshBuffer or SMeshBufferLightMap.
  54. \param mesh Mesh to copy.
  55. \return Cloned mesh. If you no longer need the
  56. cloned mesh, you should call SMesh::drop(). See
  57. IReferenceCounted::drop() for more information. */
  58. virtual SMesh *createMeshCopy(IMesh *mesh) const = 0;
  59. //! Get amount of polygons in mesh.
  60. /** \param mesh Input mesh
  61. \return Number of polygons in mesh. */
  62. virtual s32 getPolyCount(IMesh *mesh) const = 0;
  63. //! Get amount of polygons in mesh.
  64. /** \param mesh Input mesh
  65. \return Number of polygons in mesh. */
  66. virtual s32 getPolyCount(IAnimatedMesh *mesh) const = 0;
  67. //! Create a new AnimatedMesh and adds the mesh to it
  68. /** \param mesh Input mesh
  69. \param type The type of the animated mesh to create.
  70. \return Newly created animated mesh with mesh as its only
  71. content. When you don't need the animated mesh anymore, you
  72. should call IAnimatedMesh::drop(). See
  73. IReferenceCounted::drop() for more information. */
  74. virtual IAnimatedMesh *createAnimatedMesh(IMesh *mesh,
  75. scene::E_ANIMATED_MESH_TYPE type = scene::EAMT_UNKNOWN) const = 0;
  76. //! Apply a manipulator on the Meshbuffer
  77. /** \param func A functor defining the mesh manipulation.
  78. \param buffer The Meshbuffer to apply the manipulator to.
  79. \param boundingBoxUpdate Specifies if the bounding box should be updated during manipulation.
  80. \return True if the functor was successfully applied, else false. */
  81. template <typename Functor>
  82. bool apply(const Functor &func, IMeshBuffer *buffer, bool boundingBoxUpdate = false) const
  83. {
  84. return apply_(func, buffer, boundingBoxUpdate, func);
  85. }
  86. //! Apply a manipulator on the Mesh
  87. /** \param func A functor defining the mesh manipulation.
  88. \param mesh The Mesh to apply the manipulator to.
  89. \param boundingBoxUpdate Specifies if the bounding box should be updated during manipulation.
  90. \return True if the functor was successfully applied, else false. */
  91. template <typename Functor>
  92. bool apply(const Functor &func, IMesh *mesh, bool boundingBoxUpdate = false) const
  93. {
  94. if (!mesh)
  95. return true;
  96. bool result = true;
  97. core::aabbox3df bufferbox;
  98. for (u32 i = 0; i < mesh->getMeshBufferCount(); ++i) {
  99. result &= apply(func, mesh->getMeshBuffer(i), boundingBoxUpdate);
  100. if (boundingBoxUpdate) {
  101. if (0 == i)
  102. bufferbox.reset(mesh->getMeshBuffer(i)->getBoundingBox());
  103. else
  104. bufferbox.addInternalBox(mesh->getMeshBuffer(i)->getBoundingBox());
  105. }
  106. }
  107. if (boundingBoxUpdate)
  108. mesh->setBoundingBox(bufferbox);
  109. return result;
  110. }
  111. protected:
  112. //! Apply a manipulator based on the type of the functor
  113. /** \param func A functor defining the mesh manipulation.
  114. \param buffer The Meshbuffer to apply the manipulator to.
  115. \param boundingBoxUpdate Specifies if the bounding box should be updated during manipulation.
  116. \param typeTest Unused parameter, which handles the proper call selection based on the type of the Functor which is passed in two times.
  117. \return True if the functor was successfully applied, else false. */
  118. template <typename Functor>
  119. bool apply_(const Functor &func, IMeshBuffer *buffer, bool boundingBoxUpdate, const IVertexManipulator &typeTest) const
  120. {
  121. if (!buffer)
  122. return true;
  123. core::aabbox3df bufferbox;
  124. for (u32 i = 0; i < buffer->getVertexCount(); ++i) {
  125. switch (buffer->getVertexType()) {
  126. case video::EVT_STANDARD: {
  127. video::S3DVertex *verts = (video::S3DVertex *)buffer->getVertices();
  128. func(verts[i]);
  129. } break;
  130. case video::EVT_2TCOORDS: {
  131. video::S3DVertex2TCoords *verts = (video::S3DVertex2TCoords *)buffer->getVertices();
  132. func(verts[i]);
  133. } break;
  134. case video::EVT_TANGENTS: {
  135. video::S3DVertexTangents *verts = (video::S3DVertexTangents *)buffer->getVertices();
  136. func(verts[i]);
  137. } break;
  138. }
  139. if (boundingBoxUpdate) {
  140. if (0 == i)
  141. bufferbox.reset(buffer->getPosition(0));
  142. else
  143. bufferbox.addInternalPoint(buffer->getPosition(i));
  144. }
  145. }
  146. if (boundingBoxUpdate)
  147. buffer->setBoundingBox(bufferbox);
  148. return true;
  149. }
  150. };
  151. } // end namespace scene
  152. } // end namespace irr