11#ifndef CSCI441_UNIFORM_BUFFER_OBJECT_HPP
12#define CSCI441_UNIFORM_BUFFER_OBJECT_HPP
16#ifdef CSCI441_USE_GLEW
46 [[maybe_unused]]
UniformBufferObject(
const char* UNIFORM_BLOCK_NAME, std::initializer_list<const char*> uniformNamesList);
67 [[maybe_unused]]
void setupWithShaderProgram(
ShaderProgram *shaderProgram, GLuint bindingPoint );
75 [[maybe_unused]]
void copyToOffset(
unsigned int offset,
void* addr,
size_t len );
83 [[maybe_unused]]
void copyToBuffer(
const char* UNIFORM_NAME,
void* addr,
size_t len );
88 void bindBuffer()
const;
93 [[maybe_unused]]
void bufferSubData()
const;
97 std::vector<char*> _uniformNames;
101 GLuint* _uniformIndices;
102 GLint* _uniformOffsets;
104 GLuint _bindingPoint;
112 _blockName = (
char*)UNIFORM_BLOCK_NAME;
114 _numUniforms = uniformNamesList.size();
116 for(
const auto &uniformName : uniformNamesList ) {
117 char* un = (
char*)malloc(strlen(uniformName) *
sizeof(char));
118 strcpy(un, uniformName);
119 _uniformNames.push_back(un);
122 _uniformIndices = (GLuint*)malloc(_numUniforms *
sizeof(GLuint));
123 _uniformOffsets = (GLint*)malloc(_numUniforms *
sizeof(GLint));
132 glDeleteBuffers(1, &_ubod);
134 for(GLuint i = 0; i < _numUniforms; i++) {
135 free(_uniformNames[i]);
137 free(_uniformIndices);
138 free(_uniformOffsets);
145 _buffer = (GLubyte*)malloc( _blockSize );
147 glGetUniformIndices(shaderProgram->
getShaderProgramHandle(), _numUniforms, &_uniformNames[0], _uniformIndices);
148 glGetActiveUniformsiv(shaderProgram->
getShaderProgramHandle(), _numUniforms, _uniformIndices, GL_UNIFORM_OFFSET, _uniformOffsets);
150 glGenBuffers(1, &_ubod);
152 glBufferData(GL_UNIFORM_BUFFER, _blockSize,
nullptr, GL_DYNAMIC_DRAW);
154 _bindingPoint = bindingPoint;
155 glBindBufferBase(GL_UNIFORM_BUFFER, _bindingPoint, _ubod);
161 if(offset < _numUniforms) {
162 memcpy(_buffer + _uniformOffsets[offset], addr, len);
164 fprintf(stderr,
"[ERROR]: Offset %d exceeds size of Uniform Block %s which is %d\n", offset, _blockName, _numUniforms);
171 for(GLuint i = 0; i < _numUniforms; i++) {
172 if( strcmp(_uniformNames[i], UNIFORM_NAME) == 0 ) {
173 memcpy( _buffer + _uniformOffsets[i], addr, len );
179 fprintf(stderr,
"[ERROR]: Uniform Name \"%s\" not found within Uniform Block \"%s\"\n", UNIFORM_NAME, _blockName);
184 glBindBuffer(GL_UNIFORM_BUFFER, _ubod);
189 glBufferSubData(GL_UNIFORM_BUFFER, 0, _blockSize, _buffer);
Class to work with OpenGL 4.0+ Shaders.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:35
virtual GLuint getShaderProgramHandle() const final
Returns the handle for this shader program.
Definition: ShaderProgram.hpp:1417
virtual void setUniformBlockBinding(const char *uniformBlockName, GLuint binding) const final
Set the binding point for the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1293
virtual GLint getUniformBlockSize(const char *uniformBlockName) const final
Returns the size of the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1242
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17