GL Studio C++ Runtime 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) 2017 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 INCLUDED_EFFECT_DIFF_NORM_SPEC_REF_H
44 #define INCLUDED_EFFECT_DIFF_NORM_SPEC_REF_H
45 
46 #include "gls_matrix_affine.h"
47 #include "gls_render_effect.h"
48 
49 #ifdef GLES
50 # include "gls_state_manager.h"
51 #endif
52 
53 //////////////////// Provides support for creating DLLs ////////////////////////
54 #if( defined( GLSGEN_EXPORT_GLSADVANCEDMESH ) || defined( GLSGEN_IMPORT_GLSADVANCEDMESH ) || defined( GLS_EXPORT_GENERATED ) || defined( GLS_IMPORT_GENERATED ) ) \
55  && defined( _MSC_VER )
56 # if defined( GLSGEN_EXPORT_GLSADVANCEDMESH ) || defined( GLS_EXPORT_GENERATED )
57 # define GLSGEN_GlsAdvancedMesh_EXPORT __declspec( dllexport )
58 # else
59 # define GLSGEN_GlsAdvancedMesh_EXPORT __declspec( dllimport )
60 # endif
61 #else
62 # define GLSGEN_GlsAdvancedMesh_EXPORT
63 #endif
64 ///////////////////////////////////////////////////////////////////////////////
65 
66 namespace disti
67 {
68 #ifndef GLES
69 class MaxLike_Shader;
70 #endif
71 
72 /** Render effect that supports diffuse, normal, specular, and reflection maps. */
74 {
75 public:
76  static const std::string EFFECT_TYPE_NAME;
77 
79  {
80  bool _isIdentity;
81 
82  float _uOffset;
83  float _vOffset;
84  float _uTiling;
85  float _vTiling;
86  float _angle;
87 
88  GlsMatrixAffineF _matrix;
89 
90  public:
92 
93  bool operator==( const TextureUVWMatrix& rhs ) const;
94 
95  // Transform parameters
96  void UOffset( float val ) { _uOffset = val; }
97  float UOffset() const { return _uOffset; }
98  void VOffset( float val ) { _vOffset = val; }
99  float VOffset() const { return _vOffset; }
100  void UTiling( float val ) { _uTiling = val; }
101  float UTiling() const { return _uTiling; }
102  void VTiling( float val ) { _vTiling = val; }
103  float VTiling() const { return _vTiling; }
104  void Angle( float val ) { _angle = val; }
105  float Angle() const { return _angle; }
106 
107  // Should be called after changing the parameters
108  void RecalcTransformMatrix();
109 
110  // returns true if the matrix is identity
111  inline bool IsIdentity() { return _isIdentity; }
112 
113  // matrix must be an array of [16]
114  inline float* GetTransformMatrix() { return _matrix.Data(); }
115  };
116 
117 protected:
118  unsigned int _ref_count; ///< Reference count
119 
120 #ifdef GLES20
121  GlsEffect* _shader; /**< The current active effect in the system. If NULL, then no effect is active.
122  * Effects are retrieved from the GlsEffectFactory class. */
123  bool _userHasCustomShader; /** whether or not the user already had a custom shader set up */
124 #elif !defined( GLES )
125  MaxLike_Shader* _shader; ///< shader program to use (if any)
126 #endif
127 
128  unsigned int _materialIndex; ///< The material index to use
129  int _diffuseTextureIndex; ///< INVALID_INDEX, or the texture index to use
130  int _normalTextureIndex; ///< INVALID_INDEX, or the texture index to use
131  int _specularTextureIndex; ///< INVALID_INDEX, or the texture index to use
132  int _reflectionTextureIndex; ///< INVALID_INDEX, or the texture index to use
133 
134  float _diffuseAmount; ///< [0.0,1.0] "Weight" of the diffuse map
135  float _normalAmount; ///< [0.0,1.0] "Weight" of the normal map
136  float _specularAmount; ///< [0.0,1.0] "Weight" of the specular map
137  float _reflectionAmount; ///< [0.0,1.0] "Weight" of the reflection map
138 
139  bool _useVertexColors; ///< true: use vertex color for diffuse rather than material diffuse
140 
141  /// true: expect 'DirectX' format normal maps (positive Green points down)
142  /// false: expect 'OpenGL' format normal maps (positive Green points up)
143  /// Note: the sense is backwards from the Max 'Normal Bump' 'Flip Green' option because
144  /// Max defaults to 'DirectX' format and GL Studio defaults to 'OpenGL' format
146 
147  DistiAttribDict* _attribDict;
148 
149  TextureUVWMatrix _diffuseUVWMatrix;
150  TextureUVWMatrix _normalUVWMatrix;
151  TextureUVWMatrix _specularUVWMatrix;
152 
153  void SetupAttribDict();
154 
155 public:
156 #ifdef GLES20
157  static const GLenum NORMAL_MAP_TEXTURE_UNIT = GL_TEXTURE1; ///< the texture unit that the normal map should be bound to, \see glActiveTexture
158  static const GLenum SPECULAR_MAP_TEXTURE_UNIT = GL_TEXTURE2; ///< the texture unit that the specular map should be bound to, \see glActiveTexture
159  static const GLenum REFLECTION_MAP_TEXTURE_UNIT = GL_TEXTURE3; ///< the texture unit that the reflection map should be bound to, \see glActiveTexture
160 
161  /** Get the index for the sampler uniform for the given texture unit
162  * \param textureUnit the texture unit (e.g. GL_TEXTURE1)
163  * \return the index for the sampler uniform
164  */
165  static inline unsigned int TextureUnitIndex( GLenum textureUnit ) { return textureUnit - GL_TEXTURE0; }
166 #endif // GLES20
167  static const int INVALID_INDEX = -1;
168 
169  GLSGEN_GlsAdvancedMesh_EXPORT Effect_DiffNormSpecRefl();
170  GLSGEN_GlsAdvancedMesh_EXPORT ~Effect_DiffNormSpecRefl();
171 
172  /** Assignment operator */
173  GLSGEN_GlsAdvancedMesh_EXPORT Effect_DiffNormSpecRefl( const Effect_DiffNormSpecRefl& );
174  GLSGEN_GlsAdvancedMesh_EXPORT const Effect_DiffNormSpecRefl& operator=( const Effect_DiffNormSpecRefl& );
175 
176  GLSGEN_GlsAdvancedMesh_EXPORT bool operator==( const Effect_DiffNormSpecRefl& ) const;
177  inline bool operator!=( const Effect_DiffNormSpecRefl& o ) const { return !( *this == o ); }
178 
179  inline void SetMaterialIndex( unsigned int materialIndex ) { _materialIndex = materialIndex; }
180  inline unsigned int GetMaterialIndex() const { return _materialIndex; }
181 
182  inline void SetUseVertexColor( bool value )
183  {
184  _useVertexColors = value;
185 #ifndef GLES
186  SelectShader();
187 #endif
188  }
189  inline bool GetUseVertexColor() const { return _useVertexColors; }
190 
191  inline void SetDiffuseTextureIndex( int textureIndex )
192  {
193  _diffuseTextureIndex = textureIndex;
194 #ifndef GLES
195  SelectShader();
196 #endif
197  }
198  inline int GetDiffuseTextureIndex() const { return _diffuseTextureIndex; }
199 
200  inline void SetNormalTextureIndex( int textureIndex )
201  {
202  _normalTextureIndex = textureIndex;
203 #ifndef GLES
204  SelectShader();
205 #endif
206  }
207  inline int GetNormalTextureIndex() const { return _normalTextureIndex; }
208 
209  inline void SetSpecularTextureIndex( int textureIndex )
210  {
211  _specularTextureIndex = textureIndex;
212 #ifndef GLES
213  SelectShader();
214 #endif
215  }
216  inline int GetSpecularTextureIndex() const { return _specularTextureIndex; }
217 
218  inline void SetReflectionTextureIndex( int textureIndex )
219  {
220  _reflectionTextureIndex = textureIndex;
221 #ifndef GLES
222  SelectShader();
223 #endif
224  }
225  inline int GetReflectionTextureIndex() const { return _reflectionTextureIndex; }
226 
227  inline void SetDiffuseAmount( float value )
228  {
229  _diffuseAmount = value;
230 #ifndef GLES
231  SelectShader();
232 #endif
233  }
234  inline float GetDiffuseAmount() const { return _diffuseAmount; }
235 
236  inline void SetNormalAmount( float value )
237  {
238  _normalAmount = value;
239 #ifndef GLES
240  SelectShader();
241 #endif
242  }
243  inline float GetNormalAmount() const { return _normalAmount; }
244 
245  inline void SetSpecularAmount( float value )
246  {
247  _specularAmount = value;
248 #ifndef GLES
249  SelectShader();
250 #endif
251  }
252  inline float GetSpecularAmount() const { return _specularAmount; }
253 
254  inline void SetReflectionAmount( float value )
255  {
256  _reflectionAmount = value;
257 #ifndef GLES
258  SelectShader();
259 #endif
260  }
261  inline float GetReflectionAmount() const { return _reflectionAmount; }
262 
263  inline void SetNormalMapFlipGreen( bool value )
264  {
265  _normalMapFlipGreen = value;
266 #ifndef GLES
267  SelectShader();
268 #endif
269  }
270  inline bool GetNormalMapFlipGreen() const { return _normalMapFlipGreen; }
271 
272  /* Set Diffuse Map UV transform parameters
273  * Note that scaling and rotation are performed from the center of the texture.
274  * \param uOffset u axis translation
275  * \param vOffset v axis translation
276  * \param uTiling u axis scaling
277  * \param vTiling v axis scaling
278  * \param angle x axis rotation in degrees
279  */
280  GLSGEN_GlsAdvancedMesh_EXPORT void SetDiffuseUVWSettings( float uOffset, float vOffset, float uTiling, float vTiling, float angle );
281  GLSGEN_GlsAdvancedMesh_EXPORT void GetDiffuseUVWSettings( float& uOffset, float& vOffset, float& uTiling, float& vTiling, float& angle );
282 
283  /* Set Normal Map UV transform parameters
284  * Note that scaling and rotation are performed from the center of the texture.
285  * \param uOffset u axis translation
286  * \param vOffset v axis translation
287  * \param uTiling u axis scaling
288  * \param vTiling v axis scaling
289  * \param angle W-axis rotation in degrees
290  */
291  GLSGEN_GlsAdvancedMesh_EXPORT void SetNormalUVWSettings( float uOffset, float vOffset, float uTiling, float vTiling, float angle );
292  GLSGEN_GlsAdvancedMesh_EXPORT void GetNormalUVWSettings( float& uOffset, float& vOffset, float& uTiling, float& vTiling, float& angle );
293 
294  /* Set Specular Map UV transform parameters
295  * Note that scaling and rotation are performed from the center of the texture.
296  * \param uOffset u axis translation
297  * \param vOffset v axis translation
298  * \param uTiling u axis scaling
299  * \param vTiling v axis scaling
300  * \param angle W-axis rotation in degrees
301  */
302  GLSGEN_GlsAdvancedMesh_EXPORT void SetSpecularUVWSettings( float uOffset, float vOffset, float uTiling, float vTiling, float angle );
303  GLSGEN_GlsAdvancedMesh_EXPORT void GetSpecularUVWSettings( float& uOffset, float& vOffset, float& uTiling, float& vTiling, float& angle );
304 
305  // Selects the shader to use based on the current effect parameters
306  // Not normally called by users
307  GLSGEN_GlsAdvancedMesh_EXPORT void SelectShader();
308 
309  // Recalculate the texture transform matrices
310  // Not normally called by users
311  GLSGEN_GlsAdvancedMesh_EXPORT void TextureUVWSettingsChanged();
312 
313  // Returns true if the effect has non-default texture transform settings
314  // Not normally called by users
315  GLSGEN_GlsAdvancedMesh_EXPORT bool UsesTextureTransforms();
316 
317  // Returns reference to the effect attribute dictionary
318  inline DistiAttribDict& Attributes()
319  {
320  if( !_attribDict )
321  {
322  SetupAttribDict();
323  }
324  return *_attribDict;
325  }
326 
327  // Return this effect's type as a readable string
328  inline const std::string& EffectTypeName()
329  {
330  return EFFECT_TYPE_NAME;
331  }
332 
333  /////////////////////////////////////////////
334  // VertexAttribIndexLookup Interface
335  /////////////////////////////////////////////
336 
337  virtual GLint GetVertexAttribIndexForSemantic( int semanticEnum );
338 
339  ///////////////////////////////////////////////////////////////////////////
340  // GlsRenderEffect overrides
341  ///////////////////////////////////////////////////////////////////////////
342  virtual void AddRef();
343  virtual void Release();
344  virtual unsigned int GlsRenderEffect_ClassID() const;
345  virtual void SetupEffect(
346  DynamicArray<Material>& materialPalette,
347  TexturePalette* texturePalette,
348  const TextureSettings& textureSettings,
349  float* viewToWorld3x3, // 3x3 matrix (float[9])
350  unsigned int maxLightNum,
351  unsigned int activeLightMask );
352 
353  virtual void CleanupEffect();
354  virtual int Compare( const GlsRenderEffect* ) const;
355  virtual GlsRenderEffect* Clone() const;
356  virtual void GetReferencedTextures( ReferencedTextureArray& referencedTextures );
357  virtual void GetReferencedMaterials( ReferencedMaterialArray& referencedMaterials );
358 };
359 
360 } // namespace disti
361 
362 #endif
Definition: effect_diff_norm_spec_refl.h:78
Definition: gls_es20_effect.h:58
Definition: effect_diff_norm_spec_refl.h:73
unsigned int _ref_count
Reference count.
Definition: effect_diff_norm_spec_refl.h:118
float _normalAmount
[0.0,1.0] "Weight" of the normal map
Definition: effect_diff_norm_spec_refl.h:135
The GlsMatrixAffine class.
bool _useVertexColors
true: use vertex color for diffuse rather than material diffuse
Definition: effect_diff_norm_spec_refl.h:139
virtual GlsRenderEffect * Clone() const
The disti::GlsStateManager factory class. Creates an instance of a state manager that manages the GL ...
virtual void GetReferencedTextures(ReferencedTextureArray &referencedTextures)
virtual void CleanupEffect()
Remove the effect trom the OpenGL state.
virtual void SetupEffect(DynamicArray< Material > &materialPalette, TexturePalette *texturePalette, const TextureSettings &textureSettings, float *viewToWorld3x3, unsigned int maxLightNum, unsigned int activeLightMask)
virtual int Compare(const GlsRenderEffect *) const
float _specularAmount
[0.0,1.0] "Weight" of the specular map
Definition: effect_diff_norm_spec_refl.h:136
float _reflectionAmount
[0.0,1.0] "Weight" of the reflection map
Definition: effect_diff_norm_spec_refl.h:137
MaxLike_Shader * _shader
shader program to use (if any)
Definition: effect_diff_norm_spec_refl.h:125
int _diffuseTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:129
Type * Data()
Definition: gls_matrix.h:131
unsigned int _materialIndex
The material index to use.
Definition: effect_diff_norm_spec_refl.h:128
virtual GLint GetVertexAttribIndexForSemantic(int semanticEnum)
int _specularTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:131
Definition: disti_metadata.h:665
bool _normalMapFlipGreen
Definition: effect_diff_norm_spec_refl.h:145
int _reflectionTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:132
float _diffuseAmount
[0.0,1.0] "Weight" of the diffuse map
Definition: effect_diff_norm_spec_refl.h:134
int _normalTextureIndex
INVALID_INDEX, or the texture index to use.
Definition: effect_diff_norm_spec_refl.h:130
Definition: gls_render_effect.h:117
Definition: bmpimage.h:46
virtual void GetReferencedMaterials(ReferencedMaterialArray &referencedMaterials)
The disti::GlsRenderEffect class.