Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
shader_gl.h
Go to the documentation of this file.
1 #pragma once
2 #include "core/renderer/shader.h"
3 
4 
5 
19 class OpenglShader : public Shader {
20 public:
21  OpenglShader() = default;
22 
23  template <typename T>
24  T GetValue(const std::string& name);
25 
26  OpenglShader(const std::string& vertex, const std::string& fragment);
27 
28  void Bind() const override;
29 
30  void SetValue(const std::string& name, float value) override;
31 
32  void SetValue(const std::string& name, int value) override;
33 
34  void SetValue(const std::string& name, unsigned int value) override;
35 
36  void SetValue(const std::string& name, glm::mat4 value) override;
37 
38  void SetValue(const std::string& name, glm::vec2 value) override;
39 
40  void SetValue(const std::string& name, glm::vec3 value) override;
41 
42  void SetValue(const std::string& name, glm::vec4 value) override;
43 
44  void Destroy() override;
45 
46  unsigned int GetID() const override;
47 
48  bool IsValid() const override;
49 
50 private:
51  unsigned int GetUniformLocation(const std::string& name);
52 
53  unsigned int CompileShader(unsigned int type, const char* source);
54 
55 
56 };
57 
58 
59 template <typename T>
60 inline T OpenglShader::GetValue(const std::string& name) {
61  unsigned int location = GetUniformLocation(name);
62  if (location == -1) {
63  LOG_ERROR("Shader variable not found: %s", name.c_str());
64  return T();
65  }
66 
67  if constexpr (std::is_same_v<T, float>) {
68  float value;
69  glGetUniformfv(id, location, &value);
70  return value;
71  } else if constexpr (std::is_same_v<T, int>) {
72  int value;
73  glGetUniformiv(id, location, &value);
74  return value;
75  } else if constexpr (std::is_same_v<T, glm::vec2>) {
76  GLfloat data[2];
77  glGetUniformfv(id, location, data);
78  return glm::vec2(data[0], data[1]);
79  } else if constexpr (std::is_same_v<T, glm::vec3>) {
80  GLfloat data[3];
81  glGetUniformfv(id, location, data);
82  return glm::vec3(data[0], data[1], data[2]);
83  } else if constexpr (std::is_same_v<T, glm::vec4>) {
84  GLfloat data[4];
85  glGetUniformfv(id, location, data);
86  return glm::vec4(data[0], data[1], data[2], data[3]);
87  } else if constexpr (std::is_same_v<T, glm::mat4>) {
88  GLfloat data[16];
89  glGetUniformfv(id, location, data);
90  return glm::make_mat4(data);
91  } else {
92  LOG_ERROR("Unsupported type: %s", typeid(T).name());
93  }
94 
95  return T();
96 }
Opengl Shader implementation.
Definition: shader_gl.h:19
OpenglShader()=default
void Bind() const override
Definition: shader_gl.cpp:122
unsigned int GetID() const override
Definition: shader_gl.cpp:130
void SetValue(const std::string &name, float value) override
Definition: shader_gl.cpp:135
T GetValue(const std::string &name)
Definition: shader_gl.h:60
bool IsValid() const override
Definition: shader_gl.cpp:107
void Destroy() override
Definition: shader_gl.cpp:126
Shader Abstract class.
Definition: shader.h:44
#define LOG_ERROR(...)
ERROR logging.
Definition: logging.h:60