tsForestCellBatch.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. Vector<ForestItem>::const_iterator item = mItems.begin();
  68. const F32 radius = mDetail->getRadius();
  69. ImposterState state;
  70. for ( ; item != mItems.end(); item++ )
  71. {
  72. item->getWorldBox().getCenter( &state.center );
  73. state.halfSize = radius * item->getScale();
  74. state.alpha = 1.0f;
  75. item->getTransform().getColumn( 2, &state.upVec );
  76. item->getTransform().getColumn( 0, &state.rightVec );
  77. *vertPtr = state;
  78. vertPtr->corner = 0;
  79. ++vertPtr;
  80. *vertPtr = state;
  81. vertPtr->corner = 1;
  82. ++vertPtr;
  83. *vertPtr = state;
  84. vertPtr->corner = 2;
  85. ++vertPtr;
  86. *vertPtr = state;
  87. vertPtr->corner = 2;
  88. ++vertPtr;
  89. *vertPtr = state;
  90. vertPtr->corner = 3;
  91. ++vertPtr;
  92. *vertPtr = state;
  93. vertPtr->corner = 0;
  94. ++vertPtr;
  95. }
  96. mVB.unlock();
  97. }
  98. void TSForestCellBatch::_render( const SceneRenderState *state )
  99. {
  100. if ( !mVB.isValid() ||
  101. ( state->isShadowPass() && !TSLastDetail::smCanShadow ) )
  102. return;
  103. // Make sure we have a material to render with.
  104. BaseMatInstance *mat = state->getOverrideMaterial( mDetail->getMatInstance() );
  105. if ( mat == NULL )
  106. return;
  107. // We don't really render here... we submit it to
  108. // the render manager which collects all the batches
  109. // in the scene, sorts them by texture, sets up the
  110. // shader, and then renders them all at once.
  111. ImposterBatchRenderInst *inst = state->getRenderPass()->allocInst<ImposterBatchRenderInst>();
  112. inst->mat = mat;
  113. inst->vertBuff = &mVB;
  114. // We sort by the imposter type first so that RIT_Imposter and
  115. // RIT_ImposterBatches do not get mixed together.
  116. //
  117. // We then sort by material.
  118. //
  119. inst->defaultKey = 0;
  120. inst->defaultKey2 = mat->getStateHint();
  121. state->getRenderPass()->addInst( inst );
  122. }