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