shaders/Common/Shader.h

shaders/Common/Shader.h
#ifndef SHADER_H
#define SHADER_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.
***************************************************************************************/
#include <Cg/cg.h>
#include <fbsdk.h>
#define kMaxDrawInstancedSize 100
namespace Graphics
{
class Shader
{
public:
// Max light supported by MB.
static const int kMaxLight = 8;
Shader();
virtual ~Shader();
// Call after contruction.
bool Initialize(const char* pVertexShaderPath, const char* pPixelShaderPath, const char* pVertexShaderOptions = NULL, const char* pPixelShaderOptions = NULL);
void BeginShading(FBRenderOptions* pRenderOptions, FBArrayTemplate<FBLight*>* pAffectingLightList = NULL);
void EndShading();
void UploadParameters( FBRenderOptions* pRenderOptions, FBArrayTemplate<FBLight*>* pAffectingLightList = NULL);
// Will be called when the rendering will change its material state.
void SwitchMaterial(FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo, FBMaterial* pMaterial, double pShaderTransparencyFactor);
// Upload ModelView Matrix Array for Draw Instanced.
void UploadModelViewMatrixArrayForDrawInstanced(const double* pModelViewMatrixArray, int pCount);
// setup per model parameters.
void ShaderPassModelDraw ( FBRenderOptions* pRenderOptions, FBRenderingPass pPass, FBShaderModelInfo* pInfo);
protected:
static void ShowError(const char* pText);
// Call those two functions around rendering.
virtual void BindShaderPrograms();
virtual void UnbindShaderPrograms();
virtual void UnsetTextures();
// Methods to create shaders based on file input.
CGprogram CreateShader(CGprofile pProfile, const char* pPath, const char** pArgs);
bool CreateVertexShader( const char* pPath, const char* pOptions );
bool CreatePixelShader( const char* pPath, const char* pOptions );
// Global context stuff.
CGcontext mContext;
CGprofile mVertexProfile;
CGprofile mPixelProfile;
CGprogram mVertexShader;
CGprogram mPixelShader;
// Parameters needed to upload data to cg shaders
CGbuffer mParamModelViewArrayBuffer;
int mParamModelViewArrayBufferOffset;
CGparameter mParamMatEmissive;
CGparameter mParamMatAmbient;
CGparameter mParamMatDiffuse;
CGparameter mParamMatSpecular;
CGparameter mParamLightAmbient;
CGparameter mParamLightCount;
CGparameter mParamLightPositions[kMaxLight];
CGparameter mParamLightDirections[kMaxLight];
CGparameter mParamLightColours[kMaxLight];
CGparameter mParamLightAttenuations[kMaxLight];
CGparameter mParamColorTexture;
CGparameter mParamColorTextureMatrix;
CGparameter mParamColorTextureValidVS;
CGparameter mParamColorTextureValidPS;
CGparameter mParamNormalMap;
CGparameter mParamNormalMapMatrix;
CGparameter mParamNormalMapValidVS;
CGparameter mParamNormalMapValidPS;
CGparameter mParamIDBufferRender;
float mAlpha;
private:
Shader(const Shader&);
Shader& operator = (const Shader&);
};
}
#endif // SHADER_H