11#ifndef CSCI441_UNIFORM_BUFFER_OBJECT_HPP
12#define CSCI441_UNIFORM_BUFFER_OBJECT_HPP
17#ifdef CSCI441_USE_GLEW
65 [[maybe_unused]]
UniformBufferObject(const
char* UNIFORM_BLOCK_NAME, std::initializer_list<const
char*> uniformNamesList);
86 [[maybe_unused]]
void setupWithShaderProgram(
ShaderProgram *shaderProgram, GLuint bindingPoint );
94 [[maybe_unused]]
void copyToOffset(
unsigned int offset,
void* addr,
size_t len );
102 [[maybe_unused]]
void copyToBuffer( const
char* UNIFORM_NAME,
void* addr,
size_t len );
107 void bindBuffer() const;
112 [[maybe_unused]]
void bufferSubData() const;
116 std::vector<
char*> _uniformNames;
120 GLuint* _uniformIndices;
121 GLint* _uniformOffsets;
123 GLuint _bindingPoint;
134 _blockName =
new char[strlen(UNIFORM_BLOCK_NAME) + 1];
135 strncpy(_blockName, UNIFORM_BLOCK_NAME, strlen(UNIFORM_BLOCK_NAME));
137 _numUniforms = uniformNamesList.size();
139 for(
const auto &uniformName : uniformNamesList ) {
140 const auto un =
new char[ strlen(uniformName) + 1];
141 strncpy(un, uniformName, strlen(uniformName));
142 _uniformNames.push_back(un);
145 _uniformIndices =
new GLuint[_numUniforms];
146 _uniformOffsets =
new GLint[_numUniforms];
155 _moveFromSource(src);
161 _moveFromSource(src);
173 _buffer =
new GLubyte[ _blockSize ];
175 glGetUniformIndices(shaderProgram->
getShaderProgramHandle(), _numUniforms, &_uniformNames[0], _uniformIndices);
176 glGetActiveUniformsiv(shaderProgram->
getShaderProgramHandle(), _numUniforms, _uniformIndices, GL_UNIFORM_OFFSET, _uniformOffsets);
178 glGenBuffers(1, &_ubod);
180 glBufferData(GL_UNIFORM_BUFFER, _blockSize,
nullptr, GL_DYNAMIC_DRAW);
182 _bindingPoint = bindingPoint;
183 glBindBufferBase(GL_UNIFORM_BUFFER, _bindingPoint, _ubod);
189 if(offset < _numUniforms) {
190 memcpy(_buffer + _uniformOffsets[offset], addr, len);
192 fprintf(stderr,
"[ERROR]: Offset %d exceeds size of Uniform Block %s which is %d\n", offset, _blockName, _numUniforms);
199 for(GLuint i = 0; i < _numUniforms; i++) {
200 if( strcmp(_uniformNames[i], UNIFORM_NAME) == 0 ) {
201 memcpy( _buffer + _uniformOffsets[i], addr, len );
207 fprintf(stderr,
"[ERROR]: Uniform Name \"%s\" not found within Uniform Block \"%s\"\n", UNIFORM_NAME, _blockName);
212 glBindBuffer(GL_UNIFORM_BUFFER, _ubod);
217 glBufferSubData(GL_UNIFORM_BUFFER, 0, _blockSize, _buffer);
220inline void CSCI441::UniformBufferObject::_cleanupSelf() {
221 glDeleteBuffers(1, &_ubod);
224 for(GLuint i = 0; i < _numUniforms; i++) {
225 delete[] _uniformNames[i];
227 _uniformNames.clear();
229 delete[] _uniformIndices;
230 _uniformIndices =
nullptr;
232 delete[] _uniformOffsets;
233 _uniformOffsets =
nullptr;
239 _blockName =
nullptr;
247inline void CSCI441::UniformBufferObject::_moveFromSource(UniformBufferObject &src) {
248 _blockName = src._blockName;
249 src._blockName =
nullptr;
251 _numUniforms = src._numUniforms;
252 src._numUniforms = 0;
254 _uniformNames = std::move(src._uniformNames);
255 src._uniformNames.clear();
257 _uniformIndices = src._uniformIndices;
258 src._uniformIndices =
nullptr;
260 _uniformOffsets = src._uniformOffsets;
261 src._uniformOffsets =
nullptr;
263 _blockSize = src._blockSize;
266 _buffer = src._buffer;
267 src._buffer =
nullptr;
269 _bindingPoint = src._bindingPoint;
270 src._bindingPoint = 0;
Class to work with OpenGL 4.0+ Shaders.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:36
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:1380
virtual GLuint getShaderProgramHandle() const final
Returns the handle for this shader program.
Definition: ShaderProgram.hpp:1526
virtual GLint getUniformBlockSize(const GLchar *uniformBlockName) const final
Returns the size of the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1334
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17