Golias Engine
A C++ 20 'game engine' built with SDL3 with wide platform support.
ogl_struct.h
Go to the documentation of this file.
1 #pragma once
4 
6 public:
7 
9  const GpuBuffer* vertex_buffer,
10  const GpuBuffer* index_buffer,
11  const std::vector<VertexAttribute>& attributes,
12  uint32_t stride);
13 
14  ~OpenglGpuVertexLayout() override;
15 
16  void bind() const override;
17 
18  void unbind() const override;
19 
20 private:
21  GLuint _vao = 0;
22 
23  static GLenum to_gl_type(DataType type);
24 };
25 
26 class OpenglGpuBuffer final : public GpuBuffer {
27 
28 public:
30 
31  ~OpenglGpuBuffer() override;
32 
33  void bind() const override;
34 
35  void upload(const void* data, size_t size) override;
36 
37  size_t size() const override;
38 
39  GpuBufferType type() const override;
40 
41 
42 private:
43  GLuint _id = 0;
44  GpuBufferType _buffer_type = GpuBufferType::VERTEX;
45  size_t _buffer_size = 0;
46  GLenum _target = GL_ARRAY_BUFFER;
47 
48 };
49 
50 class OpenGLFramebuffer final : public Framebuffer {
51  Uint32 fbo = 0;
52  FramebufferSpecification specification;
53  std::vector<Uint32> color_attachments;
54  uint32_t depth_attachment = 0;
55 
56 public:
57  explicit OpenGLFramebuffer(const FramebufferSpecification& spec);
58 
59  ~OpenGLFramebuffer() override;
60 
61  void invalidate() override;
62 
63  void bind() override;
64 
65  void unbind() override;
66 
67  void resize(unsigned int width, unsigned int height) override;
68 
69  Uint32 get_color_attachment_id(size_t index = 0) const override;
70 
71  Uint32 get_depth_attachment_id() const override;
72 
73  const FramebufferSpecification& get_specification() const override;
74 
75  void cleanup();
76 };
77 
78 
79 class OpenglShader final : public Shader {
80 public:
81  OpenglShader() = default;
82  ~OpenglShader() override;
83 
84  template <typename T>
85  T get_value(const std::string& name);
86 
87  OpenglShader(const std::string& vertex, const std::string& fragment);
88 
89  void activate() const override;
90 
91  void set_value(const std::string& name, float value) override;
92 
93  void set_value(const std::string& name, int value) override;
94 
95  void set_value(const std::string& name, Uint32 value) override;
96 
97  void set_value(const std::string& name, glm::mat4 value, Uint32 count) override;
98 
99  void set_value(const std::string& name, const int* value, Uint32 count) override;
100 
101  void set_value(const std::string& name, const float* value, Uint32 count) override;
102 
103  void set_value(const std::string& name, glm::vec2 value, Uint32 count) override;
104 
105  void set_value(const std::string& name, glm::vec3 value, Uint32 count) override;
106 
107  void set_value(const std::string& name, glm::vec4 value, Uint32 count) override;
108 
109  void set_value(const std::string& name, const glm::mat4* values, Uint32 count) override;
110 
111  void destroy() override;
112 
113  unsigned int get_id() const override;
114 
115  bool is_valid() const override;
116 
117 private:
118  Uint32 get_uniform_location(const std::string& name);
119 
120  Uint32 compile_shader(Uint32 type, const char* source);
121 };
122 
123 
124 template <typename T>
125 inline T OpenglShader::get_value(const std::string& name) {
126  unsigned int location = get_uniform_location(name);
127  if (location == -1) {
128  printf("Shader variable not found: %s\n", name.c_str());
129  return T();
130  }
131 
132  if constexpr (std::is_same_v<T, float>) {
133  float value;
134  glGetUniformfv(id, location, &value);
135  return value;
136  } else if constexpr (std::is_same_v<T, int>) {
137  int value;
138  glGetUniformiv(id, location, &value);
139  return value;
140  } else if constexpr (std::is_same_v<T, glm::vec2>) {
141  GLfloat data[2];
142  glGetUniformfv(id, location, data);
143  return glm::vec2(data[0], data[1]);
144  } else if constexpr (std::is_same_v<T, glm::vec3>) {
145  GLfloat data[3];
146  glGetUniformfv(id, location, data);
147  return glm::vec3(data[0], data[1], data[2]);
148  } else if constexpr (std::is_same_v<T, glm::vec4>) {
149  GLfloat data[4];
150  glGetUniformfv(id, location, data);
151  return glm::vec4(data[0], data[1], data[2], data[3]);
152  } else if constexpr (std::is_same_v<T, glm::mat4>) {
153  GLfloat data[16];
154  glGetUniformfv(id, location, data);
155  return glm::make_mat4(data);
156  } else {
157  printf("Unsupported type: %s\n", typeid(T).name());
158  }
159 
160  return T();
161 }
GpuBufferType
GpuBuffer Abstract class.
Definition: base_struct.h:29
DataType
Definition: base_struct.h:60
Definition: base_struct.h:113
GpuBuffer Abstract class.
Definition: base_struct.h:46
Definition: base_struct.h:76
Definition: ogl_struct.h:50
void unbind() override
Definition: ogl_struct.cpp:345
void cleanup()
Definition: ogl_struct.cpp:367
Uint32 get_color_attachment_id(size_t index=0) const override
Definition: ogl_struct.cpp:355
void bind() override
Definition: ogl_struct.cpp:340
void invalidate() override
Definition: ogl_struct.cpp:263
~OpenGLFramebuffer() override
Definition: ogl_struct.cpp:258
void resize(unsigned int width, unsigned int height) override
Definition: ogl_struct.cpp:349
const FramebufferSpecification & get_specification() const override
Definition: ogl_struct.cpp:363
Uint32 get_depth_attachment_id() const override
Definition: ogl_struct.cpp:359
OpenGLFramebuffer(const FramebufferSpecification &spec)
Definition: ogl_struct.cpp:253
Definition: ogl_struct.h:26
OpenglGpuBuffer(GpuBufferType type)
Definition: ogl_struct.cpp:201
size_t size() const override
Definition: ogl_struct.cpp:245
~OpenglGpuBuffer() override
Definition: ogl_struct.cpp:231
void upload(const void *data, size_t size) override
Definition: ogl_struct.cpp:239
GpuBufferType type() const override
Definition: ogl_struct.cpp:249
void bind() const override
Definition: ogl_struct.cpp:235
Definition: ogl_struct.h:5
void bind() const override
Definition: ogl_struct.cpp:178
OpenglGpuVertexLayout(const GpuBuffer *vertex_buffer, const GpuBuffer *index_buffer, const std::vector< VertexAttribute > &attributes, uint32_t stride)
Definition: ogl_struct.cpp:146
~OpenglGpuVertexLayout() override
Definition: ogl_struct.cpp:173
void unbind() const override
Definition: ogl_struct.cpp:182
Definition: ogl_struct.h:79
OpenglShader()=default
~OpenglShader() override
Definition: ogl_struct.cpp:378
void activate() const override
Definition: ogl_struct.cpp:137
void destroy() override
Definition: ogl_struct.cpp:141
T get_value(const std::string &name)
Definition: ogl_struct.h:125
void set_value(const std::string &name, float value) override
Definition: ogl_struct.cpp:387
bool is_valid() const override
Definition: ogl_struct.cpp:131
unsigned int get_id() const override
Definition: ogl_struct.cpp:382
Shader Abstract class.
Definition: base_struct.h:160
Definition: base_struct.h:105