11#ifndef CSCI441_UNIFORM_BUFFER_OBJECT_HPP
12#define CSCI441_UNIFORM_BUFFER_OBJECT_HPP
16#ifdef CSCI441_USE_GLEW
64 [[maybe_unused]]
UniformBufferObject(const
char* UNIFORM_BLOCK_NAME, std::initializer_list<const
char*> uniformNamesList);
85 [[maybe_unused]]
void setupWithShaderProgram(
ShaderProgram *shaderProgram, GLuint bindingPoint );
93 [[maybe_unused]]
void copyToOffset(
unsigned int offset,
void* addr,
size_t len );
101 [[maybe_unused]]
void copyToBuffer( const
char* UNIFORM_NAME,
void* addr,
size_t len );
106 void bindBuffer() const;
111 [[maybe_unused]]
void bufferSubData() const;
115 std::vector<
char*> _uniformNames;
119 GLuint* _uniformIndices;
120 GLint* _uniformOffsets;
122 GLuint _bindingPoint;
133 _blockName =
new char[strlen(UNIFORM_BLOCK_NAME) + 1];
134 strncpy(_blockName, UNIFORM_BLOCK_NAME, strlen(UNIFORM_BLOCK_NAME));
136 _numUniforms = uniformNamesList.size();
138 for(
const auto &uniformName : uniformNamesList ) {
139 const auto un =
new char[ strlen(uniformName) + 1];
140 strncpy(un, uniformName, strlen(uniformName));
141 _uniformNames.push_back(un);
144 _uniformIndices =
new GLuint[_numUniforms];
145 _uniformOffsets =
new GLint[_numUniforms];
154 _moveFromSource(src);
160 _moveFromSource(src);
172 _buffer =
new GLubyte[ _blockSize ];
174 glGetUniformIndices(shaderProgram->
getShaderProgramHandle(), _numUniforms, &_uniformNames[0], _uniformIndices);
175 glGetActiveUniformsiv(shaderProgram->
getShaderProgramHandle(), _numUniforms, _uniformIndices, GL_UNIFORM_OFFSET, _uniformOffsets);
177 glGenBuffers(1, &_ubod);
179 glBufferData(GL_UNIFORM_BUFFER, _blockSize,
nullptr, GL_DYNAMIC_DRAW);
181 _bindingPoint = bindingPoint;
182 glBindBufferBase(GL_UNIFORM_BUFFER, _bindingPoint, _ubod);
188 if(offset < _numUniforms) {
189 memcpy(_buffer + _uniformOffsets[offset], addr, len);
191 fprintf(stderr,
"[ERROR]: Offset %d exceeds size of Uniform Block %s which is %d\n", offset, _blockName, _numUniforms);
198 for(GLuint i = 0; i < _numUniforms; i++) {
199 if( strcmp(_uniformNames[i], UNIFORM_NAME) == 0 ) {
200 memcpy( _buffer + _uniformOffsets[i], addr, len );
206 fprintf(stderr,
"[ERROR]: Uniform Name \"%s\" not found within Uniform Block \"%s\"\n", UNIFORM_NAME, _blockName);
211 glBindBuffer(GL_UNIFORM_BUFFER, _ubod);
216 glBufferSubData(GL_UNIFORM_BUFFER, 0, _blockSize, _buffer);
219inline void CSCI441::UniformBufferObject::_cleanupSelf() {
220 glDeleteBuffers(1, &_ubod);
223 for(GLuint i = 0; i < _numUniforms; i++) {
224 delete[] _uniformNames[i];
226 _uniformNames.clear();
228 delete[] _uniformIndices;
229 _uniformIndices =
nullptr;
231 delete[] _uniformOffsets;
232 _uniformOffsets =
nullptr;
238 _blockName =
nullptr;
246inline void CSCI441::UniformBufferObject::_moveFromSource(UniformBufferObject &src) {
247 _blockName = src._blockName;
248 src._blockName =
nullptr;
250 _numUniforms = src._numUniforms;
251 src._numUniforms = 0;
253 _uniformNames = std::move(src._uniformNames);
254 src._uniformNames.clear();
256 _uniformIndices = src._uniformIndices;
257 src._uniformIndices =
nullptr;
259 _uniformOffsets = src._uniformOffsets;
260 src._uniformOffsets =
nullptr;
262 _blockSize = src._blockSize;
265 _buffer = src._buffer;
266 src._buffer =
nullptr;
268 _bindingPoint = src._bindingPoint;
269 src._bindingPoint = 0;
Class to work with OpenGL 4.0+ Shaders.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:35
virtual void setUniformBlockBinding(const GLchar *uniformBlockName, GLuint binding) const final
Set the binding point for the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1359
virtual GLuint getShaderProgramHandle() const final
Returns the handle for this shader program.
Definition: ShaderProgram.hpp:1495
virtual GLint getUniformBlockSize(const GLchar *uniformBlockName) const final
Returns the size of the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1313
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17