From 6637948067517f73a3dc0848467a452306dc7720 Mon Sep 17 00:00:00 2001 From: DavidWyand-GG Date: Thu, 8 Nov 2012 17:47:32 -0500 Subject: [PATCH] Fix for Issue #132 for RenderOcclusionMgr Rendering --- .../renderInstance/renderOcclusionMgr.cpp | 119 ++++++++---------- .../renderInstance/renderOcclusionMgr.h | 18 ++- 2 files changed, 66 insertions(+), 71 deletions(-) diff --git a/Engine/source/renderInstance/renderOcclusionMgr.cpp b/Engine/source/renderInstance/renderOcclusionMgr.cpp index 4d9d32bfe..4e9839eb5 100644 --- a/Engine/source/renderInstance/renderOcclusionMgr.cpp +++ b/Engine/source/renderInstance/renderOcclusionMgr.cpp @@ -29,7 +29,11 @@ #include "gfx/gfxDrawUtil.h" #include "gfx/gfxTransformSaver.h" #include "math/util/sphereMesh.h" +#include "materials/materialManager.h" +#include "materials/sceneData.h" +#include "math/util/matrixSet.h" #include "gfx/gfxDebugEvent.h" +#include "materials/materialFeatureTypes.h" IMPLEMENT_CONOBJECT(RenderOcclusionMgr); @@ -48,14 +52,14 @@ bool RenderOcclusionMgr::smDebugRender = false; RenderOcclusionMgr::RenderOcclusionMgr() : RenderBinManager(RenderPassManager::RIT_Occluder, 1.0f, 1.0f) { - mOverrideMat = NULL; mSpherePrimCount = 0; + mMatInstance = NULL; } RenderOcclusionMgr::RenderOcclusionMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder) : RenderBinManager(riType, renderOrder, processAddOrder) { - mOverrideMat = NULL; + delete mMatInstance; } static const Point3F cubePoints[8] = @@ -72,25 +76,33 @@ static const U32 cubeFaces[6][4] = void RenderOcclusionMgr::init() { - GFXStateBlockDesc d; + delete mMatInstance; + mMaterial = MATMGR->allocateAndRegister( String::EmptyString ); + mMaterial->mDiffuse[0] = ColorF( 1, 0, 1, 1 ); + mMaterial->mEmissive[0] = true; + mMaterial->mAutoGenerated = true; + + mMatInstance = mMaterial->createMatInstance(); + FeatureSet features = MATMGR->getDefaultFeatures(); + features.removeFeature( MFT_Visibility ); + features.removeFeature( MFT_Fog ); + features.removeFeature( MFT_HDROut ); + mMatInstance->init( features, getGFXVertexFormat() ); + + GFXStateBlockDesc d; d.setBlend( false ); d.cullDefined = true; d.cullMode = GFXCullCCW; d.setZReadWrite( true, false ); - - mDebugSB = GFX->createStateBlock(d); - d.setColorWrites( false, false, false, false ); - - mNormalSB = GFX->createStateBlock(d); + mRenderSB = GFX->createStateBlock(d); d.setZReadWrite( false, false ); - mTestSB = GFX->createStateBlock(d); mBoxBuff.set( GFX, 36, GFXBufferTypeStatic ); - GFXVertexPC *verts = mBoxBuff.lock(); + GFXVertexP *verts = mBoxBuff.lock(); U32 vertexIndex = 0; U32 idx; @@ -98,32 +110,26 @@ void RenderOcclusionMgr::init() { idx = cubeFaces[i][0]; verts[vertexIndex].point = cubePoints[idx]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; idx = cubeFaces[i][1]; verts[vertexIndex].point = cubePoints[idx]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; idx = cubeFaces[i][3]; verts[vertexIndex].point = cubePoints[idx]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; idx = cubeFaces[i][1]; verts[vertexIndex].point = cubePoints[idx]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; idx = cubeFaces[i][3]; verts[vertexIndex].point = cubePoints[idx]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; idx = cubeFaces[i][2]; verts[vertexIndex].point = cubePoints[idx]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; } @@ -139,15 +145,12 @@ void RenderOcclusionMgr::init() for ( S32 i = 0; i < mSpherePrimCount; i++ ) { verts[vertexIndex].point = sphereMesh->poly[i].pnt[0]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; verts[vertexIndex].point = sphereMesh->poly[i].pnt[1]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; verts[vertexIndex].point = sphereMesh->poly[i].pnt[2]; - verts[vertexIndex].color.set( 1,0,1,1 ); vertexIndex++; } mSphereBuff.unlock(); @@ -177,62 +180,52 @@ void RenderOcclusionMgr::render( SceneRenderState *state ) if ( !mElementList.size() ) return; + GFXTransformSaver saver; + GFXDEBUGEVENT_SCOPE(RenderOcclusionMgr_Render, ColorI::BLUE); - if ( mNormalSB.isNull() ) + if ( mMatInstance == NULL ) init(); - GFX->disableShaders(); - GFX->setupGenericShaders( GFXDevice::GSColor ); + SceneData sgData; + sgData.init( state ); + // Restore transforms + MatrixSet &matrixSet = getRenderPass()->getMatrixSet(); + matrixSet.restoreSceneViewProjection(); - OccluderRenderInst *firstEl = static_cast(mElementList[0].inst); - - if ( firstEl->isSphere ) - GFX->setVertexBuffer( mSphereBuff ); - else - GFX->setVertexBuffer( mBoxBuff ); - - bool wasSphere = firstEl->isSphere; - + // The material is single pass... just setup once here. + mMatInstance->setupPass( state, sgData ); + U32 primCount; for( U32 i=0; i(mElementList[i].inst); - + OccluderRenderInst *ri = static_cast(mElementList[i].inst); AssertFatal( ri->query != NULL, "RenderOcclusionMgr::render, OcclusionRenderInst has NULL GFXOcclusionQuery" ); - if ( ri->isSphere != wasSphere ) - { - if ( ri->isSphere ) - GFX->setVertexBuffer( mSphereBuff ); - else - GFX->setVertexBuffer( mBoxBuff ); + if ( ri->isSphere ) + { + GFX->setVertexBuffer( mSphereBuff ); + primCount = mSpherePrimCount; + } + else + { + GFX->setVertexBuffer( mBoxBuff ); + primCount = 12; + } - wasSphere = ri->isSphere; - } - - GFX->pushWorldMatrix(); - MatrixF xfm( *ri->orientation ); xfm.setPosition( ri->position ); xfm.scale( ri->scale ); - //GFXTransformSaver saver; - GFX->multWorld( xfm ); + matrixSet.setWorld(xfm); + mMatInstance->setTransforms(matrixSet, state); - if ( smDebugRender ) - GFX->setStateBlock( mDebugSB ); - else - GFX->setStateBlock( mNormalSB ); - - ri->query->begin(); - - if ( wasSphere ) - GFX->drawPrimitive( GFXTriangleList, 0, mSpherePrimCount ); - else - GFX->drawPrimitive( GFXTriangleList, 0, 12 ); + if ( !smDebugRender ) + GFX->setStateBlock( mRenderSB ); + ri->query->begin(); + GFX->drawPrimitive( GFXTriangleList, 0, primCount ); ri->query->end(); if ( ri->query2 ) @@ -240,15 +233,11 @@ void RenderOcclusionMgr::render( SceneRenderState *state ) GFX->setStateBlock( mTestSB ); ri->query2->begin(); - - if ( wasSphere ) - GFX->drawPrimitive( GFXTriangleList, 0, mSpherePrimCount ); - else - GFX->drawPrimitive( GFXTriangleList, 0, 12 ); - + GFX->drawPrimitive( GFXTriangleList, 0, primCount ); ri->query2->end(); } + } - GFX->popWorldMatrix(); - } + // Call setup one more time to end the pass. + mMatInstance->setupPass( state, sgData ); } \ No newline at end of file diff --git a/Engine/source/renderInstance/renderOcclusionMgr.h b/Engine/source/renderInstance/renderOcclusionMgr.h index bcdefdffa..41050c38d 100644 --- a/Engine/source/renderInstance/renderOcclusionMgr.h +++ b/Engine/source/renderInstance/renderOcclusionMgr.h @@ -26,6 +26,10 @@ #include "renderInstance/renderBinManager.h" #endif +class Material; +class BaseMatInstance; + + //************************************************************************** // RenderOcclusionMgr //************************************************************************** @@ -46,15 +50,17 @@ public: DECLARE_CONOBJECT(RenderOcclusionMgr); protected: - BaseMatInstance* mOverrideMat; - GFXStateBlockRef mNormalSB; + GFXStateBlockRef mRenderSB; GFXStateBlockRef mTestSB; - - GFXStateBlockRef mDebugSB; + + /// The material for rendering occluders. + SimObjectPtr mMaterial; + BaseMatInstance *mMatInstance; + static bool smDebugRender; - GFXVertexBufferHandle mBoxBuff; - GFXVertexBufferHandle mSphereBuff; + GFXVertexBufferHandle mBoxBuff; + GFXVertexBufferHandle mSphereBuff; U32 mSpherePrimCount; };