You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

209 lines
7.0 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2013, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file BlenderTessellator.h
  34. * @brief A simple tessellation wrapper
  35. */
  36. #ifndef INCLUDED_AI_BLEND_TESSELLATOR_H
  37. #define INCLUDED_AI_BLEND_TESSELLATOR_H
  38. // Use these to toggle between GLU Tessellate or poly2tri
  39. // Note (acg) keep GLU Tesselate disabled by default - if it is turned on,
  40. // assimp needs to be linked against GLU, which is currently not yet
  41. // made configurable in CMake and potentially not wanted by most users
  42. // as it requires a Gl environment.
  43. #ifndef ASSIMP_BLEND_WITH_GLU_TESSELLATE
  44. # define ASSIMP_BLEND_WITH_GLU_TESSELLATE 0
  45. #endif
  46. #ifndef ASSIMP_BLEND_WITH_POLY_2_TRI
  47. # define ASSIMP_BLEND_WITH_POLY_2_TRI 1
  48. #endif
  49. #include "LogAux.h"
  50. #if ASSIMP_BLEND_WITH_GLU_TESSELLATE
  51. #if defined( WIN32 ) || defined( _WIN32 ) || defined( _MSC_VER )
  52. #include <windows.h>
  53. #endif
  54. #include <GL/glu.h>
  55. namespace Assimp
  56. {
  57. class BlenderBMeshConverter;
  58. // TinyFormatter.h
  59. namespace Formatter
  60. {
  61. template < typename T,typename TR, typename A > class basic_formatter;
  62. typedef class basic_formatter< char, std::char_traits< char >, std::allocator< char > > format;
  63. }
  64. // BlenderScene.h
  65. namespace Blender
  66. {
  67. struct MLoop;
  68. struct MVert;
  69. struct VertexGL
  70. {
  71. GLdouble X;
  72. GLdouble Y;
  73. GLdouble Z;
  74. int index;
  75. int magic;
  76. VertexGL( GLdouble X, GLdouble Y, GLdouble Z, int index, int magic ): X( X ), Y( Y ), Z( Z ), index( index ), magic( magic ) { }
  77. };
  78. struct DrawCallGL
  79. {
  80. GLenum drawMode;
  81. int baseVertex;
  82. int vertexCount;
  83. DrawCallGL( GLenum drawMode, int baseVertex ): drawMode( drawMode ), baseVertex( baseVertex ), vertexCount( 0 ) { }
  84. };
  85. struct TessDataGL
  86. {
  87. std::vector< DrawCallGL > drawCalls;
  88. std::vector< VertexGL > vertices;
  89. };
  90. }
  91. class BlenderTessellatorGL: public LogFunctions< BlenderTessellatorGL >
  92. {
  93. public:
  94. BlenderTessellatorGL( BlenderBMeshConverter& converter );
  95. ~BlenderTessellatorGL( );
  96. void Tessellate( const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices );
  97. private:
  98. void AssertVertexCount( int vertexCount );
  99. void GenerateLoopVerts( std::vector< Blender::VertexGL >& polyLoopGL, const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices );
  100. void Tesssellate( std::vector< Blender::VertexGL >& polyLoopGL, Blender::TessDataGL& tessData );
  101. void TriangulateDrawCalls( const Blender::TessDataGL& tessData );
  102. void MakeFacesFromTris( const Blender::VertexGL* vertices, int vertexCount );
  103. void MakeFacesFromTriStrip( const Blender::VertexGL* vertices, int vertexCount );
  104. void MakeFacesFromTriFan( const Blender::VertexGL* vertices, int vertexCount );
  105. static void TessellateBegin( GLenum drawModeGL, void* userData );
  106. static void TessellateEnd( void* userData );
  107. static void TessellateVertex( const void* vtxData, void* userData );
  108. static void TessellateCombine( const GLdouble intersection[ 3 ], const GLdouble* [ 4 ], const GLfloat [ 4 ], GLdouble** out, void* userData );
  109. static void TessellateEdgeFlag( GLboolean edgeFlag, void* userData );
  110. static void TessellateError( GLenum errorCode, void* userData );
  111. BlenderBMeshConverter* converter;
  112. };
  113. } // end of namespace Assimp
  114. #endif // ASSIMP_BLEND_WITH_GLU_TESSELLATE
  115. #if ASSIMP_BLEND_WITH_POLY_2_TRI
  116. #include "../contrib/poly2tri/poly2tri/poly2tri.h"
  117. namespace Assimp
  118. {
  119. class BlenderBMeshConverter;
  120. // TinyFormatter.h
  121. namespace Formatter
  122. {
  123. template < typename T,typename TR, typename A > class basic_formatter;
  124. typedef class basic_formatter< char, std::char_traits< char >, std::allocator< char > > format;
  125. }
  126. // BlenderScene.h
  127. namespace Blender
  128. {
  129. struct MLoop;
  130. struct MVert;
  131. struct PointP2T
  132. {
  133. aiVector3D point3D;
  134. p2t::Point point2D;
  135. int magic;
  136. int index;
  137. };
  138. struct PlaneP2T
  139. {
  140. aiVector3D centre;
  141. aiVector3D normal;
  142. };
  143. }
  144. class BlenderTessellatorP2T: public LogFunctions< BlenderTessellatorP2T >
  145. {
  146. public:
  147. BlenderTessellatorP2T( BlenderBMeshConverter& converter );
  148. ~BlenderTessellatorP2T( );
  149. void Tessellate( const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices );
  150. private:
  151. void AssertVertexCount( int vertexCount );
  152. void Copy3DVertices( const Blender::MLoop* polyLoop, int vertexCount, const std::vector< Blender::MVert >& vertices, std::vector< Blender::PointP2T >& targetVertices ) const;
  153. aiMatrix4x4 GeneratePointTransformMatrix( const Blender::PlaneP2T& plane ) const;
  154. void TransformAndFlattenVectices( const aiMatrix4x4& transform, std::vector< Blender::PointP2T >& vertices ) const;
  155. void ReferencePoints( std::vector< Blender::PointP2T >& points, std::vector< p2t::Point* >& pointRefs ) const;
  156. inline Blender::PointP2T& GetActualPointStructure( p2t::Point& point ) const;
  157. void MakeFacesFromTriangles( std::vector< p2t::Triangle* >& triangles ) const;
  158. // Adapted from: http://missingbytes.blogspot.co.uk/2012/06/fitting-plane-to-point-cloud.html
  159. float FindLargestMatrixElem( const aiMatrix3x3& mtx ) const;
  160. aiMatrix3x3 ScaleMatrix( const aiMatrix3x3& mtx, float scale ) const;
  161. aiVector3D GetEigenVectorFromLargestEigenValue( const aiMatrix3x3& mtx ) const;
  162. Blender::PlaneP2T FindLLSQPlane( const std::vector< Blender::PointP2T >& points ) const;
  163. BlenderBMeshConverter* converter;
  164. };
  165. } // end of namespace Assimp
  166. #endif // ASSIMP_BLEND_WITH_POLY_2_TRI
  167. #endif // INCLUDED_AI_BLEND_TESSELLATOR_H