gltut / framework / UniformBlockArray.h


//To use this file, you must include one of the glload headers before including this.

#include <string.h>
#include <vector>

namespace Framework
	//This object can only be constructed after an OpenGL context has been created and initialized.
	template<typename UniformBlockObject, int arrayCount>
	class UniformBlockArray
			: m_storage()
			, m_blockOffset(0)
			int uniformBufferAlignSize = 0;
			glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &uniformBufferAlignSize);

			m_blockOffset = sizeof(UniformBlockObject);
			m_blockOffset += uniformBufferAlignSize -
				(m_blockOffset % uniformBufferAlignSize);

			int sizeMaterialUniformBuffer = m_blockOffset * arrayCount;

			m_storage.resize(arrayCount * m_blockOffset, 0);

		int size() const {return arrayCount;}

		//The array offset should be multiplied by the array index to get the offset
		//for a particular element.
		int GetArrayOffset() const {return m_blockOffset;}

		class BlockMemberReference
			BlockMemberReference(UniformBlockArray &uboArray, int blockIndex)
				: m_array(uboArray)
				, m_blockIndex(blockIndex)

			~BlockMemberReference() {}

			operator UniformBlockObject () const
				UniformBlockObject theObject;
					&m_array.m_storage[m_blockIndex * m_array.m_blockOffset],

				return theObject;

			BlockMemberReference & operator=(const UniformBlockObject &input)
				memcpy(&m_array.m_storage[m_blockIndex * m_array.m_blockOffset],
					&input, sizeof(UniformBlockObject));

				return *this;

			UniformBlockArray &m_array;
			int m_blockIndex;

			friend class UniformBlockArray;

		BlockMemberReference operator[] (size_t index)
			assert(index < arrayCount);

			return BlockMemberReference(*this, index);

		GLuint CreateBufferObject()
			GLuint bufferObject;
			glGenBuffers(1, &bufferObject);
			glBindBuffer(GL_UNIFORM_BUFFER, bufferObject);
			glBufferData(GL_UNIFORM_BUFFER, m_storage.size(), &m_storage[0], GL_STATIC_DRAW);
			glBindBuffer(GL_UNIFORM_BUFFER, 0);

			return bufferObject;

		std::vector<GLubyte> m_storage;
		int m_blockOffset;

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.