shaders/ShadowMapping/ShadowMapping_shader.cxx

shaders/ShadowMapping/ShadowMapping_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 "ShadowMapping_shader.h"
#include <math.h>
//--- Registration defines
#define SHADOWMAPPING__CLASS SHADOWMAPPING__CLASSNAME
#define SHADOWMAPPING__DESC "Shadow Mapping Plugin" // This is what shows up in the shader window ...
//--- FiLMBOX Registration & Implementation.
FBShaderImplementation( SHADOWMAPPING__CLASS );
FBRegisterShader ( SHADOWMAPPING__DESCSTR, // Unique name
SHADOWMAPPING__CLASS, // Class
SHADOWMAPPING__DESCSTR, // Short description
SHADOWMAPPING__DESC, // Long description
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
bool ShadowMapping::mbShadowPass(false);
/************************************************
* FiLMBOX Constructor.
************************************************/
bool ShadowMapping::FBCreate()
{
// Initialize the graphics system
// Create the shadow map shader
mpShadowShader = NULL;
FBShader::RenderingPass = (FBRenderingPass)(kFBPassPreRender|kFBPassLighted);
return true;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
void ShadowMapping::FBDestroy()
{
// Delete lighting shader
delete mpShadowShader;
}
/************************************************
* Shader functions.
************************************************/
bool ShadowMapping::ShaderNeedBeginRender()
{
return true;
}
void ShadowMapping::ShaderBeginRender( FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo )
{
}
void ShadowMapping::ShadeModel( FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo, FBRenderingPass pPass )
{
if ( !mpShadowShader )
{
// Setup the path to the shader ...
FBSystem lSystem;
FBString lVertexShader;
lVertexShader = lSystem.ApplicationPath ;
// And now do the pixel shader
FBString lPixelShader;
lPixelShader = lSystem.ApplicationPath ;
FBString lProjVertexShader;
lProjVertexShader = 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\\ShadowMapVS.cg";
lPixelShader += "\\..\\system\\shaders\\ShadowMapPS.cg";
lProjVertexShader += "\\..\\system\\shaders\\ShadowProjVS.cg";
#else
//
// This is use when you build the Open Reality sample
//
lVertexShader += "\\plugins\\ShadowMapVS.cg";
lPixelShader += "\\plugins\\ShadowMapPS.cg";
lProjVertexShader += "\\plugins\\ShadowProjVS.cg";
#endif
// Create the lighting shader
mpShadowShader = new Graphics::ShadowMappingShader();
if( !mpShadowShader->Initialize(lVertexShader, lPixelShader, lProjVertexShader) )
{
return;
}
}
/***************************************************************************************
* Make sure everything in OpenGL is the same on exit as when it entered this function! *
***************************************************************************************/
if( pShaderModelInfo != NULL )
{
FBModelVertexData* lModelVertexData = pShaderModelInfo->GetFBModel()->ModelVertexData;
if (lModelVertexData)
{
const int lSubRegionCount = lModelVertexData->GetSubRegionCount();
if (lSubRegionCount)
{
// Set the actual shaders
mpShadowShader->BeginShading(pRenderOptions, NULL);
mpShadowShader->BeginShadowPass( pRenderOptions, pShaderModelInfo, pPass );
lModelVertexData->EnableOGLVertexData();
for (int lSubRegionIndex = 0; lSubRegionIndex < lSubRegionCount; lSubRegionIndex++)
{
FBMaterial* lMaterial = lModelVertexData->GetSubRegionMaterial(lSubRegionIndex);
mpShadowShader->SwitchMaterial(pRenderOptions, pShaderModelInfo, lMaterial, 1.0);
lModelVertexData->DrawSubRegion(lSubRegionIndex);
}
mpShadowShader->SwitchMaterial(pRenderOptions, pShaderModelInfo, NULL, 1.0);
lModelVertexData->DisableOGLVertexData();
mpShadowShader->EndShadowPass();
mpShadowShader->EndShading();
}
}
}
}