All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PackedDiffuseFoggedShader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Rendering.h"
6 #include "Shader.h"
7 #include "GLState.h"
8 #include "GLHelpers.h"
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"
16 
17 namespace Eegeo
18 {
19  namespace Rendering
20  {
21  namespace Shaders
22  {
23  namespace PackedDiffuseFoggedShaderCode
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 DiffuseName = "Diffuse";
34  const std::string LightDotUnpackName = "LightDotUnpack";
35 
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;
49 
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";
55 
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"
64  "}";
65 
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"
72  "}";
73 
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"
78  "}";
79 
80  }
81 
83  {
84  public:
85  static PackedDiffuseFoggedShader* Create(const TShaderId shaderId)
86  {
87  return Eegeo_NEW(PackedDiffuseFoggedShader)(
88  shaderId,
89  PackedDiffuseFoggedShaderCode::_vertexDecls + PackedDiffuseFoggedShaderCode::_vertexCode,
90  PackedDiffuseFoggedShaderCode::_fragmentDecls + PackedDiffuseFoggedShaderCode::_fragmentCode
91  );
92  }
93 
94  static PackedDiffuseFoggedShader* CreateWithPunchThrough(const TShaderId shaderId)
95  {
96  return Eegeo_NEW(PackedDiffuseFoggedShader)(
97  shaderId,
98  PackedDiffuseFoggedShaderCode::_vertexDecls + PackedDiffuseFoggedShaderCode::_vertexCode,
99  PackedDiffuseFoggedShaderCode::_fragmentDecls + PackedDiffuseFoggedShaderCode::_fragmentPunchThroughCode
100  );
101  }
102 
103  const GLuint GetDiffuseSamplerId() const { return 0; }
104 
105  void SetMVP(const m44& mvp) const
106  {
107  bool transpose = false;
108  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
109  }
110 
112  {
113  Eegeo::v4 FogColour;
114  Eegeo::v3 WorldUp;
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;
125  };
126 
127  void SetFogUniforms(Lighting::GlobalFoggingUniformValues& fogUniformValues) const
128  {
129  Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
130  }
131 
132  void SetLightColors(const m44& colors) const
133  {
134  bool transpose = false;
135  SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
136  }
137 
138  void SetUVBounds(const Eegeo::v2& min, const Eegeo::v2& max) const
139  {
140  SetUniformV2(min, m_minUVRangeUniformLocation);
141  SetUniformV2(max, m_maxUVRangeUniformLocation);
142  }
143 
144  void SetPositionBounds(const Eegeo::v4& min, const Eegeo::v4& max) const
145  {
146  SetUniformV4(min, m_minPosRangeUniformLocation);
147  SetUniformV4(max, m_maxPosRangeUniformLocation);
148  }
149 
150  void SetCameraRelativeOrigin(const Eegeo::v3& cameraRelativeOrigin) const
151  {
152  SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
153  }
154 
155  void Use(Rendering::GLState& glState) const
156  {
157  UseProgram(glState);
158  SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
159 
160  const GLfloat lightDotUnpack[] = {1.f, 32.f, 1024.f};
161  SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
162  }
163 
164  protected:
165  PackedDiffuseFoggedShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
166  {
167  CompileProgram(vertexShaderCode, fragmentShaderCode);
168 
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);
178 
179  m_fogShaderUniforms.DetermineUniformsFromShader(this);
180  }
181 
182  private:
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;
192 
193  Rendering::Shaders::FogShaderUniforms m_fogShaderUniforms;
194  };
195  }
196  }
197 }