OgreShaderExNormalMapLighting.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org
00006 
00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
00008 Permission is hereby granted, free of charge, to any person obtaining a copy
00009 of this software and associated documentation files (the "Software"), to deal
00010 in the Software without restriction, including without limitation the rights
00011 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 copies of the Software, and to permit persons to whom the Software is
00013 furnished to do so, subject to the following conditions:
00014 
00015 The above copyright notice and this permission notice shall be included in
00016 all copies or substantial portions of the Software.
00017 
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 THE SOFTWARE.
00025 -----------------------------------------------------------------------------
00026 */
00027 #ifndef _ShaderExNormalMapLighting_
00028 #define _ShaderExNormalMapLighting_
00029 
00030 #include "OgreShaderPrerequisites.h"
00031 #ifdef RTSHADER_SYSTEM_BUILD_EXT_SHADERS
00032 #include "OgreShaderParameter.h"
00033 #include "OgreShaderSubRenderState.h"
00034 #include "OgreVector4.h"
00035 #include "OgreLight.h"
00036 #include "OgreCommon.h"
00037 
00038 namespace Ogre {
00039 namespace RTShader {
00040 
00048 #define SGX_LIB_NORMALMAPLIGHTING                   "SGXLib_NormalMapLighting"
00049 #define SGX_FUNC_CONSTRUCT_TBNMATRIX                "SGX_ConstructTBNMatrix"
00050 #define SGX_FUNC_TRANSFORMNORMAL                    "SGX_TransformNormal"
00051 #define SGX_FUNC_TRANSFORMPOSITION                  "SGX_TransformPosition"
00052 #define SGX_FUNC_FETCHNORMAL                        "SGX_FetchNormal"
00053 #define SGX_FUNC_LIGHT_DIRECTIONAL_DIFFUSE          "SGX_Light_Directional_Diffuse"
00054 #define SGX_FUNC_LIGHT_DIRECTIONAL_DIFFUSESPECULAR  "SGX_Light_Directional_DiffuseSpecular"
00055 #define SGX_FUNC_LIGHT_POINT_DIFFUSE                "SGX_Light_Point_Diffuse"
00056 #define SGX_FUNC_LIGHT_POINT_DIFFUSESPECULAR        "SGX_Light_Point_DiffuseSpecular"
00057 #define SGX_FUNC_LIGHT_SPOT_DIFFUSE                 "SGX_Light_Spot_Diffuse"
00058 #define SGX_FUNC_LIGHT_SPOT_DIFFUSESPECULAR         "SGX_Light_Spot_DiffuseSpecular"
00059 
00063 class _OgreRTSSExport NormalMapLighting : public SubRenderState
00064 {
00065 
00066 // Interface.
00067 public:
00069     NormalMapLighting();
00070 
00074     virtual const String& getType() const;
00075 
00079     virtual int getExecutionOrder() const;
00080 
00084     virtual void updateGpuProgramsParams(Renderable* rend, Pass* pass, const AutoParamDataSource* source, const LightList* pLightList);
00085 
00089     virtual void copyFrom(const SubRenderState& rhs);
00090 
00091 
00095     virtual bool preAddToRenderState(const RenderState* renderState, Pass* srcPass, Pass* dstPass);
00096 
00100     void setTexCoordIndex(unsigned int index) { mVSTexCoordSetIndex = index;}
00101 
00105     unsigned int getTexCoordIndex() const { return mVSTexCoordSetIndex; }
00106 
00107     // Type of this render state.
00108     static String Type;
00109 
00110     // Normal map space definition.
00111     enum NormalMapSpace
00112     {
00113         NMS_TANGENT,        // Normal map contains normal data in tangent space.
00114                             // This is the default normal mapping behavior and it requires that the
00115                             // target mesh will have valid tangents within its vertex data.
00116         
00117         NMS_OBJECT          // Normal map contains normal data in object local space.
00118                             // This normal mapping technique has the advantages of better visualization results,
00119                             // lack of artifacts that comes from texture mirroring usage, it doesn't requires tangent
00120                             // and it also saves some instruction in the vertex shader stage.
00121                             // The main drawback of using this kind of normal map is that the target object must be static
00122                             // in terms of local space rotations and translations.
00123     };
00124 
00129     void setNormalMapSpace(NormalMapSpace normalMapSpace) { mNormalMapSpace = normalMapSpace; }
00130 
00132     NormalMapSpace getNormalMapSpace() const { return mNormalMapSpace; }
00133 
00137     void setNormalMapTextureName(const String& textureName) { mNormalMapTextureName = textureName; }
00138 
00142     const String& getNormalMapTextureName() const { return mNormalMapTextureName; }
00143 
00150     void setNormalMapFiltering(const FilterOptions minFilter, const FilterOptions magFilter, const FilterOptions mipFilter) 
00151     { mNormalMapMinFilter = minFilter; mNormalMapMagFilter = magFilter; mNormalMapMipFilter = mipFilter; }
00152 
00159     void getNormalMapFiltering(FilterOptions& minFilter, FilterOptions& magFilter, FilterOptions& mipFilter) const
00160     { minFilter = mNormalMapMinFilter; magFilter = mNormalMapMagFilter ; mipFilter = mNormalMapMipFilter; }
00161 
00165     void setNormalMapAnisotropy(unsigned int anisotropy) { mNormalMapAnisotropy = anisotropy; }
00166 
00167 
00169     unsigned int getNormalMapAnisotropy() const { return mNormalMapAnisotropy; }
00170 
00171     
00175     void setNormalMapMipBias(Real mipBias) { mNormalMapMipBias = mipBias; }
00176 
00177 
00179     Real getNormalMapMipBias() const { return mNormalMapMipBias; }
00180 
00181 
00182 
00183 // Protected types:
00184 protected:
00185     
00186     // Per light parameters.
00187     struct _OgreRTSSExport LightParams
00188     {
00189         // Light type.
00190         Light::LightTypes mType;
00191         // Light position.
00192         UniformParameterPtr mPosition;
00193         // Vertex shader output vertex position to light position direction (texture space).
00194         ParameterPtr mVSOutToLightDir;
00195         // Pixel shader input vertex position to light position direction (texture space).
00196         ParameterPtr mPSInToLightDir;
00197         // Light direction.
00198         UniformParameterPtr mDirection;
00199         // Vertex shader output light direction (texture space).
00200         ParameterPtr mVSOutDirection;
00201         // Pixel shader input light direction (texture space).      
00202         ParameterPtr mPSInDirection;
00203         // Attenuation parameters.
00204         UniformParameterPtr mAttenuatParams;
00205         // Spot light parameters.
00206         UniformParameterPtr mSpotParams;
00207         // Diffuse colour.
00208         UniformParameterPtr mDiffuseColour;
00209         // Specular colour.
00210         UniformParameterPtr mSpecularColour;
00211 
00212     };
00213 
00214     typedef vector<LightParams>::type LightParamsList;
00215     typedef LightParamsList::iterator LightParamsIterator;
00216     typedef LightParamsList::const_iterator LightParamsConstIterator;
00217 
00218 // Protected methods
00219 protected:
00220 
00226     void setTrackVertexColourType(TrackVertexColourType type) { mTrackVertexColourType = type; }
00227 
00231     TrackVertexColourType getTrackVertexColourType() const { return mTrackVertexColourType; }
00232 
00233 
00238     void setLightCount(const int lightCount[3]);
00239 
00244     void getLightCount(int lightCount[3]) const;
00250     void setSpecularEnable(bool enable) { mSpecularEnable = enable; }
00251 
00255     bool getSpecularEnable() const    { return mSpecularEnable; }
00256 
00257 
00261     virtual bool resolveParameters(ProgramSet* programSet);
00262 
00264     bool resolveGlobalParameters(ProgramSet* programSet);
00265 
00267     bool resolvePerLightParameters(ProgramSet* programSet);
00268 
00272     virtual bool resolveDependencies(ProgramSet* programSet);
00273 
00277     virtual bool addFunctionInvocations(ProgramSet* programSet);
00278     
00279 
00283     bool addVSInvocation(Function* vsMain, const int groupOrder, int& internalCounter);
00284 
00288     bool addVSIlluminationInvocation(LightParams* curLightParams, Function* vsMain, const int groupOrder, int& internalCounter);
00289 
00293     bool addPSNormalFetchInvocation(Function* psMain, const int groupOrder, int& internalCounter);
00294 
00295 
00299     bool addPSGlobalIlluminationInvocation(Function* psMain, const int groupOrder, int& internalCounter);
00300 
00304     bool addPSIlluminationInvocation(LightParams* curLightParams, Function* psMain, const int groupOrder, int& internalCounter);
00305 
00309     bool addPSFinalAssignmentInvocation(Function* psMain, const int groupOrder, int& internalCounter);
00310 
00311 
00312 // Attributes.
00313 protected:  
00314     // The normal map texture name.
00315     String mNormalMapTextureName;
00316     // Track per vertex colour type.
00317     TrackVertexColourType mTrackVertexColourType;
00318     // Specular component enabled/disabled.
00319     bool mSpecularEnable;
00320     // Light list.
00321     LightParamsList mLightParamsList;
00322     // Normal map texture sampler index.
00323     unsigned short mNormalMapSamplerIndex;
00324     // Vertex shader input texture coordinate set index.
00325     unsigned int mVSTexCoordSetIndex;
00326     // The normal map min filter.
00327     FilterOptions mNormalMapMinFilter;
00328     // The normal map mag filter.
00329     FilterOptions mNormalMapMagFilter;
00330     // The normal map mip filter.
00331     FilterOptions mNormalMapMipFilter;
00332     // The normal map max anisotropy value.
00333     unsigned int mNormalMapAnisotropy;
00334     // The normal map mip map bias.
00335     Real mNormalMapMipBias;
00336     // The normal map space.
00337     NormalMapSpace mNormalMapSpace;
00338     // World matrix parameter.
00339     UniformParameterPtr mWorldMatrix;
00340     // World matrix inverse rotation matrix parameter.
00341     UniformParameterPtr mWorldInvRotMatrix;
00342     // Camera position in world space parameter.    
00343     UniformParameterPtr mCamPosWorldSpace;
00344     // Vertex shader input position parameter.
00345     ParameterPtr mVSInPosition;
00346     // Vertex shader world position parameter.
00347     ParameterPtr mVSWorldPosition;
00348     // Vertex shader output view vector (position in camera space) parameter.
00349     ParameterPtr mVSOutView;
00350     // Pixel shader input view position (position in camera space) parameter.
00351     ParameterPtr mPSInView;
00352     // Vertex shader input normal.
00353     ParameterPtr mVSInNormal;
00354     // Vertex shader input tangent.
00355     ParameterPtr mVSInTangent;
00356     // Vertex shader local TNB matrix.
00357     ParameterPtr mVSTBNMatrix;
00358     // Vertex shader local light direction.
00359     ParameterPtr mVSLocalDir;
00360     // Normal map texture sampler parameter.
00361     UniformParameterPtr mNormalMapSampler;
00362     // Pixel shader normal parameter.
00363     ParameterPtr mPSNormal;
00364     // Vertex shader input texture coordinates.
00365     ParameterPtr mVSInTexcoord;
00366     // Vertex shader output texture coordinates.
00367     ParameterPtr mVSOutTexcoord;
00368     // Pixel shader input texture coordinates.
00369     ParameterPtr mPSInTexcoord;
00370     // Pixel shader temporary diffuse calculation parameter.
00371     ParameterPtr mPSTempDiffuseColour;
00372     // Pixel shader temporary specular calculation parameter.
00373     ParameterPtr mPSTempSpecularColour;
00374     // Pixel shader input/local diffuse parameter.  
00375     ParameterPtr mPSDiffuse;
00376     // Pixel shader input/local specular parameter. 
00377     ParameterPtr mPSSpecular;
00378     // Pixel shader output diffuse parameter.   
00379     ParameterPtr mPSOutDiffuse;
00380     // Pixel shader output specular parameter.  
00381     ParameterPtr mPSOutSpecular;
00382     // Derived scene colour parameter.
00383     UniformParameterPtr mDerivedSceneColour;
00384     // Ambient light colour parameter.
00385     UniformParameterPtr mLightAmbientColour;
00386     // Derived ambient light colour parameter.
00387     UniformParameterPtr mDerivedAmbientLightColour;
00388     // Surface ambient colour parameter.
00389     UniformParameterPtr mSurfaceAmbientColour;
00390     // Surface diffuse colour parameter.
00391     UniformParameterPtr mSurfaceDiffuseColour;
00392     // Surface specular colour parameter.
00393     UniformParameterPtr mSurfaceSpecularColour;
00394     // Surface emissive colour parameter.
00395     UniformParameterPtr mSurfaceEmissiveColour;
00396     // Surface shininess parameter.
00397     UniformParameterPtr mSurfaceShininess;
00398     // Shared blank light.
00399     static Light msBlankLight;
00400 };
00401 
00402 
00407 class _OgreRTSSExport NormalMapLightingFactory : public SubRenderStateFactory
00408 {
00409 public:
00410 
00414     virtual const String& getType() const;
00415 
00419     virtual SubRenderState* createInstance(ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator);
00420 
00424     virtual void writeInstance(MaterialSerializer* ser, SubRenderState* subRenderState, Pass* srcPass, Pass* dstPass);
00425 
00426     
00427 protected:
00428 
00432     virtual SubRenderState* createInstanceImpl();
00433 
00434 
00435 };
00436 
00440 }
00441 }
00442 
00443 #endif
00444 #endif
00445 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:23