GL Studio API
effect_diff_norm_spec_refl.h
Go to the documentation of this file.
1 /*! \file
2  \brief Render effect that supports diffuse, normal, specular, and reflection maps
3 
4  \par Copyright Information
5 
6  Copyright (c) 2015 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America. Use, distribution,
38 duplication, or disclosure by the U. S. Government is subject to
39 "Restricted Rights" as set forth in DFARS 252.227-7014(c)(1)(ii).
40 
41 */
42 
43 #ifndef _EFFECT_DIFF_NORM_SPEC_REF_H_INCLUDED
44 #define _EFFECT_DIFF_NORM_SPEC_REF_H_INCLUDED
45 
46 #include "gls_render_effect.h"
47 
48 #ifdef _MSC_VER
49  // Disable warning in gls_matrix_affine.h (DR 2156)
50  #pragma warning( push )
51  #pragma warning( disable: 4244 )
52 #endif
53 #include "gls_matrix_affine.h"
54 #ifdef _MSC_VER
55  #pragma warning( pop )
56 #endif
57 
58 #ifdef GLES
59 #include "gls_state_manager.h"
60 #endif
61 
62 //////////////////// Provides support for creating DLLs ////////////////////////
63 #if (defined(GLSGEN_EXPORT_GLSADVANCEDMESH) || \
64  defined(GLSGEN_IMPORT_GLSADVANCEDMESH) || \
65  defined(GLS_EXPORT_GENERATED) || \
66  defined(GLS_IMPORT_GENERATED)) \
67  && defined(_MSC_VER)
68 # if defined(GLSGEN_EXPORT_GLSADVANCEDMESH) || defined(GLS_EXPORT_GENERATED)
69 # define GLSGEN_GlsAdvancedMesh_EXPORT __declspec(dllexport)
70 # else
71 # define GLSGEN_GlsAdvancedMesh_EXPORT __declspec(dllimport)
72 # endif
73 #else
74 # define GLSGEN_GlsAdvancedMesh_EXPORT
75 #endif
76 ///////////////////////////////////////////////////////////////////////////////
77 
78 namespace disti {
79 
80 #ifndef GLES
81 class MaxLike_Shader;
82 #endif
83 
84 /** Render effect that supports diffuse, normal, specular, and reflection maps. */
86 {
87 public:
88 
89  static const std::string EFFECT_TYPE_NAME;
90 
92  {
93  bool _isIdentity;
94 
95  float _uOffset;
96  float _vOffset;
97  float _uTiling;
98  float _vTiling;
99  float _angle;
100 
101  GlsMatrixAffineF _matrix;
102 
103  public:
105 
106  bool operator==(const TextureUVWMatrix& rhs) const;
107 
108  // Transform parameters
109  void UOffset(float val) { _uOffset = val; }
110  float UOffset() const { return _uOffset; }
111  void VOffset(float val) { _vOffset = val; }
112  float VOffset() const { return _vOffset; }
113  void UTiling(float val) { _uTiling = val; }
114  float UTiling() const { return _uTiling; }
115  void VTiling(float val) { _vTiling = val; }
116  float VTiling() const { return _vTiling; }
117  void Angle(float val) { _angle = val; }
118  float Angle() const { return _angle; }
119 
120  // Should be called after changing the parameters
121  void RecalcTransformMatrix();
122 
123  // returns true if the matrix is identity
124  inline bool IsIdentity() { return _isIdentity; }
125 
126  // matrix must be an array of [16]
127  inline float* GetTransformMatrix() { return _matrix.Data(); }
128  };
129 
130 protected:
131 
132  unsigned int _ref_count; ///< Reference count
133 
134 #ifdef GLES20
135  GlsEffect *_shader; /**< The current active effect in the system. If NULL, then no effect is active.
136  * Effects are retrieved from the GlsEffectFactory class. */
137  bool _userHasCustomShader; /** whether or not the user already had a custom shader set up */
138 #elif !defined( GLES )
139  MaxLike_Shader *_shader; ///< shader program to use (if any)
140 #endif
141 
142  unsigned int _materialIndex; ///< The material index to use
143  int _diffuseTextureIndex; ///< INVALID_INDEX, or the texture index to use
144  int _normalTextureIndex; ///< INVALID_INDEX, or the texture index to use
145  int _specularTextureIndex; ///< INVALID_INDEX, or the texture index to use
146  int _reflectionTextureIndex; ///< INVALID_INDEX, or the texture index to use
147 
148  float _diffuseAmount; ///< [0.0,1.0] "Weight" of the diffuse map
149  float _normalAmount; ///< [0.0,1.0] "Weight" of the normal map
150  float _specularAmount; ///< [0.0,1.0] "Weight" of the specular map
151  float _reflectionAmount; ///< [0.0,1.0] "Weight" of the reflection map
152 
153  bool _useVertexColors; ///< true: use vertex color for diffuse rather than material diffuse
154 
155  /// true: expect 'DirectX' format normal maps (positive Green points down)
156  /// false: expect 'OpenGL' format normal maps (positive Green points up)
157  /// Note: the sense is backwards from the Max 'Normal Bump' 'Flip Green' option because
158  /// Max defaults to 'DirectX' format and GL Studio defaults to 'OpenGL' format
160 
161  DistiAttribDict* _attribDict;
162 
163  TextureUVWMatrix _diffuseUVWMatrix;
164  TextureUVWMatrix _normalUVWMatrix;
165  TextureUVWMatrix _specularUVWMatrix;
166 
167  void SetupAttribDict();
168 
169 public:
170 #ifdef GLES20
171  static const GLenum NORMAL_MAP_TEXTURE_UNIT = GL_TEXTURE1; ///< the texture unit that the normal map should be bound to, \see glActiveTexture
172  static const GLenum SPECULAR_MAP_TEXTURE_UNIT = GL_TEXTURE2; ///< the texture unit that the specular map should be bound to, \see glActiveTexture
173  static const GLenum REFLECTION_MAP_TEXTURE_UNIT = GL_TEXTURE3; ///< the texture unit that the reflection map should be bound to, \see glActiveTexture
174 
175  /** Get the index for the sampler uniform for the given texture unit
176  * \param textureUnit the texture unit (e.g. GL_TEXTURE1)
177  * \return the index for the sampler uniform
178  */
179  static inline unsigned int TextureUnitIndex( GLenum textureUnit ) { return textureUnit - GL_TEXTURE0; }
180 #endif // GLES20
181  static const int INVALID_INDEX = -1;
182 
183  GLSGEN_GlsAdvancedMesh_EXPORT Effect_DiffNormSpecRefl();
184  GLSGEN_GlsAdvancedMesh_EXPORT ~Effect_DiffNormSpecRefl();
185 
186  /** Assignment operator */
187  GLSGEN_GlsAdvancedMesh_EXPORT Effect_DiffNormSpecRefl(const Effect_DiffNormSpecRefl&);
188  GLSGEN_GlsAdvancedMesh_EXPORT const Effect_DiffNormSpecRefl& operator=(const Effect_DiffNormSpecRefl&);
189 
190  GLSGEN_GlsAdvancedMesh_EXPORT bool operator==(const Effect_DiffNormSpecRefl&) const;
191  inline bool operator!=(const Effect_DiffNormSpecRefl& o) const { return !(*this == o); }
192 
193  inline void SetMaterialIndex(unsigned int materialIndex) { _materialIndex = materialIndex; }
194  inline unsigned int GetMaterialIndex() const { return _materialIndex; }
195 
196  inline void SetUseVertexColor(bool value)
197  {
198  _useVertexColors = value;
199 #ifndef GLES
200  SelectShader();
201 #endif
202  }
203  inline bool GetUseVertexColor() const { return _useVertexColors; }
204 
205  inline void SetDiffuseTextureIndex(int textureIndex)
206  {
207  _diffuseTextureIndex = textureIndex;
208 #ifndef GLES
209  SelectShader();
210 #endif
211  }
212  inline int GetDiffuseTextureIndex() const { return _diffuseTextureIndex; }
213 
214  inline void SetNormalTextureIndex(int textureIndex)
215  {
216  _normalTextureIndex = textureIndex;
217 #ifndef GLES
218  SelectShader();
219 #endif
220  }
221  inline int GetNormalTextureIndex() const { return _normalTextureIndex; }
222 
223  inline void SetSpecularTextureIndex(int textureIndex)
224  {
225  _specularTextureIndex = textureIndex;
226 #ifndef GLES
227  SelectShader();
228 #endif
229  }
230  inline int GetSpecularTextureIndex() const { return _specularTextureIndex; }
231 
232  inline void SetReflectionTextureIndex(int textureIndex)
233  {
234  _reflectionTextureIndex = textureIndex;
235 #ifndef GLES
236  SelectShader();
237 #endif
238  }
239  inline int GetReflectionTextureIndex() const { return _reflectionTextureIndex; }
240 
241  inline void SetDiffuseAmount(float value)
242  {
243  _diffuseAmount = value;
244 #ifndef GLES
245  SelectShader();
246 #endif
247  }
248  inline float GetDiffuseAmount() const { return _diffuseAmount; }
249 
250  inline void SetNormalAmount(float value)
251  {
252  _normalAmount = value;
253 #ifndef GLES
254  SelectShader();
255 #endif
256  }
257  inline float GetNormalAmount() const { return _normalAmount; }
258 
259  inline void SetSpecularAmount(float value)
260  {
261  _specularAmount = value;
262 #ifndef GLES
263  SelectShader();
264 #endif
265  }
266  inline float GetSpecularAmount() const { return _specularAmount; }
267 
268  inline void SetReflectionAmount(float value)
269  {
270  _reflectionAmount = value;
271 #ifndef GLES
272  SelectShader();
273 #endif
274  }
275  inline float GetReflectionAmount() const { return _reflectionAmount; }
276 
277  inline void SetNormalMapFlipGreen(bool value)
278  {
279  _normalMapFlipGreen = value;
280 #ifndef GLES
281  SelectShader();
282 #endif
283  }
284  inline bool GetNormalMapFlipGreen() const { return _normalMapFlipGreen; }
285 
286  /* Set Diffuse Map UV transform parameters
287  * Note that scaling and rotation are performed from the center of the texture.
288  * \param uOffset u axis translation
289  * \param vOffset v axis translation
290  * \param uTiling u axis scaling
291  * \param vTiling v axis scaling
292  * \param angle x axis rotation in degrees
293  */
294  GLSGEN_GlsAdvancedMesh_EXPORT void SetDiffuseUVWSettings(float uOffset, float vOffset, float uTiling, float vTiling, float angle);
295  GLSGEN_GlsAdvancedMesh_EXPORT void GetDiffuseUVWSettings(float& uOffset, float& vOffset, float& uTiling, float& vTiling, float& angle);
296 
297  /* Set Normal Map UV transform parameters
298  * Note that scaling and rotation are performed from the center of the texture.
299  * \param uOffset u axis translation
300  * \param vOffset v axis translation
301  * \param uTiling u axis scaling
302  * \param vTiling v axis scaling
303  * \param angle W-axis rotation in degrees
304  */
305  GLSGEN_GlsAdvancedMesh_EXPORT void SetNormalUVWSettings(float uOffset, float vOffset, float uTiling, float vTiling, float angle);
306  GLSGEN_GlsAdvancedMesh_EXPORT void GetNormalUVWSettings(float& uOffset, float& vOffset, float& uTiling, float& vTiling, float& angle);
307 
308  /* Set Specular Map UV transform parameters
309  * Note that scaling and rotation are performed from the center of the texture.
310  * \param uOffset u axis translation
311  * \param vOffset v axis translation
312  * \param uTiling u axis scaling
313  * \param vTiling v axis scaling
314  * \param angle W-axis rotation in degrees
315  */
316  GLSGEN_GlsAdvancedMesh_EXPORT void SetSpecularUVWSettings(float uOffset, float vOffset, float uTiling, float vTiling, float angle);
317  GLSGEN_GlsAdvancedMesh_EXPORT void GetSpecularUVWSettings(float& uOffset, float& vOffset, float& uTiling, float& vTiling, float& angle);
318 
319  // Selects the shader to use based on the current effect parameters
320  // Not normally called by users
321  GLSGEN_GlsAdvancedMesh_EXPORT void SelectShader();
322 
323  // Recalculate the texture transform matrices
324  // Not normally called by users
325  GLSGEN_GlsAdvancedMesh_EXPORT void TextureUVWSettingsChanged();
326 
327  // Returns true if the effect has non-default texture transform settings
328  // Not normally called by users
329  GLSGEN_GlsAdvancedMesh_EXPORT bool UsesTextureTransforms();
330 
331  // Returns reference to the effect attribute dictionary
332  inline DistiAttribDict& Attributes()
333  {
334  if (!_attribDict) { SetupAttribDict(); }
335  return *_attribDict;
336  }
337 
338  // Return this effect's type as a readable string
339  inline const std::string& EffectTypeName()
340  {
341  return EFFECT_TYPE_NAME;
342  }
343 
344  /////////////////////////////////////////////
345  // VertexAttribIndexLookup Interface
346  /////////////////////////////////////////////
347 
348  virtual GLint GetVertexAttribIndexForSemantic(int semanticEnum);
349 
350  ///////////////////////////////////////////////////////////////////////////
351  // GlsRenderEffect overrides
352  ///////////////////////////////////////////////////////////////////////////
353  virtual void AddRef();
354  virtual void Release();
355  virtual unsigned int GlsRenderEffect_ClassID() const;
356  virtual void SetupEffect(
357  DynamicArray<Material, false>& materialPalette,
358  TexturePalette* texturePalette,
359  const TextureSettings& textureSettings,
360  float* viewToWorld3x3 // 3x3 matrix (float[9])
361 #if !((GLS_VERSION_MAJOR == 4 && (GLS_VERSION_MINOR == 1 || GLS_VERSION_MINOR == 0)) || GLS_VERSION_MAJOR <= 3)
362  ,
363  unsigned int maxLightNum,
364  unsigned int activeLightMask
365 #endif
366  );
367 
368  virtual void CleanupEffect();
369  virtual int Compare(const GlsRenderEffect*) const;
370  virtual GlsRenderEffect* Clone() const;
371  virtual void GetReferencedTextures(ReferencedTextureArray& referencedTextures);
372  virtual void GetReferencedMaterials(ReferencedMaterialArray& referencedMaterials);
373 
374 };
375 
376 
377 } // namespace disti
378 
379 #endif
Definition: effect_diff_norm_spec_refl.h:91
Definition: effect_diff_norm_spec_refl.h:85
unsigned int _ref_count
Reference count.
Definition: effect_diff_norm_spec_refl.h:132
float _normalAmount
[0.0,1.0] "Weight" of the normal map
Definition: effect_diff_norm_spec_refl.h:149
The GlsMatrixAffine class.
bool _useVertexColors
true: use vertex color for diffuse rather than material diffuse
Definition: effect_diff_norm_spec_refl.h:153
virtual GlsRenderEffect * Clone() const
virtual void SetupEffect(DynamicArray< Material, false > &materialPalette, TexturePalette *texturePalette, const TextureSettings &textureSettings, float *viewToWorld3x3)
virtual void GetReferencedTextures(ReferencedTextureArray &referencedTextures)
virtual void CleanupEffect()
Remove the effect trom the OpenGL state.
virtual int Compare(const GlsRenderEffect *) const
float _specularAmount
[0.0,1.0] "Weight" of the specular map
Definition: effect_diff_norm_spec_refl.h:150
float _reflectionAmount
[0.0,1.0] "Weight" of the reflection map
Definition: effect_diff_norm_spec_refl.h:151
MaxLike_Shader * _shader
shader program to use (if any)
Definition: effect_diff_norm_spec_refl.h:139
int _diffuseTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:143
Type * Data()
Definition: gls_matrix.h:132
unsigned int _materialIndex
The material index to use.
Definition: effect_diff_norm_spec_refl.h:142
virtual GLint GetVertexAttribIndexForSemantic(int semanticEnum)
int _specularTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:145
Definition: disti_metadata.h:661
bool _normalMapFlipGreen
Definition: effect_diff_norm_spec_refl.h:159
int _reflectionTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:146
float _diffuseAmount
[0.0,1.0] "Weight" of the diffuse map
Definition: effect_diff_norm_spec_refl.h:148
int _normalTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:144
Definition: gls_render_effect.h:119
Definition: bmpimage.h:46
virtual void GetReferencedMaterials(ReferencedMaterialArray &referencedMaterials)
The disti::GlsRenderEffect class.