Surface.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SURFACE_H__
  21. #define __SURFACE_H__
  22. /*
  23. ===============================================================================
  24. Surface base class.
  25. A surface is tesselated to a triangle mesh with each edge shared by
  26. at most two triangles.
  27. ===============================================================================
  28. */
  29. typedef struct surfaceEdge_s {
  30. int verts[2]; // edge vertices always with ( verts[0] < verts[1] )
  31. int tris[2]; // edge triangles
  32. } surfaceEdge_t;
  33. class idSurface {
  34. public:
  35. idSurface( void );
  36. explicit idSurface( const idSurface &surf );
  37. explicit idSurface( const idDrawVert *verts, const int numVerts, const int *indexes, const int numIndexes );
  38. ~idSurface( void );
  39. const idDrawVert & operator[]( const int index ) const;
  40. idDrawVert & operator[]( const int index );
  41. idSurface & operator+=( const idSurface &surf );
  42. int GetNumIndexes( void ) const { return indexes.Num(); }
  43. const int * GetIndexes( void ) const { return indexes.Ptr(); }
  44. int GetNumVertices( void ) const { return verts.Num(); }
  45. const idDrawVert * GetVertices( void ) const { return verts.Ptr(); }
  46. const int * GetEdgeIndexes( void ) const { return edgeIndexes.Ptr(); }
  47. const surfaceEdge_t * GetEdges( void ) const { return edges.Ptr(); }
  48. void Clear( void );
  49. void SwapTriangles( idSurface &surf );
  50. void TranslateSelf( const idVec3 &translation );
  51. void RotateSelf( const idMat3 &rotation );
  52. // splits the surface into a front and back surface, the surface itself stays unchanged
  53. // frontOnPlaneEdges and backOnPlaneEdges optionally store the indexes to the edges that lay on the split plane
  54. // returns a SIDE_?
  55. int Split( const idPlane &plane, const float epsilon, idSurface **front, idSurface **back, int *frontOnPlaneEdges = NULL, int *backOnPlaneEdges = NULL ) const;
  56. // cuts off the part at the back side of the plane, returns true if some part was at the front
  57. // if there is nothing at the front the number of points is set to zero
  58. bool ClipInPlace( const idPlane &plane, const float epsilon = ON_EPSILON, const bool keepOn = false );
  59. // returns true if each triangle can be reached from any other triangle by a traversal
  60. bool IsConnected( void ) const;
  61. // returns true if the surface is closed
  62. bool IsClosed( void ) const;
  63. // returns true if the surface is a convex hull
  64. bool IsPolytope( const float epsilon = 0.1f ) const;
  65. float PlaneDistance( const idPlane &plane ) const;
  66. int PlaneSide( const idPlane &plane, const float epsilon = ON_EPSILON ) const;
  67. // returns true if the line intersects one of the surface triangles
  68. bool LineIntersection( const idVec3 &start, const idVec3 &end, bool backFaceCull = false ) const;
  69. // intersection point is start + dir * scale
  70. bool RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale, bool backFaceCull = false ) const;
  71. protected:
  72. idList<idDrawVert> verts; // vertices
  73. idList<int> indexes; // 3 references to vertices for each triangle
  74. idList<surfaceEdge_t> edges; // edges
  75. idList<int> edgeIndexes; // 3 references to edges for each triangle, may be negative for reversed edge
  76. protected:
  77. void GenerateEdgeIndexes( void );
  78. int FindEdge( int v1, int v2 ) const;
  79. };
  80. /*
  81. ====================
  82. idSurface::idSurface
  83. ====================
  84. */
  85. ID_INLINE idSurface::idSurface( void ) {
  86. }
  87. /*
  88. =================
  89. idSurface::idSurface
  90. =================
  91. */
  92. ID_INLINE idSurface::idSurface( const idDrawVert *verts, const int numVerts, const int *indexes, const int numIndexes ) {
  93. assert( verts != NULL && indexes != NULL && numVerts > 0 && numIndexes > 0 );
  94. this->verts.SetNum( numVerts );
  95. memcpy( this->verts.Ptr(), verts, numVerts * sizeof( verts[0] ) );
  96. this->indexes.SetNum( numIndexes );
  97. memcpy( this->indexes.Ptr(), indexes, numIndexes * sizeof( indexes[0] ) );
  98. GenerateEdgeIndexes();
  99. }
  100. /*
  101. ====================
  102. idSurface::idSurface
  103. ====================
  104. */
  105. ID_INLINE idSurface::idSurface( const idSurface &surf ) {
  106. this->verts = surf.verts;
  107. this->indexes = surf.indexes;
  108. this->edges = surf.edges;
  109. this->edgeIndexes = surf.edgeIndexes;
  110. }
  111. /*
  112. ====================
  113. idSurface::~idSurface
  114. ====================
  115. */
  116. ID_INLINE idSurface::~idSurface( void ) {
  117. }
  118. /*
  119. =================
  120. idSurface::operator[]
  121. =================
  122. */
  123. ID_INLINE const idDrawVert &idSurface::operator[]( const int index ) const {
  124. return verts[ index ];
  125. };
  126. /*
  127. =================
  128. idSurface::operator[]
  129. =================
  130. */
  131. ID_INLINE idDrawVert &idSurface::operator[]( const int index ) {
  132. return verts[ index ];
  133. };
  134. /*
  135. =================
  136. idSurface::operator+=
  137. =================
  138. */
  139. ID_INLINE idSurface &idSurface::operator+=( const idSurface &surf ) {
  140. int i, m, n;
  141. n = verts.Num();
  142. m = indexes.Num();
  143. verts.Append( surf.verts ); // merge verts where possible ?
  144. indexes.Append( surf.indexes );
  145. for ( i = m; i < indexes.Num(); i++ ) {
  146. indexes[i] += n;
  147. }
  148. GenerateEdgeIndexes();
  149. return *this;
  150. }
  151. /*
  152. =================
  153. idSurface::Clear
  154. =================
  155. */
  156. ID_INLINE void idSurface::Clear( void ) {
  157. verts.Clear();
  158. indexes.Clear();
  159. edges.Clear();
  160. edgeIndexes.Clear();
  161. }
  162. /*
  163. =================
  164. idSurface::SwapTriangles
  165. =================
  166. */
  167. ID_INLINE void idSurface::SwapTriangles( idSurface &surf ) {
  168. verts.Swap( surf.verts );
  169. indexes.Swap( surf.indexes );
  170. edges.Swap( surf.edges );
  171. edgeIndexes.Swap( surf.edgeIndexes );
  172. }
  173. /*
  174. =================
  175. idSurface::TranslateSelf
  176. =================
  177. */
  178. ID_INLINE void idSurface::TranslateSelf( const idVec3 &translation ) {
  179. for ( int i = 0; i < verts.Num(); i++ ) {
  180. verts[i].xyz += translation;
  181. }
  182. }
  183. /*
  184. =================
  185. idSurface::RotateSelf
  186. =================
  187. */
  188. ID_INLINE void idSurface::RotateSelf( const idMat3 &rotation ) {
  189. for ( int i = 0; i < verts.Num(); i++ ) {
  190. verts[i].xyz *= rotation;
  191. verts[i].normal *= rotation;
  192. verts[i].tangents[0] *= rotation;
  193. verts[i].tangents[1] *= rotation;
  194. }
  195. }
  196. #endif /* !__SURFACE_H__ */