renderOcclusionMgr.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "gfx/gfxDebugEvent.h"
  31. IMPLEMENT_CONOBJECT(RenderOcclusionMgr);
  32. ConsoleDocClass( RenderOcclusionMgr,
  33. "@brief A render bin which renders occlusion query requests.\n\n"
  34. "This render bin gathers occlusion query render instances and renders them. "
  35. "It is currently used by light flares and ShapeBase reflection cubemaps.\n\n"
  36. "You can type '$RenderOcclusionMgr::debugRender = true' in the console to "
  37. "see debug rendering of the occlusion geometry.\n\n"
  38. "@ingroup RenderBin\n" );
  39. bool RenderOcclusionMgr::smDebugRender = false;
  40. RenderOcclusionMgr::RenderOcclusionMgr()
  41. : RenderBinManager(RenderPassManager::RIT_Occluder, 1.0f, 1.0f)
  42. {
  43. mOverrideMat = NULL;
  44. mSpherePrimCount = 0;
  45. }
  46. RenderOcclusionMgr::RenderOcclusionMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder)
  47. : RenderBinManager(riType, renderOrder, processAddOrder)
  48. {
  49. mOverrideMat = NULL;
  50. }
  51. static const Point3F cubePoints[8] =
  52. {
  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. 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)
  55. };
  56. static const U32 cubeFaces[6][4] =
  57. {
  58. { 0, 4, 6, 2 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  59. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  60. };
  61. void RenderOcclusionMgr::init()
  62. {
  63. GFXStateBlockDesc d;
  64. d.setBlend( false );
  65. d.cullDefined = true;
  66. d.cullMode = GFXCullCCW;
  67. d.setZReadWrite( true, false );
  68. mDebugSB = GFX->createStateBlock(d);
  69. d.setColorWrites( false, false, false, false );
  70. mNormalSB = GFX->createStateBlock(d);
  71. d.setZReadWrite( false, false );
  72. mTestSB = GFX->createStateBlock(d);
  73. mBoxBuff.set( GFX, 36, GFXBufferTypeStatic );
  74. GFXVertexPC *verts = mBoxBuff.lock();
  75. U32 vertexIndex = 0;
  76. U32 idx;
  77. for(int i = 0; i < 6; i++)
  78. {
  79. idx = cubeFaces[i][0];
  80. verts[vertexIndex].point = cubePoints[idx];
  81. verts[vertexIndex].color.set( 1,0,1,1 );
  82. vertexIndex++;
  83. idx = cubeFaces[i][1];
  84. verts[vertexIndex].point = cubePoints[idx];
  85. verts[vertexIndex].color.set( 1,0,1,1 );
  86. vertexIndex++;
  87. idx = cubeFaces[i][3];
  88. verts[vertexIndex].point = cubePoints[idx];
  89. verts[vertexIndex].color.set( 1,0,1,1 );
  90. vertexIndex++;
  91. idx = cubeFaces[i][1];
  92. verts[vertexIndex].point = cubePoints[idx];
  93. verts[vertexIndex].color.set( 1,0,1,1 );
  94. vertexIndex++;
  95. idx = cubeFaces[i][3];
  96. verts[vertexIndex].point = cubePoints[idx];
  97. verts[vertexIndex].color.set( 1,0,1,1 );
  98. vertexIndex++;
  99. idx = cubeFaces[i][2];
  100. verts[vertexIndex].point = cubePoints[idx];
  101. verts[vertexIndex].color.set( 1,0,1,1 );
  102. vertexIndex++;
  103. }
  104. mBoxBuff.unlock();
  105. SphereMesh sphere;
  106. const SphereMesh::TriangleMesh *sphereMesh = sphere.getMesh(1);
  107. mSpherePrimCount = sphereMesh->numPoly;
  108. mSphereBuff.set( GFX, mSpherePrimCount * 3, GFXBufferTypeStatic );
  109. verts = mSphereBuff.lock();
  110. vertexIndex = 0;
  111. for ( S32 i = 0; i < mSpherePrimCount; i++ )
  112. {
  113. verts[vertexIndex].point = sphereMesh->poly[i].pnt[0];
  114. verts[vertexIndex].color.set( 1,0,1,1 );
  115. vertexIndex++;
  116. verts[vertexIndex].point = sphereMesh->poly[i].pnt[1];
  117. verts[vertexIndex].color.set( 1,0,1,1 );
  118. vertexIndex++;
  119. verts[vertexIndex].point = sphereMesh->poly[i].pnt[2];
  120. verts[vertexIndex].color.set( 1,0,1,1 );
  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. Parent::initPersistFields();
  135. }
  136. //-----------------------------------------------------------------------------
  137. // render objects
  138. //-----------------------------------------------------------------------------
  139. void RenderOcclusionMgr::render( SceneRenderState *state )
  140. {
  141. PROFILE_SCOPE(RenderOcclusionMgr_render);
  142. // Early out if nothing to draw.
  143. if ( !mElementList.size() )
  144. return;
  145. GFXDEBUGEVENT_SCOPE(RenderOcclusionMgr_Render, ColorI::BLUE);
  146. if ( mNormalSB.isNull() )
  147. init();
  148. GFX->disableShaders();
  149. GFX->setupGenericShaders( GFXDevice::GSColor );
  150. OccluderRenderInst *firstEl = static_cast<OccluderRenderInst*>(mElementList[0].inst);
  151. if ( firstEl->isSphere )
  152. GFX->setVertexBuffer( mSphereBuff );
  153. else
  154. GFX->setVertexBuffer( mBoxBuff );
  155. bool wasSphere = firstEl->isSphere;
  156. for( U32 i=0; i<mElementList.size(); i++ )
  157. {
  158. OccluderRenderInst *ri = static_cast<OccluderRenderInst*>(mElementList[i].inst);
  159. AssertFatal( ri->query != NULL, "RenderOcclusionMgr::render, OcclusionRenderInst has NULL GFXOcclusionQuery" );
  160. if ( ri->isSphere != wasSphere )
  161. {
  162. if ( ri->isSphere )
  163. GFX->setVertexBuffer( mSphereBuff );
  164. else
  165. GFX->setVertexBuffer( mBoxBuff );
  166. wasSphere = ri->isSphere;
  167. }
  168. GFX->pushWorldMatrix();
  169. MatrixF xfm( *ri->orientation );
  170. xfm.setPosition( ri->position );
  171. xfm.scale( ri->scale );
  172. //GFXTransformSaver saver;
  173. GFX->multWorld( xfm );
  174. if ( smDebugRender )
  175. GFX->setStateBlock( mDebugSB );
  176. else
  177. GFX->setStateBlock( mNormalSB );
  178. ri->query->begin();
  179. if ( wasSphere )
  180. GFX->drawPrimitive( GFXTriangleList, 0, mSpherePrimCount );
  181. else
  182. GFX->drawPrimitive( GFXTriangleList, 0, 12 );
  183. ri->query->end();
  184. if ( ri->query2 )
  185. {
  186. GFX->setStateBlock( mTestSB );
  187. ri->query2->begin();
  188. if ( wasSphere )
  189. GFX->drawPrimitive( GFXTriangleList, 0, mSpherePrimCount );
  190. else
  191. GFX->drawPrimitive( GFXTriangleList, 0, 12 );
  192. ri->query2->end();
  193. }
  194. GFX->popWorldMatrix();
  195. }
  196. }