All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ColoredVertShader.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 ColoredVertShaderCode
18  {
19  const std::string PositionName = "Position";
20  const std::string ModelViewProjectionMatrixName = "ModelViewProjectionMatrix";
21  const std::string ColorName = "Color";
22  const std::string ColorVaryingName = "ColorVarying";
23 
24  const std::string _vertexDecls =
25  "attribute highp vec4 "+PositionName+";\n"
26  "attribute vec4 "+ColorName+";\n"
27  "varying lowp vec4 "+ColorVaryingName+";\n"
28  "uniform highp mat4 "+ModelViewProjectionMatrixName+";\n";
29 
30  const std::string _fragmentDecls =
31  "varying lowp vec4 "+ColorVaryingName+";\n";
32 
33  const std::string _vertexCode =
34  "void main(void) { \n"
35  ""+ColorVaryingName+" = "+ColorName+";\n"
36  "gl_Position = "+ModelViewProjectionMatrixName+" * vec4(Position.xyz, 1.0);\n"
37  "}";
38 
39  const std::string _fragmentCode =
40  "void main(void) { \n"
41  "gl_FragColor.rgba = "+ColorVaryingName+".rgba; \n"
42  "}";
43 
44  }
45 
46  class ColoredVertShader : public Shader
47  {
48  public:
49  static ColoredVertShader* Create(const TShaderId shaderId)
50  {
51  return Eegeo_NEW(ColoredVertShader)(
52  shaderId,
53  ColoredVertShaderCode::_vertexDecls + ColoredVertShaderCode::_vertexCode,
54  ColoredVertShaderCode::_fragmentDecls + ColoredVertShaderCode::_fragmentCode
55  );
56  }
57 
58  void SetMVP(const m44& mvp) const
59  {
60  bool transpose = false;
61  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
62  }
63 
64  GLuint GetPositionUniform() const { return GetVertexAttributes().GetElement(0).GetLocation(); }
65  GLuint GetColorUniform() const { return GetVertexAttributes().GetElement(1).GetLocation(); }
66 
67  void Use(Rendering::GLState& glState) const
68  {
69  UseProgram(glState);
70  }
71 
72  protected:
73  ColoredVertShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
74  {
75  CompileProgram(vertexShaderCode, fragmentShaderCode);
76  m_mvpUniformLocation = GetUniformLocation(ColoredVertShaderCode::ModelViewProjectionMatrixName);
77  }
78 
79 
80 
81  private:
82  GLuint m_mvpUniformLocation;
83  };
84  }
85  }
86 }