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