All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
LightmappedPackedDiffuseShader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "PackedDiffuseShader.h"
6 #include "ShaderMacros.h"
7 #include "VectorMath.h"
8 #include "FogShaderIncludes.h"
9 #include "FogShaderUniforms.h"
10 #include "Graphics.h"
11 #include "FogShaderOld.h"
12 #include "GlobalFogging.h"
13 #include <string>
14 
15 namespace Eegeo
16 {
17  namespace Rendering
18  {
19  namespace Shaders
20  {
21  namespace LightmappedPackedDiffuseShaderCode
22  {
23  const std::string UV2Name = "UV2";
24  const std::string GlowmapName = "Glowmap";
25  const std::string GlowmapColourName = "GlowmapColour";
26  const std::string GlowmapParamName = "GlowmapParam";
27  const std::string AmbientColourName = "AmbientColour";
28  const std::string MinPosRangeName = "MinPosRange";
29  const std::string MaxPosRangeName = "MaxPosRange";
30 
31  const std::string _vertexDecls =
32  "attribute lowp vec2 "+UV2Name+";\n"
33  "varying highp vec2 DestinationUV2;\n"
34  "uniform highp vec4 "+MinPosRangeName+";\n"
35  "uniform highp vec4 "+MaxPosRangeName+";\n"
36  FOG_VERTEX_SHADER_UNIFORMS;
37 
38  const std::string _fragmentDecls =
39  "varying highp vec2 DestinationUV2;\n"
40  "uniform lowp float "+GlowmapParamName+";\n"
41  "uniform lowp vec3 "+GlowmapColourName+";\n"
42  "uniform lowp vec3 "+AmbientColourName+";\n"
43  "uniform lowp vec4 "+Rendering::FogShaderIncludes::FogColourName+";\n"
44  "uniform sampler2D "+GlowmapName+";\n";
45 
46  const std::string _vertexCode =
47  FOG_VERTEX_SHADER_FUNCTIONS
48  "void main(void) { \n"
49  "DestinationUV = mix(MinUVRange, MaxUVRange, UV);\n"
50  "DestinationUV2 = UV2;\n"
51  "highp vec3 unpackedPosition = mix(MinPosRange.xyz, MaxPosRange.xyz, Position.xyz);\n"
52  "ColorVarying.rgb = (LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0)).rgb;\n"
53  "ColorVarying.a = CalcHeightFogDensity(unpackedPosition);\n"
54  "gl_Position = UnpackModelViewProjectionMatrix * vec4(Position.rgb, 1.0);\n"
55  "}";
56 
57  const std::string _fragmentPunchThroughCode =
58  "void main(void) { \n"
59  "highp vec4 col = " TEXTURE2D(Diffuse, DestinationUV) "; \n"
60  "if(col.w<" EEGEO_ALPHA_TEST_VALUE ") discard; \n"
61  "gl_FragColor.rgb = col.rgb * ColorVarying.rgb; \n"
62  "gl_FragColor.a = 1.0;\n"
63  "}";
64 
65  const std::string _fragmentCode =
66  "void main(void) { \n"
67  "lowp vec3 diffuse = " TEXTURE2D(Diffuse, DestinationUV) ".rgb;\n"
68  "lowp vec3 dayColour = diffuse * ColorVarying.rgb;\n"
69  "lowp float luma = dot(diffuse, vec3(0.3, 0.59, 0.11));\n"
70  "lowp vec3 greyscale = vec3(luma, luma, luma);\n"
71  "lowp vec3 glow = "+GlowmapColourName+" * " TEXTURE2D(Glowmap, DestinationUV2) ".rgb;\n"
72  "lowp vec3 desatDiffuse = mix(greyscale, diffuse, 0.2);\n"
73  "lowp vec3 nightColour = desatDiffuse * (glow + "+AmbientColourName+");\n"
74  "gl_FragColor.rgb = mix(mix(dayColour, nightColour, "+GlowmapParamName+"), FogColour.rgb, ColorVarying.a); \n"
75  "gl_FragColor.a = 1.0;\n"
76  "}";
77  }
78 
80  {
81  public:
82  static LightmappedPackedDiffuseShader* Create(const TShaderId shaderId)
83  {
84  return Eegeo_NEW(LightmappedPackedDiffuseShader)(
85  shaderId,
86  PackedDiffuseShaderCode::_vertexDecls + LightmappedPackedDiffuseShaderCode::_vertexDecls + LightmappedPackedDiffuseShaderCode::_vertexCode,
87  PackedDiffuseShaderCode::_fragmentDecls + LightmappedPackedDiffuseShaderCode::_fragmentDecls + LightmappedPackedDiffuseShaderCode::_fragmentCode
88  );
89  }
90 
91  const GLuint GetLightmapSamplerId() const { return 1; }
92 
93  void SetFogUniforms(Lighting::GlobalFoggingUniformValues& fogUniformValues) const
94  {
95  Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
96  }
97 
98  void SetLightmapColour(const v3& color) const
99  {
100  SetUniformV3(color, m_glowMapColourUniformLocation);
101  }
102 
103  void SetLightmapIntensity(const float intensity) const
104  {
105  SetUniformFloat(intensity, m_glowMapParamUniformLocation);
106  }
107 
108  void SetAmbientColor(const v3& color) const
109  {
110  SetUniformV3(color, m_ambientUniformLocation);
111  }
112 
113  void SetPositionBounds(const Eegeo::v4& min, const Eegeo::v4& max) const
114  {
115  SetUniformV4(min, m_minPosRangeUniformLocation);
116  SetUniformV4(max, m_maxPosRangeUniformLocation);
117  }
118 
119  void SetCameraRelativeOrigin(const Eegeo::v3& cameraRelativeOrigin) const
120  {
121  SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
122  }
123 
124  void Use(Rendering::GLState& glState) const
125  {
126  UseProgram(glState);
127 
128  SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
129  SetUniformTextureSampler(glState, GetLightmapSamplerId(), m_glowMapTextureSamplerUniformLocation);
130 
131  const GLfloat lightDotUnpack[] = {1.f, 32.f, 1024.f};
132  SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
133  }
134 
135  protected:
136  LightmappedPackedDiffuseShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode)
137  : PackedDiffuseShader(shaderId, vertexShaderCode, fragmentShaderCode)
138  {
139  m_glowMapColourUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::GlowmapColourName);
140  m_glowMapParamUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::GlowmapParamName);
141 
142  m_diffuseTextureSamplerUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::DiffuseName);
143  m_glowMapTextureSamplerUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::GlowmapName);
144 
145  m_lightDotUnpackUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::LightDotUnpackName);
146  m_ambientUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::AmbientColourName);
147 
148  m_cameraRelativeModelOriginUniformLocation = GetUniformLocation(Eegeo::Rendering::FogShaderIncludes::CameraRelativeModelOriginName);
149 
150  m_minPosRangeUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::MinPosRangeName);
151  m_maxPosRangeUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::MaxPosRangeName);
152 
153  m_fogShaderUniforms.DetermineUniformsFromShader(this);
154  }
155 
156  private:
157  GLuint m_glowMapColourUniformLocation;
158  GLuint m_glowMapParamUniformLocation;
159  GLuint m_diffuseTextureSamplerUniformLocation;
160  GLuint m_glowMapTextureSamplerUniformLocation;
161  GLuint m_lightDotUnpackUniformLocation;
162  GLuint m_ambientUniformLocation;
163  GLuint m_minPosRangeUniformLocation;
164  GLuint m_maxPosRangeUniformLocation;
165  GLuint m_cameraRelativeModelOriginUniformLocation;
166 
167  Rendering::Shaders::FogShaderUniforms m_fogShaderUniforms;
168  };
169  }
170  }
171 }