All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WaterTransitionMaterial.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "IMaterial.h"
7 #include "Lighting.h"
8 #include "WaterTransitionShader.h"
9 #include "IdTypes.h"
10 #include "Rendering.h"
11 #include "TextureMinifyType.h"
12 #include "VectorMath.h"
13 #include "GLState.h"
14 #include "MathsHelpers.h"
15 #include "GlobalFogging.h"
16 #include "GlobalLighting.h"
17 #include "WaterRenderable.h"
18 #include "GLHelpers.h"
19 #include "EarthConstants.h"
20 #include "WaterMaterial2.h"
21 #include "IAsyncTexture.h"
22 #include <string>
23 
24 namespace Eegeo
25 {
26  namespace Rendering
27  {
28  namespace Materials
29  {
31  {
32  public:
34  const TMaterialId materialId,
35  const std::string& name,
37  const Lighting::GlobalLighting& lighting,
38  const Lighting::GlobalFogging& fogging,
39  AsyncTexturing::IAsyncTexture& fromDiffuseTextureId,
40  AsyncTexturing::IAsyncTexture& toDiffuseTextureId,
41  AsyncTexturing::IAsyncTexture& normalMapTextureId,
42  AsyncTexturing::IAsyncTexture& fromReflectionTextureId,
43  AsyncTexturing::IAsyncTexture& toReflectionTextureId,
44  AsyncTexturing::IAsyncTexture& defaultReflectionCubeMapTextureId,
45  Rendering::TextureMinifyType textureMinifyType,
46  bool useAlternativeAmbient,
47  bool textureRepeat,
48  Eegeo::v2 animationScaleUV,
49  Eegeo::v2 animationWrapUV,
50  float envMapFadeoutAltitude
51  )
52  : m_id(materialId)
53  , m_name(name)
54  , m_shader(shader)
55  , m_lighting(lighting)
56  , m_fogging(fogging)
57  , m_fromDiffuseTextureId(&fromDiffuseTextureId)
58  , m_toDiffuseTextureId(&toDiffuseTextureId)
59  , m_normalMapTextureId(&normalMapTextureId)
60  , m_fromReflectionMapTextureId(&fromReflectionTextureId)
61  , m_toReflectionMapTextureId(&toReflectionTextureId)
62  , m_defaultReflectionCubeMapTextureId(&defaultReflectionCubeMapTextureId)
63  , m_textureMinifyType(textureMinifyType)
64  , m_textureRepeat(textureRepeat)
65  , m_useAlternativeAmbient(useAlternativeAmbient)
66  , m_altitudeScale(0.f)
67  , m_animationScaleUV(animationScaleUV)
68  , m_animationWrapUV(animationWrapUV)
69  , m_animationStateUV(0,0,1,1)
70  , m_lerpParam(0.f)
71  , m_envMapFadeoutAltitude(envMapFadeoutAltitude)
72  {
73  }
74 
75  bool IsUsingAlternativeAmbient() const { return m_useAlternativeAmbient; }
76  void UseAlternativeAmbient(bool useAlternativeAmbient) { m_useAlternativeAmbient = useAlternativeAmbient; }
77 
78  const TMaterialId GetId() const { return m_id; }
79  const Shader& GetShader() const { return m_shader; }
80  const std::string& GetName() const { return m_name; }
81 
82  void SetFromDiffuseTexture(AsyncTexturing::IAsyncTexture& textureId) { m_fromDiffuseTextureId = &textureId; }
83  AsyncTexturing::IAsyncTexture& GetFromDiffuseTextureId() const { return *m_fromDiffuseTextureId; }
84 
85  void SetToDiffuseTexture(AsyncTexturing::IAsyncTexture& textureId) { m_toDiffuseTextureId = &textureId; }
86  AsyncTexturing::IAsyncTexture& GetToDiffuseTextureId() const { return *m_toDiffuseTextureId; }
87 
88  void SetNormalMapTexture(AsyncTexturing::IAsyncTexture& textureId) { m_normalMapTextureId = &textureId; }
89  AsyncTexturing::IAsyncTexture& GetNormalMapTextureId() const { return *m_normalMapTextureId; }
90 
91  void SetFromReflectionTexture(AsyncTexturing::IAsyncTexture* textureId) { m_fromReflectionMapTextureId = textureId; }
92  AsyncTexturing::IAsyncTexture* GetFromReflectionTextureId() const { return m_fromReflectionMapTextureId; }
93 
94  void SetToReflectionTexture(AsyncTexturing::IAsyncTexture* textureId) { m_toReflectionMapTextureId = textureId; }
95  AsyncTexturing::IAsyncTexture* GetToReflectionTextureId() const { return m_toReflectionMapTextureId; }
96 
97  void SetLerpParam(float t) { m_lerpParam = t; }
98  float GetLerpParam() const { return m_lerpParam; }
99 
100  void UpdateMaterial(float deltaTime, const dv3& ecefCameraPosition)
101  {
102  // TODO: Remove dry fail.
103  float altitude = (float)(ecefCameraPosition.Length() - Space::EarthConstants::Radius);
104  const float MaxEnvMapBlend = 0.5f;
105  const float EnvMapBlendDelta = -0.5f;
106  altitude = Clamp(altitude, 0.0f, m_envMapFadeoutAltitude);
107  m_altitudeScale = Helpers::MathsHelpers::PennerQuadraticEaseOut(altitude,
108  MaxEnvMapBlend,
109  EnvMapBlendDelta,
110  m_envMapFadeoutAltitude);
111 
112  m_animationStateUV.SetX(m_animationStateUV.GetX() + deltaTime * m_animationScaleUV.GetX());
113  m_animationStateUV.SetY(m_animationStateUV.GetY() + deltaTime * m_animationScaleUV.GetY());
114 
115  if(m_animationStateUV.GetX() > m_animationWrapUV.GetX())
116  {
117  m_animationStateUV.SetX(m_animationStateUV.GetX() - m_animationWrapUV.GetX());
118  }
119  else if(m_animationStateUV.GetX() < -m_animationWrapUV.GetX())
120  {
121  m_animationStateUV.SetX(m_animationStateUV.GetX() + m_animationWrapUV.GetX());
122  }
123 
124  if(m_animationStateUV.GetY() > m_animationWrapUV.GetY())
125  {
126  m_animationStateUV.SetY(m_animationStateUV.GetY() - m_animationWrapUV.GetY());
127  }
128  else if(m_animationStateUV.GetY() < -m_animationWrapUV.GetY())
129  {
130  m_animationStateUV.SetY(m_animationStateUV.GetY() + m_animationWrapUV.GetY());
131  }
132  }
133 
134  void SetState(Rendering::GLState& glState) const
135  {
136  m_shader.Use(glState);
137 
138  glState.Blend.Disable();
139  glState.DepthTest.Enable();
140 
141  const Eegeo::m44 &lightColors = m_lighting.GetColors(m_useAlternativeAmbient);
142  m_shader.SetLightColors(lightColors);
143  m_shader.SetLerpParam(m_lerpParam);
144 
146  m_fogging.GetUniformValues(fogValues);
147  m_shader.SetFogUniforms(fogValues);
148  m_shader.SetAnimatedUV(m_animationStateUV);
149  m_shader.SetAltitudeEnvMapFade(m_altitudeScale);
150  m_shader.SetWaveDisplacement(1.0f / 16.0f); // TODO; drive from theming..
151 
152  Helpers::GLHelpers::BindTexture2D(glState, m_shader.GetFromDiffuseSamplerId(), m_fromDiffuseTextureId->GetTextureInfo().textureId, m_textureMinifyType, m_textureRepeat);
153  Helpers::GLHelpers::BindTexture2D(glState, m_shader.GetToDiffuseSamplerId(), m_toDiffuseTextureId->GetTextureInfo().textureId, m_textureMinifyType, m_textureRepeat);
154  Helpers::GLHelpers::BindTexture2D(glState, m_shader.GetNormalSamplerId(), m_normalMapTextureId->GetTextureInfo().textureId, m_textureMinifyType, m_textureRepeat);
155 
156  Helpers::GLHelpers::BindTextureCube(glState, m_shader.GetFromReflectionSamplerId(), m_fromReflectionMapTextureId != NULL ? m_fromReflectionMapTextureId->GetTextureInfo().textureId : m_defaultReflectionCubeMapTextureId->GetTextureInfo().textureId);
157  Helpers::GLHelpers::BindTextureCube(glState, m_shader.GetToReflectionSamplerId(), m_toReflectionMapTextureId != NULL ? m_toReflectionMapTextureId->GetTextureInfo().textureId : m_defaultReflectionCubeMapTextureId->GetTextureInfo().textureId);
158  }
159 
160  void SetStatePerRenderable(const Rendering::RenderableBase* renderableBase, Rendering::GLState& glState) const
161  {
162  const Rendering::Renderables::WaterRenderable* waterRenderable = Eegeo::CheckedCast<const Rendering::Renderables::WaterRenderable*>(renderableBase);
163  m_shader.SetMVP(waterRenderable->GetModelViewProjection());
164  m_shader.SetMV(waterRenderable->GetModelView());
165 
166  m44 viewToCubemapMatrix = WaterMaterial2::CalculateViewToCubeMapMatrix(*waterRenderable);
167  m_shader.SetViewToCubeMapMatrix(viewToCubemapMatrix);
168 
169  m_shader.SetUVBounds(waterRenderable->GetUVBoundsMin(), waterRenderable->GetUVBoundsMax());
170  m_shader.SetPositionBounds(waterRenderable->GetPositionBoundsMin(), waterRenderable->GetPositionBoundsMax());
171  m_shader.SetCameraRelativeOrigin(waterRenderable->GetCameraRelativeModelOrigin());
172  }
173 
174  protected:
175  const TMaterialId m_id;
176  const std::string m_name;
178  const Lighting::GlobalLighting& m_lighting;
179  const Lighting::GlobalFogging& m_fogging;
180  AsyncTexturing::IAsyncTexture* m_fromDiffuseTextureId;
181  AsyncTexturing::IAsyncTexture* m_toDiffuseTextureId;
182  AsyncTexturing::IAsyncTexture* m_normalMapTextureId;
183  AsyncTexturing::IAsyncTexture* m_fromReflectionMapTextureId;
184  AsyncTexturing::IAsyncTexture* m_toReflectionMapTextureId;
185  AsyncTexturing::IAsyncTexture* m_defaultReflectionCubeMapTextureId;
186  Rendering::TextureMinifyType m_textureMinifyType;
187  bool m_textureRepeat;
188  bool m_useAlternativeAmbient;
189  float m_altitudeScale;
190  Eegeo::v2 m_animationScaleUV;
191  Eegeo::v2 m_animationWrapUV;
192  Eegeo::v4 m_animationStateUV;
193  float m_lerpParam;
194  float m_envMapFadeoutAltitude;
195  };
196  }
197  }
198 }