8 #include "VectorMath.h"
10 #include "FogShaderUniforms.h"
11 #include "FogShaderIncludes.h"
12 #include "FogShaderOld.h"
13 #include "GlobalFogging.h"
21 namespace WaterShaderCode
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";
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"
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"
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";
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"
88 static WaterShader* Create(
const TShaderId shaderId)
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;
100 const GLuint GetDiffuseSamplerId()
const {
return 0; }
101 const GLuint GetNormalSamplerId()
const {
return 1; }
102 const GLuint GetReflectionSamplerId()
const {
return 2; }
104 void SetMVP(
const m44& mvp)
const
106 bool transpose =
false;
107 SetUniformM44(mvp, m_modelViewProjectionUniform, transpose);
110 void SetMV(
const m44& mv)
const
112 bool transpose =
false;
113 SetUniformM44(mv, m_modelViewUniform, transpose);
116 void SetViewToCubeMapMatrix(
const m44& viewToCubeMapMatrix)
118 bool transpose =
false;
119 SetUniformM44(viewToCubeMapMatrix, m_viewToCubeMapUniform, transpose);
124 Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
127 void SetLightColors(
const m44& colors)
const
129 bool transpose =
false;
130 SetUniformM44(colors, m_lightColorsUniform, transpose);
133 void SetAnimatedUV(
const v4& uv)
const
135 SetUniformV4(uv, m_animatedUVUniform);
138 void SetAltitudeEnvMapFade(
const float v)
const
140 SetUniformFloat(v, m_altitudeEnvMapFadeUniform);
145 SetUniformV2(min, m_minUVRangeUniform);
146 SetUniformV2(max, m_maxUVRangeUniform);
151 SetUniformV4(min, m_minVertRangeUniform);
152 SetUniformV4(max, m_maxVertRangeUniform);
155 void SetWaveDisplacement(
const float v)
const
157 SetUniformFloat(v, m_reflectionWaveDisplacementUniform);
160 void SetCameraRelativeOrigin(
const Eegeo::v3& cameraRelativeOrigin)
const
162 SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
168 SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSampler);
169 SetUniformTextureSampler(glState, GetNormalSamplerId(), m_normalMapTextureSampler);
170 SetUniformTextureSampler(glState, GetReflectionSamplerId(), m_reflectionMapTextureSampler);
174 WaterShader(
const TShaderId shaderId,
const std::string& vertexShaderCode,
const std::string& fragmentShaderCode) :
Shader(shaderId)
176 CompileProgram(vertexShaderCode, fragmentShaderCode);
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");
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");
194 m_cameraRelativeModelOriginUniformLocation = GetUniformLocation(
"CameraRelativeModelOrigin");
196 m_fogShaderUniforms.DetermineUniformsFromShader(
this);
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;