shaders/DynamicLighting/DynamicLighting_shader.cxx

shaders/DynamicLighting/DynamicLighting_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 "DynamicLighting_shader.h"
#include <math.h>
//--- Registration defines
#define DYNAMICLIGHTING__CLASS DYNAMICLIGHTING__CLASSNAME
#ifdef _MB_INTERNAL
#define DYNAMICLIGHTING__DESC "Dynamic Lighting" // This is what shows up in the shader window ...
#else
#define DYNAMICLIGHTING__DESC "Dynamic Lighting Plugin" // This is what shows up in the shader window ...
#endif
//--- FiLMBOX Registration & Implementation.
FBShaderImplementation( DYNAMICLIGHTING__CLASS );
FBRegisterShader (DYNAMICLIGHTING__DESCSTR, // Unique name
DYNAMICLIGHTING__CLASS, // Class
DYNAMICLIGHTING__DESCSTR, // Short description
DYNAMICLIGHTING__DESC, // Long description
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
Graphics::Shader* DynamicLighting::mpLightShader = NULL;
int DynamicLighting::mpLightShaderRefCount = 0;
/************************************************
* FiLMBOX Constructor.
************************************************/
bool DynamicLighting::FBCreate()
{
mpLightShaderRefCount++;
FBPropertyPublish( this, AffectingLights, "AffectingLights", NULL, NULL);
AffectingLights.SetFilter(FBLight::GetInternalClassId());
AffectingLights.SetSingleConnect(false);
FBPropertyPublish( this, Transparency, "Transparency", NULL, SetTransparencyProperty);
Transparency = kFBAlphaSourceNoAlpha;
RenderingPass = GetRenderingPassNeededForAlpha(Transparency);
FBPropertyPublish( this, TransparencyFactor, "TransparencyFactor",NULL, NULL);
TransparencyFactor.SetMinMax(0.0, 1.0);
TransparencyFactor = 1.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 DynamicLighting::FBDestroy()
{
// Delete lighting shader
mpLightShaderRefCount--;
if (mpLightShaderRefCount == 0)
{
delete mpLightShader;
mpLightShader = NULL;
}
ParentClass::FBDestroy();
}
/************************************************
* Shader functions.
************************************************/
void DynamicLighting::ShaderPassTypeBegin ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
if ( !mpLightShader )
{
// Setup the path to the shader ...
FBSystem lSystem;
FBString lVertexShader;
lVertexShader = lSystem.ApplicationPath ;
// And now do the pixel shader
FBString lPixelShader;
lPixelShader = lSystem.ApplicationPath ;
#ifdef _MB_INTERNAL
//
// This is use to build the native shader that shipe with MotionBuidler and the system\Shaders versions of cg should never be change
//
lVertexShader += "/../system/shaders/LightingVS.cg";
lPixelShader += "/../system/shaders/LightingPS.cg";
#else
//
// This is use when you build the Open Reality sample
//
lVertexShader += "/plugins/LightingVS.cg";
lPixelShader += "/plugins/LightingPS.cg";
#endif
// Create the lighting shader
mpLightShader = new Graphics::Shader();
if( !mpLightShader->Initialize(lVertexShader, lPixelShader) )
{
return;
}
}
}
void DynamicLighting::ShaderPassTypeEnd ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
}
void DynamicLighting::ShaderPassInstanceBegin( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
// Set the actual shaders
FBArrayTemplate<FBLight*> lAffectingLights;
for(int i = 0; i < AffectingLights.GetCount(); ++i)
{
FBLight* lFBLight = FBCast<FBLight>(AffectingLights.GetAt(i)->GetHIObject());
if (lFBLight)
lAffectingLights.Add(lFBLight);
}
mpLightShader->BeginShading(pRenderOptions, &lAffectingLights);
}
void DynamicLighting::ShaderPassInstanceEnd ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass)
{
mpLightShader->EndShading();
}
void DynamicLighting::ShaderPassMaterialBegin( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo)
{
mpLightShader->SwitchMaterial(pRenderOptions, pInfo, pInfo->GetFBMaterial(), TransparencyFactor);
}
void DynamicLighting::ShaderPassMaterialEnd ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo)
{
}
void DynamicLighting::ShaderPassModelDraw ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo)
{
mpLightShader->ShaderPassModelDraw(pRenderOptions, pPass, pInfo);
}
void DynamicLighting::ShaderPassDrawShadowBegin( FBRenderOptions* pRenderOptions)
{
//Here we should setup the shader's draw shadow states
}
void DynamicLighting::ShaderPassDrawShadowEnd( FBRenderOptions* pRenderOptions)
{
//Here we should clean the shader's draw shadow states
}
void DynamicLighting::UploadModelViewMatrixArrayForDrawInstanced(const double* pModelViewMatrixArray, int pCount)
{
mpLightShader->UploadModelViewMatrixArrayForDrawInstanced(pModelViewMatrixArray, pCount);
}
FBShaderModelInfo* DynamicLighting::NewShaderModelInfo(HKModelRenderInfo pModelRenderInfo, int pSubRegionIndex)
{
FBShaderModelInfo* lShaderModelInfo = ParentClass::NewShaderModelInfo(pModelRenderInfo, pSubRegionIndex);
return lShaderModelInfo;
}
void DynamicLighting::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);
}
void DynamicLighting::SetTransparencyType( FBAlphaSource pTransparency )
{
if (Transparency != pTransparency)
{
Transparency = pTransparency;
//To trigger render to update the model-shader information.
InvalidateShaderVersion();
}
}
FBAlphaSource DynamicLighting::GetTransparencyType()
{
return Transparency;
}
void DynamicLighting::SetTransparencyProperty( HIObject pObject, FBAlphaSource pState )
{
DynamicLighting* lShader = FBCast<DynamicLighting>(pObject);
if (lShader->Transparency != pState)
{
lShader->Transparency.SetPropertyValue(pState);
lShader->RenderingPass = GetRenderingPassNeededForAlpha(pState);
// 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, pState != kFBAlphaSourceNoAlpha);
}
}