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