All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BatchedIconShader.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 "Graphics.h"
8 #include "VectorMath.h"
9 #include "Rendering.h"
10 #include <string>
11 
12 namespace Eegeo
13 {
14  namespace Rendering
15  {
16  namespace Shaders
17  {
18  namespace BatchedIconShaderCode
19  {
20  const std::string PositionName = "Position";
21  const std::string UVName = "UV";
22  const std::string ModelViewProjectionMatrixName = "ModelViewProjectionMatrix";
23  const std::string ParamsName = "Color";
24  const std::string IconColorName = "IconColor";
25 
26  const std::string _vertexDecls =
27  "attribute highp vec3 "+PositionName+";\n"
28  "attribute mediump vec2 "+UVName+";\n"
29  "attribute lowp vec4 "+ParamsName+";\n"
30 
31  "varying mediump vec2 DestinationUV;\n"
32  "varying lowp vec4 DestinationColor;\n"
33  "uniform lowp vec4 "+IconColorName+";\n"
34  "uniform highp mat4 "+ModelViewProjectionMatrixName+";\n";
35 
36  const std::string DiffuseName = "Diffuse";
37 
38  const std::string _fragmentDecls =
39  "varying mediump vec2 DestinationUV;\n"
40  "varying lowp vec4 DestinationColor;\n"
41  "uniform sampler2D "+DiffuseName+";\n";
42 
43  const std::string _vertexCode = "void main(void) { \n"
44  "DestinationUV = UV;\n"
45  "DestinationColor = "+IconColorName+";\n"
46  "DestinationColor.a *= "+ParamsName+".x;\n"
47  "gl_Position = ModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
48  "}";
49 
50  const std::string _fragmentCode = "void main(void) { \n"
51  "gl_FragColor = DestinationColor * texture2D("+DiffuseName+", DestinationUV.xy);\n"
52  "}";
53  }
54 
58  class BatchedIconShader : public Shader
59  {
60  public:
61  static BatchedIconShader* Create(const TShaderId shaderId)
62  {
63  return Eegeo_NEW(BatchedIconShader)(
64  shaderId,
65  BatchedIconShaderCode::_vertexDecls + BatchedIconShaderCode::_vertexCode,
66  BatchedIconShaderCode::_fragmentDecls + BatchedIconShaderCode::_fragmentCode);
67  }
68 
69  const GLuint GetDiffuseSamplerId() const { return 0; }
70 
71  void SetMVP(const m44& mvp) const
72  {
73  bool transpose = false;
74  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
75  }
76 
77  void SetColor(const v4& color) const
78  {
79  SetUniformV4(color, m_colorUniformLocation);
80  }
81 
82  void Use(Rendering::GLState& glState) const
83  {
84  UseProgram(glState);
85  SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
86  }
87 
88  protected:
89  BatchedIconShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
90  {
91  CompileProgram(vertexShaderCode, fragmentShaderCode);
92 
93  m_mvpUniformLocation = GetUniformLocation(BatchedIconShaderCode::ModelViewProjectionMatrixName);
94  m_colorUniformLocation = GetUniformLocation(BatchedIconShaderCode::IconColorName);
95  m_diffuseTextureSamplerUniformLocation = GetUniformLocation(BatchedIconShaderCode::DiffuseName);
96  }
97 
98  private:
99  GLuint m_mvpUniformLocation ;
100  GLuint m_colorUniformLocation;
101  GLuint m_diffuseTextureSamplerUniformLocation;
102  };
103  }
104  }
105 }