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-2009 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 
00051 class _OgreRTSSExport NormalMapLighting : public SubRenderState
00052 {
00053 
00054 // Interface.
00055 public:
00057     NormalMapLighting();
00058 
00062     virtual const String&   getType                 () const;
00063 
00067     virtual int             getExecutionOrder       () const;
00068 
00072     virtual void            updateGpuProgramsParams (Renderable* rend, Pass* pass, const AutoParamDataSource* source, const LightList* pLightList);
00073 
00077     virtual void            copyFrom                (const SubRenderState& rhs);
00078 
00079 
00083     virtual bool            preAddToRenderState     (RenderState* renderState, Pass* srcPass, Pass* dstPass);
00084 
00088     void                    setTexCoordIndex        (unsigned int index) { mVSTexCoordSetIndex = index;}
00089 
00093     unsigned int            getTexCoordIndex        () const { return mVSTexCoordSetIndex; }
00094 
00095     // Type of this render state.
00096     static String Type;
00097 
00098     // Normal map space definition.
00099     enum NormalMapSpace
00100     {
00101         NMS_TANGENT,        // Normal map contains normal data in tangent space.
00102                             // This is the default normal mapping behavior and it requires that the
00103                             // target mesh will have valid tangents within its vertex data.
00104         
00105         NMS_OBJECT          // Normal map contains normal data in object local space.
00106                             // This normal mapping technique has the advantages of better visualization results,
00107                             // lack of artifacts that comes from texture mirroring usage, it doesn't requires tangent
00108                             // and it also saves some instruction in the vertex shader stage.
00109                             // The main drawback of using this kind of normal map is that the target object must be static
00110                             // in terms of local space rotations and translations.
00111     };
00112 
00116     void                    setNormalMapSpace           (NormalMapSpace normalMapSpace) { mNormalMapSpace = normalMapSpace; }
00117 
00119     NormalMapSpace          getNormalMapSpace           () const { return mNormalMapSpace; }
00120 
00124     void                    setNormalMapTextureName     (const String& textureName) { mNormalMapTextureName = textureName; }
00125 
00129     const String&           getNormalMapTextureName     () const { return mNormalMapTextureName; }
00130 
00131 
00132 // Protected types:
00133 protected:
00134     
00135     // Per light parameters.
00136     struct _OgreRTSSExport LightParams
00137     {
00138         Light::LightTypes       mType;              // Light type.      
00139         UniformParameterPtr     mPosition;          // Light position.
00140         ParameterPtr            mVSOutToLightDir;   // Vertex shader output vertex position to light position direction (texture space).
00141         ParameterPtr            mPSInToLightDir;    // Pixel shader input vertex position to light position direction (texture space).
00142         UniformParameterPtr     mDirection;         // Light direction.
00143         ParameterPtr            mVSOutDirection;    // Vertex shader output light direction (texture space).
00144         ParameterPtr            mPSInDirection;     // Pixel shader input light direction (texture space).      
00145         UniformParameterPtr     mAttenuatParams;    // Attenuation parameters.
00146         UniformParameterPtr     mSpotParams;        // Spot light parameters.
00147         UniformParameterPtr     mDiffuseColour;     // Diffuse colour.
00148         UniformParameterPtr     mSpecularColour;    // Specular colour.
00149 
00150     };
00151 
00152     typedef vector<LightParams>::type               LightParamsList;
00153     typedef LightParamsList::iterator               LightParamsIterator;
00154     typedef LightParamsList::const_iterator         LightParamsConstIterator;
00155 
00156 // Protected methods
00157 protected:
00158 
00164     void                    setTrackVertexColourType(TrackVertexColourType type) { mTrackVertexColourType = type; }
00165 
00169     TrackVertexColourType   getTrackVertexColourType() const { return mTrackVertexColourType; }
00170 
00171 
00176     void                    setLightCount           (const int lightCount[3]);
00177 
00182     void                    getLightCount           (int lightCount[3]) const;
00188     void                    setSpecularEnable       (bool enable) { mSpecularEnable = enable; }
00189 
00193     bool                    getSpecularEnable       () const      { return mSpecularEnable; }
00194 
00195 
00199     virtual bool            resolveParameters           (ProgramSet* programSet);
00200 
00202     bool                    resolveGlobalParameters     (ProgramSet* programSet);
00203 
00205     bool                    resolvePerLightParameters   (ProgramSet* programSet);
00206 
00210     virtual bool            resolveDependencies     (ProgramSet* programSet);
00211 
00215     virtual bool            addFunctionInvocations  (ProgramSet* programSet);
00216     
00217 
00221     bool            addVSInvocation                     (Function* vsMain, const int groupOrder, int& internalCounter);
00222 
00226     bool            addVSIlluminationInvocation         (LightParams* curLightParams, Function* vsMain, const int groupOrder, int& internalCounter);
00227 
00231     bool            addPSNormalFetchInvocation      (Function* psMain, const int groupOrder, int& internalCounter);
00232 
00233 
00237     bool            addPSGlobalIlluminationInvocation   (Function* psMain, const int groupOrder, int& internalCounter);
00238 
00242     bool            addPSIlluminationInvocation     (LightParams* curLightParams, Function* psMain, const int groupOrder, int& internalCounter);
00243 
00247     bool            addPSFinalAssignmentInvocation  (Function* psMain, const int groupOrder, int& internalCounter);
00248 
00249 
00250 // Attributes.
00251 protected:  
00252     String                  mNormalMapTextureName;          // The normal map texture name.
00253     TrackVertexColourType   mTrackVertexColourType;         // Track per vertex colour type.
00254     bool                    mSpecularEnable;                // Specular component enabled/disabled.
00255     LightParamsList         mLightParamsList;               // Light list.
00256     unsigned short          mNormalMapSamplerIndex;         // Normal map texture sampler index.
00257     unsigned int            mVSTexCoordSetIndex;            // Vertex shader input texture coordinate set index.
00258     NormalMapSpace          mNormalMapSpace;                // The normal map space.
00259     UniformParameterPtr     mWorldMatrix;                   // World matrix parameter.
00260     UniformParameterPtr     mWorldInvRotMatrix;             // World matrix inverse rotation matrix parameter.
00261     UniformParameterPtr     mCamPosWorldSpace;              // Camera position in world space parameter.    
00262     ParameterPtr            mVSInPosition;                  // Vertex shader input position parameter.
00263     ParameterPtr            mVSWorldPosition;               // Vertex shader world position parameter.
00264     ParameterPtr            mVSOutView;                     // Vertex shader output view vector (position in camera space) parameter.
00265     ParameterPtr            mPSInView;                      // Pixel shader input view position (position in camera space) parameter.
00266     ParameterPtr            mVSInNormal;                    // Vertex shader input normal.
00267     ParameterPtr            mVSInTangent;                   // Vertex shader input tangent.
00268     ParameterPtr            mVSTBNMatrix;                   // Vertex shader local TNB matrix.
00269     ParameterPtr            mVSLocalDir;                    // Vertex shader local light direction.
00270     UniformParameterPtr     mNormalMapSampler;              // Normal map texture sampler parameter.
00271     ParameterPtr            mPSNormal;                      // Pixel shader normal parameter.
00272     ParameterPtr            mVSInTexcoord;                  // Vertex shader input texture coordinates.
00273     ParameterPtr            mVSOutTexcoord;                 // Vertex shader output texture coordinates.
00274     ParameterPtr            mPSInTexcoord;                  // Pixel shader input texture coordinates.
00275     ParameterPtr            mPSTempDiffuseColour;           // Pixel shader temporary diffuse calculation parameter.
00276     ParameterPtr            mPSTempSpecularColour;          // Pixel shader temporary specular calculation parameter.
00277     ParameterPtr            mPSDiffuse;                     // Pixel shader input/local diffuse parameter.  
00278     ParameterPtr            mPSSpecular;                    // Pixel shader input/local specular parameter. 
00279     ParameterPtr            mPSOutDiffuse;                  // Pixel shader output diffuse parameter.   
00280     ParameterPtr            mPSOutSpecular;                 // Pixel shader output specular parameter.  
00281     UniformParameterPtr     mDerivedSceneColour;            // Derived scene colour parameter.
00282     UniformParameterPtr     mLightAmbientColour;            // Ambient light colour parameter.
00283     UniformParameterPtr     mDerivedAmbientLightColour;     // Derived ambient light colour parameter.
00284     UniformParameterPtr     mSurfaceAmbientColour;          // Surface ambient colour parameter.
00285     UniformParameterPtr     mSurfaceDiffuseColour;          // Surface diffuse colour parameter.
00286     UniformParameterPtr     mSurfaceSpecularColour;         // Surface specular colour parameter.
00287     UniformParameterPtr     mSurfaceEmissiveColour;         // Surface emissive colour parameter.
00288     UniformParameterPtr     mSurfaceShininess;              // Surface shininess parameter.
00289     static Light            msBlankLight;                   // Shared blank light.
00290 };
00291 
00292 
00297 class _OgreRTSSExport NormalMapLightingFactory : public SubRenderStateFactory
00298 {
00299 public:
00300 
00304     virtual const String&   getType             () const;
00305 
00309     virtual SubRenderState* createInstance      (ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass);
00310 
00314     virtual void            writeInstance       (MaterialSerializer* ser, SubRenderState* subRenderState, Pass* srcPass, Pass* dstPass);
00315 
00316     
00317 protected:
00318 
00322     virtual SubRenderState* createInstanceImpl  ();
00323 
00324 
00325 };
00326 
00330 }
00331 }
00332 
00333 #endif
00334 #endif
00335 

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Feb 28 21:10:41 2010