All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
FireworksShader.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 "ShaderMacros.h"
10 #include <string>
11 
12 namespace Eegeo
13 {
14  namespace Rendering
15  {
16  namespace Shaders
17  {
18  namespace FireworksShaderCode
19  {
20  const std::string PositionName = "Position";
21  const std::string UVName = "UV";
22  const std::string ModelViewProjectionMatrixName = "ModelViewProjectionMatrix";
23  const std::string ColorName = "Color";
24  const std::string ColorVaryingName = "ColorVarying";
25  const std::string UVVaryingName = "UVVarying";
26  const std::string DiffuseName = "Diffuse";
27 
28  const std::string _vertexDecls =
29  "attribute highp vec3 "+PositionName+";\n"
30  "attribute mediump vec2 "+UVName+";\n"
31  "attribute vec4 "+ColorName+";\n"
32  "varying lowp vec4 "+ColorVaryingName+";\n"
33  "varying mediump vec2 "+UVVaryingName+";\n"
34  "uniform highp mat4 "+ModelViewProjectionMatrixName+";\n";
35 
36  const std::string _fragmentDecls =
37  "varying lowp vec4 "+ColorVaryingName+";\n"
38  "varying mediump vec2 "+UVVaryingName+";\n"
39  "uniform sampler2D "+DiffuseName+";\n";
40 
41  const std::string _vertexCode =
42  "void main(void) { \n"
43  ""+ColorVaryingName+" = "+ColorName+";\n"
44  ""+UVVaryingName+" = "+UVName+";\n"
45  "gl_Position = "+ModelViewProjectionMatrixName+" * vec4(Position.xyz, 1.0);\n"
46  "}";
47 
48  const std::string _fragmentCode =
49  "void main(void) { \n"
50  "lowp vec4 sampleCol = " TEXTURE2D(Diffuse, UVVarying) ".rgba;\n"
51  "gl_FragColor.rgba = (sampleCol.rgba * mix(" + ColorVaryingName + ".rgba, vec4(1,1,1,1), sampleCol.r)) * "+ColorVaryingName+".a;\n"
52  "}";
53 
54  }
55 
56  class FireworksShader : public Shader
57  {
58  public:
59  static FireworksShader* Create(const TShaderId shaderId)
60  {
61  return Eegeo_NEW(FireworksShader)(
62  shaderId,
63  FireworksShaderCode::_vertexDecls + FireworksShaderCode::_vertexCode,
64  FireworksShaderCode::_fragmentDecls + FireworksShaderCode::_fragmentCode
65  );
66  }
67 
68  void SetMVP(const m44& mvp) const
69  {
70  bool transpose = false;
71  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
72  }
73 
74  GLuint GetPositionUniform() const { return GetVertexAttributes().GetElement(0).GetLocation(); }
75  GLuint GetUVUniform() const { return GetVertexAttributes().GetElement(1).GetLocation(); }
76  GLuint GetColorUniform() const { return GetVertexAttributes().GetElement(2).GetLocation(); }
77 
78  void Use(Rendering::GLState& glState) const
79  {
80  UseProgram(glState);
81  }
82 
83  protected:
84  FireworksShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
85  {
86  CompileProgram(vertexShaderCode, fragmentShaderCode);
87  m_mvpUniformLocation = GetUniformLocation(FireworksShaderCode::ModelViewProjectionMatrixName);
88  }
89 
90 
91 
92  private:
93  GLuint m_mvpUniformLocation;
94  };
95  }
96  }
97 }