OGRE  1.9
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
32 #include "OgreShaderParameter.h"
34 #include "OgreVector4.h"
35 #include "OgreLight.h"
36 #include "OgreCommon.h"
37 
38 namespace Ogre {
39 namespace RTShader {
40 
48 #define SGX_LIB_NORMALMAPLIGHTING "SGXLib_NormalMapLighting"
49 #define SGX_FUNC_CONSTRUCT_TBNMATRIX "SGX_ConstructTBNMatrix"
50 #define SGX_FUNC_TRANSFORMNORMAL "SGX_TransformNormal"
51 #define SGX_FUNC_TRANSFORMPOSITION "SGX_TransformPosition"
52 #define SGX_FUNC_FETCHNORMAL "SGX_FetchNormal"
53 #define SGX_FUNC_LIGHT_DIRECTIONAL_DIFFUSE "SGX_Light_Directional_Diffuse"
54 #define SGX_FUNC_LIGHT_DIRECTIONAL_DIFFUSESPECULAR "SGX_Light_Directional_DiffuseSpecular"
55 #define SGX_FUNC_LIGHT_POINT_DIFFUSE "SGX_Light_Point_Diffuse"
56 #define SGX_FUNC_LIGHT_POINT_DIFFUSESPECULAR "SGX_Light_Point_DiffuseSpecular"
57 #define SGX_FUNC_LIGHT_SPOT_DIFFUSE "SGX_Light_Spot_Diffuse"
58 #define SGX_FUNC_LIGHT_SPOT_DIFFUSESPECULAR "SGX_Light_Spot_DiffuseSpecular"
59 
63 class _OgreRTSSExport NormalMapLighting : public SubRenderState
64 {
65 
66 // Interface.
67 public:
69  NormalMapLighting();
70 
74  virtual const String& getType() const;
75 
79  virtual int getExecutionOrder() const;
80 
84  virtual void updateGpuProgramsParams(Renderable* rend, Pass* pass, const AutoParamDataSource* source, const LightList* pLightList);
85 
89  virtual void copyFrom(const SubRenderState& rhs);
90 
91 
95  virtual bool preAddToRenderState(const RenderState* renderState, Pass* srcPass, Pass* dstPass);
96 
100  void setTexCoordIndex(unsigned int index) { mVSTexCoordSetIndex = index;}
101 
105  unsigned int getTexCoordIndex() const { return mVSTexCoordSetIndex; }
106 
107  // Type of this render state.
108  static String Type;
109 
110  // Normal map space definition.
111  enum NormalMapSpace
112  {
113  NMS_TANGENT, // Normal map contains normal data in tangent space.
114  // This is the default normal mapping behavior and it requires that the
115  // target mesh will have valid tangents within its vertex data.
116 
117  NMS_OBJECT // Normal map contains normal data in object local space.
118  // This normal mapping technique has the advantages of better visualization results,
119  // lack of artifacts that comes from texture mirroring usage, it doesn't requires tangent
120  // and it also saves some instruction in the vertex shader stage.
121  // The main drawback of using this kind of normal map is that the target object must be static
122  // in terms of local space rotations and translations.
123  };
124 
129  void setNormalMapSpace(NormalMapSpace normalMapSpace) { mNormalMapSpace = normalMapSpace; }
130 
132  NormalMapSpace getNormalMapSpace() const { return mNormalMapSpace; }
133 
137  void setNormalMapTextureName(const String& textureName) { mNormalMapTextureName = textureName; }
138 
142  const String& getNormalMapTextureName() const { return mNormalMapTextureName; }
143 
150  void setNormalMapFiltering(const FilterOptions minFilter, const FilterOptions magFilter, const FilterOptions mipFilter)
151  { mNormalMapMinFilter = minFilter; mNormalMapMagFilter = magFilter; mNormalMapMipFilter = mipFilter; }
152 
159  void getNormalMapFiltering(FilterOptions& minFilter, FilterOptions& magFilter, FilterOptions& mipFilter) const
160  { minFilter = mNormalMapMinFilter; magFilter = mNormalMapMagFilter ; mipFilter = mNormalMapMipFilter; }
161 
165  void setNormalMapAnisotropy(unsigned int anisotropy) { mNormalMapAnisotropy = anisotropy; }
166 
167 
169  unsigned int getNormalMapAnisotropy() const { return mNormalMapAnisotropy; }
170 
171 
175  void setNormalMapMipBias(Real mipBias) { mNormalMapMipBias = mipBias; }
176 
177 
179  Real getNormalMapMipBias() const { return mNormalMapMipBias; }
180 
181 
182 
183 // Protected types:
184 protected:
185 
186  // Per light parameters.
187  struct _OgreRTSSExport LightParams
188  {
189  // Light type.
190  Light::LightTypes mType;
191  // Light position.
192  UniformParameterPtr mPosition;
193  // Vertex shader output vertex position to light position direction (texture space).
194  ParameterPtr mVSOutToLightDir;
195  // Pixel shader input vertex position to light position direction (texture space).
196  ParameterPtr mPSInToLightDir;
197  // Light direction.
198  UniformParameterPtr mDirection;
199  // Vertex shader output light direction (texture space).
200  ParameterPtr mVSOutDirection;
201  // Pixel shader input light direction (texture space).
202  ParameterPtr mPSInDirection;
203  // Attenuation parameters.
204  UniformParameterPtr mAttenuatParams;
205  // Spot light parameters.
206  UniformParameterPtr mSpotParams;
207  // Diffuse colour.
208  UniformParameterPtr mDiffuseColour;
209  // Specular colour.
210  UniformParameterPtr mSpecularColour;
211 
212  };
213 
214  typedef vector<LightParams>::type LightParamsList;
215  typedef LightParamsList::iterator LightParamsIterator;
216  typedef LightParamsList::const_iterator LightParamsConstIterator;
217 
218 // Protected methods
219 protected:
220 
226  void setTrackVertexColourType(TrackVertexColourType type) { mTrackVertexColourType = type; }
227 
231  TrackVertexColourType getTrackVertexColourType() const { return mTrackVertexColourType; }
232 
233 
238  void setLightCount(const int lightCount[3]);
239 
244  void getLightCount(int lightCount[3]) const;
250  void setSpecularEnable(bool enable) { mSpecularEnable = enable; }
251 
255  bool getSpecularEnable() const { return mSpecularEnable; }
256 
257 
261  virtual bool resolveParameters(ProgramSet* programSet);
262 
264  bool resolveGlobalParameters(ProgramSet* programSet);
265 
267  bool resolvePerLightParameters(ProgramSet* programSet);
268 
272  virtual bool resolveDependencies(ProgramSet* programSet);
273 
277  virtual bool addFunctionInvocations(ProgramSet* programSet);
278 
279 
283  bool addVSInvocation(Function* vsMain, const int groupOrder, int& internalCounter);
284 
288  bool addVSIlluminationInvocation(LightParams* curLightParams, Function* vsMain, const int groupOrder, int& internalCounter);
289 
293  bool addPSNormalFetchInvocation(Function* psMain, const int groupOrder, int& internalCounter);
294 
295 
299  bool addPSGlobalIlluminationInvocation(Function* psMain, const int groupOrder, int& internalCounter);
300 
304  bool addPSIlluminationInvocation(LightParams* curLightParams, Function* psMain, const int groupOrder, int& internalCounter);
305 
309  bool addPSFinalAssignmentInvocation(Function* psMain, const int groupOrder, int& internalCounter);
310 
311 
312 // Attributes.
313 protected:
314  // The normal map texture name.
315  String mNormalMapTextureName;
316  // Track per vertex colour type.
317  TrackVertexColourType mTrackVertexColourType;
318  // Specular component enabled/disabled.
319  bool mSpecularEnable;
320  // Light list.
321  LightParamsList mLightParamsList;
322  // Normal map texture sampler index.
323  unsigned short mNormalMapSamplerIndex;
324  // Vertex shader input texture coordinate set index.
325  unsigned int mVSTexCoordSetIndex;
326  // The normal map min filter.
327  FilterOptions mNormalMapMinFilter;
328  // The normal map mag filter.
329  FilterOptions mNormalMapMagFilter;
330  // The normal map mip filter.
331  FilterOptions mNormalMapMipFilter;
332  // The normal map max anisotropy value.
333  unsigned int mNormalMapAnisotropy;
334  // The normal map mip map bias.
335  Real mNormalMapMipBias;
336  // The normal map space.
337  NormalMapSpace mNormalMapSpace;
338  // World matrix parameter.
339  UniformParameterPtr mWorldMatrix;
340  // World matrix inverse rotation matrix parameter.
341  UniformParameterPtr mWorldInvRotMatrix;
342  // Camera position in world space parameter.
343  UniformParameterPtr mCamPosWorldSpace;
344  // Vertex shader input position parameter.
345  ParameterPtr mVSInPosition;
346  // Vertex shader world position parameter.
347  ParameterPtr mVSWorldPosition;
348  // Vertex shader output view vector (position in camera space) parameter.
349  ParameterPtr mVSOutView;
350  // Pixel shader input view position (position in camera space) parameter.
351  ParameterPtr mPSInView;
352  // Vertex shader input normal.
353  ParameterPtr mVSInNormal;
354  // Vertex shader input tangent.
355  ParameterPtr mVSInTangent;
356  // Vertex shader local TNB matrix.
357  ParameterPtr mVSTBNMatrix;
358  // Vertex shader local light direction.
359  ParameterPtr mVSLocalDir;
360  // Normal map texture sampler parameter.
361  UniformParameterPtr mNormalMapSampler;
362  // Pixel shader normal parameter.
363  ParameterPtr mPSNormal;
364  // Vertex shader input texture coordinates.
365  ParameterPtr mVSInTexcoord;
366  // Vertex shader output texture coordinates.
367  ParameterPtr mVSOutTexcoord;
368  // Pixel shader input texture coordinates.
369  ParameterPtr mPSInTexcoord;
370  // Pixel shader temporary diffuse calculation parameter.
371  ParameterPtr mPSTempDiffuseColour;
372  // Pixel shader temporary specular calculation parameter.
373  ParameterPtr mPSTempSpecularColour;
374  // Pixel shader input/local diffuse parameter.
375  ParameterPtr mPSDiffuse;
376  // Pixel shader input/local specular parameter.
377  ParameterPtr mPSSpecular;
378  // Pixel shader output diffuse parameter.
379  ParameterPtr mPSOutDiffuse;
380  // Pixel shader output specular parameter.
381  ParameterPtr mPSOutSpecular;
382  // Derived scene colour parameter.
383  UniformParameterPtr mDerivedSceneColour;
384  // Ambient light colour parameter.
385  UniformParameterPtr mLightAmbientColour;
386  // Derived ambient light colour parameter.
387  UniformParameterPtr mDerivedAmbientLightColour;
388  // Surface ambient colour parameter.
389  UniformParameterPtr mSurfaceAmbientColour;
390  // Surface diffuse colour parameter.
391  UniformParameterPtr mSurfaceDiffuseColour;
392  // Surface specular colour parameter.
393  UniformParameterPtr mSurfaceSpecularColour;
394  // Surface emissive colour parameter.
395  UniformParameterPtr mSurfaceEmissiveColour;
396  // Surface shininess parameter.
397  UniformParameterPtr mSurfaceShininess;
398  // Shared blank light.
399  static Light msBlankLight;
400 };
401 
402 
407 class _OgreRTSSExport NormalMapLightingFactory : public SubRenderStateFactory
408 {
409 public:
410 
414  virtual const String& getType() const;
415 
419  virtual SubRenderState* createInstance(ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator);
420 
424  virtual void writeInstance(MaterialSerializer* ser, SubRenderState* subRenderState, Pass* srcPass, Pass* dstPass);
425 
426 
427 protected:
428 
432  virtual SubRenderState* createInstanceImpl();
433 
434 
435 };
436 
440 }
441 }
442 
443 #endif
444 #endif
445 
float Real
Software floating point type.
HashedVector< Light * > LightList
Definition: OgreCommon.h:541
LightTypes
Defines the type of light.
Definition: OgreLight.h:82
SharedPtr< Parameter > ParameterPtr
#define _OgreRTSSExport
_StringBase String
SharedPtr< UniformParameter > UniformParameterPtr
FilterOptions
Filtering options for textures / mipmaps.
Definition: OgreCommon.h:100
int TrackVertexColourType
An enumeration describing which material properties should track the vertex colours.
Definition: OgreCommon.h:276