Merge pull request #515 from wheremyfoodat/wheremyfoodat-patch-2

Add UBO support to opengl.hpp
This commit is contained in:
wheremyfoodat 2024-05-12 22:29:05 +00:00 committed by GitHub
commit 79bfb67156
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -485,6 +485,48 @@ namespace OpenGL {
#endif #endif
}; };
struct UniformBuffer {
GLuint m_handle = 0;
void create() {
if (m_handle == 0) {
glGenBuffers(1, &m_handle);
}
}
void createFixedSize(GLsizei size, GLenum usage = GL_DYNAMIC_DRAW) {
create();
bind();
glBufferData(GL_UNIFORM_BUFFER, size, nullptr, usage);
}
UniformBuffer(bool shouldCreate = false) {
if (shouldCreate) {
create();
}
}
#ifdef OPENGL_DESTRUCTORS
~UniformBuffer() { free(); }
#endif
GLuint handle() const { return m_handle; }
bool exists() const { return m_handle != 0; }
void bind() const { glBindBuffer(GL_UNIFORM_BUFFER, m_handle); }
void free() { glDeleteBuffers(1, &m_handle); }
// Reallocates the buffer on every call. Prefer the sub version if possible.
template <typename UniformType>
void buffer(const UniformType& uniformData, GLenum usage = GL_DYNAMIC_DRAW) {
glBufferData(GL_UNIFORM_BUFFER, sizeof(uniformData), &uniformData, usage);
}
// Only use if you used createFixedSize
template <typename UniformType>
void bufferSub(const UniformType& uniformData, int vertCount, GLintptr offset = 0) {
glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(uniformData), &uniformData);
}
};
enum DepthFunc { enum DepthFunc {
Never = GL_NEVER, // Depth test never passes Never = GL_NEVER, // Depth test never passes
Always = GL_ALWAYS, // Depth test always passes Always = GL_ALWAYS, // Depth test always passes