All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
LightmappedPackedDiffuseTransitionShader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "PackedDiffuseTransitionShader.h"
6 #include "ShaderMacros.h"
7 #include "FogShaderIncludes.h"
8 #include "VectorMath.h"
9 #include "Graphics.h"
10 #include "LightmappedPackedDiffuseShader.h"
11 #include <string>
12 
13 namespace Eegeo
14 {
15  namespace Rendering
16  {
17  namespace Shaders
18  {
19  namespace LightmappedPackedDiffuseTransitionShaderCode
20  {
21  const std::string UV2Name = "UV2";
22  const std::string GlowmapName = "Glowmap";
23  const std::string GlowmapColourName = "GlowmapColour";
24  const std::string GlowmapParamName = "GlowmapParam";
25  const std::string AmbientColourName = "AmbientColour";
26 
27  const std::string _vertexDecls =
28  "attribute lowp vec2 "+UV2Name+";\n"
29  "varying highp vec2 DestinationUV2;\n";
30 
31  const std::string _fragmentDecls =
32  "varying highp vec2 DestinationUV2;\n"
33  "uniform lowp float "+GlowmapParamName+";\n"
34  "uniform lowp vec3 "+GlowmapColourName+";\n"
35  "uniform sampler2D "+GlowmapName+";\n"
36  "uniform lowp vec3 "+AmbientColourName+";\n";
37 
38  const std::string _vertexCode =
39  FOG_VERTEX_SHADER_FUNCTIONS
40  "void main(void) { \n"
41  "DestinationUV = mix(MinUVRange, MaxUVRange, UV);\n"
42  "DestinationUV2 = UV2;\n"
43  "highp vec3 unpackedPosition = mix(MinPosRange.xyz, MaxPosRange.xyz, Position.xyz);\n"
44  "ColorVarying.rgb = (LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0)).rgb;\n"
45  "ColorVarying.a = CalcHeightFogDensity(unpackedPosition);\n"
46  "gl_Position = UnpackModelViewProjectionMatrix * vec4(Position.rgb, 1.0);\n"
47  "}";
48 
49  const std::string _fragmentPunchThroughCode =
50  "void main(void) { \n"
51  "highp vec4 col = " TEXTURE2D(Diffuse, DestinationUV) "; \n"
52  "if(col.w<" EEGEO_ALPHA_TEST_VALUE ") discard; \n"
53  "gl_FragColor.rgb = col.rgb * ColorVarying.rgb; \n"
54  "gl_FragColor.a = 1.0;\n"
55  "}";
56 
57  const std::string _fragmentCode =
58  "void main(void) { \n"
59  "highp vec3 fromColor = " TEXTURE2D(DiffuseFrom, DestinationUV) ".rgb; \n"
60  "highp vec3 toColor = " TEXTURE2D(DiffuseTo, DestinationUV) ".rgb; \n"
61  "lowp vec3 diffuse = mix(fromColor, toColor, LerpParam);\n"
62  "lowp vec3 dayColour = diffuse * ColorVarying.rgb;\n"
63  "lowp float luma = dot(diffuse, vec3(0.3, 0.59, 0.11));\n"
64  "lowp vec3 greyscale = vec3(luma, luma, luma);\n"
65  "lowp vec3 glow = "+GlowmapColourName+" * " TEXTURE2D(Glowmap, DestinationUV2) ".rgb;\n"
66  "lowp vec3 desatDiffuse = mix(greyscale, diffuse, 0.2);\n"
67  "lowp vec3 nightColour = desatDiffuse * (glow + "+AmbientColourName+");\n"
68  "gl_FragColor.rgb = mix(mix(dayColour, nightColour, "+GlowmapParamName+"), FogColour.rgb, ColorVarying.a); \n"
69  "gl_FragColor.a = 1.0;\n"
70  "}";
71  }
72 
74  {
75  public:
76  static LightmappedPackedDiffuseTransitionShader* Create(const TShaderId shaderId)
77  {
79  shaderId,
80  PackedDiffuseTransitionShaderCode::_vertexDecls + LightmappedPackedDiffuseTransitionShaderCode::_vertexDecls + LightmappedPackedDiffuseTransitionShaderCode::_vertexCode,
81  PackedDiffuseTransitionShaderCode::_fragmentDecls + LightmappedPackedDiffuseTransitionShaderCode::_fragmentDecls + LightmappedPackedDiffuseTransitionShaderCode::_fragmentCode
82  );
83  }
84 
85  const GLuint GetLightmapSamplerId() const { return 2; }
86 
87  void SetLightmapColour(const v3& color) const
88  {
89  SetUniformV3(color, m_glowMapColourUniformLocation);
90  }
91 
92  void SetLightmapIntensity(const float intensity) const
93  {
94  SetUniformFloat(intensity, m_glowMapParamUniformLocation);
95  }
96 
97  void SetAmbientColor(const v3& color) const
98  {
99  SetUniformV3(color, m_ambientUniformLocation);
100  }
101 
102  void Use(Rendering::GLState& glState) const
103  {
104  UseProgram(glState);
105 
106  SetUniformTextureSampler(glState, GetFromDiffuseSamplerId(), m_fromDiffuseTextureSamplerUniformLocation);
107  SetUniformTextureSampler(glState, GetToDiffuseSamplerId(), m_toDiffuseTextureSamplerUniformLocation);
108  SetUniformTextureSampler(glState, GetLightmapSamplerId(), m_glowMapTextureSamplerUniformLocation);
109 
110  const GLfloat lightDotUnpack[] = {1.f, 32.f, 1024.f};
111  SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
112  }
113 
114  protected:
115  LightmappedPackedDiffuseTransitionShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode)
116  : PackedDiffuseTransitionShader(shaderId, vertexShaderCode, fragmentShaderCode)
117  {
118  m_glowMapColourUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::GlowmapColourName);
119  m_glowMapParamUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::GlowmapParamName);
120 
121  m_glowMapTextureSamplerUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::GlowmapName);
122 
123  m_lightDotUnpackUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::LightDotUnpackName);
124  m_ambientUniformLocation = GetUniformLocation(LightmappedPackedDiffuseShaderCode::AmbientColourName);
125  }
126 
127  private:
128  GLuint m_glowMapColourUniformLocation;
129  GLuint m_glowMapParamUniformLocation;
130  GLuint m_glowMapTextureSamplerUniformLocation;
131  GLuint m_lightDotUnpackUniformLocation;
132  GLuint m_ambientUniformLocation;
133  };
134  }
135  }
136 }