Untitled Game engine no.5  1.0
Shader.hpp
1 #ifndef SHADERS_H
2 #define SHADERS_H
3 
4 
5 #include <GL/glew.h>
6 #include <iostream>
7 #include <glm/glm.hpp>
8 
9 namespace Engine {
11  enum class ShaderDataType : uint8_t {
12  None = 0, Float, Float2, Float3, Float4,
13  Mat3, Mat4,
14  Int, Int2, Int3, Int4,
15  Bool
16  };
17 
19  static int F_SIZE = sizeof(float);
21  static int I_SIZE = sizeof(int);
23  static int B_SIZE = sizeof(bool);
24 
27  static GLenum ShaderDataTypeToOpenGL(ShaderDataType type) {
28  switch (type) {
29  case ShaderDataType::Mat3:
30  case ShaderDataType::Mat4:
31  case ShaderDataType::Float:
32  case ShaderDataType::Float2:
33  case ShaderDataType::Float3:
34  case ShaderDataType::Float4: return GL_FLOAT;
35  case ShaderDataType::Int:
36  case ShaderDataType::Int2:
37  case ShaderDataType::Int3:
38  case ShaderDataType::Int4: return GL_INT;
39  case ShaderDataType::Bool: return GL_BOOL;
40  default: return 0;
41  }
42 
43  }
44 
46  static uint32_t ShaderDataTypeSize(ShaderDataType type) {
47  switch(type) {
48  case ShaderDataType::Float: return F_SIZE;
49  case ShaderDataType::Float2: return F_SIZE * 2;
50  case ShaderDataType::Float3: return F_SIZE * 3;
51  case ShaderDataType::Float4: return F_SIZE * 4;
52  case ShaderDataType::Mat3: return F_SIZE * 3 * 3;
53  case ShaderDataType::Mat4: return F_SIZE * 4 * 4;
54  case ShaderDataType::Int: return I_SIZE;
55  case ShaderDataType::Int2: return I_SIZE * 2;
56  case ShaderDataType::Int3: return I_SIZE * 3;
57  case ShaderDataType::Int4: return I_SIZE *4;
58  case ShaderDataType::Bool: return B_SIZE;
59  default: return 0;
60  }
61 
62  return 0;
63 
64  }
65 
67  class Shader {
68  private:
69  unsigned int m_id;
70  std::string m_FilePath;
71  public:
73  Shader(const std::string& vert, const std::string& frag);
75  Shader(const std::string& path);
76  ~Shader();
77 
79  void Bind();
81  void Unbind();
82 
84  void SetUniformInt1(const std::string& name, int i);
86  void SetUniformIntN(const std::string& name, int* values, const uint32_t& n);
88  void SetUniformFloat1(const std::string& name, float v0);
90  void SetUniformFloat2(const std::string& name, float v0, float v1);
92  void SetUniformFloat3(const std::string& name, float v0, float v1, float v2);
94  void SetUniformFloat4(const std::string& name, float v0, float v1, float v2, float v3);
96  void SetUniformMat4(const std::string& name, const glm::mat4);
98  unsigned int CreateShader(const std::string& vert, const std::string& frag);
99  private:
101  int GetUniformLocation(const std::string& name);
103  unsigned int CompileShader(unsigned int type, const std::string& source);
104  };
105 
106 }
107 #endif
Engine::Shader::CreateShader
unsigned int CreateShader(const std::string &vert, const std::string &frag)
create a shader from vertex string and a fragment string returns a system id
Definition: Shader.cpp:42
Engine::Shader::SetUniformFloat2
void SetUniformFloat2(const std::string &name, float v0, float v1)
set a uniform float pair
Definition: Shader.cpp:116
Engine::ShaderDataType
ShaderDataType
data type enum for shader parameter
Definition: Shader.hpp:11
Engine::Shader::Shader
Shader(const std::string &vert, const std::string &frag)
constructor, requires a vertex shader and a fragment shader in text form
Engine::Shader::SetUniformFloat3
void SetUniformFloat3(const std::string &name, float v0, float v1, float v2)
set a uniform float triplet
Definition: Shader.cpp:120
Engine::Shader::Bind
void Bind()
bind the shader to drawspace
Definition: Shader.cpp:94
Engine::Shader::SetUniformIntN
void SetUniformIntN(const std::string &name, int *values, const uint32_t &n)
set n uniform ints
Definition: Shader.cpp:108
Engine::Shader::SetUniformInt1
void SetUniformInt1(const std::string &name, int i)
set a uniform int
Definition: Shader.cpp:104
Engine::Shader::SetUniformFloat4
void SetUniformFloat4(const std::string &name, float v0, float v1, float v2, float v3)
set a uniform float quad
Definition: Shader.cpp:124
Engine::Shader::Unbind
void Unbind()
unbind the the shader from the drawing register
Definition: Shader.cpp:98
Engine
Definition: Animation.hpp:14
Engine::Shader::SetUniformMat4
void SetUniformMat4(const std::string &name, const glm::mat4)
set a uniform float 4x4 matrix
Definition: Shader.cpp:128
Engine::Shader
Container for a shader.
Definition: Shader.hpp:67
Engine::Shader::SetUniformFloat1
void SetUniformFloat1(const std::string &name, float v0)
set a uniform float
Definition: Shader.cpp:112