6 #include "ShaderMacros.h"
10 #include "VectorMath.h"
11 #include "FogShaderOld.h"
12 #include "FogShaderUniforms.h"
13 #include "FogShaderIncludes.h"
14 #include "GlobalFogging.h"
23 namespace PackedDiffuseTransitionShaderCode
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 DiffuseFromName =
"DiffuseFrom";
34 const std::string DiffuseToName =
"DiffuseTo";
35 const std::string LerpParamName =
"LerpParam";
36 const std::string LightDotUnpackName =
"LightDotUnpack";
38 const std::string _vertexDecls =
39 "attribute highp vec4 "+PositionName+
";\n"
40 "attribute highp vec2 "+UVName+
";\n"
41 "varying lowp vec4 ColorVarying;\n"
42 "varying mediump vec2 DestinationUV;\n"
43 "uniform highp mat4 "+UnpackModelViewProjectionMatrixName+
";\n"
44 "uniform lowp mat4 "+LightColorMatrixName+
";\n"
45 "uniform highp vec2 "+MinUVRangeName+
";\n"
46 "uniform highp vec2 "+MaxUVRangeName+
";\n"
47 "uniform highp vec4 "+MinPosRangeName+
";\n"
48 "uniform highp vec4 "+MaxPosRangeName+
";\n"
49 "uniform highp vec3 "+LightDotUnpackName+
";\n"
50 FOG_VERTEX_SHADER_UNIFORMS;
52 const std::string _fragmentDecls =
53 "varying lowp vec4 ColorVarying;\n"
54 "varying mediump vec2 DestinationUV;\n"
55 "uniform sampler2D "+DiffuseFromName+
";\n"
56 "uniform sampler2D "+DiffuseToName+
";\n"
57 "uniform lowp vec4 "+Rendering::FogShaderIncludes::FogColourName+
";\n"
58 "uniform lowp float "+LerpParamName+
";\n";
60 const std::string _vertexCode =
61 FOG_VERTEX_SHADER_FUNCTIONS
62 "void main(void) { \n"
63 "DestinationUV = mix(MinUVRange, MaxUVRange, UV);\n"
64 "highp vec3 unpackedPosition = mix(MinPosRange.xyz, MaxPosRange.xyz, Position.xyz);\n"
65 "ColorVarying.rgb = (LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0)).rgb;\n"
66 "ColorVarying.a = CalcHeightFogDensity(unpackedPosition);\n"
67 "gl_Position = UnpackModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
70 const std::string _fragmentPunchThroughCode =
71 "void main(void) { \n"
72 "highp vec4 fromColor = " TEXTURE2D(DiffuseFrom, DestinationUV)
"; \n"
73 "highp vec4 toColor = " TEXTURE2D(DiffuseTo, DestinationUV)
"; \n"
74 "highp vec4 col = mix(fromColor, toColor, LerpParam);\n"
75 "if(col.w<" EEGEO_ALPHA_TEST_VALUE
") discard; \n"
76 "gl_FragColor.rgb = mix(col.rgb * ColorVarying.rgb, FogColour.rgb, ColorVarying.a); \n"
77 "gl_FragColor.a = 1.0;\n"
80 const std::string _fragmentCode =
81 "void main(void) { \n"
82 "highp vec3 fromColor = " TEXTURE2D(DiffuseFrom, DestinationUV)
".rgb; \n"
83 "highp vec3 toColor = " TEXTURE2D(DiffuseTo, DestinationUV)
".rgb; \n"
84 "gl_FragColor.rgb = mix(mix(fromColor, toColor, LerpParam) * ColorVarying.rgb, FogColour.rgb, ColorVarying.a);\n"
85 "gl_FragColor.a = 1.0;\n"
96 PackedDiffuseTransitionShaderCode::_vertexDecls + PackedDiffuseTransitionShaderCode::_vertexCode,
97 PackedDiffuseTransitionShaderCode::_fragmentDecls + PackedDiffuseTransitionShaderCode::_fragmentCode
105 PackedDiffuseTransitionShaderCode::_vertexDecls + PackedDiffuseTransitionShaderCode::_vertexCode,
106 PackedDiffuseTransitionShaderCode::_fragmentDecls + PackedDiffuseTransitionShaderCode::_fragmentPunchThroughCode
110 const GLuint GetFromDiffuseSamplerId()
const {
return 0; }
111 const GLuint GetToDiffuseSamplerId()
const {
return 1; }
115 Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
118 void SetMVP(
const m44& mvp)
const
120 bool transpose =
false;
121 SetUniformM44(mvp, m_mvpUniformLocation, transpose);
124 void SetLightColors(
const m44& colors)
const
126 bool transpose =
false;
127 SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
132 SetUniformV2(min, m_minUVRangeUniformLocation);
133 SetUniformV2(max, m_maxUVRangeUniformLocation);
136 void SetLerpParam(
float t)
const
138 SetUniformFloat(t, m_lerpParamUniformLocation);
143 SetUniformV4(min, m_minPosRangeUniformLocation);
144 SetUniformV4(max, m_maxPosRangeUniformLocation);
147 void SetCameraRelativeOrigin(
const Eegeo::v3& cameraRelativeOrigin)
const
149 SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
155 SetUniformTextureSampler(glState, GetFromDiffuseSamplerId(), m_fromDiffuseTextureSamplerUniformLocation);
156 SetUniformTextureSampler(glState, GetToDiffuseSamplerId(), m_toDiffuseTextureSamplerUniformLocation);
158 const GLfloat lightDotUnpack[] = {1.f, 32.f, 1024.f};
159 SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
165 CompileProgram(vertexShaderCode, fragmentShaderCode);
167 m_mvpUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::UnpackModelViewProjectionMatrixName);
168 m_lightColorsUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::LightColorMatrixName);
169 m_minUVRangeUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::MinUVRangeName);
170 m_maxUVRangeUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::MaxUVRangeName);
171 m_minPosRangeUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::MinPosRangeName);
172 m_maxPosRangeUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::MaxPosRangeName);
174 m_fromDiffuseTextureSamplerUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::DiffuseFromName);
175 m_toDiffuseTextureSamplerUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::DiffuseToName);
176 m_lerpParamUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::LerpParamName);
177 m_lightDotUnpackUniformLocation = GetUniformLocation(PackedDiffuseTransitionShaderCode::LightDotUnpackName);
178 m_cameraRelativeModelOriginUniformLocation = GetUniformLocation(Eegeo::Rendering::FogShaderIncludes::CameraRelativeModelOriginName);
180 m_fogShaderUniforms.DetermineUniformsFromShader(
this);
184 GLuint m_mvpUniformLocation ;
185 GLuint m_lightColorsUniformLocation;
186 GLuint m_minUVRangeUniformLocation;
187 GLuint m_maxUVRangeUniformLocation;
188 GLuint m_minPosRangeUniformLocation;
189 GLuint m_maxPosRangeUniformLocation;
190 GLuint m_toDiffuseTextureSamplerUniformLocation;
191 GLuint m_fromDiffuseTextureSamplerUniformLocation;
192 GLuint m_lerpParamUniformLocation;
193 GLuint m_lightDotUnpackUniformLocation;
194 GLuint m_cameraRelativeModelOriginUniformLocation;