OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreShaderExNormalMapLighting.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 -----------------------------------------------------------------------------
26 */
27 #ifndef _ShaderExNormalMapLighting_
28 #define _ShaderExNormalMapLighting_
29 
31 #ifdef RTSHADER_SYSTEM_BUILD_EXT_SHADERS
33 #include "OgreLight.h"
34 #include "OgreCommon.h"
35 
36 namespace Ogre {
37 namespace RTShader {
38 
46 #define SGX_LIB_NORMALMAPLIGHTING "SGXLib_NormalMapLighting"
47 #define SGX_FUNC_CONSTRUCT_TBNMATRIX "SGX_ConstructTBNMatrix"
48 #define SGX_FUNC_TRANSFORMNORMAL "SGX_TransformNormal"
49 #define SGX_FUNC_TRANSFORMPOSITION "SGX_TransformPosition"
50 #define SGX_FUNC_FETCHNORMAL "SGX_FetchNormal"
51 #define SGX_FUNC_LIGHT_DIRECTIONAL_DIFFUSE "SGX_Light_Directional_Diffuse"
52 #define SGX_FUNC_LIGHT_DIRECTIONAL_DIFFUSESPECULAR "SGX_Light_Directional_DiffuseSpecular"
53 #define SGX_FUNC_LIGHT_POINT_DIFFUSE "SGX_Light_Point_Diffuse"
54 #define SGX_FUNC_LIGHT_POINT_DIFFUSESPECULAR "SGX_Light_Point_DiffuseSpecular"
55 #define SGX_FUNC_LIGHT_SPOT_DIFFUSE "SGX_Light_Spot_Diffuse"
56 #define SGX_FUNC_LIGHT_SPOT_DIFFUSESPECULAR "SGX_Light_Spot_DiffuseSpecular"
57 
61 class _OgreRTSSExport NormalMapLighting : public SubRenderState
62 {
63 
64 // Interface.
65 public:
67  NormalMapLighting();
68 
72  virtual const String& getType() const;
73 
77  virtual int getExecutionOrder() const;
78 
82  virtual void updateGpuProgramsParams(Renderable* rend, Pass* pass, const AutoParamDataSource* source, const LightList* pLightList);
83 
87  virtual void copyFrom(const SubRenderState& rhs);
88 
89 
93  virtual bool preAddToRenderState(const RenderState* renderState, Pass* srcPass, Pass* dstPass);
94 
98  void setTexCoordIndex(unsigned int index) { mVSTexCoordSetIndex = index;}
99 
103  unsigned int getTexCoordIndex() const { return mVSTexCoordSetIndex; }
104 
105  // Type of this render state.
106  static String Type;
107 
108  // Normal map space definition.
109  enum NormalMapSpace
110  {
111  NMS_TANGENT, // Normal map contains normal data in tangent space.
112  // This is the default normal mapping behavior and it requires that the
113  // target mesh will have valid tangents within its vertex data.
114 
115  NMS_OBJECT // Normal map contains normal data in object local space.
116  // This normal mapping technique has the advantages of better visualization results,
117  // lack of artifacts that comes from texture mirroring usage, it doesn't requires tangent
118  // and it also saves some instruction in the vertex shader stage.
119  // The main drawback of using this kind of normal map is that the target object must be static
120  // in terms of local space rotations and translations.
121  };
122 
127  void setNormalMapSpace(NormalMapSpace normalMapSpace) { mNormalMapSpace = normalMapSpace; }
128 
130  NormalMapSpace getNormalMapSpace() const { return mNormalMapSpace; }
131 
135  void setNormalMapTextureName(const String& textureName) { mNormalMapTextureName = textureName; }
136 
140  const String& getNormalMapTextureName() const { return mNormalMapTextureName; }
141 
148  void setNormalMapFiltering(const FilterOptions minFilter, const FilterOptions magFilter, const FilterOptions mipFilter)
149  { mNormalMapMinFilter = minFilter; mNormalMapMagFilter = magFilter; mNormalMapMipFilter = mipFilter; }
150 
157  void getNormalMapFiltering(FilterOptions& minFilter, FilterOptions& magFilter, FilterOptions& mipFilter) const
158  { minFilter = mNormalMapMinFilter; magFilter = mNormalMapMagFilter ; mipFilter = mNormalMapMipFilter; }
159 
163  void setNormalMapAnisotropy(unsigned int anisotropy) { mNormalMapAnisotropy = anisotropy; }
164 
165 
167  unsigned int getNormalMapAnisotropy() const { return mNormalMapAnisotropy; }
168 
169 
173  void setNormalMapMipBias(Real mipBias) { mNormalMapMipBias = mipBias; }
174 
175 
177  Real getNormalMapMipBias() const { return mNormalMapMipBias; }
178 
179 
180 
181 // Protected types:
182 protected:
183 
184  // Per light parameters.
185  struct _OgreRTSSExport LightParams
186  {
187  // Light type.
188  Light::LightTypes mType;
189  // Light position.
190  UniformParameterPtr mPosition;
191  // Vertex shader output vertex position to light position direction (texture space).
192  ParameterPtr mVSOutToLightDir;
193  // Pixel shader input vertex position to light position direction (texture space).
194  ParameterPtr mPSInToLightDir;
195  // Light direction.
196  UniformParameterPtr mDirection;
197  // Vertex shader output light direction (texture space).
198  ParameterPtr mVSOutDirection;
199  // Pixel shader input light direction (texture space).
200  ParameterPtr mPSInDirection;
201  // Attenuation parameters.
202  UniformParameterPtr mAttenuatParams;
203  // Spot light parameters.
204  UniformParameterPtr mSpotParams;
205  // Diffuse colour.
206  UniformParameterPtr mDiffuseColour;
207  // Specular colour.
208  UniformParameterPtr mSpecularColour;
209 
210  };
211 
212  typedef vector<LightParams>::type LightParamsList;
213  typedef LightParamsList::iterator LightParamsIterator;
214  typedef LightParamsList::const_iterator LightParamsConstIterator;
215 
216 // Protected methods
217 protected:
218 
224  void setTrackVertexColourType(TrackVertexColourType type) { mTrackVertexColourType = type; }
225 
229  TrackVertexColourType getTrackVertexColourType() const { return mTrackVertexColourType; }
230 
231 
236  void setLightCount(const int lightCount[3]);
237 
242  void getLightCount(int lightCount[3]) const;
248  void setSpecularEnable(bool enable) { mSpecularEnable = enable; }
249 
253  bool getSpecularEnable() const { return mSpecularEnable; }
254 
255 
259  virtual bool resolveParameters(ProgramSet* programSet);
260 
262  bool resolveGlobalParameters(ProgramSet* programSet);
263 
265  bool resolvePerLightParameters(ProgramSet* programSet);
266 
270  virtual bool resolveDependencies(ProgramSet* programSet);
271 
275  virtual bool addFunctionInvocations(ProgramSet* programSet);
276 
277 
281  bool addVSInvocation(Function* vsMain, const int groupOrder, int& internalCounter);
282 
286  bool addVSIlluminationInvocation(LightParams* curLightParams, Function* vsMain, const int groupOrder, int& internalCounter);
287 
291  bool addPSNormalFetchInvocation(Function* psMain, const int groupOrder, int& internalCounter);
292 
293 
297  bool addPSGlobalIlluminationInvocation(Function* psMain, const int groupOrder, int& internalCounter);
298 
302  bool addPSIlluminationInvocation(LightParams* curLightParams, Function* psMain, const int groupOrder, int& internalCounter);
303 
307  bool addPSFinalAssignmentInvocation(Function* psMain, const int groupOrder, int& internalCounter);
308 
309 
310 // Attributes.
311 protected:
312  // The normal map texture name.
313  String mNormalMapTextureName;
314  // Track per vertex colour type.
315  TrackVertexColourType mTrackVertexColourType;
316  // Specular component enabled/disabled.
317  bool mSpecularEnable;
318  // Light list.
319  LightParamsList mLightParamsList;
320  // Normal map texture sampler index.
321  unsigned short mNormalMapSamplerIndex;
322  // Vertex shader input texture coordinate set index.
323  unsigned int mVSTexCoordSetIndex;
324  // The normal map min filter.
325  FilterOptions mNormalMapMinFilter;
326  // The normal map mag filter.
327  FilterOptions mNormalMapMagFilter;
328  // The normal map mip filter.
329  FilterOptions mNormalMapMipFilter;
330  // The normal map max anisotropy value.
331  unsigned int mNormalMapAnisotropy;
332  // The normal map mip map bias.
333  Real mNormalMapMipBias;
334  // The normal map space.
335  NormalMapSpace mNormalMapSpace;
336  // World matrix parameter.
337  UniformParameterPtr mWorldMatrix;
338  // World matrix inverse rotation matrix parameter.
339  UniformParameterPtr mWorldInvRotMatrix;
340  // Camera position in world space parameter.
341  UniformParameterPtr mCamPosWorldSpace;
342  // Vertex shader input position parameter.
343  ParameterPtr mVSInPosition;
344  // Vertex shader world position parameter.
345  ParameterPtr mVSWorldPosition;
346  // Vertex shader output view vector (position in camera space) parameter.
347  ParameterPtr mVSOutView;
348  // Pixel shader input view position (position in camera space) parameter.
349  ParameterPtr mPSInView;
350  // Vertex shader input normal.
351  ParameterPtr mVSInNormal;
352  // Vertex shader input tangent.
353  ParameterPtr mVSInTangent;
354  // Vertex shader local TNB matrix.
355  ParameterPtr mVSTBNMatrix;
356  // Vertex shader local light direction.
357  ParameterPtr mVSLocalDir;
358  // Normal map texture sampler parameter.
359  UniformParameterPtr mNormalMapSampler;
360  // Pixel shader normal parameter.
361  ParameterPtr mPSNormal;
362  // Vertex shader input texture coordinates.
363  ParameterPtr mVSInTexcoord;
364  // Vertex shader output texture coordinates.
365  ParameterPtr mVSOutTexcoord;
366  // Pixel shader input texture coordinates.
367  ParameterPtr mPSInTexcoord;
368  // Pixel shader temporary diffuse calculation parameter.
369  ParameterPtr mPSTempDiffuseColour;
370  // Pixel shader temporary specular calculation parameter.
371  ParameterPtr mPSTempSpecularColour;
372  // Pixel shader input/local diffuse parameter.
373  ParameterPtr mPSDiffuse;
374  // Pixel shader input/local specular parameter.
375  ParameterPtr mPSSpecular;
376  // Pixel shader output diffuse parameter.
377  ParameterPtr mPSOutDiffuse;
378  // Pixel shader output specular parameter.
379  ParameterPtr mPSOutSpecular;
380  // Derived scene colour parameter.
381  UniformParameterPtr mDerivedSceneColour;
382  // Ambient light colour parameter.
383  UniformParameterPtr mLightAmbientColour;
384  // Derived ambient light colour parameter.
385  UniformParameterPtr mDerivedAmbientLightColour;
386  // Surface ambient colour parameter.
387  UniformParameterPtr mSurfaceAmbientColour;
388  // Surface diffuse colour parameter.
389  UniformParameterPtr mSurfaceDiffuseColour;
390  // Surface specular colour parameter.
391  UniformParameterPtr mSurfaceSpecularColour;
392  // Surface emissive colour parameter.
393  UniformParameterPtr mSurfaceEmissiveColour;
394  // Surface shininess parameter.
395  UniformParameterPtr mSurfaceShininess;
396 };
397 
398 
403 class _OgreRTSSExport NormalMapLightingFactory : public SubRenderStateFactory
404 {
405 public:
406 
410  virtual const String& getType() const;
411 
415  virtual SubRenderState* createInstance(ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator);
416 
420  virtual void writeInstance(MaterialSerializer* ser, SubRenderState* subRenderState, Pass* srcPass, Pass* dstPass);
421 
422 
423 protected:
424 
428  virtual SubRenderState* createInstanceImpl();
429 
430 
431 };
432 
436 }
437 }
438 
439 #endif
440 #endif
441 
float Real
Software floating point type.
LightTypes
Defines the type of light.
Definition: OgreLight.h:79
#define _OgreRTSSExport
SharedPtr< Parameter > ParameterPtr
HashedVector< LightClosest > LightList
Definition: OgreCommon.h:569
SharedPtr< UniformParameter > UniformParameterPtr
_StringBase String
Definition: OgreCommon.h:53
FilterOptions
Filtering options for textures / mipmaps.
Definition: OgreCommon.h:131
int TrackVertexColourType
An enumeration describing which material properties should track the vertex colours.
Definition: OgreCommon.h:221