Explorar el Código

Merge pull request #1352 from marauder2k9-torque/Probe-baking-matrix-issues

BUG: Probe baking look at matrix was wrong
Brian Roberts hace 8 meses
padre
commit
67ae3d136b

+ 13 - 19
Engine/source/math/mMatrix.h

@@ -508,30 +508,24 @@ inline MatrixF& MatrixF::add( const MatrixF& a )
 
 inline void MatrixF::LookAt(const VectorF& eye, const VectorF& target, const VectorF& up)
 {
-   VectorF yAxis = target - eye;          // Forward
-   yAxis.normalize();
+   // Calculate the forward vector (camera direction).
+   VectorF zAxis = target; // Camera looks towards the target
+   zAxis.normalize();
 
-   VectorF xAxis = mCross(up, yAxis);     // Right
+   // Calculate the right vector.
+   VectorF xAxis = mCross(up, zAxis);
    xAxis.normalize();
 
-   VectorF zAxis = mCross(yAxis, xAxis);  // Up
+   // Recalculate the up vector.
+   VectorF yAxis = mCross(zAxis, xAxis);
 
-   // Right vector.
-   setColumn(0, xAxis);
-   m[12] = -mDot(xAxis, eye);
+   // Set the rotation part of the matrix (camera axes).
+   setColumn(0, xAxis); // Right
+   setColumn(1, zAxis); // Forward
+   setColumn(2, yAxis); // Up
 
-   // Forward vector.
-   setColumn(1, yAxis);
-   m[13] = -mDot(yAxis, eye);
-
-   // Up vector.
-   setColumn(2, zAxis);
-   m[14] = -mDot(zAxis, eye);
-
-   m[3] = 0.0f;
-   m[7] = 0.0f;
-   m[11] = 0.0f;
-   m[15] = 1.0f;
+   // Set the translation part (camera position).
+   setPosition(eye);
 
 }
 

+ 4 - 2
Engine/source/renderInstance/renderProbeMgr.cpp

@@ -566,13 +566,15 @@ void RenderProbeMgr::bakeProbe(ReflectionProbe* probe)
 
    ReflectParams reflParams;
 
+   MatrixF camTrans = clientProbe->getTransform();
+   camTrans.setPosition(clientProbe->getTransform().getPosition() + clientProbe->mProbeRefOffset);
    //need to get the query somehow. Likely do some sort of get function to fetch from the guiTSControl that's active
    CameraQuery query; //need to get the last cameraQuery
    query.fov = 90; //90 degree slices for each of the 6 sides
-   query.nearPlane = 0.1f;
+   query.nearPlane = 0.0001f;
    query.farPlane = farPlane;
    query.headMatrix = MatrixF();
-   query.cameraMatrix = clientProbe->getTransform();
+   query.cameraMatrix = camTrans;
 
    Frustum culler;
    culler.set(false,

+ 39 - 35
Engine/source/scene/reflector.cpp

@@ -326,36 +326,32 @@ void CubeReflector::updateReflection( const ReflectParams &params, Point3F expli
    if ( mRenderTarget.isNull() )
       mRenderTarget = GFX->allocRenderToTextureTarget();   
 
-   GFX->pushActiveRenderTarget();
    mDepthBuff = LightShadowMap::_getDepthTarget(texDim, texDim);
    mRenderTarget->attachTexture(GFXTextureTarget::DepthStencil, mDepthBuff);
    F32 oldVisibleDist = gClientSceneGraph->getVisibleDistance();
    gClientSceneGraph->setVisibleDistance( mDesc->farDist );   
 
-   // store current matrices
-   GFXTransformSaver saver;
-
    F32 detailAdjustBackup = TSShapeInstance::smDetailAdjust;
    TSShapeInstance::smDetailAdjust *= mDesc->detailAdjust;
 
+   // store current matrices
+   GFXTransformSaver saver;
+
    // set projection to 90 degrees vertical and horizontal
    F32 left, right, top, bottom;
    MathUtils::makeFrustum(&left, &right, &top, &bottom, M_HALFPI_F, 1.0f, mDesc->nearDist);
    GFX->setFrustum(left, right, bottom, top, mDesc->nearDist, mDesc->farDist);
 
-   // We don't use a special clipping projection, but still need to initialize 
-   // this for objects like SkyBox which will use it during a reflect pass.
-   gClientSceneGraph->setNonClipProjection(GFX->getProjectionMatrix());
-
+   GFX->pushActiveRenderTarget();
    for (S32 i = 5; i >= 0; i--) {
       updateFace(params, i, explicitPostion);
    }
+   GFX->popActiveRenderTarget();
 
    TSShapeInstance::smDetailAdjust = detailAdjustBackup;
 
    mCubemap->generateMipMaps();
 
-   GFX->popActiveRenderTarget();
 
    gClientSceneGraph->setVisibleDistance(oldVisibleDist);
 
@@ -379,47 +375,51 @@ void CubeReflector::updateFace( const ReflectParams &params, U32 faceidx, Point3
       eye = explicitPostion;
    }
 
-   VectorF vUpVec(0.0f, 0.0f, 0.0f);
+   // Standard view that will be overridden below.
+   VectorF vLookatPt(0.0f, 0.0f, 0.0f), vUpVec(0.0f, 0.0f, 0.0f), vRight(0.0f, 0.0f, 0.0f);
 
-   switch( faceidx )
+   switch (faceidx)
    {
-   case 0 : // D3DCUBEMAP_FACE_POSITIVE_X:
-      target    = eye + VectorF( 1.0f, 0.0f, 0.0f );
-      vUpVec    = VectorF( 0.0f, 1.0f, 0.0f );
+   case 0: // D3DCUBEMAP_FACE_POSITIVE_X:
+      vLookatPt = VectorF(1.0f, 0.0f, 0.0f);
+      vUpVec = VectorF(0.0f, 1.0f, 0.0f);
       break;
-   case 1 : // D3DCUBEMAP_FACE_NEGATIVE_X:
-      target    = eye + VectorF( -1.0f, 0.0f, 0.0f );
-      vUpVec    = VectorF( 0.0f, 1.0f, 0.0f );
+   case 1: // D3DCUBEMAP_FACE_NEGATIVE_X:
+      vLookatPt = VectorF(-1.0f, 0.0f, 0.0f);
+      vUpVec = VectorF(0.0f, 1.0f, 0.0f);
       break;
-   case 2 : // D3DCUBEMAP_FACE_POSITIVE_Y:
-      target    = eye + VectorF( 0.0f, 1.0f, 0.0f );
-      vUpVec    = VectorF( 0.0f, 0.0f,-1.0f );
+   case 2: // D3DCUBEMAP_FACE_POSITIVE_Y:
+      vLookatPt = VectorF(0.0f, 1.0f, 0.0f);
+      vUpVec = VectorF(0.0f, 0.0f, -1.0f);
       break;
-   case 3 : // D3DCUBEMAP_FACE_NEGATIVE_Y:
-      target    = eye + VectorF( 0.0f, -1.0f, 0.0f );
-      vUpVec    = VectorF( 0.0f, 0.0f, 1.0f );
+   case 3: // D3DCUBEMAP_FACE_NEGATIVE_Y:
+      vLookatPt = VectorF(0.0f, -1.0f, 0.0f);
+      vUpVec = VectorF(0.0f, 0.0f, 1.0f);
       break;
-   case 4 : // D3DCUBEMAP_FACE_POSITIVE_Z:
-      target    = eye + VectorF( 0.0f, 0.0f, 1.0f );
-      vUpVec    = VectorF( 0.0f, 1.0f, 0.0f );
+   case 4: // D3DCUBEMAP_FACE_POSITIVE_Z:
+      vLookatPt = VectorF(0.0f, 0.0f, 1.0f);
+      vUpVec = VectorF(0.0f, 1.0f, 0.0f);
       break;
    case 5: // D3DCUBEMAP_FACE_NEGATIVE_Z:
-      target    = eye + VectorF( 0.0f, 0.0f, -1.0f );
-      vUpVec    = VectorF( 0.0f, 1.0f, 0.0f );
+      vLookatPt = VectorF(0.0f, 0.0f, -1.0f);
+      vUpVec = VectorF(0.0f, 1.0f, 0.0f);
       break;
    }
 
    // create camera matrix
-   MatrixF matView(true);
-   matView.LookAt(eye, target, vUpVec);
-   matView.inverse();
+   MatrixF lightMatrix(true);
+   lightMatrix.LookAt(eye, vLookatPt, vUpVec);
+   lightMatrix.inverse();
 
-   GFX->setWorldMatrix(matView);
+   GFX->setWorldMatrix(lightMatrix);
    GFX->clearTextureStateImmediate(0);
-   mRenderTarget->attachTexture( GFXTextureTarget::Color0, mCubemap, faceidx );
+   mRenderTarget->attachTexture( GFXTextureTarget::Color0, mCubemap, faceidx );   // Setup textures and targets...
+   S32 texDim = mDesc->texSize;
+   texDim = getMax(texDim, 32);
+   mRenderTarget->attachTexture(GFXTextureTarget::DepthStencil, LightShadowMap::_getDepthTarget(texDim, texDim));
    
    GFX->setActiveRenderTarget(mRenderTarget);
-   GFX->clear( GFXClearStencil | GFXClearTarget | GFXClearZBuffer, gCanvasClearColor, 0.0f, 0);
+   GFX->clear( GFXClearStencil | GFXClearTarget | GFXClearZBuffer, gCanvasClearColor, 1.0f, 0);
 
    SceneRenderState reflectRenderState
    (
@@ -429,7 +429,11 @@ void CubeReflector::updateFace( const ReflectParams &params, U32 faceidx, Point3
    );
 
    reflectRenderState.getMaterialDelegate().bind( REFLECTMGR, &ReflectionManager::getReflectionMaterial );
-   reflectRenderState.setDiffuseCameraTransform( params.query->headMatrix );
+   reflectRenderState.setDiffuseCameraTransform(lightMatrix);//params.query->headMatrix );
+
+   // We don't use a special clipping projection, but still need to initialize 
+   // this for objects like SkyBox which will use it during a reflect pass.
+   gClientSceneGraph->setNonClipProjection(GFX->getProjectionMatrix());
 
    // render scene
    LIGHTMGR->registerGlobalLights( &reflectRenderState.getCullingFrustum(), false );