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::LightTypes       mType;              // Light type.      
00190         UniformParameterPtr     mPosition;          // Light position.
00191         ParameterPtr            mVSOutToLightDir;   // Vertex shader output vertex position to light position direction (texture space).
00192         ParameterPtr            mPSInToLightDir;    // Pixel shader input vertex position to light position direction (texture space).
00193         UniformParameterPtr     mDirection;         // Light direction.
00194         ParameterPtr            mVSOutDirection;    // Vertex shader output light direction (texture space).
00195         ParameterPtr            mPSInDirection;     // Pixel shader input light direction (texture space).      
00196         UniformParameterPtr     mAttenuatParams;    // Attenuation parameters.
00197         UniformParameterPtr     mSpotParams;        // Spot light parameters.
00198         UniformParameterPtr     mDiffuseColour;     // Diffuse colour.
00199         UniformParameterPtr     mSpecularColour;    // Specular colour.
00200 
00201     };
00202 
00203     typedef vector<LightParams>::type               LightParamsList;
00204     typedef LightParamsList::iterator               LightParamsIterator;
00205     typedef LightParamsList::const_iterator         LightParamsConstIterator;
00206 
00207 // Protected methods
00208 protected:
00209 
00215     void                    setTrackVertexColourType(TrackVertexColourType type) { mTrackVertexColourType = type; }
00216 
00220     TrackVertexColourType   getTrackVertexColourType() const { return mTrackVertexColourType; }
00221 
00222 
00227     void                    setLightCount           (const int lightCount[3]);
00228 
00233     void                    getLightCount           (int lightCount[3]) const;
00239     void                    setSpecularEnable       (bool enable) { mSpecularEnable = enable; }
00240 
00244     bool                    getSpecularEnable       () const      { return mSpecularEnable; }
00245 
00246 
00250     virtual bool            resolveParameters           (ProgramSet* programSet);
00251 
00253     bool                    resolveGlobalParameters     (ProgramSet* programSet);
00254 
00256     bool                    resolvePerLightParameters   (ProgramSet* programSet);
00257 
00261     virtual bool            resolveDependencies     (ProgramSet* programSet);
00262 
00266     virtual bool            addFunctionInvocations  (ProgramSet* programSet);
00267     
00268 
00272     bool            addVSInvocation                     (Function* vsMain, const int groupOrder, int& internalCounter);
00273 
00277     bool            addVSIlluminationInvocation         (LightParams* curLightParams, Function* vsMain, const int groupOrder, int& internalCounter);
00278 
00282     bool            addPSNormalFetchInvocation      (Function* psMain, const int groupOrder, int& internalCounter);
00283 
00284 
00288     bool            addPSGlobalIlluminationInvocation   (Function* psMain, const int groupOrder, int& internalCounter);
00289 
00293     bool            addPSIlluminationInvocation     (LightParams* curLightParams, Function* psMain, const int groupOrder, int& internalCounter);
00294 
00298     bool            addPSFinalAssignmentInvocation  (Function* psMain, const int groupOrder, int& internalCounter);
00299 
00300 
00301 // Attributes.
00302 protected:  
00303     String                  mNormalMapTextureName;          // The normal map texture name.
00304     TrackVertexColourType   mTrackVertexColourType;         // Track per vertex colour type.
00305     bool                    mSpecularEnable;                // Specular component enabled/disabled.
00306     LightParamsList         mLightParamsList;               // Light list.
00307     unsigned short          mNormalMapSamplerIndex;         // Normal map texture sampler index.
00308     unsigned int            mVSTexCoordSetIndex;            // Vertex shader input texture coordinate set index.
00309     FilterOptions           mNormalMapMinFilter;            // The normal map min filter.
00310     FilterOptions           mNormalMapMagFilter;            // The normal map mag filter.
00311     FilterOptions           mNormalMapMipFilter;            // The normal map mip filter.
00312     unsigned int            mNormalMapAnisotropy;           // The normal map max anisotropy value.
00313     Real                    mNormalMapMipBias;              // The normal map mip map bias.
00314     NormalMapSpace          mNormalMapSpace;                // The normal map space.
00315     UniformParameterPtr     mWorldMatrix;                   // World matrix parameter.
00316     UniformParameterPtr     mWorldInvRotMatrix;             // World matrix inverse rotation matrix parameter.
00317     UniformParameterPtr     mCamPosWorldSpace;              // Camera position in world space parameter.    
00318     ParameterPtr            mVSInPosition;                  // Vertex shader input position parameter.
00319     ParameterPtr            mVSWorldPosition;               // Vertex shader world position parameter.
00320     ParameterPtr            mVSOutView;                     // Vertex shader output view vector (position in camera space) parameter.
00321     ParameterPtr            mPSInView;                      // Pixel shader input view position (position in camera space) parameter.
00322     ParameterPtr            mVSInNormal;                    // Vertex shader input normal.
00323     ParameterPtr            mVSInTangent;                   // Vertex shader input tangent.
00324     ParameterPtr            mVSTBNMatrix;                   // Vertex shader local TNB matrix.
00325     ParameterPtr            mVSLocalDir;                    // Vertex shader local light direction.
00326     UniformParameterPtr     mNormalMapSampler;              // Normal map texture sampler parameter.
00327     ParameterPtr            mPSNormal;                      // Pixel shader normal parameter.
00328     ParameterPtr            mVSInTexcoord;                  // Vertex shader input texture coordinates.
00329     ParameterPtr            mVSOutTexcoord;                 // Vertex shader output texture coordinates.
00330     ParameterPtr            mPSInTexcoord;                  // Pixel shader input texture coordinates.
00331     ParameterPtr            mPSTempDiffuseColour;           // Pixel shader temporary diffuse calculation parameter.
00332     ParameterPtr            mPSTempSpecularColour;          // Pixel shader temporary specular calculation parameter.
00333     ParameterPtr            mPSDiffuse;                     // Pixel shader input/local diffuse parameter.  
00334     ParameterPtr            mPSSpecular;                    // Pixel shader input/local specular parameter. 
00335     ParameterPtr            mPSOutDiffuse;                  // Pixel shader output diffuse parameter.   
00336     ParameterPtr            mPSOutSpecular;                 // Pixel shader output specular parameter.  
00337     UniformParameterPtr     mDerivedSceneColour;            // Derived scene colour parameter.
00338     UniformParameterPtr     mLightAmbientColour;            // Ambient light colour parameter.
00339     UniformParameterPtr     mDerivedAmbientLightColour;     // Derived ambient light colour parameter.
00340     UniformParameterPtr     mSurfaceAmbientColour;          // Surface ambient colour parameter.
00341     UniformParameterPtr     mSurfaceDiffuseColour;          // Surface diffuse colour parameter.
00342     UniformParameterPtr     mSurfaceSpecularColour;         // Surface specular colour parameter.
00343     UniformParameterPtr     mSurfaceEmissiveColour;         // Surface emissive colour parameter.
00344     UniformParameterPtr     mSurfaceShininess;              // Surface shininess parameter.
00345     static Light            msBlankLight;                   // Shared blank light.
00346 };
00347 
00348 
00353 class _OgreRTSSExport NormalMapLightingFactory : public SubRenderStateFactory
00354 {
00355 public:
00356 
00360     virtual const String&   getType             () const;
00361 
00365     virtual SubRenderState* createInstance      (ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator);
00366 
00370     virtual void            writeInstance       (MaterialSerializer* ser, SubRenderState* subRenderState, Pass* srcPass, Pass* dstPass);
00371 
00372     
00373 protected:
00374 
00378     virtual SubRenderState* createInstanceImpl  ();
00379 
00380 
00381 };
00382 
00386 }
00387 }
00388 
00389 #endif
00390 #endif
00391 

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 Fri May 25 23:36:26 2012