shaders/DynamicLightingShadow/ShadowMappingShader.h

shaders/DynamicLightingShadow/ShadowMappingShader.h
#ifndef SHADOWMAPPINGSHADER_H
#define SHADOWMAPPINGSHADER_H
/***************************************************************************************
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.h
This is the shadow mapping shader. This class will take care of all data management
for the shadow mapping shader.
Author: Jason Lacroix
***************************************************************************************/
#include <Shader.h>
#include <Cg/cg.h>
namespace Graphics
{
class ShadowMappingShader : public Shader
{
public:
ShadowMappingShader();
virtual ~ShadowMappingShader();
virtual bool Initialize(const char* pVertexShaderPath, const char* pPixelShaderPath, const char* pShadowShaderPath);
void BeginShading(FBRenderOptions* pRenderOptions, FBArrayTemplate<FBLight*>* pAffectingLightList);
void EndShading();
void ShaderPassModelDraw ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo );
bool RenderShadowMaps( int pShadowMapSize, FBRenderOptions* pRenderOptions, double pOffsetScale, double pOffsetBias, FBArrayTemplate<FBLight*>* pAffectingLightList, FBArrayTemplate<FBModel*>* pShadowCasters );
void SetShadowPCFKernelSize( int pShadowPCFKernelSize );
void SetShadowStrength( float pShadowStrength );
protected:
bool InitializeBuffers( int pShadowMapSize, FBArrayTemplate<FBLight*>* pAffectingLightList );
bool InitializeBuffer( int pIndex, int pShadowMapSize );
bool UninitializeBuffers();
bool UninitializeBuffer( int pIndex );
void BindShadowPrograms();
void UnbindShadowPrograms();
void UploadParameters( FBRenderOptions* pRenderOptions, FBArrayTemplate<FBLight*>* pAffectingLightList = NULL);
void UploadShadowParameters( int pLightIndex, FBRenderOptions* pRenderOptions );
void UnsetTextures();
void SaveFrameBuffer();
void RestoreFrameBuffer();
void RenderShadowModels( FBArrayTemplate<FBModel*>* pShadowCasters );
void RenderShadowModel( FBModel* pModel );
bool ComputeLightMatrices( FBLight* pLight, FBMatrix& pLightView, FBMatrix& pLightProj );
bool ComputeSpotLightMatrices( FBLight* pLight, FBMatrix& pLightView, FBMatrix& pLightProj );
bool ComputeInfiniteLightMatrices( FBLight* pLight, FBMatrix& pLightView, FBMatrix& pLightProj );
void ComputeWorldBounds();
void ComputeWorldBounds( FBModel* pModel, bool pRecursive = true );
void GetWorldBounds( FBLight* pLight, double& pRadius, FBVector4d& pPos );
private:
#define MAX_DRAW_BUFFERS 10
GLenum mDrawBuffers[MAX_DRAW_BUFFERS];
GLint mMaxDrawBuffers;
GLint mLastFramebuffer;
GLuint mFrameBuffer[kMaxLight];
GLuint mDepthRenderBuffer[kMaxLight];
GLuint mShadowMap[kMaxLight];
int mShadowMapSize[kMaxLight];
int mShadowPCFKernelSize;
float mShadowStrength;
CGprogram mShadowProjVertexShader;
CGparameter mParamShadowProjModel;
CGparameter mParamShadowProjViewProjection;
CGparameter mParamShadowMVP[kMaxLight];
CGparameter mParamShadowMap[kMaxLight];
CGparameter mParamShadowMapSize[kMaxLight];
CGparameter mParamShadowPCFKernelSize;
CGparameter mParamShadowStrength;
int mShadowCaster[kMaxLight];
FBMatrix mLightViewMatrix[kMaxLight];
FBMatrix mLightProjMatrix[kMaxLight];
FBVector4d mWorldMin;
FBVector4d mWorldMax;
bool mUseHardwarePCF;
};
}
#endif // SHADOWMAPPINGSHADER_H