All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PackedDiffuseShader.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 "ShaderMacros.h"
8 #include "Rendering.h"
9 #include "VectorMath.h"
10 #include "Graphics.h"
11 #include <string>
12 
13 namespace Eegeo
14 {
15  namespace Rendering
16  {
17  namespace Shaders
18  {
19  namespace PackedDiffuseShaderCode
20  {
21  const std::string PositionName = "Position";
22  const std::string UVName = "UV";
23  const std::string UnpackModelViewProjectionMatrixName = "UnpackModelViewProjectionMatrix";
24  const std::string LightColorMatrixName = "LightColorMatrix";
25  const std::string MinUVRangeName = "MinUVRange";
26  const std::string MaxUVRangeName = "MaxUVRange";
27  const std::string DiffuseName = "Diffuse";
28  const std::string LightDotUnpackName = "LightDotUnpack";
29 
30  const std::string _vertexDecls =
31  "attribute highp vec4 "+PositionName+";\n"
32  "attribute highp vec2 "+UVName+";\n"
33  "varying lowp vec4 ColorVarying;\n"
34  "varying mediump vec2 DestinationUV;\n"
35  "uniform highp mat4 "+UnpackModelViewProjectionMatrixName+";\n"
36  "uniform lowp mat4 "+LightColorMatrixName+";\n"
37  "uniform highp vec2 "+MinUVRangeName+";\n"
38  "uniform highp vec2 "+MaxUVRangeName+";\n"
39  "uniform highp vec3 "+LightDotUnpackName+";\n";
40 
41  const std::string _fragmentDecls =
42  "varying lowp vec4 ColorVarying;\n"
43  "varying mediump vec2 DestinationUV;\n"
44  "uniform sampler2D "+DiffuseName+";\n";
45 
46  const std::string _vertexCode =
47  "void main(void) { \n"
48  "DestinationUV = mix(MinUVRange, MaxUVRange, UV);\n"
49  "ColorVarying = LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0);\n"
50  "gl_Position = UnpackModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
51  "}";
52 
53  const std::string _fragmentPunchThroughCode =
54  "void main(void) { \n"
55  "highp vec4 col = " TEXTURE2D(Diffuse, DestinationUV) "; \n"
56  "if(col.w<" EEGEO_ALPHA_TEST_VALUE ") discard; \n"
57  "gl_FragColor.rgb = col.rgb * ColorVarying.rgb; \n"
58  "gl_FragColor.a = 1.0;\n"
59  "}";
60 
61  const std::string _fragmentCode =
62  "void main(void) { \n"
63  "gl_FragColor.rgb = " TEXTURE2D(Diffuse, DestinationUV) ".rgb * ColorVarying.rgb; \n"
64  "gl_FragColor.a = 1.0;\n"
65  "}";
66  }
67 
68  class PackedDiffuseShader : public Shader
69  {
70  public:
71  static PackedDiffuseShader* Create(const TShaderId shaderId)
72  {
73  return Eegeo_NEW(PackedDiffuseShader)(
74  shaderId,
75  PackedDiffuseShaderCode::_vertexDecls + PackedDiffuseShaderCode::_vertexCode,
76  PackedDiffuseShaderCode::_fragmentDecls + PackedDiffuseShaderCode::_fragmentCode
77  );
78  }
79 
80  static PackedDiffuseShader* CreateWithPunchThrough(const TShaderId shaderId)
81  {
82  return Eegeo_NEW(PackedDiffuseShader)(
83  shaderId,
84  PackedDiffuseShaderCode::_vertexDecls + PackedDiffuseShaderCode::_vertexCode,
85  PackedDiffuseShaderCode::_fragmentDecls + PackedDiffuseShaderCode::_fragmentPunchThroughCode
86  );
87  }
88 
89  const GLuint GetDiffuseSamplerId() const { return 0; }
90 
91  void SetMVP(const m44& mvp) const
92  {
93  bool transpose = false;
94  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
95  }
96 
97  void SetLightColors(const m44& colors) const
98  {
99  bool transpose = false;
100  SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
101  }
102 
103  void SetUVBounds(const Eegeo::v2& min, const Eegeo::v2& max) const
104  {
105  SetUniformV2(min, m_minUVRangeUniformLocation);
106  SetUniformV2(max, m_maxUVRangeUniformLocation);
107  }
108 
109  void Use(Rendering::GLState& glState) const
110  {
111  UseProgram(glState);
112  SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
113 
114  Eegeo::v3 lightDotUnpack(1.f, 32.f, 1024.f);
115  SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
116  }
117 
118  protected:
119  PackedDiffuseShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
120  {
121  CompileProgram(vertexShaderCode, fragmentShaderCode);
122 
123  m_mvpUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::UnpackModelViewProjectionMatrixName);
124  m_lightColorsUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::LightColorMatrixName);
125  m_minUVRangeUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::MinUVRangeName);
126  m_maxUVRangeUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::MaxUVRangeName);
127  m_diffuseTextureSamplerUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::DiffuseName);
128  m_lightDotUnpackUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::LightDotUnpackName);
129  }
130 
131  private:
132  GLuint m_mvpUniformLocation ;
133  GLuint m_lightColorsUniformLocation;
134  GLuint m_minUVRangeUniformLocation;
135  GLuint m_maxUVRangeUniformLocation;
136  GLuint m_diffuseTextureSamplerUniformLocation;
137  GLuint m_lightDotUnpackUniformLocation;
138  };
139  }
140  }
141 }