All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ColorShader.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 "VectorMath.h"
8 #include "Graphics.h"
9 #include <string>
10 
11 namespace Eegeo
12 {
13  namespace Rendering
14  {
15  namespace Shaders
16  {
17  namespace ColorShaderCode
18  {
19  const std::string PositionName = "Position";
20  const std::string ModelViewProjectionMatrixName = "ModelViewProjectionMatrix";
21  const std::string ColorName = "ColorValue";
22 
23  const std::string _vertexDecls =
24  "attribute highp vec4 "+PositionName+";\n"
25  "uniform highp mat4 "+ModelViewProjectionMatrixName+";\n";
26 
27  const std::string _fragmentDecls =
28  "uniform lowp vec4 "+ColorName+";\n";
29 
30  const std::string _vertexCode =
31  "void main(void) { \n"
32  "gl_Position = "+ModelViewProjectionMatrixName+" * vec4(Position.xyz, 1.0);\n"
33  "}";
34 
35  const std::string _fragmentCode =
36  "void main(void) { \n"
37  "gl_FragColor.rgba = "+ColorName+".rgba; \n"
38  "}";
39 
40  }
41 
42  class ColorShader : public Shader
43  {
44  public:
45  static ColorShader* Create(const TShaderId shaderId)
46  {
47  return Eegeo_NEW(ColorShader)(
48  shaderId,
49  ColorShaderCode::_vertexDecls + ColorShaderCode::_vertexCode,
50  ColorShaderCode::_fragmentDecls + ColorShaderCode::_fragmentCode
51  );
52  }
53 
54  void SetMVP(const m44& mvp) const
55  {
56  bool transpose = false;
57  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
58  }
59 
60  void SetColor(const v4& color) const
61  {
62  SetUniformV4(color, m_colorUniformLocation);
63  }
64 
65  void Use(Rendering::GLState& glState) const
66  {
67  UseProgram(glState);
68  }
69 
70  protected:
71  ColorShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
72  {
73  CompileProgram(vertexShaderCode, fragmentShaderCode);
74  m_mvpUniformLocation = GetUniformLocation(ColorShaderCode::ModelViewProjectionMatrixName);
75  m_colorUniformLocation = GetUniformLocation(ColorShaderCode::ColorName);
76  }
77 
78  private:
79  GLuint m_mvpUniformLocation;
80  GLuint m_colorUniformLocation;
81  };
82  }
83  }
84 }