renderOcclusionMgr.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. static const Point3F cubePoints[8] =
  51. {
  52. 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),
  53. 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)
  54. };
  55. static const U32 cubeFaces[6][4] =
  56. {
  57. { 0, 4, 6, 2 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  58. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  59. };
  60. void RenderOcclusionMgr::init()
  61. {
  62. delete mMatInstance;
  63. mMaterial = MATMGR->allocateAndRegister( String::EmptyString );
  64. mMaterial->mDiffuse[0] = LinearColorF( 1, 0, 1, 1 );
  65. mMaterial->mReceiveShadows[0] = false;
  66. mMaterial->mAutoGenerated = true;
  67. mMatInstance = mMaterial->createMatInstance();
  68. FeatureSet features = MATMGR->getDefaultFeatures();
  69. features.removeFeature( MFT_Visibility );
  70. features.removeFeature( MFT_Fog );
  71. features.removeFeature( MFT_HDROut );
  72. mMatInstance->init( features, getGFXVertexFormat<GFXVertexP>() );
  73. GFXStateBlockDesc d;
  74. d.setBlend( false );
  75. d.cullDefined = true;
  76. d.cullMode = GFXCullCCW;
  77. d.setZReadWrite( true, false );
  78. d.setColorWrites( false, false, false, false );
  79. mRenderSB = GFX->createStateBlock(d);
  80. d.setZReadWrite( false, false );
  81. mTestSB = GFX->createStateBlock(d);
  82. mBoxBuff.set( GFX, 36, GFXBufferTypeStatic );
  83. GFXVertexP *verts = mBoxBuff.lock();
  84. U32 vertexIndex = 0;
  85. U32 idx;
  86. for(S32 i = 0; i < 6; i++)
  87. {
  88. idx = cubeFaces[i][0];
  89. verts[vertexIndex].point = cubePoints[idx];
  90. vertexIndex++;
  91. idx = cubeFaces[i][1];
  92. verts[vertexIndex].point = cubePoints[idx];
  93. vertexIndex++;
  94. idx = cubeFaces[i][3];
  95. verts[vertexIndex].point = cubePoints[idx];
  96. vertexIndex++;
  97. idx = cubeFaces[i][1];
  98. verts[vertexIndex].point = cubePoints[idx];
  99. vertexIndex++;
  100. idx = cubeFaces[i][3];
  101. verts[vertexIndex].point = cubePoints[idx];
  102. vertexIndex++;
  103. idx = cubeFaces[i][2];
  104. verts[vertexIndex].point = cubePoints[idx];
  105. vertexIndex++;
  106. }
  107. mBoxBuff.unlock();
  108. SphereMesh sphere;
  109. const SphereMesh::TriangleMesh *sphereMesh = sphere.getMesh(1);
  110. mSpherePrimCount = sphereMesh->numPoly;
  111. mSphereBuff.set( GFX, mSpherePrimCount * 3, GFXBufferTypeStatic );
  112. verts = mSphereBuff.lock();
  113. vertexIndex = 0;
  114. for ( S32 i = 0; i < mSpherePrimCount; i++ )
  115. {
  116. verts[vertexIndex].point = sphereMesh->poly[i].pnt[0];
  117. vertexIndex++;
  118. verts[vertexIndex].point = sphereMesh->poly[i].pnt[1];
  119. vertexIndex++;
  120. verts[vertexIndex].point = sphereMesh->poly[i].pnt[2];
  121. vertexIndex++;
  122. }
  123. mSphereBuff.unlock();
  124. }
  125. void RenderOcclusionMgr::consoleInit()
  126. {
  127. Con::addVariable( "$RenderOcclusionMgr::debugRender", TypeBool, &RenderOcclusionMgr::smDebugRender,
  128. "@brief A debugging feature which renders the occlusion volumes to the scene.\n"
  129. "@see RenderOcclusionMgr\n"
  130. "@ingroup RenderBin\n" );
  131. }
  132. void RenderOcclusionMgr::initPersistFields()
  133. {
  134. docsURL;
  135. Parent::initPersistFields();
  136. }
  137. //-----------------------------------------------------------------------------
  138. // render objects
  139. //-----------------------------------------------------------------------------
  140. void RenderOcclusionMgr::render( SceneRenderState *state )
  141. {
  142. PROFILE_SCOPE(RenderOcclusionMgr_render);
  143. // Early out if nothing to draw.
  144. if ( !mElementList.size() )
  145. return;
  146. GFXTransformSaver saver;
  147. GFXDEBUGEVENT_SCOPE(RenderOcclusionMgr_Render, ColorI::BLUE);
  148. if ( mMatInstance == NULL )
  149. init();
  150. SceneData sgData;
  151. sgData.init( state );
  152. // Restore transforms
  153. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  154. matrixSet.restoreSceneViewProjection();
  155. // The material is single pass... just setup once here.
  156. mMatInstance->setupPass( state, sgData );
  157. U32 primCount;
  158. for( U32 i=0; i<mElementList.size(); i++ )
  159. {
  160. OccluderRenderInst *ri = static_cast<OccluderRenderInst*>(mElementList[i].inst);
  161. AssertFatal( ri->query != NULL, "RenderOcclusionMgr::render, OcclusionRenderInst has NULL GFXOcclusionQuery" );
  162. if ( ri->isSphere )
  163. {
  164. GFX->setVertexBuffer( mSphereBuff );
  165. primCount = mSpherePrimCount;
  166. }
  167. else
  168. {
  169. GFX->setVertexBuffer( mBoxBuff );
  170. primCount = 12;
  171. }
  172. MatrixF xfm( *ri->orientation );
  173. xfm.setPosition( ri->position );
  174. xfm.scale( ri->scale );
  175. matrixSet.setWorld(xfm);
  176. mMatInstance->setTransforms(matrixSet, state);
  177. if ( !smDebugRender )
  178. GFX->setStateBlock( mRenderSB );
  179. ri->query->begin();
  180. GFX->drawPrimitive( GFXTriangleList, 0, primCount );
  181. ri->query->end();
  182. if ( ri->query2 )
  183. {
  184. GFX->setStateBlock( mTestSB );
  185. ri->query2->begin();
  186. GFX->drawPrimitive( GFXTriangleList, 0, primCount );
  187. ri->query2->end();
  188. }
  189. }
  190. // Call setup one more time to end the pass.
  191. mMatInstance->setupPass( state, sgData );
  192. }