All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PackedDiffuseTransitionShader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Shader.h"
6 #include "ShaderMacros.h"
7 #include "IdTypes.h"
8 #include "Graphics.h"
9 #include "Rendering.h"
10 #include "VectorMath.h"
11 #include "FogShaderOld.h"
12 #include "FogShaderUniforms.h"
13 #include "FogShaderIncludes.h"
14 #include "GlobalFogging.h"
15 #include <string>
16 
17 namespace Eegeo
18 {
19  namespace Rendering
20  {
21  namespace Shaders
22  {
23  namespace PackedDiffuseTransitionShaderCode
24  {
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";
37 
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;
51 
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";
59 
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"
68  "}";
69 
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"
78  "}";
79 
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"
86  "}";
87  }
88 
90  {
91  public:
92  static PackedDiffuseTransitionShader* Create(const TShaderId shaderId)
93  {
94  return Eegeo_NEW(PackedDiffuseTransitionShader)(
95  shaderId,
96  PackedDiffuseTransitionShaderCode::_vertexDecls + PackedDiffuseTransitionShaderCode::_vertexCode,
97  PackedDiffuseTransitionShaderCode::_fragmentDecls + PackedDiffuseTransitionShaderCode::_fragmentCode
98  );
99  }
100 
101  static PackedDiffuseTransitionShader* CreateWithPunchThrough(const TShaderId shaderId)
102  {
103  return Eegeo_NEW(PackedDiffuseTransitionShader)(
104  shaderId,
105  PackedDiffuseTransitionShaderCode::_vertexDecls + PackedDiffuseTransitionShaderCode::_vertexCode,
106  PackedDiffuseTransitionShaderCode::_fragmentDecls + PackedDiffuseTransitionShaderCode::_fragmentPunchThroughCode
107  );
108  }
109 
110  const GLuint GetFromDiffuseSamplerId() const { return 0; }
111  const GLuint GetToDiffuseSamplerId() const { return 1; }
112 
113  void SetFogUniforms(Lighting::GlobalFoggingUniformValues& fogUniformValues) const
114  {
115  Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
116  }
117 
118  void SetMVP(const m44& mvp) const
119  {
120  bool transpose = false;
121  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
122  }
123 
124  void SetLightColors(const m44& colors) const
125  {
126  bool transpose = false;
127  SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
128  }
129 
130  void SetUVBounds(const Eegeo::v2& min, const Eegeo::v2& max) const
131  {
132  SetUniformV2(min, m_minUVRangeUniformLocation);
133  SetUniformV2(max, m_maxUVRangeUniformLocation);
134  }
135 
136  void SetLerpParam(float t) const
137  {
138  SetUniformFloat(t, m_lerpParamUniformLocation);
139  }
140 
141  void SetPositionBounds(const Eegeo::v4& min, const Eegeo::v4& max) const
142  {
143  SetUniformV4(min, m_minPosRangeUniformLocation);
144  SetUniformV4(max, m_maxPosRangeUniformLocation);
145  }
146 
147  void SetCameraRelativeOrigin(const Eegeo::v3& cameraRelativeOrigin) const
148  {
149  SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
150  }
151 
152  void Use(Rendering::GLState& glState) const
153  {
154  UseProgram(glState);
155  SetUniformTextureSampler(glState, GetFromDiffuseSamplerId(), m_fromDiffuseTextureSamplerUniformLocation);
156  SetUniformTextureSampler(glState, GetToDiffuseSamplerId(), m_toDiffuseTextureSamplerUniformLocation);
157 
158  const GLfloat lightDotUnpack[] = {1.f, 32.f, 1024.f};
159  SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
160  }
161 
162  protected:
163  PackedDiffuseTransitionShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
164  {
165  CompileProgram(vertexShaderCode, fragmentShaderCode);
166 
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);
173 
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);
179 
180  m_fogShaderUniforms.DetermineUniformsFromShader(this);
181  }
182 
183  protected:
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;
195 
196  Rendering::Shaders::FogShaderUniforms m_fogShaderUniforms;
197  };
198  }
199  }
200 }
201