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