CTriangleSelector.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #include "CTriangleSelector.h"
  5. #include "ISceneNode.h"
  6. #include "IMeshBuffer.h"
  7. #include "IAnimatedMeshSceneNode.h"
  8. namespace irr
  9. {
  10. namespace scene
  11. {
  12. //! constructor
  13. CTriangleSelector::CTriangleSelector(ISceneNode* node)
  14. : SceneNode(node), AnimatedNode(0), LastMeshFrame(0)
  15. {
  16. #ifdef _DEBUG
  17. setDebugName("CTriangleSelector");
  18. #endif
  19. BoundingBox.reset(0.f, 0.f, 0.f);
  20. }
  21. //! constructor
  22. CTriangleSelector::CTriangleSelector(const core::aabbox3d<f32>& box, ISceneNode* node)
  23. : SceneNode(node), AnimatedNode(0), LastMeshFrame(0)
  24. {
  25. #ifdef _DEBUG
  26. setDebugName("CTriangleSelector");
  27. #endif
  28. BoundingBox=box;
  29. // TODO
  30. }
  31. //! constructor
  32. CTriangleSelector::CTriangleSelector(const IMesh* mesh, ISceneNode* node)
  33. : SceneNode(node), AnimatedNode(0), LastMeshFrame(0)
  34. {
  35. #ifdef _DEBUG
  36. setDebugName("CTriangleSelector");
  37. #endif
  38. createFromMesh(mesh);
  39. }
  40. CTriangleSelector::CTriangleSelector(IAnimatedMeshSceneNode* node)
  41. : SceneNode(node), AnimatedNode(node), LastMeshFrame(0)
  42. {
  43. #ifdef _DEBUG
  44. setDebugName("CTriangleSelector");
  45. #endif
  46. if (!AnimatedNode)
  47. return;
  48. IAnimatedMesh* animatedMesh = AnimatedNode->getMesh();
  49. if (!animatedMesh)
  50. return;
  51. LastMeshFrame = (u32)AnimatedNode->getFrameNr();
  52. IMesh* mesh = animatedMesh->getMesh(LastMeshFrame);
  53. if (mesh)
  54. createFromMesh(mesh);
  55. }
  56. void CTriangleSelector::createFromMesh(const IMesh* mesh)
  57. {
  58. const u32 cnt = mesh->getMeshBufferCount();
  59. u32 totalFaceCount = 0;
  60. for (u32 j=0; j<cnt; ++j)
  61. totalFaceCount += mesh->getMeshBuffer(j)->getIndexCount();
  62. totalFaceCount /= 3;
  63. Triangles.reallocate(totalFaceCount);
  64. BoundingBox.reset(0.f, 0.f, 0.f);
  65. for (u32 i=0; i<cnt; ++i)
  66. {
  67. const IMeshBuffer* buf = mesh->getMeshBuffer(i);
  68. const u32 idxCnt = buf->getIndexCount();
  69. const u16* const indices = buf->getIndices();
  70. for (u32 j=0; j<idxCnt; j+=3)
  71. {
  72. Triangles.push_back(core::triangle3df(
  73. buf->getPosition(indices[j+0]),
  74. buf->getPosition(indices[j+1]),
  75. buf->getPosition(indices[j+2])));
  76. const core::triangle3df& tri = Triangles.getLast();
  77. BoundingBox.addInternalPoint(tri.pointA);
  78. BoundingBox.addInternalPoint(tri.pointB);
  79. BoundingBox.addInternalPoint(tri.pointC);
  80. }
  81. }
  82. }
  83. void CTriangleSelector::updateFromMesh(const IMesh* mesh) const
  84. {
  85. if (!mesh)
  86. return;
  87. u32 meshBuffers = mesh->getMeshBufferCount();
  88. u32 triangleCount = 0;
  89. BoundingBox.reset(0.f, 0.f, 0.f);
  90. for (u32 i = 0; i < meshBuffers; ++i)
  91. {
  92. IMeshBuffer* buf = mesh->getMeshBuffer(i);
  93. u32 idxCnt = buf->getIndexCount();
  94. const u16* indices = buf->getIndices();
  95. for (u32 index = 0; index < idxCnt; index += 3)
  96. {
  97. core::triangle3df& tri = Triangles[triangleCount++];
  98. tri.pointA = buf->getPosition(indices[index + 0]);
  99. tri.pointB = buf->getPosition(indices[index + 1]);
  100. tri.pointC = buf->getPosition(indices[index + 2]);
  101. BoundingBox.addInternalPoint(tri.pointA);
  102. BoundingBox.addInternalPoint(tri.pointB);
  103. BoundingBox.addInternalPoint(tri.pointC);
  104. }
  105. }
  106. }
  107. void CTriangleSelector::update(void) const
  108. {
  109. if (!AnimatedNode)
  110. return; //< harmless no-op
  111. const u32 currentFrame = (u32)AnimatedNode->getFrameNr();
  112. if (currentFrame == LastMeshFrame)
  113. return; //< Nothing to do
  114. LastMeshFrame = currentFrame;
  115. IAnimatedMesh * animatedMesh = AnimatedNode->getMesh();
  116. if (animatedMesh)
  117. {
  118. IMesh * mesh = animatedMesh->getMesh(LastMeshFrame);
  119. if (mesh)
  120. updateFromMesh(mesh);
  121. }
  122. }
  123. //! Gets all triangles.
  124. void CTriangleSelector::getTriangles(core::triangle3df* triangles,
  125. s32 arraySize, s32& outTriangleCount,
  126. const core::matrix4* transform) const
  127. {
  128. // Update my triangles if necessary
  129. update();
  130. u32 cnt = Triangles.size();
  131. if (cnt > (u32)arraySize)
  132. cnt = (u32)arraySize;
  133. core::matrix4 mat;
  134. if (transform)
  135. mat = *transform;
  136. if (SceneNode)
  137. mat *= SceneNode->getAbsoluteTransformation();
  138. for (u32 i=0; i<cnt; ++i)
  139. {
  140. mat.transformVect( triangles[i].pointA, Triangles[i].pointA );
  141. mat.transformVect( triangles[i].pointB, Triangles[i].pointB );
  142. mat.transformVect( triangles[i].pointC, Triangles[i].pointC );
  143. }
  144. outTriangleCount = cnt;
  145. }
  146. //! Gets all triangles which lie within a specific bounding box.
  147. void CTriangleSelector::getTriangles(core::triangle3df* triangles,
  148. s32 arraySize, s32& outTriangleCount,
  149. const core::aabbox3d<f32>& box,
  150. const core::matrix4* transform) const
  151. {
  152. // Update my triangles if necessary
  153. update();
  154. core::matrix4 mat(core::matrix4::EM4CONST_NOTHING);
  155. core::aabbox3df tBox(box);
  156. if (SceneNode)
  157. {
  158. SceneNode->getAbsoluteTransformation().getInverse(mat);
  159. mat.transformBoxEx(tBox);
  160. }
  161. if (transform)
  162. mat = *transform;
  163. else
  164. mat.makeIdentity();
  165. if (SceneNode)
  166. mat *= SceneNode->getAbsoluteTransformation();
  167. outTriangleCount = 0;
  168. if (!tBox.intersectsWithBox(BoundingBox))
  169. return;
  170. s32 triangleCount = 0;
  171. const u32 cnt = Triangles.size();
  172. for (u32 i=0; i<cnt; ++i)
  173. {
  174. // This isn't an accurate test, but it's fast, and the
  175. // API contract doesn't guarantee complete accuracy.
  176. if (Triangles[i].isTotalOutsideBox(tBox))
  177. continue;
  178. triangles[triangleCount] = Triangles[i];
  179. mat.transformVect(triangles[triangleCount].pointA);
  180. mat.transformVect(triangles[triangleCount].pointB);
  181. mat.transformVect(triangles[triangleCount].pointC);
  182. ++triangleCount;
  183. if (triangleCount == arraySize)
  184. break;
  185. }
  186. outTriangleCount = triangleCount;
  187. }
  188. //! Gets all triangles which have or may have contact with a 3d line.
  189. void CTriangleSelector::getTriangles(core::triangle3df* triangles,
  190. s32 arraySize, s32& outTriangleCount,
  191. const core::line3d<f32>& line,
  192. const core::matrix4* transform) const
  193. {
  194. // Update my triangles if necessary
  195. update();
  196. core::aabbox3d<f32> box(line.start);
  197. box.addInternalPoint(line.end);
  198. // TODO: Could be optimized for line a little bit more.
  199. getTriangles(triangles, arraySize, outTriangleCount,
  200. box, transform);
  201. }
  202. //! Returns amount of all available triangles in this selector
  203. s32 CTriangleSelector::getTriangleCount() const
  204. {
  205. return Triangles.size();
  206. }
  207. /* Get the number of TriangleSelectors that are part of this one.
  208. Only useful for MetaTriangleSelector others return 1
  209. */
  210. u32 CTriangleSelector::getSelectorCount() const
  211. {
  212. return 1;
  213. }
  214. /* Get the TriangleSelector based on index based on getSelectorCount.
  215. Only useful for MetaTriangleSelector others return 'this' or 0
  216. */
  217. ITriangleSelector* CTriangleSelector::getSelector(u32 index)
  218. {
  219. if (index)
  220. return 0;
  221. else
  222. return this;
  223. }
  224. /* Get the TriangleSelector based on index based on getSelectorCount.
  225. Only useful for MetaTriangleSelector others return 'this' or 0
  226. */
  227. const ITriangleSelector* CTriangleSelector::getSelector(u32 index) const
  228. {
  229. if (index)
  230. return 0;
  231. else
  232. return this;
  233. }
  234. } // end namespace scene
  235. } // end namespace irr