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