forestRender.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/forest.h"
  24. #include "forest/forestCell.h"
  25. #include "forest/forestDataFile.h"
  26. #include "gfx/gfxTransformSaver.h"
  27. #include "renderInstance/renderPassManager.h"
  28. #include "scene/sceneManager.h"
  29. #include "scene/sceneRenderState.h"
  30. #include "lighting/lightManager.h"
  31. #include "ts/tsMesh.h"
  32. #include "ts/tsRenderState.h"
  33. #include "ts/tsShapeInstance.h"
  34. #include "gfx/primBuilder.h"
  35. #include "gfx/gfxDrawUtil.h"
  36. #include "math/mathUtils.h"
  37. U32 Forest::smTotalCells = 0;
  38. U32 Forest::smCellsRendered = 0;
  39. U32 Forest::smCellItemsRendered = 0;
  40. U32 Forest::smCellsBatched = 0;
  41. U32 Forest::smCellItemsBatched = 0;
  42. F32 Forest::smAverageItemsPerCell = 0.0f;
  43. void Forest::_clearStats(bool beginFrame)
  44. {
  45. // Reset the rendering stats!
  46. if (beginFrame)
  47. {
  48. smTotalCells = 0;
  49. smCellsRendered = 0;
  50. smCellItemsRendered = 0;
  51. smCellsBatched = 0;
  52. smCellItemsBatched = 0;
  53. smAverageItemsPerCell = 0.0f;
  54. }
  55. }
  56. void Forest::prepRenderImage( SceneRenderState *state )
  57. {
  58. PROFILE_SCOPE(Forest_RenderCells);
  59. // TODO: Fix stats.
  60. /*
  61. ForestCellVector &theCells = mData->getCells();
  62. smTotalCells += theCells.size();
  63. // Don't render if we don't have a grid!
  64. if ( theCells.empty() )
  65. return false;
  66. */
  67. // Prepare to render.
  68. GFXTransformSaver saver;
  69. // Figure out the grid range in the viewing area.
  70. const bool isReflectPass = state->isReflectPass();
  71. const F32 cullScale = isReflectPass ? mReflectionLodScalar : 1.0f;
  72. // If we need to update our cached
  73. // zone state then do it now.
  74. if ( mZoningDirty )
  75. {
  76. mZoningDirty = false;
  77. Vector<ForestCell*> cells;
  78. mData->getCells( &cells );
  79. for ( U32 i=0; i < cells.size(); i++ )
  80. cells[i]->_updateZoning( getSceneManager()->getZoneManager() );
  81. }
  82. // TODO: Move these into the TSForestItemData as something we
  83. // setup once and don't do per-instance.
  84. // Set up the TS render state.
  85. TSRenderState rdata;
  86. rdata.setSceneState( state );
  87. // Use origin sort on all forest elements as
  88. // its alot cheaper than the bounds sort.
  89. rdata.setOriginSort( true );
  90. // We may have some forward lit materials in
  91. // the forest, so pass down a LightQuery for it.
  92. LightQuery lightQuery;
  93. rdata.setLightQuery( &lightQuery );
  94. Frustum culler = state->getCullingFrustum();
  95. // Adjust the far distance if the cull scale has changed.
  96. if ( !mIsEqual( cullScale, 1.0f ) )
  97. {
  98. const F32 visFarDist = culler.getFarDist() * cullScale;
  99. culler.setFarDist( visFarDist );
  100. }
  101. Box3F worldBox;
  102. // Used for debug drawing.
  103. GFXDrawUtil* drawer = GFX->getDrawUtil();
  104. drawer->clearBitmapModulation();
  105. // Go thru the visible cells.
  106. const Box3F &cullerBounds = culler.getBounds();
  107. const Point3F &camPos = state->getDiffuseCameraPosition();
  108. U32 clipMask;
  109. smAverageItemsPerCell = 0.0f;
  110. U32 cellsProcessed = 0;
  111. ForestCell *cell;
  112. // First get all the top level cells which
  113. // intersect the frustum.
  114. Vector<ForestCell*> cellStack;
  115. mData->getCells( culler, &cellStack );
  116. // Get the culling zone state.
  117. const BitVector &zoneState = state->getCullingState().getZoneVisibilityFlags();
  118. // Now loop till we run out of cells.
  119. while ( !cellStack.empty() )
  120. {
  121. // Pop off the next cell.
  122. cell = cellStack.last();
  123. cellStack.pop_back();
  124. const Box3F &cellBounds = cell->getBounds();
  125. // If the cell is empty or its bounds is outside the frustum
  126. // bounds then we have nothing nothing more to do.
  127. if ( cell->isEmpty() || !cullerBounds.isOverlapped( cellBounds ) )
  128. continue;
  129. // Can we cull this cell entirely?
  130. clipMask = culler.testPlanes( cellBounds, Frustum::PlaneMaskAll );
  131. if ( clipMask == -1 )
  132. continue;
  133. // Test cell visibility for interior zones.
  134. const bool visibleInside = !cell->getZoneOverlap().empty() ? zoneState.testAny( cell->getZoneOverlap() ) : false;
  135. // Test cell visibility for outdoor zone, but only
  136. // if we need to.
  137. bool visibleOutside = false;
  138. if( !cell->mIsInteriorOnly && !visibleInside )
  139. {
  140. U32 outdoorZone = SceneZoneSpaceManager::RootZoneId;
  141. visibleOutside = !state->getCullingState().isCulled( cellBounds, &outdoorZone, 1 );
  142. }
  143. // Skip cell if neither visible indoors nor outdoors.
  144. if( !visibleInside && !visibleOutside )
  145. continue;
  146. // Update the stats.
  147. smAverageItemsPerCell += cell->getItems().size();
  148. ++cellsProcessed;
  149. //if ( cell->isLeaf() )
  150. //++leafCellsProcessed;
  151. // Get the distance from the camera to the cell bounds.
  152. F32 dist = cellBounds.getDistanceToPoint( camPos );
  153. // If the largest item in the cell can be billboarded
  154. // at the cell distance to the camera... then the whole
  155. // cell can be billboarded.
  156. //
  157. if ( smForceImposters ||
  158. ( dist > 0.0f && cell->getLargestItem().canBillboard( state, dist ) ) )
  159. {
  160. // If imposters are disabled then skip out.
  161. if ( smDisableImposters )
  162. continue;
  163. // if cell are to far for largest item, then skip out.
  164. if( TSShapeInstance::smLastPixelSize < TSShapeInstance::smSmallestVisiblePixelSize )
  165. continue;
  166. PROFILE_SCOPE(Forest_RenderBatches);
  167. // Keep track of how many cells were batched.
  168. ++smCellsBatched;
  169. // Ok... everything in this cell should be batched. First
  170. // create the batches if we don't have any.
  171. if ( !cell->hasBatches() )
  172. cell->buildBatches();
  173. //if ( drawCells )
  174. //mCellRenderFlag[ cellIter - theCells.begin() ] = 1;
  175. // TODO: Light queries for batches?
  176. // Now render the batches... we pass the culler if the
  177. // cell wasn't fully visible so that each batch can be culled.
  178. smCellItemsBatched += cell->renderBatches( state, clipMask != 0 ? &culler : NULL );
  179. continue;
  180. }
  181. // If this isn't a leaf then recurse.
  182. if ( !cell->isLeaf() )
  183. {
  184. cell->getChildren( &cellStack );
  185. continue;
  186. }
  187. // This cell has mixed billboards and mesh based items.
  188. ++smCellsRendered;
  189. PROFILE_SCOPE(Forest_RenderItems);
  190. //if ( drawCells )
  191. //mCellRenderFlag[ cellIter - theCells.begin() ] = 2;
  192. // Use the cell bounds as the light query volume.
  193. //
  194. // This means all forward lit items in this cell will
  195. // get the same lights, but it performs much better.
  196. lightQuery.init( cellBounds );
  197. // This cell is visible... have it render its items.
  198. smCellItemsRendered += cell->render( &rdata, clipMask != 0 ? &culler : NULL );
  199. }
  200. // Keep track of the average items per cell.
  201. if ( cellsProcessed > 0 )
  202. smAverageItemsPerCell /= (F32)cellsProcessed;
  203. // Got debug drawing to do?
  204. if ( smDrawCells && state->isDiffusePass() )
  205. {
  206. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  207. ri->renderDelegate.bind( this, &Forest::_renderCellBounds );
  208. ri->type = RenderPassManager::RIT_Editor;
  209. state->getRenderPass()->addInst( ri );
  210. }
  211. }
  212. void Forest::_renderCellBounds( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  213. {
  214. PROFILE_SCOPE( Forest_RenderCellBounds );
  215. if ( overrideMat )
  216. return;
  217. GFXTransformSaver saver;
  218. MatrixF projBias(true);
  219. const Frustum frustum = GFX->getFrustum();
  220. MathUtils::getZBiasProjectionMatrix( 0.001f, frustum, &projBias );
  221. GFX->setProjectionMatrix( projBias );
  222. VectorF extents;
  223. Point3F pos;
  224. // Get top level cells
  225. Vector<ForestCell*> cellStack;
  226. mData->getCells( &cellStack );
  227. // Holds child cells we need to render as we encounter them.
  228. Vector<ForestCell*> frontier;
  229. GFXDrawUtil *drawer = GFX->getDrawUtil();
  230. GFXStateBlockDesc desc;
  231. desc.setZReadWrite( true, false );
  232. desc.setBlend( true );
  233. desc.setFillModeWireframe();
  234. while ( !cellStack.empty() )
  235. {
  236. while ( !cellStack.empty() )
  237. {
  238. const ForestCell *cell = cellStack.last();
  239. cellStack.pop_back();
  240. Box3F box = cell->getBounds();
  241. drawer->drawCube( desc, box, ColorI( 0, 255, 0 ) );
  242. RectF rect = cell->getRect();
  243. box.minExtents.set( rect.point.x, rect.point.y, box.minExtents.z );
  244. box.maxExtents.set( rect.point.x + rect.extent.x, rect.point.y + rect.extent.y, box.minExtents.z );
  245. drawer->drawCube( desc, box, ColorI::RED );
  246. // If this cell has children, add them to the frontier.
  247. if ( !cell->isLeaf() )
  248. cell->getChildren( &frontier );
  249. }
  250. // Now the frontier becomes the cellStack and we empty the frontier.
  251. cellStack = frontier;
  252. frontier.clear();
  253. }
  254. }