shaders/ShadowMapping/ShadowMappingShader.cpp

shaders/ShadowMapping/ShadowMappingShader.cpp
/***************************************************************************************
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.
***************************************************************************************/
#include "ShadowMappingShader.h"
#include <math.h>
namespace Graphics
{
ShadowMappingShader::ShadowMappingShader()
: mShadowSize(2048)
, mShadowPCFKernelSize(9)
{
}
ShadowMappingShader::~ShadowMappingShader()
{
glDeleteTextures( 1, &mShadowMap );
glDeleteFramebuffers( 1, &mFrameBuffer );
glDeleteRenderbuffers( 1, &mDepthRenderBuffer );
}
bool ShadowMappingShader::Initialize(const char* pVertexShaderPath, const char* pPixelShaderPath, const char* pVertexProjShaderPath)
{
bool lResult = Shader::Initialize(pVertexShaderPath, pPixelShaderPath, "-DENABLE_SHADOWS=1", "-DENABLE_SHADOWS=1");
if ( !lResult )
{
return false;
}
mShadowProjVertexShader = CreateShader(mVertexProfile, pVertexProjShaderPath, NULL);
if ( !mShadowProjVertexShader )
{
ShowError( cgGetLastListing(mContext) );
return false;
}
// Now that the shader was created, store the parameters so
// we can upload data to it at render time
mParamShadowProjMVP = cgGetNamedParameter( mShadowProjVertexShader, "ShadowMVP" );
mParamShadowMVP = cgGetNamedParameter( mVertexShader, "ShadowMVP" );
mParamShadowMap = cgGetNamedParameter( mPixelShader, "ShadowMap" );
mParamShadowCaster = cgGetNamedParameter( mPixelShader, "ShadowCaster" );
mParamShadowMapSize = cgGetNamedParameter( mPixelShader, "ShadowMapSize" );
mParamShadowPCFKernelSize = cgGetNamedParameter( mPixelShader, "ShadowPCFKernelSize" );
mParamShadowPCFKernelStep = cgGetNamedParameter( mPixelShader, "ShadowPCFKernelStep" );
// Ok, for this shader,we also need to create all the extra buffers
// and what not for rendering off scene to create our shadow maps
glGenTextures( 1, &mShadowMap );
glBindTexture( GL_TEXTURE_2D, mShadowMap );
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32,
mShadowSize, mShadowSize,
0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
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 colour[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, colour );
// create fbo and attach texture to ti
glGenFramebuffers( 1, &mFrameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER_EXT, mFrameBuffer );
glFramebufferTexture2D( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mShadowMap, 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;
}
// Make sure we are on default render target
glBindFramebuffer( GL_FRAMEBUFFER_EXT, 0 );
return true;
}
void ShadowMappingShader::UploadParameters( FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo, FBArrayTemplate<FBLight*> *pAffectingLightList )
{
Shader::UploadParameters( pRenderOptions, pAffectingLightList);
// Test ...
cgGLSetTextureParameter( mParamShadowMap, mShadowMap );
// And enabling it ...
cgGLEnableTextureParameter( mParamShadowMap );
// Get the model's world matrix (transformation matrix) and set it
FBMatrix modelMatrix;
pShaderModelInfo->GetFBModel()->GetMatrix(modelMatrix);
FBMatrix modelView;
FBMatrix mvp;
FBMatrixMult(modelView, mLightViewMatrix, modelMatrix);
FBMatrixMult(mvp, mPerspectiveMatrix, modelView);
// Set the projection matrix
cgSetMatrixParameterdc( mParamShadowMVP, mvp);
// Upload the shadow caster's index
cgSetParameter1i( mParamShadowCaster, mShadowCaster );
// Upload the shadow caster's index
cgSetParameter1i( mParamShadowMapSize, mShadowSize );
cgSetParameter1i( mParamShadowPCFKernelSize, mShadowPCFKernelSize );
cgSetParameter1f( mParamShadowPCFKernelStep, 1.0f / mShadowSize );
}
void ShadowMappingShader::BeginShadowPass( FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo, FBRenderingPass pPass )
{
FBRenderingPass lLastPass = mCurrentPass;
mCurrentPass = pPass;
if ( kFBPassPreRender == mCurrentPass )
{
// Setup to render into the shadow buffer ...
glBindFramebuffer( GL_FRAMEBUFFER_EXT, mFrameBuffer );
glPushAttrib( GL_VIEWPORT_BIT ); // save the viewport glViewport (0, 0, shadowSize, shadowSize);
if ( kFBPassPreRender != lLastPass )
{
// Clear the depth buffer
glClearDepth( 1.0 );
glClear( GL_DEPTH_BUFFER_BIT );
}
// Set the viewport to the proper size
glViewport( 0, 0, mShadowSize, mShadowSize );
glPolygonOffset( 4, 10 );
glEnable(GL_POLYGON_OFFSET_FILL);
// Calculate all of the light matrices we need (this way we don't have to always recalculate for each
// item we render ...
// Get the total number of lights we have to parse through
int lightCount = FBSystem::TheOne().Scene->Lights.GetCount();
// We will pick the first spot light we come across as our shadow caster ...
for( int i = 0; i < lightCount; ++i )
{
FBLight* light = FBSystem::TheOne().Scene->Lights[i];
if( light->LightType == kFBLightTypeSpot )
{
// 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;
light->GetMatrix( rotationMat, kModelRotation, true );
FBMatrixMult( transformationMat, rotationMat, baseMat );
transformationMat(3,0) = ((FBVector3d)light->Translation)[0];
transformationMat(3,1) = ((FBVector3d)light->Translation)[1];
transformationMat(3,2) = ((FBVector3d)light->Translation)[2];
transformationMat(3,3) = 1.0f;
FBMatrixInverse( mLightViewMatrix, transformationMat );
mShadowCaster = i;
// Ok .. now we just need a projection matrix ...
float fov = 1.2f * light->OuterAngle / 2.0f;
float fFar = (double)light->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
};
mPerspectiveMatrix = FBMatrix( perspectiveValues );
break;
}
}
// Set the actual shaders
BindShadowPrograms();
// Upload all the info we need to render ...
UploadShadowParams( pRenderOptions, pShaderModelInfo );
}
else if( kFBPassLighted == mCurrentPass )
{
// Set the actual shaders
BeginShading( pRenderOptions, NULL );
}
}
void ShadowMappingShader::EndShadowPass()
{
if ( kFBPassPreRender == mCurrentPass )
{
// 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 );
}
else if ( kFBPassLighted == mCurrentPass )
{
EndShading();
}
}
void ShadowMappingShader::UploadShadowParams( FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo )
{
// Get the model's world matrix (transformation matrix) and set it
FBMatrix modelMatrix;
pShaderModelInfo->GetFBModel()->GetMatrix(modelMatrix);
FBMatrix modelView;
FBMatrix mvp;
FBMatrixMult(modelView, mLightViewMatrix, modelMatrix);
FBMatrixMult(mvp, mPerspectiveMatrix, modelView);
// Set the projection matrix
cgSetMatrixParameterdc( mParamShadowProjMVP, mvp);
}
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();
cgGLDisableTextureParameter( mParamShadowMap );
}
}