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 
18 class OpenglShader final : public Shader {
19 public:
20  OpenglShader() = default;
21 
22  template <typename T>
23  T get_value(const std::string& name);
24 
25  OpenglShader(const std::string& vertex, const std::string& fragment);
26 
27  void bind() const override;
28 
29  void set_value(const std::string& name, float value) override;
30 
31  void set_value(const std::string& name, int value) override;
32 
33  void set_value(const std::string& name, unsigned int value) override;
34 
35  void set_value(const std::string& name, glm::mat4 value, Uint32 count = 1) override;
36 
37  void set_value(const std::string& name, const int* value, Uint32 count = 1) override;
38 
39  void set_value(const std::string& name, const float* value, Uint32 count = 1) override;
40 
41  void set_value(const std::string& name, glm::vec2 value, Uint32 count = 1) override;
42 
43  void set_value(const std::string& name, glm::vec3 value, Uint32 count = 1) override;
44 
45  void set_value(const std::string& name, glm::vec4 value, Uint32 count = 1) override;
46 
47  void destroy() override;
48 
49  unsigned int get_id() const override;
50 
51  bool is_valid() const override;
52 
53 private:
54  unsigned int get_uniform_location(const std::string& name);
55 
56  unsigned int CompileShader(unsigned int type, const char* source);
57 };
58 
59 
60 template <typename T>
61 inline T OpenglShader::get_value(const std::string& name) {
62  unsigned int location = get_uniform_location(name);
63  if (location == -1) {
64  LOG_ERROR("Shader variable not found: %s", name.c_str());
65  return T();
66  }
67 
68  if constexpr (std::is_same_v<T, float>) {
69  float value;
70  glGetUniformfv(id, location, &value);
71  return value;
72  } else if constexpr (std::is_same_v<T, int>) {
73  int value;
74  glGetUniformiv(id, location, &value);
75  return value;
76  } else if constexpr (std::is_same_v<T, glm::vec2>) {
77  GLfloat data[2];
78  glGetUniformfv(id, location, data);
79  return glm::vec2(data[0], data[1]);
80  } else if constexpr (std::is_same_v<T, glm::vec3>) {
81  GLfloat data[3];
82  glGetUniformfv(id, location, data);
83  return glm::vec3(data[0], data[1], data[2]);
84  } else if constexpr (std::is_same_v<T, glm::vec4>) {
85  GLfloat data[4];
86  glGetUniformfv(id, location, data);
87  return glm::vec4(data[0], data[1], data[2], data[3]);
88  } else if constexpr (std::is_same_v<T, glm::mat4>) {
89  GLfloat data[16];
90  glGetUniformfv(id, location, data);
91  return glm::make_mat4(data);
92  } else {
93  LOG_ERROR("Unsupported type: %s", typeid(T).name());
94  }
95 
96  return T();
97 }
Opengl Shader implementation.
Definition: shader_gl.h:18
OpenglShader()=default
void destroy() override
Definition: shader_gl.cpp:111
T get_value(const std::string &name)
Definition: shader_gl.h:61
void set_value(const std::string &name, float value) override
Definition: shader_gl.cpp:120
void bind() const override
Definition: shader_gl.cpp:107
unsigned int get_id() const override
Definition: shader_gl.cpp:115
bool is_valid() const override
Definition: shader_gl.cpp:92
Shader Abstract class.
Definition: shader.h:67
#define LOG_ERROR(...)
ERROR logging.
Definition: logging_sys.h:60