All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WaterTransitionShader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Shader.h"
6 #include "Graphics.h"
7 #include "Rendering.h"
8 #include "IdTypes.h"
9 #include "FogShaderUniforms.h"
10 #include "FogShaderOld.h"
11 #include "VectorMath.h"
12 #include "WaterShader.h"
13 #include "FogShaderIncludes.h"
14 #include <string>
15 
16 namespace Eegeo
17 {
18  namespace Rendering
19  {
20  namespace Shaders
21  {
22  namespace WaterTransitionShaderCode
23  {
24  const std::string _fragmentShaderUniformDeclarations =
25  "varying highp vec4 ColorVarying;\n"
26  "varying highp vec2 DestinationUV;\n"
27  "varying highp vec2 DestinationUV2;\n"
28  "varying highp vec4 ReflectVector;\n"
29  "uniform sampler2D FromDiffuse;\n"
30  "uniform sampler2D ToDiffuse;\n"
31  "uniform sampler2D NormalMap;\n"
32  "uniform samplerCube FromReflectionMap;\n"
33  "uniform samplerCube ToReflectionMap;\n"
34  "uniform lowp float ReflectionWaveDisplacement;\n"
35  "uniform lowp float LerpParam;\n";
36 
37 
38  const std::string _fragmentShaderMain =
39  "void main(void) { \n"
40  "highp vec4 diffuseColor = mix(texture2D(FromDiffuse, DestinationUV), texture2D(ToDiffuse, DestinationUV), LerpParam);\n"
41  "highp vec3 normal = ((2.0 * texture2D(NormalMap, DestinationUV2).xyz) - 1.0);\n"
42  "highp vec3 refl = normalize(ReflectVector.xyz + normal*ReflectionWaveDisplacement);\n"
43  "highp vec4 reflection = mix(textureCube(FromReflectionMap, refl), textureCube(ToReflectionMap, refl), LerpParam);\n"
44  "lowp vec3 colour = (diffuseColor.xyz * ColorVarying.xyz) + (reflection.xyz * ColorVarying.w);\n"
45  "gl_FragColor.rgb = mix(colour, FogColour.rgb, ReflectVector.w);\n"
46  "gl_FragColor.a = 1.0;\n"
47  "}\n";
48  }
49 
51  {
52  public:
53  static WaterTransitionShader* Create(const TShaderId shaderId)
54  {
55  std::string vertexShaderCode = WaterShaderCode::_vertexShaderUniformDeclarations + FOG_VERTEX_SHADER_UNIFORMS + FOG_VERTEX_SHADER_FUNCTIONS + WaterShaderCode::_vertexShaderMain;
56  std::string fragmentShaderCode = WaterTransitionShaderCode::_fragmentShaderUniformDeclarations + FOG_FRAGMENT_SHADER_UNIFORMS + WaterTransitionShaderCode::_fragmentShaderMain;
57 
58  return Eegeo_NEW(WaterTransitionShader)(
59  shaderId,
60  vertexShaderCode,
61  fragmentShaderCode
62  );
63  }
64 
65  const GLuint GetFromDiffuseSamplerId() const { return 0; }
66  const GLuint GetToDiffuseSamplerId() const { return 1; }
67  const GLuint GetNormalSamplerId() const { return 2; }
68  const GLuint GetFromReflectionSamplerId() const { return 3; }
69  const GLuint GetToReflectionSamplerId() const { return 3; }
70 
71  void SetLerpParam(const float l)
72  {
73  SetUniformFloat(l, m_lerpParamUniform);
74  }
75 
76  void SetMVP(const m44& mvp) const
77  {
78  bool transpose = false;
79  SetUniformM44(mvp, m_modelViewProjectionUniform, transpose);
80  }
81 
82  void SetMV(const m44& mv) const
83  {
84  bool transpose = false;
85  SetUniformM44(mv, m_modelViewUniform, transpose);
86  }
87 
88  void SetFogUniforms(Lighting::GlobalFoggingUniformValues& fogUniformValues) const
89  {
90  Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
91  }
92 
93  void SetLightColors(const m44& colors) const
94  {
95  bool transpose = false;
96  SetUniformM44(colors, m_lightColorsUniform, transpose);
97  }
98 
99  void SetAnimatedUV(const v4& uv) const
100  {
101  SetUniformV4(uv, m_animatedUVUniform);
102  }
103 
104  void SetAltitudeEnvMapFade(const float v) const
105  {
106  SetUniformFloat(v, m_altitudeEnvMapFadeUniform);
107  }
108 
109  void SetUVBounds(const Eegeo::v2& min, const Eegeo::v2& max) const
110  {
111  SetUniformV2(min, m_minUVRangeUniform);
112  SetUniformV2(max, m_maxUVRangeUniform);
113  }
114 
115  void SetPositionBounds(const Eegeo::v4& min, const Eegeo::v4& max) const
116  {
117  SetUniformV4(min, m_minVertRangeUniform);
118  SetUniformV4(max, m_maxVertRangeUniform);
119  }
120 
121  void SetWaveDisplacement(const float v) const
122  {
123  SetUniformFloat(v, m_reflectionWaveDisplacementUniform);
124  }
125 
126  void SetCameraRelativeOrigin(const Eegeo::v3& cameraRelativeOrigin) const
127  {
128  SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
129  }
130 
131  void SetViewToCubeMapMatrix(const m44& viewToCubeMapMatrix)
132  {
133  bool transpose = false;
134  SetUniformM44(viewToCubeMapMatrix, m_viewToCubeMapUniform, transpose);
135  }
136 
137  void Use(Rendering::GLState& glState) const
138  {
139  UseProgram(glState);
140  SetUniformTextureSampler(glState, GetFromDiffuseSamplerId(), m_fromDiffuseTextureSampler);
141  SetUniformTextureSampler(glState, GetToDiffuseSamplerId(), m_toDiffuseTextureSampler);
142  SetUniformTextureSampler(glState, GetNormalSamplerId(), m_normalMapTextureSampler);
143  SetUniformTextureSampler(glState, GetFromReflectionSamplerId(), m_fromReflectionMapTextureSampler);
144  SetUniformTextureSampler(glState, GetToReflectionSamplerId(), m_toReflectionMapTextureSampler);
145  }
146 
147  protected:
148  WaterTransitionShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
149  {
150  CompileProgram(vertexShaderCode, fragmentShaderCode);
151 
152  m_modelViewProjectionUniform = GetUniformLocation("ModelViewProjectionMatrix");
153  m_modelViewUniform = GetUniformLocation("ModelViewMatrix");
154  m_lightColorsUniform = GetUniformLocation("LightColorMatrix");
155  m_animatedUVUniform = GetUniformLocation("AnimatedUVUniform");
156  m_fromDiffuseTextureSampler = GetUniformLocation("FromDiffuse");
157  m_toDiffuseTextureSampler = GetUniformLocation("ToDiffuse");
158  m_normalMapTextureSampler = GetUniformLocation("NormalMap");
159  m_fromReflectionMapTextureSampler = GetUniformLocation("FromReflectionMap");
160  m_toReflectionMapTextureSampler = GetUniformLocation("ToReflectionMap");
161  m_altitudeEnvMapFadeUniform = GetUniformLocation("AltitudeEnvMapFade");
162  m_lerpParamUniform = GetUniformLocation("LerpParam");
163 
164  m_minVertRangeUniform = GetUniformLocation("MinVertRange");
165  m_maxVertRangeUniform = GetUniformLocation("MaxVertRange");
166  m_minUVRangeUniform = GetUniformLocation("MinUVRange");
167  m_maxUVRangeUniform = GetUniformLocation("MaxUVRange");
168  m_reflectionWaveDisplacementUniform = GetUniformLocation("ReflectionWaveDisplacement");
169 
170  m_cameraRelativeModelOriginUniformLocation = GetUniformLocation("CameraRelativeModelOrigin");
171  m_viewToCubeMapUniform = GetUniformLocation("ViewToCubeMapMatrix");
172  m_fogShaderUniforms.DetermineUniformsFromShader(this);
173  }
174 
175  private:
176  GLuint m_modelViewProjectionUniform;
177  GLuint m_modelViewUniform;
178  GLuint m_altitudeEnvMapFadeUniform;
179  GLuint m_lightColorsUniform;
180  GLuint m_minVertRangeUniform;
181  GLuint m_maxVertRangeUniform;
182  GLuint m_minUVRangeUniform;
183  GLuint m_maxUVRangeUniform;
184  GLuint m_fromDiffuseTextureSampler;
185  GLuint m_toDiffuseTextureSampler;
186  GLuint m_normalMapTextureSampler;
187  GLuint m_fromReflectionMapTextureSampler;
188  GLuint m_toReflectionMapTextureSampler;
189  GLuint m_animatedUVUniform;
190  GLuint m_reflectionWaveDisplacementUniform;
191  GLuint m_cameraRelativeModelOriginUniformLocation;
192  GLuint m_lerpParamUniform;
193  GLuint m_viewToCubeMapUniform;
194  Rendering::Shaders::FogShaderUniforms m_fogShaderUniforms;
195  };
196  }
197  }
198 }