浏览代码

Merge pull request #133 from DavidWyand-GG/issue132-RenderOcclusionMgrRendering

Fix for Issue #132 for RenderOcclusionMgr Rendering
David Wyand 13 年之前
父节点
当前提交
34db24a3e8
共有 2 个文件被更改,包括 66 次插入71 次删除
  1. 54 65
      Engine/source/renderInstance/renderOcclusionMgr.cpp
  2. 12 6
      Engine/source/renderInstance/renderOcclusionMgr.h

+ 54 - 65
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<GFXVertexP>() );
 
+   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<OccluderRenderInst*>(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.size(); i++ )
    {
-      OccluderRenderInst *ri = static_cast<OccluderRenderInst*>(mElementList[i].inst);
-      
+      OccluderRenderInst *ri = static_cast<OccluderRenderInst*>(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 );
-
-         wasSphere = ri->isSphere;
-      }
+     if ( ri->isSphere )
+     {
+        GFX->setVertexBuffer( mSphereBuff );
+        primCount = mSpherePrimCount;
+     }
+     else
+     {
+        GFX->setVertexBuffer( mBoxBuff );
+        primCount = 12;
+     }
 
-      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 );
 }

+ 12 - 6
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<Material> mMaterial;
+   BaseMatInstance *mMatInstance;
+
    static bool smDebugRender;
 
-   GFXVertexBufferHandle<GFXVertexPC> mBoxBuff;
-   GFXVertexBufferHandle<GFXVertexPC> mSphereBuff;
+   GFXVertexBufferHandle<GFXVertexP> mBoxBuff;
+   GFXVertexBufferHandle<GFXVertexP> mSphereBuff;
    U32 mSpherePrimCount;
 };