SpriteBatchQuery.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "2d/core/SpriteBatchQuery.h"
  23. #ifndef _SPRITE_BATCH_H_
  24. #include "2d/core/SpriteBatch.h"
  25. #endif
  26. #ifndef _SPRITE_BATCH_ITEM_H_
  27. #include "2d/core/SpriteBatchItem.h"
  28. #endif
  29. // Debug Profiling.
  30. #include "debug/profiler.h"
  31. //-----------------------------------------------------------------------------
  32. SpriteBatchQuery::SpriteBatchQuery( SpriteBatch* pSpriteBatch ) :
  33. mpSpriteBatch(pSpriteBatch),
  34. mIsRaycastQueryResult(false),
  35. mMasterQueryKey(0),
  36. mCheckPoint(false),
  37. mComparePoint(0.0f, 0.0f)
  38. {
  39. // Set debug associations.
  40. VECTOR_SET_ASSOCIATION( mQueryResults );
  41. // Clear the query.
  42. clearQuery();
  43. }
  44. //-----------------------------------------------------------------------------
  45. S32 SpriteBatchQuery::add( SpriteBatchItem* pSpriteBatchItem )
  46. {
  47. // Debug Profiling.
  48. PROFILE_SCOPE(SpriteBatchQuery_Add);
  49. return CreateProxy( pSpriteBatchItem->getLocalAABB(), pSpriteBatchItem );
  50. }
  51. //-----------------------------------------------------------------------------
  52. void SpriteBatchQuery::remove( SpriteBatchItem* pSpriteBatchItem )
  53. {
  54. // Debug Profiling.
  55. PROFILE_SCOPE(SpriteBatchQuery_Remove);
  56. DestroyProxy( pSpriteBatchItem->getProxyId() );
  57. }
  58. //-----------------------------------------------------------------------------
  59. bool SpriteBatchQuery::update( SpriteBatchItem* pSpriteBatchItem, const b2AABB& aabb, const b2Vec2& displacement )
  60. {
  61. // Debug Profiling.
  62. PROFILE_SCOPE(SpriteBatchQuery_Update);
  63. return MoveProxy( pSpriteBatchItem->getProxyId(), aabb, displacement );
  64. }
  65. //-----------------------------------------------------------------------------
  66. U32 SpriteBatchQuery::queryOOBB( const b2AABB& aabb, b2PolygonShape& oobb, const bool targetOOBB )
  67. {
  68. // This function is used exclusively when picking rectangular areas using CompositeSprite's pickArea ConsoleMethod
  69. // For rendering, SpriteBatchQuery::queryArea is used instead
  70. // Debug Profiling.
  71. PROFILE_SCOPE(SpriteBatchQuery_QueryArea);
  72. mMasterQueryKey++;
  73. // Flag as not a ray-cast query result.
  74. mIsRaycastQueryResult = false;
  75. mComparePolygonShape.Set(oobb.m_vertices,4);
  76. mComparePolygonShape.m_centroid = oobb.m_centroid;
  77. mCompareTransform.SetIdentity();
  78. mCheckOOBB = targetOOBB;
  79. Query( this, aabb );
  80. mCheckOOBB = false;
  81. return getQueryResultsCount();
  82. }
  83. U32 SpriteBatchQuery::queryArea( const b2AABB& aabb, const bool targetOOBB )
  84. {
  85. // Debug Profiling.
  86. PROFILE_SCOPE(SpriteBatchQuery_QueryArea);
  87. mMasterQueryKey++;
  88. // Flag as not a ray-cast query result.
  89. mIsRaycastQueryResult = false;
  90. // Query.
  91. b2Vec2 verts[4];
  92. verts[0].Set( aabb.lowerBound.x, aabb.lowerBound.y );
  93. verts[1].Set( aabb.upperBound.x, aabb.lowerBound.y );
  94. verts[2].Set( aabb.upperBound.x, aabb.upperBound.y );
  95. verts[3].Set( aabb.lowerBound.x, aabb.upperBound.y );
  96. mComparePolygonShape.Set( verts, 4 );
  97. mCompareTransform.SetIdentity();
  98. mCheckOOBB = targetOOBB;
  99. Query( this, aabb );
  100. mCheckOOBB = false;
  101. return getQueryResultsCount();
  102. }
  103. //-----------------------------------------------------------------------------
  104. U32 SpriteBatchQuery::queryRay( const Vector2& point1, const Vector2& point2, const bool targetOOBB )
  105. {
  106. // Debug Profiling.
  107. PROFILE_SCOPE(SpriteBatchQuery_QueryRay);
  108. mMasterQueryKey++;
  109. // Flag as a ray-cast query result.
  110. mIsRaycastQueryResult = true;
  111. // Query.
  112. mCompareRay.p1 = point1;
  113. mCompareRay.p2 = point2;
  114. mCompareRay.maxFraction = 1.0f;
  115. mCompareTransform.SetIdentity();
  116. mCheckOOBB = targetOOBB;
  117. RayCast( this, mCompareRay );
  118. mCheckOOBB = false;
  119. return getQueryResultsCount();
  120. }
  121. //-----------------------------------------------------------------------------
  122. U32 SpriteBatchQuery::queryPoint( const Vector2& point, const bool targetOOBB )
  123. {
  124. // Debug Profiling.
  125. PROFILE_SCOPE(SpriteBatchQuery_QueryPoint);
  126. mMasterQueryKey++;
  127. // Flag as not a ray-cast query result.
  128. mIsRaycastQueryResult = false;
  129. // Query.
  130. b2AABB aabb;
  131. aabb.lowerBound = point;
  132. aabb.upperBound = point;
  133. mCompareTransform.SetIdentity();
  134. mCheckOOBB = targetOOBB;
  135. Query( this, aabb );
  136. mCheckOOBB = false;
  137. return getQueryResultsCount();
  138. }
  139. //-----------------------------------------------------------------------------
  140. void SpriteBatchQuery::clearQuery( void )
  141. {
  142. mQueryResults.clear();
  143. }
  144. //-----------------------------------------------------------------------------
  145. void SpriteBatchQuery::sortRaycastQueryResult( void )
  146. {
  147. // Debug Profiling.
  148. PROFILE_SCOPE(SpriteBatchQuery_SortRayCastQueryResult);
  149. // Ignore if not a ray-cast query result or there are not results to sort.
  150. if ( !getIsRaycastQueryResult() || getQueryResultsCount() == 0 )
  151. return;
  152. // Sort query results.
  153. dQsort( mQueryResults.address(), mQueryResults.size(), sizeof(SpriteBatchQueryResult), rayCastFractionSort );
  154. }
  155. //-----------------------------------------------------------------------------
  156. bool SpriteBatchQuery::QueryCallback( S32 proxyId )
  157. {
  158. // Debug Profiling.
  159. PROFILE_SCOPE(SpriteBatchQuery_QueryCallback);
  160. // Fetch sprite batch item.
  161. SpriteBatchItem* pSpriteBatchItem = static_cast<SpriteBatchItem*>(GetUserData( proxyId ));
  162. // Ignore if already tagged with the sprite batch query key.
  163. if ( pSpriteBatchItem->getSpriteBatchQueryKey() == mMasterQueryKey )
  164. return true;
  165. // Check OOBB.
  166. if ( mCheckOOBB )
  167. {
  168. // Fetch the shapes render OOBB.
  169. b2PolygonShape oobb;
  170. oobb.Set( pSpriteBatchItem->getLocalOOBB(), 4);
  171. if ( mCheckPoint )
  172. {
  173. if ( !oobb.TestPoint( mCompareTransform, mComparePoint ) )
  174. return true;
  175. }
  176. else
  177. {
  178. if ( !b2TestOverlap( &mComparePolygonShape, 0, &oobb, 0, mCompareTransform, mCompareTransform ) )
  179. return true;
  180. }
  181. }
  182. // Tag with world query key.
  183. pSpriteBatchItem->setSpriteBatchQueryKey( mMasterQueryKey );
  184. SpriteBatchQueryResult queryResult( pSpriteBatchItem );
  185. mQueryResults.push_back( queryResult );
  186. return true;
  187. }
  188. //-----------------------------------------------------------------------------
  189. F32 SpriteBatchQuery::RayCastCallback( const b2RayCastInput& input, S32 proxyId )
  190. {
  191. // Debug Profiling.
  192. PROFILE_SCOPE(SpriteBatchQuery_RayCastCallback);
  193. // Fetch sprite batch item.
  194. SpriteBatchItem* pSpriteBatchItem = static_cast<SpriteBatchItem*>(GetUserData( proxyId ));
  195. // Ignore if already tagged with the sprite batch query key.
  196. if ( pSpriteBatchItem->getSpriteBatchQueryKey() == mMasterQueryKey )
  197. return 1.0f;
  198. // Check OOBB.
  199. if ( mCheckOOBB )
  200. {
  201. // Fetch the shapes render OOBB.
  202. b2PolygonShape oobb;
  203. oobb.Set( pSpriteBatchItem->getLocalOOBB(), 4);
  204. b2RayCastOutput rayOutput;
  205. if ( !oobb.RayCast( &rayOutput, mCompareRay, mCompareTransform, 0 ) )
  206. return true;
  207. }
  208. // Tag with world query key.
  209. pSpriteBatchItem->setSpriteBatchQueryKey( mMasterQueryKey );
  210. SpriteBatchQueryResult queryResult( pSpriteBatchItem );
  211. mQueryResults.push_back( queryResult );
  212. return 1.0f;
  213. }
  214. //-----------------------------------------------------------------------------
  215. S32 QSORT_CALLBACK SpriteBatchQuery::rayCastFractionSort(const void* a, const void* b)
  216. {
  217. // Debug Profiling.
  218. PROFILE_SCOPE(SpriteBatchQuery_RayCastFractionSort);
  219. // Fetch scene objects.
  220. SpriteBatchQueryResult* pQueryResultA = (SpriteBatchQueryResult*)a;
  221. SpriteBatchQueryResult* pQueryResultB = (SpriteBatchQueryResult*)b;
  222. // Fetch fractions.
  223. const F32 queryFractionA = pQueryResultA->mFraction;
  224. const F32 queryFractionB = pQueryResultB->mFraction;
  225. if ( queryFractionA < queryFractionB )
  226. return -1;
  227. if ( queryFractionA > queryFractionB )
  228. return 1;
  229. return 0;
  230. }