tsForestCellBatch.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "forest/ts/tsForestCellBatch.h"
  24. #include "forest/ts/tsForestItemData.h"
  25. #include "scene/sceneManager.h"
  26. #include "ts/tsLastDetail.h"
  27. TSForestCellBatch::TSForestCellBatch( TSLastDetail *detail )
  28. : mDetail( detail )
  29. {
  30. }
  31. TSForestCellBatch::~TSForestCellBatch()
  32. {
  33. }
  34. bool TSForestCellBatch::_prepBatch( const ForestItem &item )
  35. {
  36. // Make sure it's our item type!
  37. TSForestItemData* data = dynamic_cast<TSForestItemData*>( item.getData() );
  38. if ( !data )
  39. return false;
  40. // TODO: Eventually we should atlas multiple details into
  41. // a single combined texture map. Till then we have to
  42. // do one batch per-detail type.
  43. // If the detail type doesn't match then
  44. // we need to start a new batch.
  45. if ( data->getLastDetail() != mDetail )
  46. return false;
  47. return true;
  48. }
  49. void TSForestCellBatch::_rebuildBatch()
  50. {
  51. // Clean up first.
  52. mVB = NULL;
  53. if ( mItems.empty() )
  54. return;
  55. // How big do we need to make this?
  56. U32 verts = mItems.size() * 6;
  57. mVB.set( GFX, verts, GFXBufferTypeStatic );
  58. if ( !mVB.isValid() )
  59. {
  60. // If we failed it is probably because we requested
  61. // a size bigger than a VB can be. Warn the user.
  62. AssertWarn( false, "TSForestCellBatch::_rebuildBatch: Batch too big... try reducing the forest cell size!" );
  63. return;
  64. }
  65. // Fill this puppy!
  66. ImposterState *vertPtr = mVB.lock();
  67. if(!vertPtr) return;
  68. Vector<ForestItem>::const_iterator item = mItems.begin();
  69. const F32 radius = mDetail->getRadius();
  70. ImposterState state;
  71. for ( ; item != mItems.end(); item++ )
  72. {
  73. item->getWorldBox().getCenter( &state.center );
  74. state.halfSize = radius * item->getScale();
  75. state.alpha = 1.0f;
  76. item->getTransform().getColumn( 2, &state.upVec );
  77. item->getTransform().getColumn( 0, &state.rightVec );
  78. *vertPtr = state;
  79. vertPtr->corner = 0;
  80. ++vertPtr;
  81. *vertPtr = state;
  82. vertPtr->corner = 1;
  83. ++vertPtr;
  84. *vertPtr = state;
  85. vertPtr->corner = 2;
  86. ++vertPtr;
  87. *vertPtr = state;
  88. vertPtr->corner = 2;
  89. ++vertPtr;
  90. *vertPtr = state;
  91. vertPtr->corner = 3;
  92. ++vertPtr;
  93. *vertPtr = state;
  94. vertPtr->corner = 0;
  95. ++vertPtr;
  96. }
  97. mVB.unlock();
  98. }
  99. void TSForestCellBatch::_render( const SceneRenderState *state )
  100. {
  101. if ( !mVB.isValid() ||
  102. ( state->isShadowPass() && !TSLastDetail::smCanShadow ) )
  103. return;
  104. // Make sure we have a material to render with.
  105. BaseMatInstance *mat = state->getOverrideMaterial( mDetail->getMatInstance() );
  106. if ( mat == NULL )
  107. return;
  108. // We don't really render here... we submit it to
  109. // the render manager which collects all the batches
  110. // in the scene, sorts them by texture, sets up the
  111. // shader, and then renders them all at once.
  112. ImposterBatchRenderInst *inst = state->getRenderPass()->allocInst<ImposterBatchRenderInst>();
  113. inst->mat = mat;
  114. inst->vertBuff = &mVB;
  115. // We sort by the imposter type first so that RIT_Imposter and
  116. // RIT_ImposterBatches do not get mixed together.
  117. //
  118. // We then sort by material.
  119. //
  120. inst->defaultKey = 0;
  121. inst->defaultKey2 = mat->getStateHint();
  122. state->getRenderPass()->addInst( inst );
  123. }