renderOcclusionMgr.cpp
Engine/source/renderInstance/renderOcclusionMgr.cpp
Public Functions
Detailed Description
Public Variables
const U32 cubeFaces [6][4]
const Point3F cubePoints [8]
Public Functions
ConsoleDocClass(RenderOcclusionMgr )
IMPLEMENT_CONOBJECT(RenderOcclusionMgr )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "renderOcclusionMgr.h" 26 27#include "console/consoleTypes.h" 28#include "scene/sceneObject.h" 29#include "gfx/gfxOcclusionQuery.h" 30#include "gfx/gfxDrawUtil.h" 31#include "gfx/gfxTransformSaver.h" 32#include "math/util/sphereMesh.h" 33#include "materials/materialManager.h" 34#include "materials/sceneData.h" 35#include "math/util/matrixSet.h" 36#include "gfx/gfxDebugEvent.h" 37#include "materials/materialFeatureTypes.h" 38 39 40IMPLEMENT_CONOBJECT(RenderOcclusionMgr); 41 42ConsoleDocClass( RenderOcclusionMgr, 43 "@brief A render bin which renders occlusion query requests.\n\n" 44 "This render bin gathers occlusion query render instances and renders them. " 45 "It is currently used by light flares and ShapeBase reflection cubemaps.\n\n" 46 "You can type '$RenderOcclusionMgr::debugRender = true' in the console to " 47 "see debug rendering of the occlusion geometry.\n\n" 48 "@ingroup RenderBin\n" ); 49 50 51bool RenderOcclusionMgr::smDebugRender = false; 52 53RenderOcclusionMgr::RenderOcclusionMgr() 54: RenderBinManager(RenderPassManager::RIT_Occluder, 1.0f, 1.0f) 55{ 56 mSpherePrimCount = 0; 57 mMatInstance = NULL; 58} 59 60static const Point3F cubePoints[8] = 61{ 62 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), 63 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) 64}; 65 66static const U32 cubeFaces[6][4] = 67{ 68 { 0, 4, 6, 2 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 }, 69 { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 } 70}; 71 72void RenderOcclusionMgr::init() 73{ 74 delete mMatInstance; 75 76 mMaterial = MATMGR->allocateAndRegister( String::EmptyString ); 77 mMaterial->mDiffuse[0] = LinearColorF( 1, 0, 1, 1 ); 78 mMaterial->mEmissive[0] = true; 79 mMaterial->mAutoGenerated = true; 80 81 mMatInstance = mMaterial->createMatInstance(); 82 FeatureSet features = MATMGR->getDefaultFeatures(); 83 features.removeFeature( MFT_Visibility ); 84 features.removeFeature( MFT_Fog ); 85 features.removeFeature( MFT_HDROut ); 86 mMatInstance->init( features, getGFXVertexFormat<GFXVertexP>() ); 87 88 GFXStateBlockDesc d; 89 d.setBlend( false ); 90 d.cullDefined = true; 91 d.cullMode = GFXCullCCW; 92 d.setZReadWrite( true, false ); 93 d.setColorWrites( false, false, false, false ); 94 mRenderSB = GFX->createStateBlock(d); 95 96 d.setZReadWrite( false, false ); 97 mTestSB = GFX->createStateBlock(d); 98 99 mBoxBuff.set( GFX, 36, GFXBufferTypeStatic ); 100 GFXVertexP *verts = mBoxBuff.lock(); 101 102 U32 vertexIndex = 0; 103 U32 idx; 104 for(S32 i = 0; i < 6; i++) 105 { 106 idx = cubeFaces[i][0]; 107 verts[vertexIndex].point = cubePoints[idx]; 108 vertexIndex++; 109 110 idx = cubeFaces[i][1]; 111 verts[vertexIndex].point = cubePoints[idx]; 112 vertexIndex++; 113 114 idx = cubeFaces[i][3]; 115 verts[vertexIndex].point = cubePoints[idx]; 116 vertexIndex++; 117 118 idx = cubeFaces[i][1]; 119 verts[vertexIndex].point = cubePoints[idx]; 120 vertexIndex++; 121 122 idx = cubeFaces[i][3]; 123 verts[vertexIndex].point = cubePoints[idx]; 124 vertexIndex++; 125 126 idx = cubeFaces[i][2]; 127 verts[vertexIndex].point = cubePoints[idx]; 128 vertexIndex++; 129 } 130 131 mBoxBuff.unlock(); 132 133 SphereMesh sphere; 134 const SphereMesh::TriangleMesh *sphereMesh = sphere.getMesh(1); 135 mSpherePrimCount = sphereMesh->numPoly; 136 mSphereBuff.set( GFX, mSpherePrimCount * 3, GFXBufferTypeStatic ); 137 verts = mSphereBuff.lock(); 138 vertexIndex = 0; 139 140 for ( S32 i = 0; i < mSpherePrimCount; i++ ) 141 { 142 verts[vertexIndex].point = sphereMesh->poly[i].pnt[0]; 143 vertexIndex++; 144 145 verts[vertexIndex].point = sphereMesh->poly[i].pnt[1]; 146 vertexIndex++; 147 148 verts[vertexIndex].point = sphereMesh->poly[i].pnt[2]; 149 vertexIndex++; 150 } 151 mSphereBuff.unlock(); 152} 153 154void RenderOcclusionMgr::consoleInit() 155{ 156 Con::addVariable( "$RenderOcclusionMgr::debugRender", TypeBool, &RenderOcclusionMgr::smDebugRender, 157 "@brief A debugging feature which renders the occlusion volumes to the scene.\n" 158 "@see RenderOcclusionMgr\n" 159 "@ingroup RenderBin\n" ); 160} 161 162void RenderOcclusionMgr::initPersistFields() 163{ 164 Parent::initPersistFields(); 165} 166 167//----------------------------------------------------------------------------- 168// render objects 169//----------------------------------------------------------------------------- 170void RenderOcclusionMgr::render( SceneRenderState *state ) 171{ 172 PROFILE_SCOPE(RenderOcclusionMgr_render); 173 174 // Early out if nothing to draw. 175 if ( !mElementList.size() ) 176 return; 177 178 GFXTransformSaver saver; 179 180 GFXDEBUGEVENT_SCOPE(RenderOcclusionMgr_Render, ColorI::BLUE); 181 182 if ( mMatInstance == NULL ) 183 init(); 184 185 SceneData sgData; 186 sgData.init( state ); 187 188 // Restore transforms 189 MatrixSet &matrixSet = getRenderPass()->getMatrixSet(); 190 matrixSet.restoreSceneViewProjection(); 191 192 // The material is single pass... just setup once here. 193 mMatInstance->setupPass( state, sgData ); 194 195 U32 primCount; 196 for( U32 i=0; i<mElementList.size(); i++ ) 197 { 198 OccluderRenderInst *ri = static_cast<OccluderRenderInst*>(mElementList[i].inst); 199 AssertFatal( ri->query != NULL, "RenderOcclusionMgr::render, OcclusionRenderInst has NULL GFXOcclusionQuery" ); 200 201 if ( ri->isSphere ) 202 { 203 GFX->setVertexBuffer( mSphereBuff ); 204 primCount = mSpherePrimCount; 205 } 206 else 207 { 208 GFX->setVertexBuffer( mBoxBuff ); 209 primCount = 12; 210 } 211 212 MatrixF xfm( *ri->orientation ); 213 xfm.setPosition( ri->position ); 214 xfm.scale( ri->scale ); 215 216 matrixSet.setWorld(xfm); 217 mMatInstance->setTransforms(matrixSet, state); 218 219 if ( !smDebugRender ) 220 GFX->setStateBlock( mRenderSB ); 221 222 ri->query->begin(); 223 GFX->drawPrimitive( GFXTriangleList, 0, primCount ); 224 ri->query->end(); 225 226 if ( ri->query2 ) 227 { 228 GFX->setStateBlock( mTestSB ); 229 230 ri->query2->begin(); 231 GFX->drawPrimitive( GFXTriangleList, 0, primCount ); 232 ri->query2->end(); 233 } 234 } 235 236 // Call setup one more time to end the pass. 237 mMatInstance->setupPass( state, sgData ); 238} 239