shaders/DynamicLightingShadow/ShadowMappingShader.cxx

shaders/DynamicLightingShadow/ShadowMappingShader.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
/***************************************************************************************
ShadowMappingShader.cpp
This is the normal mapping shader. This class will take care of all data management
for the normal mapping shader.
Author: Jason Lacroix
***************************************************************************************/
#include <math.h>
#include "ShadowMappingShader.h"
#include <Cg/cgGL.h>
#ifdef _MB_INTERNAL
#include <klib/kconfigfile.h>
#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
namespace Graphics
{
ShadowMappingShader::ShadowMappingShader()
: mShadowPCFKernelSize(9)
, mUseHardwarePCF(false)
, mShadowStrength(1.0f)
{
for( int i = 0; i < kMaxLight; i++ )
{
mShadowMapSize[i] = -1;
}
}
ShadowMappingShader::~ShadowMappingShader()
{
UninitializeBuffers();
if ( mShadowProjVertexShader )
{
cgDestroyProgram( mShadowProjVertexShader );
}
}
void ShadowMappingShader::SaveFrameBuffer()
{
glGetIntegerv( GL_FRAMEBUFFER_BINDING, &mLastFramebuffer );
if ( mLastFramebuffer > 0 )
{
glGetIntegerv( GL_MAX_DRAW_BUFFERS, &mMaxDrawBuffers );
GLint lActualUse = 0;
for ( GLint i = 0; i < mMaxDrawBuffers && i < MAX_DRAW_BUFFERS; i++ )
{
glGetIntegerv( GL_DRAW_BUFFER0+i, (GLint *)&(mDrawBuffers[i]) );
if ( mDrawBuffers[i] == GL_NONE )
{
break;
}
lActualUse ++;
}
mMaxDrawBuffers = lActualUse;
}
else
{
mLastFramebuffer = 0;
}
}
void ShadowMappingShader::RestoreFrameBuffer()
{
if ( mLastFramebuffer > 0 )
{
glBindFramebuffer( GL_FRAMEBUFFER_EXT, mLastFramebuffer );
if ( mMaxDrawBuffers > 0 )
{
glDrawBuffers( mMaxDrawBuffers, &(mDrawBuffers[0]) );
}
}
}
bool ShadowMappingShader::Initialize(const char* pVertexShaderPath, const char* pPixelShaderPath, const char* pVertexProjShaderPath)
{
#ifdef _MB_INTERNAL
KConfigFile ConfigFile(CONFIGFILE);
const char* lConfigHardwarePCF = ConfigFile.GetOrSet( "Display", "UseHardwarePCF", "Yes", "(Yes/No)" );
mUseHardwarePCF = (_strcmpi(lConfigHardwarePCF,"Yes")==0);
#else
// Hardware PCF requires ARB_fragment_program_shadow extension
mUseHardwarePCF = true; // OpenGL 1.3, so should be available
#endif
FBTrace("DynamicLightingShadow: %s\n", mUseHardwarePCF ? "using hardware PCF" : "hardware PCF not supported");
const char* lShaderOptions = mUseHardwarePCF ? "-DENABLE_SHADOWS=1 -DENABLE_HWPCF=1" : "-DENABLE_SHADOWS=1";
bool lResult = Shader::Initialize( pVertexShaderPath, pPixelShaderPath, lShaderOptions, lShaderOptions );
if ( !lResult )
{
return false;
}
mShadowProjVertexShader = CreateShader(mVertexProfile, pVertexProjShaderPath, NULL);
if ( !mShadowProjVertexShader )
{
ShowError( cgGetLastListing(mContext) );
return false;
}
// Store CG shader parameters for data upload.
mParamShadowProjModel = cgGetNamedParameter( mShadowProjVertexShader, "ShadowModel" );
mParamShadowProjViewProjection = cgGetNamedParameter( mShadowProjVertexShader, "ShadowViewProj" );
mParamShadowPCFKernelSize = cgGetNamedParameter( mPixelShader, "ShadowPCFKernelSize" );
mParamShadowStrength = cgGetNamedParameter( mPixelShader, "ShadowStrength" );
char variableName[64];
for( int i = 0; i < kMaxLight; ++i )
{
sprintf_s( variableName, "ShadowMVP[%d]", i );
mParamShadowMVP[i] = cgGetNamedParameter( mPixelShader, variableName );
sprintf_s( variableName, "ShadowMap[%d]", i );
mParamShadowMap[i] = cgGetNamedParameter( mPixelShader, variableName );
sprintf_s( variableName, "ShadowMapSize[%d]", i );
mParamShadowMapSize[i] = cgGetNamedParameter( mPixelShader, variableName );
assert(mParamShadowMVP[i]);
assert(mParamShadowMap[i]);
assert(mParamShadowMapSize[i]);
}
return true;
}
bool ShadowMappingShader::InitializeBuffers( int pShadowMapSize, FBArrayTemplate<FBLight*>* pAffectingLightList )
{
// Store default render target
SaveFrameBuffer();
// Initialize buffers
int lLightCount = pAffectingLightList->GetCount();
for( int i = 0; i < lLightCount; i++ )
{
FBLight* lLight = pAffectingLightList->GetAt(i);
bool lShadowSupport = lLight && (lLight->LightType == kFBLightTypeSpot || lLight->LightType == kFBLightTypeInfinite);
if( lShadowSupport )
{
InitializeBuffer( i, pShadowMapSize );
}
else
{
if( mShadowMapSize[i] > 0 )
{
UninitializeBuffer( i );
}
}
}
for( int i = lLightCount; i < kMaxLight; i++ )
{
if( mShadowMapSize[i] > 0 )
{
UninitializeBuffer( i );
}
}
// Restore default render target
glBindFramebuffer( GL_FRAMEBUFFER_EXT, 0 );
RestoreFrameBuffer();
return true;
}
bool ShadowMappingShader::InitializeBuffer( int pIndex, int pShadowMapSize )
{
if( pIndex < 0 || pIndex > kMaxLight )
{
return false;
}
// Only re-initialize if the size changed.
if( mShadowMapSize[pIndex] != pShadowMapSize )
{
UninitializeBuffer( pIndex );
// Create offscreen textures and FBOs for offscreen shadow map rendering.
glGenTextures( 1, &mShadowMap[pIndex] );
glGenFramebuffers( 1, &mFrameBuffer[pIndex] );
// Specify texture parameters and attach each texture to an FBO.
glBindTexture( GL_TEXTURE_2D, mShadowMap[pIndex] );
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, pShadowMapSize, pShadowMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color );
if (mUseHardwarePCF)
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
}
else
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
}
glBindFramebuffer( GL_FRAMEBUFFER_EXT, mFrameBuffer[pIndex] );
glFramebufferTexture2D( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mShadowMap[pIndex], 0 );
glDrawBuffer( GL_FALSE );
glReadBuffer( GL_FALSE );
// Check for errors...
GLenum status = glCheckFramebufferStatus( GL_FRAMEBUFFER_EXT );
switch( status )
{
case GL_FRAMEBUFFER_COMPLETE_EXT: // Succeeded
break;
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
default:
ShowError("GL_FRAMEBUFFER_UNSUPPORTED_EXT");
return false;
break;
}
// Success! Record the shadow map size.
mShadowMapSize[pIndex] = pShadowMapSize;
}
return true;
}
bool ShadowMappingShader::UninitializeBuffers()
{
glDeleteTextures( kMaxLight, mShadowMap );
glDeleteFramebuffers( kMaxLight, mFrameBuffer );
glDeleteRenderbuffers( kMaxLight, mDepthRenderBuffer );
for( int i = 0; i < kMaxLight; i++ )
{
mShadowMapSize[i] = -1;
}
return true;
}
bool ShadowMappingShader::UninitializeBuffer( int pIndex )
{
if( pIndex < 0 || pIndex > kMaxLight )
{
return false;
}
glDeleteTextures( 1, &mShadowMap[pIndex] );
glDeleteFramebuffers( 1, &mFrameBuffer[pIndex] );
glDeleteRenderbuffers( 1, &mDepthRenderBuffer[pIndex] );
mShadowMapSize[pIndex] = -1;
return true;
}
void ShadowMappingShader::BeginShading(FBRenderOptions* pRenderOptions, FBArrayTemplate<FBLight*>* pAffectingLightList)
{
Shader::BindShaderPrograms();
UploadParameters(pRenderOptions, pAffectingLightList);
}
void ShadowMappingShader::EndShading()
{
UnsetTextures();
Shader::UnbindShaderPrograms();
}
void ShadowMappingShader::UploadParameters( FBRenderOptions* pRenderOptions, FBArrayTemplate<FBLight*> *pAffectingLightList )
{
Shader::UploadParameters( pRenderOptions, pAffectingLightList );
// Get the camera's location
FBRenderer* lRender = FBSystem::TheOne().Renderer;
FBMatrix lViewMatrix;
lRender->CurrentCamera->GetCameraMatrix( lViewMatrix, kFBModelView );
// Update infinite light positions
//
// Shadow projections for infinite lights need to reposition the
// position of the infinite light in order to project their shadows.
//
if( pAffectingLightList ) // Should always be valid
{
double lOrigin[4] = { 0.0, 0.0, 0.0, 0.0 };
for( int i = 0; i < pAffectingLightList->GetCount(); i++ )
{
FBLight* lLight = pAffectingLightList->GetAt(i);
if( lLight && lLight->LightType == kFBLightTypeInfinite )
{
// Use the precomputed mLightViewMatrix to determine the
// world space location of the infinite light.
FBVector4d lLightPos;
lLightPos.Set( lOrigin );
FBMatrix lLightWorldMat;
FBMatrixInverse( lLightWorldMat, mLightViewMatrix[i] );
FBVectorMatrixMult( lLightPos, lLightWorldMat, lLightPos );
FBVector4d lViewPosition;
FBVectorMatrixMult(lViewPosition, lViewMatrix, lLightPos);
// Denote to the shader that this is an infinite light.
lLightPos[3] = 0.0;
cgSetParameter4dv( mParamLightPositions[i], lViewPosition.mValue );
}
}
}
// Set the shadow maps and caster indices
for( int i = 0; i < kMaxLight; i++ )
{
if( mShadowMapSize[i] > 0 )
{
cgGLSetTextureParameter( mParamShadowMap[i], mShadowMap[i] );
cgGLEnableTextureParameter( mParamShadowMap[i] );
}
else
{
cgGLDisableTextureParameter( mParamShadowMap[i] );
}
cgSetParameter1i( mParamShadowMapSize[i], mShadowMapSize[i] );
}
// Set the PCF kernel size
cgSetParameter1i( mParamShadowPCFKernelSize, mShadowPCFKernelSize );
// Set the Shadow Strength
cgSetParameter1f( mParamShadowStrength, mShadowStrength );
}
void ShadowMappingShader::ShaderPassModelDraw (
FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo)
{
// Get the model's world matrix (transformation matrix) and set it
FBMatrix modelMatrix;
pInfo->GetFBModel()->GetMatrix(modelMatrix);
for( int i = 0; i < kMaxLight; i++ )
{
FBMatrix modelView;
FBMatrix mvp;
FBMatrixMult(modelView, mLightViewMatrix[i], modelMatrix);
FBMatrixMult(mvp, mLightProjMatrix[i], modelView);
// Set the projection matrices
cgSetMatrixParameterdc( mParamShadowMVP[i], mvp);
}
Shader::ShaderPassModelDraw( pRenderOptions, pPass, pInfo );
}
void ShadowMappingShader::UploadShadowParameters( int pLightIndex, FBRenderOptions* pRenderOptions )
{
if( pLightIndex < 0 || pLightIndex > kMaxLight )
{
return;
}
FBMatrix vp;
FBMatrixMult( vp, mLightProjMatrix[pLightIndex], mLightViewMatrix[pLightIndex] );
cgSetMatrixParameterdc( mParamShadowProjViewProjection, vp );
}
void ShadowMappingShader::BindShadowPrograms()
{
cgGLBindProgram(mShadowProjVertexShader); // Bind program. (You can bind only one vertex and one fragment program at a time)
cgGLBindProgram(0); // No pixel shader as we are not writing colour output
// Set the pixel/vertex shader profiles
cgGLEnableProfile(mVertexProfile);
}
void ShadowMappingShader::UnbindShadowPrograms()
{
UnbindShaderPrograms();
}
void ShadowMappingShader::UnsetTextures()
{
// Call the base
Graphics::Shader::UnsetTextures();
for( int i = 0; i < kMaxLight; i++ )
{
cgGLDisableTextureParameter( mParamShadowMap[i] );
}
}
bool ShadowMappingShader::RenderShadowMaps( int pShadowMapSize, FBRenderOptions* pRenderOptions, double pOffsetScale, double pOffsetBias, FBArrayTemplate<FBLight*>* pAffectingLightList, FBArrayTemplate<FBModel*>* pShadowCasters )
{
if( !pAffectingLightList || pAffectingLightList->GetCount() == 0 )
{
return false;
}
// Initialize shadow buffers
InitializeBuffers( pShadowMapSize, pAffectingLightList );
// Store current frame buffer
SaveFrameBuffer();
// Compute the world bounds for infinite light adjustment.
ComputeWorldBounds();
// Save the viewport
glPushAttrib( GL_VIEWPORT_BIT );
// Set the viewport to the proper size
glViewport( 0, 0, pShadowMapSize, pShadowMapSize );
glPolygonOffset( GLfloat(pOffsetScale), GLfloat(pOffsetBias) );
glEnable( GL_POLYGON_OFFSET_FILL );
// Set the actual shaders
BindShadowPrograms();
// Compute shadow maps
int lLightCount = min( kMaxLight, pAffectingLightList->GetCount() );
for( int i = 0; i < lLightCount; i++ )
{
// Setup to render into the shadow buffer ...
glBindFramebuffer( GL_FRAMEBUFFER_EXT, mFrameBuffer[i] );
// Clear the depth buffer
glClearDepth( 1.0 );
glClear( GL_DEPTH_BUFFER_BIT );
// Calculate the light matrices
FBLight* light = pAffectingLightList->GetAt(i);
ComputeLightMatrices( light, mLightViewMatrix[i], mLightProjMatrix[i] );
// Upload model independent shadow parameters
UploadShadowParameters( i, pRenderOptions );
// Draw the shadow models
RenderShadowModels( pShadowCasters );
}
// Set the actual shaders
UnbindShadowPrograms();
// Set our frame buffer back to the default one ...
glDisable(GL_POLYGON_OFFSET_FILL);
glPopAttrib(); // restore the viewport
glBindFramebuffer( GL_FRAMEBUFFER_EXT, 0 );
RestoreFrameBuffer();
return true;
}
void ShadowMappingShader::RenderShadowModels( FBArrayTemplate<FBModel*>* pShadowCasters )
{
if( pShadowCasters )
{
for( int i = 0; i < pShadowCasters->GetCount(); i++ )
{
FBModel* lModel = pShadowCasters->GetAt(i);
if( !lModel )
{
continue;
}
// Set the model matrix
FBMatrix lModelMatrix;
lModel->GetMatrix( lModelMatrix );
cgSetMatrixParameterdc( mParamShadowProjModel, lModelMatrix );
// Draw the model
RenderShadowModel( lModel );
}
}
else
{
FBRenderer* lRenderer = FBSystem().Renderer;
if( lRenderer )
{
for( int i = 0; i < lRenderer->DisplayableGeometryCount; i++ )
{
FBModel* lModel = lRenderer->GetDisplayableGeometry(i);
if( !lModel )
{
continue;
}
// Set the model matrix
FBMatrix lModelMatrix;
lModel->GetMatrix( lModelMatrix );
cgSetMatrixParameterdc( mParamShadowProjModel, lModelMatrix );
// Draw the model
RenderShadowModel( lModel );
}
}
}
}
void ShadowMappingShader::RenderShadowModel( FBModel* pModel )
{
if( !pModel )
{
return;
}
FBModelVertexData* lModelVertexData = pModel->ModelVertexData;
if( lModelVertexData && lModelVertexData->IsDrawable() /*ACME-2464*/ )
{
const int lSubRegionCount = lModelVertexData->GetSubRegionCount();
if( lSubRegionCount )
{
lModelVertexData->EnableOGLVertexData();
for (int lSubRegionIndex = 0; lSubRegionIndex < lSubRegionCount; lSubRegionIndex++)
{
lModelVertexData->DrawSubRegion( lSubRegionIndex );
}
lModelVertexData->DisableOGLVertexData();
}
}
}
void ShadowMappingShader::SetShadowPCFKernelSize( int pShadowPCFKernelSize )
{
mShadowPCFKernelSize = pShadowPCFKernelSize;
}
void ShadowMappingShader::SetShadowStrength( float pShadowStrength )
{
mShadowStrength = pShadowStrength;
}
bool ShadowMappingShader::ComputeLightMatrices( FBLight* pLight, FBMatrix& pLightView, FBMatrix& pLightProj )
{
bool status = false;
if( pLight->LightType == kFBLightTypeSpot )
{
status = ComputeSpotLightMatrices( pLight, pLightView, pLightProj );
}
else if( pLight->LightType == kFBLightTypeInfinite )
{
status = ComputeInfiniteLightMatrices( pLight, pLightView, pLightProj );
}
return status;
}
bool ShadowMappingShader::ComputeSpotLightMatrices( FBLight* pLight, FBMatrix& pLightView, FBMatrix& pLightProj )
{
if( pLight->LightType != kFBLightTypeSpot )
{
return false;
}
pLightView.Identity();
pLightProj.Identity();
// Spotlight support
// Get all the information necessary to setup the lighting matrix
// Will need to create a MODELVIEWPROJ matrix using:
// - Transformation matrix of light
// - Custom projection matrix based on light
// We need a base matrix because the transformation matrix doesn't take into account that lights
// start out with transforms that are not represented ... grr ...
double base[16] =
{
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
FBMatrix baseMat( base );
// Factor out the scale, because we don't want it ...
FBMatrix rotationMat;
FBMatrix transformationMat;
pLight->GetMatrix( rotationMat, kModelRotation, true );
FBMatrixMult( transformationMat, rotationMat, baseMat );
transformationMat(3,0) = ((FBVector3d)pLight->Translation)[0];
transformationMat(3,1) = ((FBVector3d)pLight->Translation)[1];
transformationMat(3,2) = ((FBVector3d)pLight->Translation)[2];
transformationMat(3,3) = 1.0f;
FBMatrixInverse( pLightView, transformationMat );
// Ok .. now we just need a projection matrix ...
float fov = 1.2f * pLight->ConeAngle / 2.0f;
float fFar = (double) pLight->Intensity * 2.0f;
float fNear = 1.0f;
float top = tan(fov*3.14159f/360.0f) * fNear;
float bottom = -top;
float left = bottom;
float right = top;
double perspectiveValues[16] =
{
(2.0*fNear)/(right-left), 0, 0, 0,
0, (2.0*fNear)/(top-bottom), 0, 0,
0, 0, -(fFar+fNear)/(fFar-fNear), -(2.0f*fFar*fNear)/(fFar-fNear),
0, 0, -1.0f, 0
};
pLightProj = FBMatrix( perspectiveValues );
return true;
}
bool ShadowMappingShader::ComputeInfiniteLightMatrices( FBLight* pLight, FBMatrix& pLightView, FBMatrix& pLightProj )
{
if( pLight->LightType != kFBLightTypeInfinite )
{
return false;
}
pLightView.Identity();
pLightProj.Identity();
// Directional light support
// Get all the information necessary to setup the lighting matrix
// Will need to create a MODELVIEWPROJ matrix using:
// - Transformation matrix of light
// - Custom projection matrix based on light
// We need a base matrix because the transformation matrix doesn't take into account that lights
// start out with transforms that are not represented ... grr ...
double base[16] =
{
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
FBMatrix baseMat( base );
// Factor out the scale, because we don't want it ...
FBMatrix rotationMat;
FBMatrix transformationMat;
pLight->GetMatrix( rotationMat, kModelRotation, true );
FBMatrixMult( transformationMat, rotationMat, baseMat );
double radius;
FBVector4d newPos;
GetWorldBounds( pLight, radius, newPos );
transformationMat(3,0) = newPos[0];
transformationMat(3,1) = newPos[1];
transformationMat(3,2) = newPos[2];
transformationMat(3,3) = 1.0f;
FBMatrixInverse( pLightView, transformationMat );
// Ok .. now we just need a projection matrix ...
double left = -radius;
double right = radius;
double top = radius;
double bottom = -radius;
double fNear = 0.0f;
double fFar = radius * 2.0f;
double diffRL = 1.0 / (right - left);
double diffTB = 1.0 / (top - bottom);
double diffFN = 1.0 / (fFar - fNear);
double orthoValues[16] =
{
2.0 * diffRL, 0, 0, 0,
0, 2.0 * diffTB, 0, 0,
0, 0, -2.0 * diffFN, 0,
0, 0, -(fFar + fNear) * diffFN, 1.0
};
pLightProj = FBMatrix( orthoValues );
return true;
}
void ShadowMappingShader::ComputeWorldBounds()
{
double dblMin[4] = { DBL_MIN, DBL_MIN, DBL_MIN, 1.0 };
double dblMax[4] = { DBL_MAX, DBL_MAX, DBL_MAX, 1.0 };
mWorldMin.Set( dblMax );
mWorldMax.Set( dblMin );
ComputeWorldBounds( FBSystem().SceneRootModel );
}
void ShadowMappingShader::ComputeWorldBounds( FBModel* pModel, bool pRecursive /* = true */ )
{
bool lLightCam = pModel->Is( FBCamera::TypeInfo ) || pModel->Is( FBLight::TypeInfo ) ;
if( pModel->Geometry && !lLightCam )
{
// Only collect geometric world bounds.
FBVector3d lMin, lMax;
pModel->GetBoundingBox( lMin, lMax );
FBVector4d l4Min, l4Max;
l4Min[0] = lMin[0];
l4Min[1] = lMin[1];
l4Min[2] = lMin[2];
l4Min[3] = 1.0;
l4Max[0] = lMax[0];
l4Max[1] = lMax[1];
l4Max[2] = lMax[2];
l4Max[3] = 1.0;
FBMatrix lMtx;
pModel->GetMatrix( lMtx );
FBVectorMatrixMult( l4Min, lMtx, l4Min );
FBVectorMatrixMult( l4Max, lMtx, l4Max );
if( l4Min[0] < mWorldMin[0] ) mWorldMin[0] = l4Min[0];
if( l4Min[1] < mWorldMin[1] ) mWorldMin[1] = l4Min[1];
if( l4Min[2] < mWorldMin[2] ) mWorldMin[2] = l4Min[2];
if( l4Max[0] > mWorldMax[0] ) mWorldMax[0] = l4Max[0];
if( l4Max[1] > mWorldMax[1] ) mWorldMax[1] = l4Max[1];
if( l4Max[2] > mWorldMax[2] ) mWorldMax[2] = l4Max[2];
}
// Recursively iterate through the scene hierarchy.
if( pRecursive )
{
for( int i = 0; i < pModel->Children.GetCount(); i++ )
{
ComputeWorldBounds( pModel->Children[i] );
}
}
}
void ShadowMappingShader::GetWorldBounds( FBLight* pLight, double& pRadius, FBVector4d& pPos )
{
// Default light position is pointing down the -Y axis
double lBaseLightDir[4] = { 0.0, -1.0, 0.0, 0.0 };
FBVector4d lDir;
lDir.Set( lBaseLightDir );
FBMatrix rotationMat;
pLight->GetMatrix( rotationMat, kModelRotation, true );
// Compute transformation matrix.
double base[16] =
{
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
FBMatrix baseMat( base );
FBMatrix transformMat, invTransformMat;
FBMatrixMult( transformMat, rotationMat, baseMat );
FBMatrixInverse( invTransformMat, transformMat );
// Compute bounds in lightView space.
FBVector4d lBounds[2];
FBVectorMatrixMult( lBounds[0], invTransformMat, mWorldMin );
FBVectorMatrixMult( lBounds[1], invTransformMat, mWorldMax );
double dblMin[4] = { DBL_MIN, DBL_MIN, DBL_MIN, 1.0 };
double dblMax[4] = { DBL_MAX, DBL_MAX, DBL_MAX, 1.0 };
FBVector4d lLightMin, lLightMax;
lLightMin.Set( dblMax );
lLightMax.Set( dblMin );
for( int i = 0; i < 3; i++ )
{
if( lBounds[0][i] < lLightMin[i] ) { lLightMin[i] = lBounds[0][i]; }
if( lBounds[1][i] < lLightMin[i] ) { lLightMin[i] = lBounds[1][i]; }
if( lBounds[0][i] > lLightMax[i] ) { lLightMax[i] = lBounds[0][i]; }
if( lBounds[1][i] > lLightMax[i] ) { lLightMax[i] = lBounds[1][i]; }
}
// Compute the box center (in world space)
FBVector4d center;
center[0] = 0.5 * (lLightMin[0] + lLightMax[0]);
center[1] = 0.5 * (lLightMin[1] + lLightMax[1]);
center[2] = 0.5 * (lLightMin[2] + lLightMax[2]);
center[3] = 1.0;
FBVectorMatrixMult( center, transformMat, center );
double w = lLightMax[0] - lLightMin[0];
double h = lLightMax[1] - lLightMin[1];
double d = lLightMax[2] - lLightMin[2];
// Expand the radius by 10%
double sceneRadius = 0.5 * max( w, max(h, d) );
sceneRadius *= 1.1f;
FBVectorMatrixMult( lDir, rotationMat, lDir );
lDir[0] *= sceneRadius;
lDir[1] *= sceneRadius;
lDir[2] *= sceneRadius;
FBVector4d newPos;
newPos[0] = center[0] - lDir[0];
newPos[1] = center[1] - lDir[1];
newPos[2] = center[2] - lDir[2];
newPos[3] = 1.0;
// Return values
pRadius = sceneRadius;
pPos = newPos;
}
}