Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

Commit 8d2101f4 authored by schultezub's avatar schultezub
Browse files

more work on tgt OpenGL 3+ implementation

git-svn-id: https://camplinux.in.tum.de/svn/campvis/trunk@269 bb408c1c-ae56-11e1-83d9-df6b3e0c105e
parent 59c3ecfe
......@@ -4,6 +4,8 @@
namespace tgt {
BufferObject::BufferObject()
: _id(0)
, _baseType(BYTE)
, _elementSize(1)
, _size(0)
, _numElements(0)
{
......@@ -24,9 +26,11 @@ namespace tgt {
glBindBuffer(target, _id);
}
void BufferObject::data(TargetType target, UsageType usage, const void* data, size_t numBytes) {
void BufferObject::data(TargetType target, UsageType usage, const void* data, size_t numBytes, BaseType baseType, size_t elementSize) {
bind(target);
glBufferData(target, numBytes, data, usage);
_baseType = baseType;
_elementSize = elementSize;
}
void BufferObject::subdata(TargetType target, UsageType usage, size_t offset, const void* data, size_t numBytes) {
......@@ -42,4 +46,12 @@ namespace tgt {
_assignedAttributes.erase(va);
}
BufferObject::BaseType BufferObject::getBaseType() const {
return _baseType;
}
size_t BufferObject::getElementSize() const {
return _elementSize;
}
}
\ No newline at end of file
......@@ -54,8 +54,16 @@ namespace tgt {
virtual ~BufferObject();
/**
* Gets the base data type in the buffer.
* \return _baseType
*/
BaseType getBaseType() const;
/**
* Gets the number of elements per element (1, 2, 3 or 4).
* \return _elementSize
*/
size_t getElementSize() const;
......@@ -67,12 +75,14 @@ namespace tgt {
/**
* Binds the buffer, reserves space on the OpenGL device and initializes it with the given data in \a data.
* \param target Target type of buffer object.
* \param usage Usage of data.
* \param data Pointer to host memory containing the data.
* \param numBytes Size of \a data in bytes.
* \param target Target type of buffer object.
* \param usage Usage of data.
* \param data Pointer to host memory containing the data.
* \param numBytes Size of \a data in bytes.
* \param baseType Base data type
* \param elementSize Number of elements per element (must be 1, 2, 3 or 4)
*/
void data(TargetType target, UsageType usage, const void* data, size_t numBytes);
void data(TargetType target, UsageType usage, const void* data, size_t numBytes, BaseType baseType, size_t elementSize);
/**
* Updates a subset of the buffer's data.
......@@ -85,8 +95,6 @@ namespace tgt {
void subdata(TargetType target, UsageType usage, size_t offset, const void* data, size_t numBytes);
protected:
/**
* To be called from VertexAttribute's constructor, to keep track of the VBO-VA bindings.
......@@ -102,8 +110,8 @@ namespace tgt {
GLuint _id; ///< OpenGL ID of this buffer
BaseType _baseType;
size_t _elementSize;
BaseType _baseType; ///< Base data type
size_t _elementSize; ///< Number of elements per element *lol* (must be 1, 2, 3 or 4)
size_t _size; ///< Buffer size in bytes
size_t _numElements; ///< Number of elements in this buffer
......
......@@ -18,6 +18,7 @@ namespace tgt {
_bufferObject->bindToVertexAttribute(this);
// Todo: implement normalized flag if needed
_bufferObject->bind(BufferObject::ARRAY_BUFFER);
glVertexAttribPointer(_index, _bufferObject->getElementSize(), _bufferObject->getBaseType(), false, _stride, reinterpret_cast<void*>(_offset));
}
......@@ -27,10 +28,14 @@ namespace tgt {
// ================================================================================================
size_t VertexArrayObject::_currentlyBoundVertexArray = 0;
bool VertexArrayObject::_initialized = true;
const std::string VertexArrayObject::loggerCat_ = "tgt.VertexArrayObject";
VertexArrayObject::VertexArrayObject(bool autoBind)
: _id(0)
, _enabledAttributes(16, false)
{
glGenVertexArrays(1, &_id);
if (_id == 0) {
......@@ -49,20 +54,20 @@ namespace tgt {
if (!_initialized)
initStaticMembers();
if (_currentlyBoundVertexArray != _id) {
//if (_currentlyBoundVertexArray != _id) {
glBindVertexArray(_id);
_currentlyBoundVertexArray = _id;
}
// _currentlyBoundVertexArray = _id;
// }
}
void VertexArrayObject::unbind() {
if (!_initialized)
initStaticMembers();
if (_currentlyBoundVertexArray != 0) {
// if (_currentlyBoundVertexArray != 0) {
glBindVertexArray(0);
_currentlyBoundVertexArray = 0;
}
// _currentlyBoundVertexArray = 0;
// }
}
size_t VertexArrayObject::addVertexAttribute(AttributeType attributeType, BufferObject* bufferObject, size_t stride /*= 0*/, size_t offset /*= 0*/, bool enableNow /*= true*/) {
......@@ -75,7 +80,7 @@ namespace tgt {
}
tgtAssert(
attributeType != UnspecifiedAttribute && _attributeTypeMap.find(attributeType) != _attributeTypeMap.end(),
attributeType != UnspecifiedAttribute || _attributeTypeMap.find(attributeType) != _attributeTypeMap.end(),
"Tried to add two VertexAttributes with the same type. This is currently not supported.");
// bind and create VertexAttribute
......@@ -104,28 +109,33 @@ namespace tgt {
void VertexArrayObject::enableVertexAttribute(size_t index) {
tgtAssert(index < _enabledAttributes.size(), "Index out of bounds.");
if (!_initialized)
initStaticMembers();
bind();
glEnableVertexAttribArray(index);
_enabledAttributes[index] = true;
}
void VertexArrayObject::enableAllVertexAttributes() {
for (size_t i = 0; i < _attributes.size(); ++i)
enableVertexAttribute(i);
}
void VertexArrayObject::disableVertexAttribute(size_t index) {
tgtAssert(index < _enabledAttributes.size(), "Index out of bounds.");
if (!_initialized)
initStaticMembers();
bind();
glDisableVertexAttribArray(index);
_enabledAttributes[index] = false;
}
void VertexArrayObject::disableAllVertexAttributes() {
for (size_t i = 0; i < _attributes.size(); ++i)
disableVertexAttribute(i);
}
void VertexArrayObject::initStaticMembers() {
// TODO: The better way would be to us glGet(GL_MAX_VERTEX_ATTRIBS) as dimension, but the standard says 16
// is the minimum number to be supported and that should be enough and I currently feel lazy. If
// you're reading this, feel free to improve this allocation...
_enabledAttributes = std::vector<bool>(16, false);
_currentlyBoundVertexArray = 0;
_initialized = true;
}
......@@ -143,4 +153,5 @@ namespace tgt {
}
}
\ No newline at end of file
......@@ -117,12 +117,22 @@ namespace tgt {
*/
void enableVertexAttribute(size_t index);
/**
* Enables all VertexAttributes of this VAO.
*/
void enableAllVertexAttributes();
/**
* Disables the VertexAttribute with the given index in the OpenGL client state.
* \param index Index of the VertexAttribute to disable.
*/
void disableVertexAttribute(size_t index);
/**
* Disables all VertexAttributes of this VAO.
*/
void disableAllVertexAttributes();
/**
* Returns the index of the VertexAttribute which was previously created with the given type.
* \note A VertexAttribute of type \a type must have been created beforehand. Otherwise
......@@ -139,14 +149,14 @@ namespace tgt {
/**
* Initializes all static members.
*/
void initStaticMembers();
static void initStaticMembers();
size_t _id; ///< Id of the internal OpenGL handle to the VAO.
std::vector<VertexAttribute> _attributes; ///< List of all VertexAttributes of this VAO.
std::vector<bool> _enabledAttributes; ///< List of the enabled VertexAttributes.
std::map<AttributeType, size_t> _attributeTypeMap; ///< Map of the AttributeTypes to their indices in the VertexAttributes list.
static bool _initialized; ///< Flag whether the static members are initialized;
static std::vector<bool> _enabledAttributes; ///< List of the enabled VertexAttributes. TODO: They are global in the OpenGL client side (hence, the static), right?
static size_t _currentlyBoundVertexArray; ///< Id of the VertexArrayObject which was bound most recently. 0 if the most recent bind call was unbind().
static const std::string loggerCat_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment