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()
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)
169 elements_.push_back(tmp);
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_;
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;
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;
361 for (
unsigned int i = 0; i < numElements; ++i)
381 glClientActiveTexture(GL_TEXTURE0);
406 for (
unsigned int i = 0; i < numElements; ++i)
432 for (
unsigned int i = 0; i < numElements; ++i)
444 GLboolean normalizeElem = GL_TRUE;
447 normalizeElem = GL_FALSE;
452 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &curVBO);
453 glBindBuffer(GL_ARRAY_BUFFER, pElem->
vbo_);
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);
482 for (
unsigned int i = 0; i < numElements; ++i)
490 glDisableVertexAttribArray(loc);
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)
536 std::map<unsigned int, unsigned int>::const_iterator it =
vertexStridesVBO_.find(vbo);
562 const char* usageStrings[] =
574 QTextStream resultStrm(&result);
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_
621 static int status_ = -1;
623 #ifdef GL_ARB_instanced_arrays
void deactivateShaderPipeline(GLSL::Program *_prog) const
Namespace providing different geometric functions concerning angles.
static unsigned int getGLTypeSize(unsigned int _type)
static void colorPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glColorPointer, supports locking
void deactivateFixedFunction() const
void setByteOffset(unsigned int _offset)
int getAttributeLocation(const char *_name)
Get location of the specified attribute.
bool openGLVersion(const int _major, const int _minor)
void activateShaderPipeline(GLSL::Program *_prog) const
unsigned int vertexStride_
Offset in bytes between each vertex.
const VertexElement * getElement(unsigned int i) const
Description of one vertex element.
unsigned int getVertexStride(unsigned int i=0) const
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
bool checkExtensionSupported(const std::string &_extension)
static unsigned int getElementSize(const VertexElement *_pElement)
unsigned int getNumElements() const
static bool supportsInstancedArrays()
defined by user via VertexElement::shaderInputName_
static void normalPointer(GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glNormalPointer, supports locking
int strideUserDefined_
Flag that indicates, whether the stride was set by user or derived automatically. ...
void updateShaderInputName(VertexElement *_pElem)
static void texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glTexcoordPointer, supports locking
int findElementIdByUsage(VERTEX_USAGE _usage) const
void activateFixedFunction() const
unsigned int divisor_
For instanced rendering: Step rate describing how many instances are drawn before advancing to the ne...
unsigned int vbo_
Explicit vbo source of this element, set to 0 if the buffer bound at the time of activating the decla...
unsigned int type_
GL_FLOAT, GL_UNSIGNED_BYTE, GL_INT, ...
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
const VertexElement * findElementByUsage(VERTEX_USAGE _usage) const
unsigned int numElements_
how many elements of type_
unsigned int getByteOffset() const
VERTEX_USAGE usage_
position, normal, shader input ..
const char * shaderInputName_
set shader input name, if usage_ = VERTEX_USAGE_USER_DEFINED otherwise this is set automatically...
VERTEX_USAGE
-— input name in vertex shader ----—
const void * pointer_
Offset in bytes to the first occurrence of this element in vertex buffer; Or address to vertex data i...
void addElements(unsigned int _numElements, const VertexElement *_pElements)
std::map< unsigned int, unsigned int > vertexStridesVBO_
Map vbo to offset in bytes between each vertex in that vbo.
void addElement(const VertexElement *_pElement)
void setVertexStride(unsigned int _stride)