44 #include <ACG/GL/acg_glew.hh> 45 #include <ACG/GL/gl.hh> 46 #include <ACG/GL/GLState.hh> 47 #include <ACG/ShaderUtils/GLSLShader.hh> 49 #include <QTextStream> 51 #include "VertexDeclaration.hh" 57 VertexElement::VertexElement()
72 offset.u =
static_cast<unsigned long>(_offset);
88 return static_cast<unsigned int>(offset.u);
93 return (type_ == _other.
type_) &&
95 (usage_ == _other.
usage_) &&
99 (vbo_ == _other.
vbo_);
102 VertexDeclaration::VertexDeclaration()
103 : vertexStride_(0), strideUserDefined_(0)
109 VertexDeclaration::~VertexDeclaration()
117 addElements(1, _pElement);
121 unsigned int _numElements,
123 const void* _pointer,
124 const char* _shaderInputName,
125 unsigned int _divisor,
143 unsigned int _numElements,
146 const char* _shaderInputName,
147 unsigned int _divisor,
168 elements_.reserve(elements_.size() + _numElements);
170 for (
unsigned int i = 0; i < _numElements; ++i)
173 updateShaderInputName(&tmp);
174 elements_.push_back(tmp);
179 if (!strideUserDefined_)
183 unsigned int n = getNumElements();
194 } lastOffset, firstOffset;
198 std::map<unsigned int, VertexElement*> vboFirstElements;
199 std::map<unsigned int, VertexElement*> vboLastElements;
201 for (
unsigned int i = 0; i < n; ++i)
203 if (vboFirstElements.find(elements_[i].vbo_) == vboFirstElements.end())
204 vboFirstElements[elements_[i].vbo_] = &elements_[i];
206 vboLastElements[elements_[i].vbo_] = &elements_[i];
210 for (std::map<unsigned int, VertexElement*>::iterator it = vboFirstElements.begin(); it != vboFirstElements.end(); ++it)
213 firstOffset.p = it->second->pointer_;
214 lastOffset.p = lastElement->
pointer_;
216 vertexStridesVBO_[it->first] =
static_cast<unsigned int>(lastOffset.u + getElementSize( lastElement ) - firstOffset.u);
219 vertexStride_ = vertexStridesVBO_.begin()->second;
235 unsigned int numElements = getNumElements();
237 if (!numElements)
return;
242 std::map<unsigned int, VertexDeclaration_ptr2uint> vboOffsets;
243 std::map<unsigned int, VertexElement*> vboPrevElements;
245 for (
unsigned int i = 0; i < numElements; ++i)
247 if (vboOffsets.find(elements_[i].vbo_) == vboOffsets.end())
249 vboOffsets[elements_[i].vbo_].p = elements_[i].pointer_;
250 vboPrevElements[elements_[i].vbo_] = &elements_[i];
254 for (
unsigned int i = 0; i < numElements; ++i)
258 bool updateOffset =
false;
263 vboPrevElements[el->
vbo_] = el;
271 vboOffsets[el->
vbo_].u += getElementSize(prevEl);
273 vboPrevElements[el->
vbo_] = el;
300 std::cerr <<
"VertexDeclaration::updateShaderInputName - unknown vertex usage - " << _pElem->
usage_ << std::endl;
311 return elements_.size();
325 case GL_UNSIGNED_INT:
332 case GL_UNSIGNED_SHORT:
336 case GL_UNSIGNED_BYTE:
340 std::cerr <<
"VertexDeclaration::getElementSize - unknown type - " << _type << std::endl;
351 return _pElement ? getGLTypeSize(_pElement->
type_) * _pElement->
numElements_ : 0;
363 unsigned int numElements = getNumElements();
364 unsigned int vertexStride = getVertexStride();
366 for (
unsigned int i = 0; i < numElements; ++i)
386 glClientActiveTexture(GL_TEXTURE0);
409 unsigned int numElements = getNumElements();
411 for (
unsigned int i = 0; i < numElements; ++i)
435 unsigned int numElements = getNumElements();
437 for (
unsigned int i = 0; i < numElements; ++i)
439 unsigned int vertexStride = getVertexStride(i);
449 GLboolean normalizeElem = GL_TRUE;
452 normalizeElem = GL_FALSE;
457 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &curVBO);
458 glBindBuffer(GL_ARRAY_BUFFER, pElem->
vbo_);
463 if (supportsInstancedArrays())
467 glVertexAttribDivisor(loc, pElem->
divisor_);
471 glVertexAttribDivisorARB(loc, pElem->
divisor_);
475 std::cerr <<
"error: VertexDeclaration::activateShaderPipeline - instanced arrays not supported by gpu!" << std::endl;
477 glEnableVertexAttribArray(loc);
480 glBindBuffer(GL_ARRAY_BUFFER, curVBO);
490 unsigned int numElements = getNumElements();
492 for (
unsigned int i = 0; i < numElements; ++i)
500 glDisableVertexAttribArray(loc);
502 if (supportsInstancedArrays() && pElem->
divisor_)
506 glVertexAttribDivisor(loc, 0);
510 glVertexAttribDivisorARB(loc, 0);
521 return &elements_[i];
527 for (
size_t i = 0; i < elements_.size(); ++i)
528 if (elements_[i].usage_ == _usage)
536 int eid = findElementIdByUsage(_usage);
539 return getElement((
unsigned int)eid);
547 if (strideUserDefined_)
548 return vertexStride_;
554 unsigned int vbo = getElement(i)->vbo_;
555 std::map<unsigned int, unsigned int>::const_iterator it = vertexStridesVBO_.find(vbo);
557 return (it != vertexStridesVBO_.end()) ? it->second : vertexStride_;
566 strideUserDefined_ = 1;
567 vertexStride_ = _stride;
572 strideUserDefined_ = 0;
576 vertexStridesVBO_.clear();
583 const char* usageStrings[] =
595 QTextStream resultStrm(&result);
596 resultStrm <<
"stride = " << getVertexStride() <<
"\n";
599 for (
unsigned int i = 0; i < getNumElements(); ++i)
604 const char* typeString =
"";
608 case GL_FLOAT: typeString =
"GL_FLOAT";
break;
609 case GL_DOUBLE: typeString =
"GL_DOUBLE";
break;
611 case GL_INT: typeString =
"GL_INT";
break;
612 case GL_UNSIGNED_INT: typeString =
"GL_UNSIGNED_INT";
break;
614 case GL_SHORT: typeString =
"GL_SHORT";
break;
615 case GL_UNSIGNED_SHORT: typeString =
"GL_UNSIGNED_SHORT";
break;
617 case GL_BYTE: typeString =
"GL_BYTE";
break;
618 case GL_UNSIGNED_BYTE: typeString =
"GL_UNSIGNED_BYTE";
break;
619 default: typeString =
"unknown";
break;
625 resultStrm <<
"element " << i
626 <<
" - [type: " << typeString
628 <<
", usage: " << usage
632 <<
", vbo: " << el->
vbo_ 633 <<
", stride: " << getVertexStride(i)
646 if (elements_ != _other.elements_)
657 static int status_ = -1;
void setVertexStride(unsigned int _stride)
const char * shaderInputName_
set shader input name, if usage_ = VERTEX_USAGE_USER_DEFINED otherwise this is set automatically...
defined by user via VertexElement::shaderInputName_
unsigned int numElements_
how many elements of type_
const void * pointer_
Offset in bytes to the first occurrence of this element in vertex buffer; Or address to vertex data i...
unsigned int getNumElements() const
Namespace providing different geometric functions concerning angles.
Class to define the vertex input layout.
void deactivateShaderPipeline(GLSL::Program *_prog) const
unsigned int vertexStride_
Offset in bytes between each vertex.
VERTEX_USAGE usage_
position, normal, shader input ..
static void texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glTexcoordPointer, supports locking
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
void updateShaderInputName(VertexElement *_pElem)
bool ACGDLLEXPORT openGLVersionTest(const int _major, const int _minor)
unsigned int getVertexStride(unsigned int i=0) const
std::map< unsigned int, unsigned int > vertexStridesVBO_
Map vbo to offset in bytes between each vertex in that vbo.
void activateFixedFunction() const
Description of one vertex element.
void setByteOffset(unsigned int _offset)
void addElement(const VertexElement *_pElement)
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
bool openGLVersion(const int _major, const int _minor, bool _verbose)
int findElementIdByUsage(VERTEX_USAGE _usage) const
static bool supportsInstancedArrays()
unsigned int getByteOffset() const
VERTEX_USAGE
-— input name in vertex shader ----—
void addElements(unsigned int _numElements, const VertexElement *_pElements)
bool operator==(const VertexDeclaration &_other) const
bool operator==(const VertexElement &_other) const
static void colorPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glColorPointer, supports locking
const VertexElement * getElement(unsigned int i) const
unsigned int vbo_
Explicit vbo source of this element, set to 0 if the buffer bound at the time of activating the decla...
static size_t getElementSize(const VertexElement *_pElement)
unsigned int type_
GL_FLOAT, GL_UNSIGNED_BYTE, GL_INT, ...
bool checkExtensionSupported(const std::string &_extension)
int getAttributeLocation(const char *_name)
Get location of the specified attribute.
unsigned int divisor_
For instanced rendering: Step rate describing how many instances are drawn before advancing to the ne...
void activateShaderPipeline(GLSL::Program *_prog) const
const VertexElement * findElementByUsage(VERTEX_USAGE _usage) const
static size_t getGLTypeSize(unsigned int _type)
void deactivateFixedFunction() const
int strideUserDefined_
Flag that indicates, whether the stride was set by user or derived automatically. ...
static void normalPointer(GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glNormalPointer, supports locking