Surface.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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();
  36. explicit idSurface( const idSurface &surf );
  37. explicit idSurface( const idDrawVert *verts, const int numVerts, const int *indexes, const int numIndexes );
  38. ~idSurface();
  39. const idDrawVert & operator[]( const int index ) const;
  40. idDrawVert & operator[]( const int index );
  41. idSurface & operator+=( const idSurface &surf );
  42. int GetNumIndexes() const { return indexes.Num(); }
  43. const int * GetIndexes() const { return indexes.Ptr(); }
  44. int GetNumVertices() const { return verts.Num(); }
  45. const idDrawVert * GetVertices() const { return verts.Ptr(); }
  46. const int * GetEdgeIndexes() const { return edgeIndexes.Ptr(); }
  47. const surfaceEdge_t * GetEdges() const { return edges.Ptr(); }
  48. void Clear();
  49. void TranslateSelf( const idVec3 &translation );
  50. void RotateSelf( const idMat3 &rotation );
  51. // splits the surface into a front and back surface, the surface itself stays unchanged
  52. // frontOnPlaneEdges and backOnPlaneEdges optionally store the indexes to the edges that lay on the split plane
  53. // returns a SIDE_?
  54. int Split( const idPlane &plane, const float epsilon, idSurface **front, idSurface **back, int *frontOnPlaneEdges = NULL, int *backOnPlaneEdges = NULL ) const;
  55. // cuts off the part at the back side of the plane, returns true if some part was at the front
  56. // if there is nothing at the front the number of points is set to zero
  57. bool ClipInPlace( const idPlane &plane, const float epsilon = ON_EPSILON, const bool keepOn = false );
  58. // returns true if each triangle can be reached from any other triangle by a traversal
  59. bool IsConnected() const;
  60. // returns true if the surface is closed
  61. bool IsClosed() const;
  62. // returns true if the surface is a convex hull
  63. bool IsPolytope( const float epsilon = 0.1f ) const;
  64. float PlaneDistance( const idPlane &plane ) const;
  65. int PlaneSide( const idPlane &plane, const float epsilon = ON_EPSILON ) const;
  66. // returns true if the line intersects one of the surface triangles
  67. bool LineIntersection( const idVec3 &start, const idVec3 &end, bool backFaceCull = false ) const;
  68. // intersection point is start + dir * scale
  69. bool RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale, bool backFaceCull = false ) const;
  70. protected:
  71. idList<idDrawVert, TAG_IDLIB_LIST_SURFACE> verts; // vertices
  72. idList<int, TAG_IDLIB_LIST_SURFACE> indexes; // 3 references to vertices for each triangle
  73. idList<surfaceEdge_t, TAG_IDLIB_LIST_SURFACE> edges; // edges
  74. idList<int, TAG_IDLIB_LIST_SURFACE> edgeIndexes; // 3 references to edges for each triangle, may be negative for reversed edge
  75. protected:
  76. void GenerateEdgeIndexes();
  77. int FindEdge( int v1, int v2 ) const;
  78. };
  79. /*
  80. ====================
  81. idSurface::idSurface
  82. ====================
  83. */
  84. ID_INLINE idSurface::idSurface() {
  85. }
  86. /*
  87. =================
  88. idSurface::idSurface
  89. =================
  90. */
  91. ID_INLINE idSurface::idSurface( const idDrawVert *verts, const int numVerts, const int *indexes, const int numIndexes ) {
  92. assert( verts != NULL && indexes != NULL && numVerts > 0 && numIndexes > 0 );
  93. this->verts.SetNum( numVerts );
  94. memcpy( this->verts.Ptr(), verts, numVerts * sizeof( verts[0] ) );
  95. this->indexes.SetNum( numIndexes );
  96. memcpy( this->indexes.Ptr(), indexes, numIndexes * sizeof( indexes[0] ) );
  97. GenerateEdgeIndexes();
  98. }
  99. /*
  100. ====================
  101. idSurface::idSurface
  102. ====================
  103. */
  104. ID_INLINE idSurface::idSurface( const idSurface &surf ) {
  105. this->verts = surf.verts;
  106. this->indexes = surf.indexes;
  107. this->edges = surf.edges;
  108. this->edgeIndexes = surf.edgeIndexes;
  109. }
  110. /*
  111. ====================
  112. idSurface::~idSurface
  113. ====================
  114. */
  115. ID_INLINE idSurface::~idSurface() {
  116. }
  117. /*
  118. =================
  119. idSurface::operator[]
  120. =================
  121. */
  122. ID_INLINE const idDrawVert &idSurface::operator[]( const int index ) const {
  123. return verts[ index ];
  124. };
  125. /*
  126. =================
  127. idSurface::operator[]
  128. =================
  129. */
  130. ID_INLINE idDrawVert &idSurface::operator[]( const int index ) {
  131. return verts[ index ];
  132. };
  133. /*
  134. =================
  135. idSurface::operator+=
  136. =================
  137. */
  138. ID_INLINE idSurface &idSurface::operator+=( const idSurface &surf ) {
  139. int i, m, n;
  140. n = verts.Num();
  141. m = indexes.Num();
  142. verts.Append( surf.verts ); // merge verts where possible ?
  143. indexes.Append( surf.indexes );
  144. for ( i = m; i < indexes.Num(); i++ ) {
  145. indexes[i] += n;
  146. }
  147. GenerateEdgeIndexes();
  148. return *this;
  149. }
  150. /*
  151. =================
  152. idSurface::Clear
  153. =================
  154. */
  155. ID_INLINE void idSurface::Clear() {
  156. verts.Clear();
  157. indexes.Clear();
  158. edges.Clear();
  159. edgeIndexes.Clear();
  160. }
  161. /*
  162. =================
  163. idSurface::TranslateSelf
  164. =================
  165. */
  166. ID_INLINE void idSurface::TranslateSelf( const idVec3 &translation ) {
  167. for ( int i = 0; i < verts.Num(); i++ ) {
  168. verts[i].xyz += translation;
  169. }
  170. }
  171. /*
  172. =================
  173. idSurface::RotateSelf
  174. =================
  175. */
  176. ID_INLINE void idSurface::RotateSelf( const idMat3 &rotation ) {
  177. for ( int i = 0; i < verts.Num(); i++ ) {
  178. verts[i].xyz *= rotation;
  179. verts[i].SetNormal( verts[i].GetNormal() * rotation );
  180. verts[i].SetTangent( verts[i].GetTangent() * rotation );
  181. }
  182. }
  183. #endif /* !__SURFACE_H__ */