All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BlueSphereShader.h
1 // Copyright eeGeo Ltd (2012-2015), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Shader.h"
6 #include "IdTypes.h"
7 #include "Graphics.h"
8 #include "VectorMath.h"
9 #include "Rendering.h"
10 #include <string>
11 
12 namespace Eegeo
13 {
14  namespace BlueSphere
15  {
16  namespace BlueSphereShaderCode
17  {
18  const std::string PositionName = "Position";
19  const std::string UVName = "UV";
20  const std::string ModelViewProjectionMatrixName = "ModelViewProjectionMatrix";
21 
22  const std::string _vertexDecls =
23  "attribute highp vec3 "+PositionName+";\n"
24  "attribute mediump vec2 "+UVName+";\n"
25  "varying mediump vec2 DestinationUV;\n"
26  "uniform highp mat4 "+ModelViewProjectionMatrixName+";\n";
27 
28  const std::string DiffuseDayName = "DiffuseDay";
29  const std::string DiffuseNightName = "DiffuseNight";
30  const std::string TextureLerpValueName = "TextureLerpValue";
31  const std::string ColorName = "ColorName";
32  const std::string ColorLerpValueName = "ColorLerpValue";
33 
34  const std::string _fragmentDecls =
35  "varying mediump vec2 DestinationUV;\n"
36  "uniform sampler2D "+DiffuseDayName+";\n"
37  "uniform sampler2D "+DiffuseNightName+";\n"
38  "uniform lowp float "+TextureLerpValueName+";\n"
39  "uniform lowp vec4 "+ColorName+";\n"
40  "uniform lowp float "+ColorLerpValueName+";\n";
41 
42  const std::string _vertexCode = "void main(void) { \n"
43  "DestinationUV = UV;\n"
44  "gl_Position = ModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
45  "}";
46 
47  const std::string _fragmentCode = "void main(void) { \n"
48  "highp vec4 textureColor = mix(texture2D("+DiffuseDayName+", DestinationUV.xy), texture2D("+DiffuseNightName+", DestinationUV.xy), "+TextureLerpValueName+");\n"
49  "gl_FragColor = mix(textureColor, "+ColorName+", "+ColorLerpValueName+");\n"
50  "}";
51  }
52 
54  {
55  public:
56  static BlueSphereShader* Create(const Eegeo::Rendering::TShaderId shaderId)
57  {
58  return Eegeo_NEW(BlueSphereShader)(shaderId,
59  BlueSphereShaderCode::_vertexDecls + BlueSphereShaderCode::_vertexCode,
60  BlueSphereShaderCode::_fragmentDecls + BlueSphereShaderCode::_fragmentCode);
61  }
62 
63  const GLuint GetDiffuseDaySamplerId() const { return 0; }
64  const GLuint GetDiffuseNightSamplerId() const { return 1; }
65 
66  void SetMVP(const Eegeo::m44& mvp) const
67  {
68  bool transpose = false;
69  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
70  }
71 
72  void SetColor(const Eegeo::v4& color) const
73  {
74  SetUniformV4(color, m_colorUniformLocation);
75  }
76 
77  void SetColorLerpValue(float colorLerpValue) const
78  {
79  SetUniformFloat(colorLerpValue, m_colorLerpValueLocation);
80  }
81 
82  void SetTextureLerpValue(float textureLerpValue) const
83  {
84  SetUniformFloat(textureLerpValue, m_textureLerpValueLocation);
85  }
86 
87  void Use(Eegeo::Rendering::GLState& glState) const
88  {
89  UseProgram(glState);
90  SetUniformTextureSampler(glState, GetDiffuseDaySamplerId(), m_diffuseDayTextureSamplerUniformLocation);
91  SetUniformTextureSampler(glState, GetDiffuseNightSamplerId(), m_diffuseNightTextureSamplerUniformLocation);
92  }
93 
94  protected:
95  BlueSphereShader(const Eegeo::Rendering::TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
96  {
97  CompileProgram(vertexShaderCode, fragmentShaderCode);
98 
99  m_mvpUniformLocation = GetUniformLocation(BlueSphereShaderCode::ModelViewProjectionMatrixName);
100  m_colorUniformLocation = GetUniformLocation(BlueSphereShaderCode::ColorName);
101  m_diffuseDayTextureSamplerUniformLocation = GetUniformLocation(BlueSphereShaderCode::DiffuseDayName);
102  m_diffuseNightTextureSamplerUniformLocation = GetUniformLocation(BlueSphereShaderCode::DiffuseNightName);
103  m_colorLerpValueLocation = GetUniformLocation(BlueSphereShaderCode::ColorLerpValueName);
104  m_textureLerpValueLocation = GetUniformLocation(BlueSphereShaderCode::TextureLerpValueName);
105  }
106 
107  private:
108  GLuint m_mvpUniformLocation;
109  GLuint m_colorUniformLocation;
110  GLuint m_colorLerpValueLocation;
111  GLuint m_diffuseDayTextureSamplerUniformLocation;
112  GLuint m_diffuseNightTextureSamplerUniformLocation;
113  GLuint m_textureLerpValueLocation;
114  };
115  }
116 }