shaders/DynamicLightingShadow/DynamicLightingShadow_shader.cxx

shaders/DynamicLightingShadow/DynamicLightingShadow_shader.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.
***************************************************************************************/
// Class declaration
#include "DynamicLightingShadow_shader.h"
#include <math.h>
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
//--- Registration defines
#define DYNAMICLIGHTINGSHADOW__CLASS DYNAMICLIGHTING__CLASSNAME
#ifdef _MB_INTERNAL
#define DYNAMICLIGHTINGSHADOW__DESC "Dynamic Lighting & Shadow" // This is what shows up in the shader window ...
#else
#define DYNAMICLIGHTINGSHADOW__DESC "Dynamic Lighting & Shadow Plugin" // This is what shows up in the shader window ...
#endif
//--- FiLMBOX Registration & Implementation.
FBShaderImplementation( DYNAMICLIGHTINGSHADOW__CLASS );
FBRegisterShader (DYNAMICLIGHTINGSHADOW__DESCSTR, // Unique name
DYNAMICLIGHTINGSHADOW__CLASS, // Class
DYNAMICLIGHTINGSHADOW__DESCSTR, // Short description
DYNAMICLIGHTINGSHADOW__DESC, // Long description
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
Graphics::ShadowMappingShader* DynamicLightingShadow::mpLightShadowShader = NULL;
int DynamicLightingShadow::mpLightShadowShaderRefCount = 0;
/************************************************
* FiLMBOX Constructor.
************************************************/
bool DynamicLightingShadow::FBCreate()
{
mpLightShadowShaderRefCount++;
FBPropertyPublish( this, AffectingLights, "AffectingLights", NULL, NULL );
AffectingLights.SetFilter(FBLight::GetInternalClassId());
AffectingLights.SetSingleConnect(false);
FBPropertyPublish( this, Transparency, "Transparency", NULL, SetTransparencyProperty );
Transparency = kFBAlphaSourceNoAlpha;
FBRenderingPass pass = GetRenderingPassNeededForAlpha( Transparency );
RenderingPass = (FBRenderingPass)( pass );
FBPropertyPublish( this, TransparencyFactor, "TransparencyFactor", NULL, NULL );
TransparencyFactor.SetMinMax( 0.0, 1.0 );
TransparencyFactor = 1.0;
FBPropertyPublish( this, ShadowMapSize, "ShadowMapSize", NULL, NULL);
ShadowMapSize.SetMinMax( 128, 8192, true, false );
ShadowMapSize = 2048;
FBPropertyPublish( this, ShadowPCFKernelSize, "ShadowPCFKernelSize", NULL, NULL);
ShadowPCFKernelSize.SetMinMax( 1, 9, true, true );
ShadowPCFKernelSize = 9;
FBPropertyPublish( this, ShadowCasters, "ShadowCasters", NULL, NULL );
ShadowCasters.SetFilter(FBModel::GetInternalClassId());
ShadowCasters.SetSingleConnect(false);
FBPropertyPublish( this, ShadowStrength, "ShadowStrength", NULL, NULL );
ShadowStrength.SetMinMax(0.0, 1.0f, true, true );
ShadowStrength = 1.0f;
FBPropertyPublish( this, OffsetScale, "OffsetScale", NULL, NULL );
OffsetScale.SetMinMax( -10.0, 10.0 );
OffsetScale = 5.0;
FBPropertyPublish( this, OffsetBias, "OffsetBias", NULL, NULL );
OffsetBias.SetMinMax( -100000.0, 100000.0 );
OffsetBias = 0.0;
//Set up shader capacity. It seems cg2.0 has problem regarding INSTNCEID currently.
//SetShaderCapacity(FBShaderCapacity(kFBShaderCapacityMaterialEffect | kFBShaderCapacityDrawInstanced), true);
//
//Hook up the callback
SetDrawInstancedMaximumSize(kMaxDrawInstancedSize);
return true;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
void DynamicLightingShadow::FBDestroy()
{
// Delete lighting shader
mpLightShadowShaderRefCount--;
if (mpLightShadowShaderRefCount == 0)
{
delete mpLightShadowShader;
mpLightShadowShader = NULL;
}
ParentClass::FBDestroy();
}
/************************************************
* Shader functions.
************************************************/
void DynamicLightingShadow::ShaderPassTypeBegin ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
if ( !mpLightShadowShader )
{
// Setup the path to the shader ...
FBSystem lSystem;
FBString lVertexShader;
lVertexShader = lSystem.ApplicationPath ;
// And now do the pixel shader
FBString lPixelShader;
FBString lProjVertexShader;
lPixelShader = lSystem.ApplicationPath ;
lProjVertexShader = lSystem.ApplicationPath;
#ifdef _MB_INTERNAL
//
// This is use to build the native shader that ships with MotionBuidler and the system\Shaders versions of cg should never be change
//
lVertexShader += "/../system/shaders/LightingShadowMapVS.cg";
lPixelShader += "/../system/shaders/LightingShadowMapPS.cg";
lProjVertexShader += "/../system/shaders/LightingShadowProjVS.cg";
#else
//
// This is use when you build the Open Reality sample
//
lVertexShader += "/plugins/LightingShadowMapVS.cg";
lPixelShader += "/plugins/LightingShadowMapPS.cg";
lProjVertexShader += "/plugins/LightingShadowProjVS.cg";
#endif
// Create the lighting shader
mpLightShadowShader = new Graphics::ShadowMappingShader();
if( !mpLightShadowShader->Initialize(lVertexShader, lPixelShader, lProjVertexShader) )
{
return;
}
}
// Render shadow maps
FBArrayTemplate<FBLight*> lAffectingLights;
FBArrayTemplate<FBModel*> lShadowCasters;
GetAffectingLights( &lAffectingLights );
GetShadowCasters( &lShadowCasters );
mpLightShadowShader->RenderShadowMaps( ShadowMapSize, pRenderOptions, OffsetScale, OffsetBias, &lAffectingLights, &lShadowCasters );
// Set pixel shader shadow values
mpLightShadowShader->SetShadowPCFKernelSize( ShadowPCFKernelSize );
mpLightShadowShader->SetShadowStrength( ShadowStrength );
}
void DynamicLightingShadow::ShaderPassTypeEnd ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
}
void DynamicLightingShadow::ShaderPassInstanceBegin( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
FBArrayTemplate<FBLight*> lAffectingLights;
GetAffectingLights( &lAffectingLights );
mpLightShadowShader->BeginShading(pRenderOptions, &lAffectingLights);
}
void DynamicLightingShadow::ShaderPassInstanceEnd ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
mpLightShadowShader->EndShading();
}
void DynamicLightingShadow::ShaderPassMaterialBegin( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo)
{
mpLightShadowShader->SwitchMaterial(pRenderOptions, pInfo, pInfo->GetFBMaterial(), TransparencyFactor);
}
void DynamicLightingShadow::ShaderPassMaterialEnd ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo)
{
}
void DynamicLightingShadow::ShaderPassModelDraw ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo)
{
mpLightShadowShader->ShaderPassModelDraw(pRenderOptions, pPass, pInfo);
}
void DynamicLightingShadow::ShaderPassDrawShadowBegin( FBRenderOptions* pRenderOptions)
{
printf("In shadow pass\n" );
//Here we should setup the shader's draw shadow states
}
void DynamicLightingShadow::ShaderPassDrawShadowEnd( FBRenderOptions* pRenderOptions)
{
printf("End shadow pass\n" );
//Here we should clean the shader's draw shadow states
}
void DynamicLightingShadow::UploadModelViewMatrixArrayForDrawInstanced(const double* pModelViewMatrixArray, int pCount)
{
mpLightShadowShader->UploadModelViewMatrixArrayForDrawInstanced(pModelViewMatrixArray, pCount);
}
FBShaderModelInfo* DynamicLightingShadow::NewShaderModelInfo(HKModelRenderInfo pModelRenderInfo, int pSubRegionIndex)
{
FBShaderModelInfo* lShaderModelInfo = ParentClass::NewShaderModelInfo(pModelRenderInfo, pSubRegionIndex);
return lShaderModelInfo;
}
void DynamicLightingShadow::UpdateModelShaderInfo( FBRenderOptions* pOptions, FBShaderModelInfo* pModelRenderInfo )
{
pModelRenderInfo->UpdateModelShaderInfo(GetShaderVersion());
if (pModelRenderInfo->GetOriginalTextureFlag())
{
FBMaterial* lMaterial = pModelRenderInfo->GetFBMaterial();
if (lMaterial)
{
//FBTexture* lDiffuseTexture = pMaterial->GetTexture(kFBMaterialTextureDiffuse);
FBTexture* lNormalMapTexture = lMaterial->GetTexture(kFBMaterialTextureBump);
if (lNormalMapTexture == NULL)
lNormalMapTexture = lMaterial->GetTexture(kFBMaterialTextureNormalMap);
if (lNormalMapTexture)
}
}
//pModelRenderInfo->SetGeometryArrayIds(lVBOFormat);
pModelRenderInfo->SetGeometryArrayIds(pModelRenderInfo->GetGeometryArrayIds() | lVBOFormat);
}
void DynamicLightingShadow::SetTransparencyType( FBAlphaSource pTransparency )
{
if (Transparency != pTransparency)
{
Transparency = pTransparency;
//To trigger render to update the model-shader information.
InvalidateShaderVersion();
}
}
FBAlphaSource DynamicLightingShadow::GetTransparencyType()
{
return Transparency;
}
void DynamicLightingShadow::SetTransparencyProperty( HIObject pObject, FBAlphaSource pState )
{
DynamicLightingShadow* lShader = FBCast<DynamicLightingShadow>(pObject);
if (lShader->Transparency != pState)
{
lShader->Transparency.SetPropertyValue(pState);
FBRenderingPass pass = GetRenderingPassNeededForAlpha(pState);
lShader->RenderingPass = (FBRenderingPass)(pass);
// If shader use alpha and thus generate custom shape than the original geometry shape,
// we need to let it handle DrawShadow functiionality as well.
lShader->SetShaderCapacity(kFBShaderCapacityDrawShadow, true);
}
}
int DynamicLightingShadow::GetAffectingLights( FBArrayTemplate<FBLight*>* pLights )
{
if( pLights )
{
pLights->Clear();
}
int nLights = 0;
if( AffectingLights.GetCount() > 0 )
{
int nAffectingLights = min( AffectingLights.GetCount(), Graphics::ShadowMappingShader::kMaxLight ); // ACME-1814
for(int i = 0; i < nAffectingLights; ++i) // ACME-1814
{
FBLight* lFBLight = FBCast<FBLight>(AffectingLights.GetAt(i)->GetHIObject());
if( lFBLight )
{
if( pLights ) { pLights->Add(lFBLight); }
nLights++;
}
}
}
else
{
int nSceneLights = min( FBSystem::TheOne().Scene->Lights.GetCount(), Graphics::ShadowMappingShader::kMaxLight );
for( int i = 0; i < nSceneLights; ++i )
{
FBLight* lFBLight = FBSystem::TheOne().Scene->Lights[i];
if( lFBLight )
{
if( pLights ) { pLights->Add( lFBLight ); }
nLights++;
}
}
}
return nLights;
}
int DynamicLightingShadow::GetShadowCasters( FBArrayTemplate<FBModel*>* pShadowCasters )
{
if( pShadowCasters )
{
pShadowCasters->Clear();
}
int nShadowCasters = 0;
if( ShadowCasters.GetCount() > 0 )
{
for( int i = 0; i < ShadowCasters.GetCount(); i++ )
{
FBModel* lModel = FBCast<FBModel>(ShadowCasters.GetAt(i)->GetHIObject());
bool lLightCam = lModel->Is( FBCamera::TypeInfo ) || lModel->Is( FBLight::TypeInfo ) ;
if( lModel && !lLightCam )
{
if( pShadowCasters ) { pShadowCasters->Add( lModel ); }
nShadowCasters++;
}
}
}
else
{
FBRenderer* lRenderer = FBSystem().Renderer;
for( int i = 0; i < lRenderer->DisplayableGeometryCount; i++ )
{
FBModel* lModel = lRenderer->GetDisplayableGeometry(i);
if( lModel )
{
if( pShadowCasters ) { pShadowCasters->Add( lModel ); }
nShadowCasters++;
}
}
}
return nShadowCasters;
}