Surface_SweptSpline.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include "../precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. ====================
  24. idSurface_SweptSpline::SetSpline
  25. ====================
  26. */
  27. void idSurface_SweptSpline::SetSpline( idCurve_Spline<idVec4> *spline ) {
  28. if ( this->spline ) {
  29. delete this->spline;
  30. }
  31. this->spline = spline;
  32. }
  33. /*
  34. ====================
  35. idSurface_SweptSpline::SetSweptSpline
  36. ====================
  37. */
  38. void idSurface_SweptSpline::SetSweptSpline( idCurve_Spline<idVec4> *sweptSpline ) {
  39. if ( this->sweptSpline ) {
  40. delete this->sweptSpline;
  41. }
  42. this->sweptSpline = sweptSpline;
  43. }
  44. /*
  45. ====================
  46. idSurface_SweptSpline::SetSweptCircle
  47. Sets the swept spline to a NURBS circle.
  48. ====================
  49. */
  50. void idSurface_SweptSpline::SetSweptCircle( const float radius ) {
  51. idCurve_NURBS<idVec4> *nurbs = new idCurve_NURBS<idVec4>();
  52. nurbs->Clear();
  53. nurbs->AddValue( 0.0f, idVec4( radius, radius, 0.0f, 0.00f ) );
  54. nurbs->AddValue( 100.0f, idVec4( -radius, radius, 0.0f, 0.25f ) );
  55. nurbs->AddValue( 200.0f, idVec4( -radius, -radius, 0.0f, 0.50f ) );
  56. nurbs->AddValue( 300.0f, idVec4( radius, -radius, 0.0f, 0.75f ) );
  57. nurbs->SetBoundaryType( idCurve_NURBS<idVec4>::BT_CLOSED );
  58. nurbs->SetCloseTime( 100.0f );
  59. if ( sweptSpline ) {
  60. delete sweptSpline;
  61. }
  62. sweptSpline = nurbs;
  63. }
  64. /*
  65. ====================
  66. idSurface_SweptSpline::GetFrame
  67. ====================
  68. */
  69. void idSurface_SweptSpline::GetFrame( const idMat3 &previousFrame, const idVec3 dir, idMat3 &newFrame ) {
  70. float wx, wy, wz;
  71. float xx, yy, yz;
  72. float xy, xz, zz;
  73. float x2, y2, z2;
  74. float a, c, s, x, y, z;
  75. idVec3 d, v;
  76. idMat3 axis;
  77. d = dir;
  78. d.Normalize();
  79. v = d.Cross( previousFrame[2] );
  80. v.Normalize();
  81. a = idMath::ACos( previousFrame[2] * d ) * 0.5f;
  82. c = idMath::Cos( a );
  83. s = idMath::Sqrt( 1.0f - c * c );
  84. x = v[0] * s;
  85. y = v[1] * s;
  86. z = v[2] * s;
  87. x2 = x + x;
  88. y2 = y + y;
  89. z2 = z + z;
  90. xx = x * x2;
  91. xy = x * y2;
  92. xz = x * z2;
  93. yy = y * y2;
  94. yz = y * z2;
  95. zz = z * z2;
  96. wx = c * x2;
  97. wy = c * y2;
  98. wz = c * z2;
  99. axis[0][0] = 1.0f - ( yy + zz );
  100. axis[0][1] = xy - wz;
  101. axis[0][2] = xz + wy;
  102. axis[1][0] = xy + wz;
  103. axis[1][1] = 1.0f - ( xx + zz );
  104. axis[1][2] = yz - wx;
  105. axis[2][0] = xz - wy;
  106. axis[2][1] = yz + wx;
  107. axis[2][2] = 1.0f - ( xx + yy );
  108. newFrame = previousFrame * axis;
  109. newFrame[2] = dir;
  110. newFrame[2].Normalize();
  111. newFrame[1].Cross( newFrame[ 2 ], newFrame[ 0 ] );
  112. newFrame[1].Normalize();
  113. newFrame[0].Cross( newFrame[ 1 ], newFrame[ 2 ] );
  114. newFrame[0].Normalize();
  115. }
  116. /*
  117. ====================
  118. idSurface_SweptSpline::Tessellate
  119. tesselate the surface
  120. ====================
  121. */
  122. void idSurface_SweptSpline::Tessellate( const int splineSubdivisions, const int sweptSplineSubdivisions ) {
  123. int i, j, offset, baseOffset, splineDiv, sweptSplineDiv;
  124. int i0, i1, j0, j1;
  125. float totalTime, t;
  126. idVec4 splinePos, splineD1;
  127. idMat3 splineMat;
  128. if ( !spline || !sweptSpline ) {
  129. idSurface::Clear();
  130. return;
  131. }
  132. verts.SetNum( splineSubdivisions * sweptSplineSubdivisions, false );
  133. // calculate the points and first derivatives for the swept spline
  134. totalTime = sweptSpline->GetTime( sweptSpline->GetNumValues() - 1 ) - sweptSpline->GetTime( 0 ) + sweptSpline->GetCloseTime();
  135. sweptSplineDiv = sweptSpline->GetBoundaryType() == idCurve_Spline<idVec3>::BT_CLOSED ? sweptSplineSubdivisions : sweptSplineSubdivisions - 1;
  136. baseOffset = (splineSubdivisions-1) * sweptSplineSubdivisions;
  137. for ( i = 0; i < sweptSplineSubdivisions; i++ ) {
  138. t = totalTime * i / sweptSplineDiv;
  139. splinePos = sweptSpline->GetCurrentValue( t );
  140. splineD1 = sweptSpline->GetCurrentFirstDerivative( t );
  141. verts[baseOffset+i].xyz = splinePos.ToVec3();
  142. verts[baseOffset+i].st[0] = splinePos.w;
  143. verts[baseOffset+i].tangents[0] = splineD1.ToVec3();
  144. }
  145. // sweep the spline
  146. totalTime = spline->GetTime( spline->GetNumValues() - 1 ) - spline->GetTime( 0 ) + spline->GetCloseTime();
  147. splineDiv = spline->GetBoundaryType() == idCurve_Spline<idVec3>::BT_CLOSED ? splineSubdivisions : splineSubdivisions - 1;
  148. splineMat.Identity();
  149. for ( i = 0; i < splineSubdivisions; i++ ) {
  150. t = totalTime * i / splineDiv;
  151. splinePos = spline->GetCurrentValue( t );
  152. splineD1 = spline->GetCurrentFirstDerivative( t );
  153. GetFrame( splineMat, splineD1.ToVec3(), splineMat );
  154. offset = i * sweptSplineSubdivisions;
  155. for ( j = 0; j < sweptSplineSubdivisions; j++ ) {
  156. idDrawVert *v = &verts[offset+j];
  157. v->xyz = splinePos.ToVec3() + verts[baseOffset+j].xyz * splineMat;
  158. v->st[0] = verts[baseOffset+j].st[0];
  159. v->st[1] = splinePos.w;
  160. v->tangents[0] = verts[baseOffset+j].tangents[0] * splineMat;
  161. v->tangents[1] = splineD1.ToVec3();
  162. v->normal = v->tangents[1].Cross( v->tangents[0] );
  163. v->normal.Normalize();
  164. v->color[0] = v->color[1] = v->color[2] = v->color[3] = 0;
  165. }
  166. }
  167. indexes.SetNum( splineDiv * sweptSplineDiv * 2 * 3, false );
  168. // create indexes for the triangles
  169. for ( offset = i = 0; i < splineDiv; i++ ) {
  170. i0 = (i+0) * sweptSplineSubdivisions;
  171. i1 = (i+1) % splineSubdivisions * sweptSplineSubdivisions;
  172. for ( j = 0; j < sweptSplineDiv; j++ ) {
  173. j0 = (j+0);
  174. j1 = (j+1) % sweptSplineSubdivisions;
  175. indexes[offset++] = i0 + j0;
  176. indexes[offset++] = i0 + j1;
  177. indexes[offset++] = i1 + j1;
  178. indexes[offset++] = i1 + j1;
  179. indexes[offset++] = i1 + j0;
  180. indexes[offset++] = i0 + j0;
  181. }
  182. }
  183. GenerateEdgeIndexes();
  184. }