splineUtil.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "graphics/splineUtil.h"
  23. #include "platform/platformGL.h"
  24. namespace SplineUtil{
  25. //------------------------------------------------------------------------------
  26. // Draws strip of specified width along spline. Polys on strip segments are
  27. // front-facing (billboarded)
  28. //------------------------------------------------------------------------------
  29. void drawSplineBeam( const Point3F& camPos, U32 numSegments,
  30. F32 width, SplinePatch &spline, F32 uvOffset, F32 numTexRep )
  31. {
  32. Point3F beginPoint, endPoint;
  33. spline.calc( 0.0, beginPoint );
  34. spline.calc( 1.0, endPoint );
  35. F32 approxBeamLength = (beginPoint - endPoint).len();
  36. F32 texRepFactor = approxBeamLength * numTexRep;
  37. glBegin(GL_TRIANGLE_STRIP);
  38. for( U32 i=0; i<numSegments; i++ )
  39. {
  40. F32 t = ((F32)i) / ((F32)(numSegments - 1));
  41. Point3F curPoint;
  42. spline.calc( t, curPoint );
  43. Point3F segmentDir;
  44. // handle last segment case
  45. Point3F nextPoint;
  46. if( i == (numSegments - 1) )
  47. {
  48. F32 modT = ((F32)(numSegments - 1)) / ((F32)numSegments);
  49. spline.calc( modT, nextPoint );
  50. segmentDir = curPoint - nextPoint;
  51. }
  52. else
  53. {
  54. F32 modT = t + (1.0f / numSegments);
  55. spline.calc( modT, nextPoint );
  56. segmentDir = nextPoint - curPoint;
  57. }
  58. if( segmentDir.isZero() ) continue;
  59. segmentDir.normalize();
  60. Point3F dirFromCam = curPoint - camPos;
  61. Point3F crossVec;
  62. mCross(dirFromCam, segmentDir, &crossVec);
  63. crossVec.normalize();
  64. crossVec *= width * 0.5f;
  65. F32 u = uvOffset + texRepFactor * t;
  66. glTexCoord2f( u, 0.0f );
  67. glVertex3fv( curPoint + crossVec );
  68. glTexCoord2f( u, 1.0f );
  69. glVertex3fv( curPoint - crossVec );
  70. }
  71. glEnd();
  72. }
  73. //------------------------------------------------------------------------------
  74. // Draws strip of specified width along spline. Polys on strip segments are
  75. // front-facing (billboarded)
  76. //------------------------------------------------------------------------------
  77. void drawSplineBeam( SplineBeamInfo &sbi )
  78. {
  79. if( !sbi.camPos || !sbi.spline ) return;
  80. Point3F beginPoint, endPoint;
  81. sbi.spline->calc( 0.0, beginPoint );
  82. sbi.spline->calc( 1.0, endPoint );
  83. F32 approxBeamLength = (beginPoint - endPoint).len();
  84. F32 texRepFactor = approxBeamLength * sbi.numTexRep;
  85. glBegin(GL_TRIANGLE_STRIP);
  86. for( U32 i=0; i<sbi.numSegments; i++ )
  87. {
  88. F32 t = ((F32)i) / ((F32)(sbi.numSegments - 1));
  89. Point3F curPoint;
  90. sbi.spline->calc( t, curPoint );
  91. Point3F segmentDir;
  92. // handle last segment case
  93. Point3F nextPoint;
  94. if( i == (sbi.numSegments - 1) )
  95. {
  96. F32 modT = ((F32)(sbi.numSegments - 1)) / ((F32)sbi.numSegments);
  97. sbi.spline->calc( modT, nextPoint );
  98. segmentDir = curPoint - nextPoint;
  99. }
  100. else
  101. {
  102. F32 modT = t + (1.0f / sbi.numSegments);
  103. sbi.spline->calc( modT, nextPoint );
  104. segmentDir = nextPoint - curPoint;
  105. }
  106. if( segmentDir.isZero() ) continue;
  107. segmentDir.normalize();
  108. Point3F dirFromCam = curPoint - *sbi.camPos;
  109. Point3F crossVec;
  110. mCross(dirFromCam, segmentDir, &crossVec);
  111. crossVec.normalize();
  112. crossVec *= sbi.width * 0.5f;
  113. F32 u = sbi.uvOffset + texRepFactor * t;
  114. if( i== 0 && sbi.zeroAlphaStart )
  115. {
  116. glColor4f( sbi.color.red, sbi.color.green, sbi.color.blue, 0.0f );
  117. }
  118. else
  119. {
  120. glColor4fv( sbi.color.address());
  121. }
  122. glTexCoord2f( u, 0.0 );
  123. glVertex3fv( curPoint + crossVec );
  124. glTexCoord2f( u, 1.0 );
  125. glVertex3fv( curPoint - crossVec );
  126. }
  127. glEnd();
  128. }
  129. } // end SplineUtil namespace