9 #include "AllVertexTypes.h"
10 #include "ShaderMacros.h"
11 #include "VertexBinding.h"
12 #include "GlobalFogging.h"
13 #include "FogShaderUniforms.h"
14 #include "FogShaderIncludes.h"
15 #include "FogShaderOld.h"
23 namespace PackedDiffuseFoggedShaderCode
25 const std::string PositionName =
"Position";
26 const std::string UVName =
"UV";
27 const std::string UnpackModelViewProjectionMatrixName =
"UnpackModelViewProjectionMatrix";
28 const std::string LightColorMatrixName =
"LightColorMatrix";
29 const std::string MinUVRangeName =
"MinUVRange";
30 const std::string MaxUVRangeName =
"MaxUVRange";
31 const std::string MinPosRangeName =
"MinPosRange";
32 const std::string MaxPosRangeName =
"MaxPosRange";
33 const std::string DiffuseName =
"Diffuse";
34 const std::string LightDotUnpackName =
"LightDotUnpack";
36 const std::string _vertexDecls =
37 "attribute highp vec4 "+PositionName+
";\n"
38 "attribute highp vec2 "+UVName+
";\n"
39 "varying lowp vec4 ColorVarying;\n"
40 "varying mediump vec2 DestinationUV;\n"
41 "uniform highp mat4 "+UnpackModelViewProjectionMatrixName+
";\n"
42 "uniform lowp mat4 "+LightColorMatrixName+
";\n"
43 "uniform highp vec2 "+MinUVRangeName+
";\n"
44 "uniform highp vec2 "+MaxUVRangeName+
";\n"
45 "uniform highp vec4 "+MinPosRangeName+
";\n"
46 "uniform highp vec4 "+MaxPosRangeName+
";\n"
47 "uniform highp vec3 "+LightDotUnpackName+
";\n"
48 FOG_VERTEX_SHADER_UNIFORMS;
50 const std::string _fragmentDecls =
51 "varying lowp vec4 ColorVarying;\n"
52 "varying mediump vec2 DestinationUV;\n"
53 "uniform lowp vec4 "+Rendering::FogShaderIncludes::FogColourName+
";\n"
54 "uniform sampler2D "+DiffuseName+
";\n";
56 const std::string _vertexCode =
57 FOG_VERTEX_SHADER_FUNCTIONS
58 "void main(void) { \n"
59 "DestinationUV = mix(MinUVRange, MaxUVRange, UV);\n"
60 "highp vec3 unpackedPosition = mix(MinPosRange.xyz, MaxPosRange.xyz, Position.xyz);\n"
61 "ColorVarying.rgb = (LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0)).rgb;\n"
62 "ColorVarying.a = CalcHeightFogDensity(unpackedPosition);\n"
63 "gl_Position = UnpackModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
66 const std::string _fragmentPunchThroughCode =
67 "void main(void) { \n"
68 "highp vec4 col = " TEXTURE2D(Diffuse, DestinationUV)
"; \n"
69 "if(col.w<" EEGEO_ALPHA_TEST_VALUE
") discard; \n"
70 "gl_FragColor.rgb = mix(col.rgb * ColorVarying.rgb, FogColour.rgb, ColorVarying.a); \n"
71 "gl_FragColor.a = 1.0;\n"
74 const std::string _fragmentCode =
75 "void main(void) { \n"
76 "gl_FragColor.rgb = mix(" TEXTURE2D(Diffuse, DestinationUV)
".rgb * ColorVarying.rgb, FogColour.rgb, ColorVarying.a); \n"
77 "gl_FragColor.a = 1.0;\n"
89 PackedDiffuseFoggedShaderCode::_vertexDecls + PackedDiffuseFoggedShaderCode::_vertexCode,
90 PackedDiffuseFoggedShaderCode::_fragmentDecls + PackedDiffuseFoggedShaderCode::_fragmentCode
98 PackedDiffuseFoggedShaderCode::_vertexDecls + PackedDiffuseFoggedShaderCode::_vertexCode,
99 PackedDiffuseFoggedShaderCode::_fragmentDecls + PackedDiffuseFoggedShaderCode::_fragmentPunchThroughCode
103 const GLuint GetDiffuseSamplerId()
const {
return 0; }
105 void SetMVP(
const m44& mvp)
const
107 bool transpose =
false;
108 SetUniformM44(mvp, m_mvpUniformLocation, transpose);
115 float FogDensityGlobal;
116 float HeightFogIntensity;
117 float DistanceFogIntensity;
118 float CameraAltitude;
119 float HeightFogMinAltitude;
120 float HeightFogMaxAltitude;
121 float DistanceFogNear;
122 float DistanceFogFar;
123 float DistanceFogLow;
124 float DistanceFogHigh;
129 Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
132 void SetLightColors(
const m44& colors)
const
134 bool transpose =
false;
135 SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
140 SetUniformV2(min, m_minUVRangeUniformLocation);
141 SetUniformV2(max, m_maxUVRangeUniformLocation);
146 SetUniformV4(min, m_minPosRangeUniformLocation);
147 SetUniformV4(max, m_maxPosRangeUniformLocation);
150 void SetCameraRelativeOrigin(
const Eegeo::v3& cameraRelativeOrigin)
const
152 SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
155 void Use(Rendering::GLState& glState)
const
158 SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
160 const GLfloat lightDotUnpack[] = {1.f, 32.f, 1024.f};
161 SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
165 PackedDiffuseFoggedShader(
const TShaderId shaderId,
const std::string& vertexShaderCode,
const std::string& fragmentShaderCode) : Shader(shaderId)
167 CompileProgram(vertexShaderCode, fragmentShaderCode);
169 m_mvpUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::UnpackModelViewProjectionMatrixName);
170 m_lightColorsUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::LightColorMatrixName);
171 m_minUVRangeUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::MinUVRangeName);
172 m_maxUVRangeUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::MaxUVRangeName);
173 m_minPosRangeUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::MinPosRangeName);
174 m_maxPosRangeUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::MaxPosRangeName);
175 m_diffuseTextureSamplerUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::DiffuseName);
176 m_lightDotUnpackUniformLocation = GetUniformLocation(PackedDiffuseFoggedShaderCode::LightDotUnpackName);
177 m_cameraRelativeModelOriginUniformLocation = GetUniformLocation(Eegeo::Rendering::FogShaderIncludes::CameraRelativeModelOriginName);
179 m_fogShaderUniforms.DetermineUniformsFromShader(
this);
183 GLuint m_mvpUniformLocation ;
184 GLuint m_lightColorsUniformLocation;
185 GLuint m_minUVRangeUniformLocation;
186 GLuint m_maxUVRangeUniformLocation;
187 GLuint m_minPosRangeUniformLocation;
188 GLuint m_maxPosRangeUniformLocation;
189 GLuint m_diffuseTextureSamplerUniformLocation;
190 GLuint m_lightDotUnpackUniformLocation;
191 GLuint m_cameraRelativeModelOriginUniformLocation;
193 Rendering::Shaders::FogShaderUniforms m_fogShaderUniforms;