All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PackedStencilShadowShader.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 PackedStencilShadowShaderCode
18  {
19  const std::string PositionName = "Position";
20  const std::string ExtrusionUnpackName = "ExtrusionUnpack";
21  const std::string UnpackModelViewProjectionMatrixName = "UnpackModelViewProjectionMatrix";
22 
23  const std::string _vertexCode =
24  "attribute highp vec4 "+PositionName+";\n"
25  "uniform highp mat4 "+UnpackModelViewProjectionMatrixName+";\n"
26  "uniform highp vec3 "+ExtrusionUnpackName+";\n"
27  "void main()\n"
28  "{\n"
29  "highp vec3 packedPos = Position.xyz - ExtrusionUnpack*Position.w;\n"
30  "gl_Position = UnpackModelViewProjectionMatrix * vec4(packedPos, 1.0);\n"
31  "}";
32 
33  const std::string _fragmentCode =
34  "void main()\n"
35  "{\n"
36  "gl_FragColor.rgb = vec3(1.0,0.0,0.0);\n"
37  "gl_FragColor.a = 1.0;\n"
38  "}\n";
39  }
40 
42  {
43  public:
44  static PackedStencilShadowShader* Create(const TShaderId shaderId)
45  {
46  return Eegeo_NEW(PackedStencilShadowShader)(
47  shaderId,
48  PackedStencilShadowShaderCode::_vertexCode,
49  PackedStencilShadowShaderCode::_fragmentCode
50  );
51  }
52 
53  void SetMVP(const m44& mvp) const
54  {
55  bool transpose = false;
56  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
57  }
58 
59  void SetExtrusionUnpack(const v3& extrusionUnpack) const
60  {
61  SetUniformV3(extrusionUnpack, m_extrusionUnpackUniform);
62  }
63 
64  void Use(Rendering::GLState& glState) const
65  {
66  UseProgram(glState);
67  }
68 
69  protected:
70  PackedStencilShadowShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode)
71  : Shader(shaderId)
72  , m_mvpUniformLocation(0)
73  , m_extrusionUnpackUniform(0)
74  {
75  CompileProgram(vertexShaderCode, fragmentShaderCode);
76 
77  m_mvpUniformLocation = GetUniformLocation(PackedStencilShadowShaderCode::UnpackModelViewProjectionMatrixName);
78  m_extrusionUnpackUniform = GetUniformLocation(PackedStencilShadowShaderCode::ExtrusionUnpackName);
79  }
80 
81  private:
82  GLuint m_mvpUniformLocation;
83  GLuint m_extrusionUnpackUniform;
84  };
85  }
86  }
87 }