windEmitter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "T3D/fx/windEmitter.h"
  24. #include "math/mBox.h"
  25. #include "core/tAlgorithm.h"
  26. #include "platform/profiler.h"
  27. Vector<WindEmitter*> WindEmitter::smAllEmitters;
  28. WindEmitter::WindEmitter()
  29. {
  30. smAllEmitters.push_back( this );
  31. mEnabled = true;
  32. mScore = 0.0f;
  33. mSphere.center.zero();
  34. mSphere.radius = 0.0f;
  35. mStrength = 0.0f;
  36. mTurbulenceFrequency = 0.0f;
  37. mTurbulenceStrength = 0.0f;
  38. mVelocity.zero();
  39. }
  40. WindEmitter::~WindEmitter()
  41. {
  42. WindEmitterList::iterator iter = T3D::find( smAllEmitters.begin(), smAllEmitters.end(), this );
  43. smAllEmitters.erase( iter );
  44. }
  45. void WindEmitter::setPosition( const Point3F& pos )
  46. {
  47. mSphere.center = pos;
  48. }
  49. void WindEmitter::update( const Point3F& pos, const VectorF& velocity )
  50. {
  51. mSphere.center = pos;
  52. mVelocity = velocity;
  53. }
  54. void WindEmitter::setRadius( F32 radius )
  55. {
  56. mSphere.radius = radius;
  57. }
  58. void WindEmitter::setStrength( F32 strength )
  59. {
  60. mStrength = strength;
  61. }
  62. void WindEmitter::setTurbulency( F32 frequency, F32 strength )
  63. {
  64. mTurbulenceFrequency = frequency;
  65. mTurbulenceStrength = strength;
  66. }
  67. S32 QSORT_CALLBACK WindEmitter::_sortByScore(const void* a, const void* b)
  68. {
  69. return mSign((*(WindEmitter**)b)->mScore - (*(WindEmitter**)a)->mScore);
  70. }
  71. bool WindEmitter::findBest( const Point3F& cameraPos,
  72. const VectorF& cameraDir,
  73. F32 viewDistance,
  74. U32 maxResults,
  75. WindEmitterList* results )
  76. {
  77. PROFILE_START(WindEmitter_findBest);
  78. // Build a sphere from the camera point.
  79. SphereF cameraSphere;
  80. cameraSphere.center = cameraPos;
  81. cameraSphere.radius = viewDistance;
  82. // Collect the active spheres within the camera space and score them.
  83. WindEmitterList best;
  84. WindEmitterList::iterator iter = smAllEmitters.begin();
  85. for ( ; iter != smAllEmitters.end(); iter++ )
  86. {
  87. const SphereF& sphere = *(*iter);
  88. // Skip any spheres outside of our camera range or that are disabled.
  89. if ( !(*iter)->mEnabled || !cameraSphere.isIntersecting( sphere ) )
  90. continue;
  91. // Simple score calculation...
  92. //
  93. // score = ( radius / distance to camera ) * dot( cameraDir, vector from camera to sphere )
  94. //
  95. Point3F vect = sphere.center - cameraSphere.center;
  96. F32 dist = vect.len();
  97. (*iter)->mScore = dist * sphere.radius;
  98. vect /= getMax( dist, 0.001f );
  99. (*iter)->mScore *= mDot( vect, cameraDir );
  100. best.push_back( *iter );
  101. }
  102. // Sort the results by score!
  103. dQsort( best.address(), best.size(), sizeof(WindEmitter*), &WindEmitter::_sortByScore );
  104. // Clip the results to the max requested.
  105. if ( best.size() > maxResults )
  106. best.setSize( maxResults );
  107. // Merge the results and return.
  108. results->merge( best );
  109. PROFILE_END(); // WindEmitter_findBest
  110. return best.size() > 0;
  111. }