All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WeatherOverlayShader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Shader.h"
6 #include "IdTypes.h"
7 #include "Rendering.h"
8 #include "Graphics.h"
9 #include "VectorMath.h"
10 #include <string>
11 
12 namespace Eegeo
13 {
14  namespace Rendering
15  {
16  namespace Shaders
17  {
18  namespace WeatherOverlayShaderCode
19  {
20  const std::string PositionName = "Position";
21  const std::string UVName = "UV";
22  const std::string DiffuseName = "Diffuse";
23  const std::string LayerAlphasName = "LayerAlphas";
24  const std::string UvOffsetAndScaleLayerOne = "UvOffsetAndScaleLayerOne";
25  const std::string UvOffsetAndScaleLayerTwo = "UvOffsetAndScaleLayerTwo";
26 
27 
28  const std::string _vertexDecls =
29  "attribute lowp vec2 "+PositionName+";\n"
30  "attribute lowp vec2 "+UVName+";\n"
31  "varying mediump vec2 DestUvLayerOne;\n"
32  "varying mediump vec2 DestUvLayerTwo;\n"
33  "uniform mediump vec4 "+UvOffsetAndScaleLayerOne+";\n"
34  "uniform mediump vec4 "+UvOffsetAndScaleLayerTwo+";\n";
35 
36  const std::string _fragmentDecls =
37  "varying mediump vec2 DestUvLayerOne;\n"
38  "varying mediump vec2 DestUvLayerTwo;\n"
39  "uniform sampler2D "+DiffuseName+";\n"
40  "uniform lowp vec2 "+LayerAlphasName+";\n";
41 
42 
43  const std::string _vertexCode =
44  "void main()\n"
45  "{\n"
46  "DestUvLayerOne = UvOffsetAndScaleLayerOne.xy + (UV.xy * UvOffsetAndScaleLayerOne.zw);\n"
47  "DestUvLayerTwo = UvOffsetAndScaleLayerTwo.xy + (UV.xy * UvOffsetAndScaleLayerTwo.zw);\n"
48  "gl_Position = vec4(Position.xy, 0.0, 1.0);\n"
49  "}\n";
50 
51  const std::string _fragmentCode =
52  "void main()\n"
53  "{\n"
54  "lowp vec3 layerOne = texture2D(Diffuse, DestUvLayerOne).rgb*LayerAlphas.x;\n"
55  "lowp vec3 layerTwo = texture2D(Diffuse, DestUvLayerTwo).rgb*LayerAlphas.y;\n"
56  "gl_FragColor.rgb = layerOne + layerTwo;\n"
57  "gl_FragColor.a = 1.0;\n"
58  "}\n";
59  }
60 
62  {
63  public:
64  static WeatherOverlayShader* Create(const TShaderId shaderId)
65  {
66  return Eegeo_NEW(WeatherOverlayShader)(
67  shaderId,
68  WeatherOverlayShaderCode::_vertexDecls + WeatherOverlayShaderCode::_vertexCode,
69  WeatherOverlayShaderCode::_fragmentDecls + WeatherOverlayShaderCode::_fragmentCode
70  );
71  }
72 
73  const GLuint GetDiffuseSamplerId() const { return 0; }
74  void SetMVP(const m44& mvp) const
75  {
76 
77  }
78 
79  void SetLayerAlphas(Eegeo::v2 layerAlphas)
80  {
81  SetUniformV2(layerAlphas, m_layerAlphasUniformLocation);
82  }
83 
84  void SetUVOffsetAndScaleLayerUniforms(Eegeo::v4 UvOffsetAndScaleLayerOne, Eegeo::v4 UvOffsetAndScaleLayerTwo)
85  {
86  SetUniformV4(UvOffsetAndScaleLayerOne, m_uvOffsetAndScaleLayerOneUniformLocation);
87  SetUniformV4(UvOffsetAndScaleLayerTwo, m_uvOffsetAndScaleLayerTwoUniformLocation);
88  }
89 
90  void Use(Rendering::GLState& glState) const
91  {
92  UseProgram(glState);
93  SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
94  }
95 
96  protected:
97  WeatherOverlayShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
98  {
99  CompileProgram(vertexShaderCode, fragmentShaderCode);
100 
101  m_diffuseTextureSamplerUniformLocation = GetUniformLocation(WeatherOverlayShaderCode::DiffuseName);
102  m_layerAlphasUniformLocation = GetUniformLocation(WeatherOverlayShaderCode::LayerAlphasName);
103  m_uvOffsetAndScaleLayerOneUniformLocation = GetUniformLocation(WeatherOverlayShaderCode::UvOffsetAndScaleLayerOne);
104  m_uvOffsetAndScaleLayerTwoUniformLocation = GetUniformLocation(WeatherOverlayShaderCode::UvOffsetAndScaleLayerTwo);
105  }
106 
107  private:
108  GLuint m_diffuseTextureSamplerUniformLocation;
109  GLuint m_layerAlphasUniformLocation ;
110  GLuint m_uvOffsetAndScaleLayerOneUniformLocation;
111  GLuint m_uvOffsetAndScaleLayerTwoUniformLocation;
112  };
113  }
114  }
115 }