50 #include <ACG/GL/acg_glew.hh> 51 #include <ACG/GL/gl.hh> 52 #include <ACG/GL/GLState.hh> 53 #include <ACG/ShaderUtils/GLSLShader.hh> 55 #include <QTextStream> 57 #include "VertexDeclaration.hh" 63 VertexElement::VertexElement()
78 offset.u =
static_cast<unsigned long>(_offset);
94 return static_cast<unsigned int>(offset.u);
97 VertexDeclaration::VertexDeclaration()
98 : vertexStride_(0), strideUserDefined_(0)
104 VertexDeclaration::~VertexDeclaration()
112 addElements(1, _pElement);
116 unsigned int _numElements,
118 const void* _pointer,
119 const char* _shaderInputName,
120 unsigned int _divisor,
138 unsigned int _numElements,
141 const char* _shaderInputName,
142 unsigned int _divisor,
163 elements_.reserve(elements_.size() + _numElements);
165 for (
unsigned int i = 0; i < _numElements; ++i)
168 updateShaderInputName(&tmp);
169 elements_.push_back(tmp);
174 if (!strideUserDefined_)
178 unsigned int n = getNumElements();
189 } lastOffset, firstOffset;
193 std::map<unsigned int, VertexElement*> vboFirstElements;
194 std::map<unsigned int, VertexElement*> vboLastElements;
196 for (
unsigned int i = 0; i < n; ++i)
198 if (vboFirstElements.find(elements_[i].vbo_) == vboFirstElements.end())
199 vboFirstElements[elements_[i].vbo_] = &elements_[i];
201 vboLastElements[elements_[i].vbo_] = &elements_[i];
205 for (std::map<unsigned int, VertexElement*>::iterator it = vboFirstElements.begin(); it != vboFirstElements.end(); ++it)
208 firstOffset.p = it->second->pointer_;
209 lastOffset.p = lastElement->
pointer_;
211 vertexStridesVBO_[it->first] =
static_cast<unsigned int>(lastOffset.u + getElementSize( lastElement ) - firstOffset.u);
214 vertexStride_ = vertexStridesVBO_.begin()->second;
230 unsigned int numElements = getNumElements();
232 if (!numElements)
return;
237 std::map<unsigned int, VertexDeclaration_ptr2uint> vboOffsets;
238 std::map<unsigned int, VertexElement*> vboPrevElements;
240 for (
unsigned int i = 0; i < numElements; ++i)
242 if (vboOffsets.find(elements_[i].vbo_) == vboOffsets.end())
244 vboOffsets[elements_[i].vbo_].p = elements_[i].pointer_;
245 vboPrevElements[elements_[i].vbo_] = &elements_[i];
249 for (
unsigned int i = 0; i < numElements; ++i)
253 bool updateOffset =
false;
258 vboPrevElements[el->
vbo_] = el;
266 vboOffsets[el->
vbo_].u += getElementSize(prevEl);
268 vboPrevElements[el->
vbo_] = el;
295 std::cerr <<
"VertexDeclaration::updateShaderInputName - unknown vertex usage - " << _pElem->
usage_ << std::endl;
306 return elements_.size();
312 unsigned int size = 0;
320 case GL_UNSIGNED_INT:
327 case GL_UNSIGNED_SHORT:
331 case GL_UNSIGNED_BYTE:
335 std::cerr <<
"VertexDeclaration::getElementSize - unknown type - " << _type << std::endl;
346 return _pElement ? getGLTypeSize(_pElement->
type_) * _pElement->
numElements_ : 0;
358 unsigned int numElements = getNumElements();
359 unsigned int vertexStride = getVertexStride();
361 for (
unsigned int i = 0; i < numElements; ++i)
381 glClientActiveTexture(GL_TEXTURE0);
404 unsigned int numElements = getNumElements();
406 for (
unsigned int i = 0; i < numElements; ++i)
430 unsigned int numElements = getNumElements();
432 for (
unsigned int i = 0; i < numElements; ++i)
434 unsigned int vertexStride = getVertexStride(i);
444 GLboolean normalizeElem = GL_TRUE;
447 normalizeElem = GL_FALSE;
452 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &curVBO);
453 glBindBuffer(GL_ARRAY_BUFFER, pElem->
vbo_);
458 if (supportsInstancedArrays())
460 #ifdef GL_ARB_instanced_arrays 461 glVertexAttribDivisor(loc, pElem->
divisor_);
465 std::cerr <<
"error: VertexDeclaration::activateShaderPipeline - instanced arrays not supported by gpu!" << std::endl;
467 glEnableVertexAttribArray(loc);
470 glBindBuffer(GL_ARRAY_BUFFER, curVBO);
480 unsigned int numElements = getNumElements();
482 for (
unsigned int i = 0; i < numElements; ++i)
490 glDisableVertexAttribArray(loc);
492 if (supportsInstancedArrays() && pElem->
divisor_)
494 #ifdef GL_ARB_instanced_arrays 495 glVertexAttribDivisor(loc, 0);
506 return &elements_[i];
512 for (
size_t i = 0; i < elements_.size(); ++i)
513 if (elements_[i].usage_ == _usage)
521 int eid = findElementIdByUsage(_usage);
524 return getElement((
unsigned int)eid);
532 if (strideUserDefined_)
533 return vertexStride_;
535 unsigned int vbo = getElement(i)->vbo_;
536 std::map<unsigned int, unsigned int>::const_iterator it = vertexStridesVBO_.find(vbo);
538 return (it != vertexStridesVBO_.end()) ? it->second : vertexStride_;
545 strideUserDefined_ = 1;
546 vertexStride_ = _stride;
551 strideUserDefined_ = 0;
555 vertexStridesVBO_.clear();
562 const char* usageStrings[] =
574 QTextStream resultStrm(&result);
575 resultStrm <<
"stride = " << getVertexStride() <<
"\n";
578 for (
unsigned int i = 0; i < getNumElements(); ++i)
583 const char* typeString =
"";
587 case GL_FLOAT: typeString =
"GL_FLOAT";
break;
588 case GL_DOUBLE: typeString =
"GL_DOUBLE";
break;
590 case GL_INT: typeString =
"GL_INT";
break;
591 case GL_UNSIGNED_INT: typeString =
"GL_UNSIGNED_INT";
break;
593 case GL_SHORT: typeString =
"GL_SHORT";
break;
594 case GL_UNSIGNED_SHORT: typeString =
"GL_UNSIGNED_SHORT";
break;
596 case GL_BYTE: typeString =
"GL_BYTE";
break;
597 case GL_UNSIGNED_BYTE: typeString =
"GL_UNSIGNED_BYTE";
break;
598 default: typeString =
"unknown";
break;
604 resultStrm <<
"element " << i
605 <<
" - [type: " << typeString
607 <<
", usage: " << usage
611 <<
", vbo: " << el->
vbo_ 612 <<
", stride: " << getVertexStride(i)
621 static int status_ = -1;
623 #ifdef GL_ARB_instanced_arrays const VertexElement * findElementByUsage(VERTEX_USAGE _usage) const
static bool supportsInstancedArrays()
unsigned int getVertexStride(unsigned int i=0) const
VERTEX_USAGE
-— input name in vertex shader ----—
unsigned int type_
GL_FLOAT, GL_UNSIGNED_BYTE, GL_INT, ...
void activateShaderPipeline(GLSL::Program *_prog) const
void activateFixedFunction() const
unsigned int getNumElements() const
Description of one vertex element.
int findElementIdByUsage(VERTEX_USAGE _usage) const
unsigned int vbo_
Explicit vbo source of this element, set to 0 if the buffer bound at the time of activating the decla...
void setVertexStride(unsigned int _stride)
const void * pointer_
Offset in bytes to the first occurrence of this element in vertex buffer; Or address to vertex data i...
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
void updateShaderInputName(VertexElement *_pElem)
defined by user via VertexElement::shaderInputName_
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
unsigned int numElements_
how many elements of type_
static void texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glTexcoordPointer, supports locking
VERTEX_USAGE usage_
position, normal, shader input ..
bool openGLVersion(const int _major, const int _minor)
unsigned int divisor_
For instanced rendering: Step rate describing how many instances are drawn before advancing to the ne...
void addElements(unsigned int _numElements, const VertexElement *_pElements)
void setByteOffset(unsigned int _offset)
unsigned int getByteOffset() const
const char * shaderInputName_
set shader input name, if usage_ = VERTEX_USAGE_USER_DEFINED otherwise this is set automatically...
int getAttributeLocation(const char *_name)
Get location of the specified attribute.
void deactivateFixedFunction() const
Namespace providing different geometric functions concerning angles.
void deactivateShaderPipeline(GLSL::Program *_prog) const
static void colorPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glColorPointer, supports locking
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
void addElement(const VertexElement *_pElement)
static unsigned int getElementSize(const VertexElement *_pElement)
static unsigned int getGLTypeSize(unsigned int _type)
bool checkExtensionSupported(const std::string &_extension)
const VertexElement * getElement(unsigned int i) const
static void normalPointer(GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glNormalPointer, supports locking