renderOcclusionMgr.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "renderOcclusionMgr.h"
  24. #include "console/consoleTypes.h"
  25. #include "scene/sceneObject.h"
  26. #include "gfx/gfxOcclusionQuery.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "gfx/gfxTransformSaver.h"
  29. #include "math/util/sphereMesh.h"
  30. #include "materials/materialManager.h"
  31. #include "materials/sceneData.h"
  32. #include "math/util/matrixSet.h"
  33. #include "gfx/gfxDebugEvent.h"
  34. #include "materials/materialFeatureTypes.h"
  35. IMPLEMENT_CONOBJECT(RenderOcclusionMgr);
  36. ConsoleDocClass( RenderOcclusionMgr,
  37. "@brief A render bin which renders occlusion query requests.\n\n"
  38. "This render bin gathers occlusion query render instances and renders them. "
  39. "It is currently used by light flares and ShapeBase reflection cubemaps.\n\n"
  40. "You can type '$RenderOcclusionMgr::debugRender = true' in the console to "
  41. "see debug rendering of the occlusion geometry.\n\n"
  42. "@ingroup RenderBin\n" );
  43. bool RenderOcclusionMgr::smDebugRender = false;
  44. RenderOcclusionMgr::RenderOcclusionMgr()
  45. : RenderBinManager(RenderPassManager::RIT_Occluder, 1.0f, 1.0f)
  46. {
  47. mSpherePrimCount = 0;
  48. mMatInstance = NULL;
  49. }
  50. RenderOcclusionMgr::RenderOcclusionMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder)
  51. : RenderBinManager(riType, renderOrder, processAddOrder)
  52. {
  53. delete mMatInstance;
  54. }
  55. static const Point3F cubePoints[8] =
  56. {
  57. Point3F(-0.5, -0.5, -0.5), Point3F(-0.5, -0.5, 0.5), Point3F(-0.5, 0.5, -0.5), Point3F(-0.5, 0.5, 0.5),
  58. Point3F( 0.5, -0.5, -0.5), Point3F( 0.5, -0.5, 0.5), Point3F( 0.5, 0.5, -0.5), Point3F( 0.5, 0.5, 0.5)
  59. };
  60. static const U32 cubeFaces[6][4] =
  61. {
  62. { 0, 4, 6, 2 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  63. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  64. };
  65. void RenderOcclusionMgr::init()
  66. {
  67. delete mMatInstance;
  68. mMaterial = MATMGR->allocateAndRegister( String::EmptyString );
  69. mMaterial->mDiffuse[0] = ColorF( 1, 0, 1, 1 );
  70. mMaterial->mEmissive[0] = true;
  71. mMaterial->mAutoGenerated = true;
  72. mMatInstance = mMaterial->createMatInstance();
  73. FeatureSet features = MATMGR->getDefaultFeatures();
  74. features.removeFeature( MFT_Visibility );
  75. features.removeFeature( MFT_Fog );
  76. features.removeFeature( MFT_HDROut );
  77. mMatInstance->init( features, getGFXVertexFormat<GFXVertexP>() );
  78. GFXStateBlockDesc d;
  79. d.setBlend( false );
  80. d.cullDefined = true;
  81. d.cullMode = GFXCullCCW;
  82. d.setZReadWrite( true, false );
  83. d.setColorWrites( false, false, false, false );
  84. mRenderSB = GFX->createStateBlock(d);
  85. d.setZReadWrite( false, false );
  86. mTestSB = GFX->createStateBlock(d);
  87. mBoxBuff.set( GFX, 36, GFXBufferTypeStatic );
  88. GFXVertexP *verts = mBoxBuff.lock();
  89. U32 vertexIndex = 0;
  90. U32 idx;
  91. for(S32 i = 0; i < 6; i++)
  92. {
  93. idx = cubeFaces[i][0];
  94. verts[vertexIndex].point = cubePoints[idx];
  95. vertexIndex++;
  96. idx = cubeFaces[i][1];
  97. verts[vertexIndex].point = cubePoints[idx];
  98. vertexIndex++;
  99. idx = cubeFaces[i][3];
  100. verts[vertexIndex].point = cubePoints[idx];
  101. vertexIndex++;
  102. idx = cubeFaces[i][1];
  103. verts[vertexIndex].point = cubePoints[idx];
  104. vertexIndex++;
  105. idx = cubeFaces[i][3];
  106. verts[vertexIndex].point = cubePoints[idx];
  107. vertexIndex++;
  108. idx = cubeFaces[i][2];
  109. verts[vertexIndex].point = cubePoints[idx];
  110. vertexIndex++;
  111. }
  112. mBoxBuff.unlock();
  113. SphereMesh sphere;
  114. const SphereMesh::TriangleMesh *sphereMesh = sphere.getMesh(1);
  115. mSpherePrimCount = sphereMesh->numPoly;
  116. mSphereBuff.set( GFX, mSpherePrimCount * 3, GFXBufferTypeStatic );
  117. verts = mSphereBuff.lock();
  118. vertexIndex = 0;
  119. for ( S32 i = 0; i < mSpherePrimCount; i++ )
  120. {
  121. verts[vertexIndex].point = sphereMesh->poly[i].pnt[0];
  122. vertexIndex++;
  123. verts[vertexIndex].point = sphereMesh->poly[i].pnt[1];
  124. vertexIndex++;
  125. verts[vertexIndex].point = sphereMesh->poly[i].pnt[2];
  126. vertexIndex++;
  127. }
  128. mSphereBuff.unlock();
  129. }
  130. void RenderOcclusionMgr::consoleInit()
  131. {
  132. Con::addVariable( "$RenderOcclusionMgr::debugRender", TypeBool, &RenderOcclusionMgr::smDebugRender,
  133. "@brief A debugging feature which renders the occlusion volumes to the scene.\n"
  134. "@see RenderOcclusionMgr\n"
  135. "@ingroup RenderBin\n" );
  136. }
  137. void RenderOcclusionMgr::initPersistFields()
  138. {
  139. Parent::initPersistFields();
  140. }
  141. //-----------------------------------------------------------------------------
  142. // render objects
  143. //-----------------------------------------------------------------------------
  144. void RenderOcclusionMgr::render( SceneRenderState *state )
  145. {
  146. PROFILE_SCOPE(RenderOcclusionMgr_render);
  147. // Early out if nothing to draw.
  148. if ( !mElementList.size() )
  149. return;
  150. GFXTransformSaver saver;
  151. GFXDEBUGEVENT_SCOPE(RenderOcclusionMgr_Render, ColorI::BLUE);
  152. if ( mMatInstance == NULL )
  153. init();
  154. SceneData sgData;
  155. sgData.init( state );
  156. // Restore transforms
  157. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  158. matrixSet.restoreSceneViewProjection();
  159. // The material is single pass... just setup once here.
  160. mMatInstance->setupPass( state, sgData );
  161. U32 primCount;
  162. for( U32 i=0; i<mElementList.size(); i++ )
  163. {
  164. OccluderRenderInst *ri = static_cast<OccluderRenderInst*>(mElementList[i].inst);
  165. AssertFatal( ri->query != NULL, "RenderOcclusionMgr::render, OcclusionRenderInst has NULL GFXOcclusionQuery" );
  166. if ( ri->isSphere )
  167. {
  168. GFX->setVertexBuffer( mSphereBuff );
  169. primCount = mSpherePrimCount;
  170. }
  171. else
  172. {
  173. GFX->setVertexBuffer( mBoxBuff );
  174. primCount = 12;
  175. }
  176. MatrixF xfm( *ri->orientation );
  177. xfm.setPosition( ri->position );
  178. xfm.scale( ri->scale );
  179. matrixSet.setWorld(xfm);
  180. mMatInstance->setTransforms(matrixSet, state);
  181. if ( !smDebugRender )
  182. GFX->setStateBlock( mRenderSB );
  183. ri->query->begin();
  184. GFX->drawPrimitive( GFXTriangleList, 0, primCount );
  185. ri->query->end();
  186. if ( ri->query2 )
  187. {
  188. GFX->setStateBlock( mTestSB );
  189. ri->query2->begin();
  190. GFX->drawPrimitive( GFXTriangleList, 0, primCount );
  191. ri->query2->end();
  192. }
  193. }
  194. // Call setup one more time to end the pass.
  195. mMatInstance->setupPass( state, sgData );
  196. }