All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Shader.h
1 // Copyright eeGeo Ltd (2012-2023), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "Rendering.h"
7 #include "Graphics.h"
8 #include "VectorMathDecl.h"
9 #include "IdTypes.h"
10 #include "VertexAttribs.h"
11 #include <vector>
12 #include <map>
13 #include <string>
14 
15 namespace Eegeo
16 {
17  namespace Rendering
18  {
24  class Shader : protected Eegeo::NonCopyable
25  {
26  public:
27  u32 GetId() const { return m_id; }
28 
29  virtual void Use(Rendering::GLState& glState) const = 0;
30 
31  virtual void SetMVP(const m44& mvp) const = 0;
32 
33  GLuint GetUniformLocation(const std::string& uniformName) const;
34 
35  inline const VertexLayouts::VertexAttribs& GetVertexAttributes() const
36  {
37  return m_vertexAttributes;
38  }
39 
40  virtual ~Shader()
41  {
42  if (m_programHandle != 0)
43  {
44  Eegeo_GL(glDeleteProgram(m_programHandle));
45  m_programHandle = 0;
46  }
47  }
48 
49  protected:
50  Shader(const TShaderId shaderId)
51  : m_programHandle(0)
52  , m_id(shaderId)
53  {
54  }
55 
56  void CompileProgram(const std::string& vertexShaderCode, const std::string& fragmentShaderCode);
57  void UseProgram(Rendering::GLState& glState) const;
58 
59  void SetUniformFloat(const float value, GLuint uniformLocation) const;
60  void SetUniformM44(const m44& matrix, GLuint uniformLocation, bool transpose) const;
61  void SetUniformV2(const v2& vec, GLuint uniformLocation) const;
62  void SetUniformV3(const v3& vec, GLuint uniformLocation) const;
63  void SetUniformV4(const v4& vec, GLuint uniformLocation) const;
64  void SetUniformTextureSampler(Rendering::GLState& glState, GLuint samplerId, GLuint uniformLocation) const;
65  void SetUniformBool(const bool value, GLuint uniformLocation) const;
66  void SetUniformV4v(const v4* vec, GLsizei count, GLuint uniformLocation) const;
67 
68  u32 m_programHandle;
69 
70  private:
71  const TShaderId m_id;
72  void DetectShaderUniforms();
73  std::map<std::string, GLuint> m_uniformsToUniformLocations;
74  VertexLayouts::VertexAttribs m_vertexAttributes;
75  };
76  }
77 }