/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #ifndef OSG_TRIANGLINDEXEFUNCTOR #define OSG_TRIANGLINDEXEFUNCTOR 1 #include #include namespace osg { template class TriangleIndexFunctor : public Drawable::PrimitiveIndexFunctor, public T { public: virtual void setVertexArray(unsigned int,const Vec2*) { } virtual void setVertexArray(unsigned int ,const Vec3* ) { } virtual void setVertexArray(unsigned int,const Vec4* ) { } virtual void begin(GLenum mode) { _modeCache = mode; _indexCache.clear(); } virtual void vertex(unsigned int vert) { _indexCache.push_back(vert); } virtual void end() { if (!_indexCache.empty()) { drawElements(_modeCache,_indexCache.size(),&_indexCache.front()); } } virtual void drawArrays(GLenum mode,GLint first,GLsizei count) { switch(mode) { case(GL_TRIANGLES): { unsigned int pos=first; for(GLsizei i=2;ioperator()(pos,pos+1,pos+2); } break; } case(GL_TRIANGLE_STRIP): { unsigned int pos=first; for(GLsizei i=2;ioperator()(pos,pos+2,pos+1); else this->operator()(pos,pos+1,pos+2); } break; } case(GL_QUADS): { unsigned int pos=first; for(GLsizei i=3;ioperator()(pos,pos+1,pos+2); this->operator()(pos,pos+2,pos+3); } break; } case(GL_QUAD_STRIP): { unsigned int pos=first; for(GLsizei i=3;ioperator()(pos,pos+1,pos+2); this->operator()(pos+1,pos+3,pos+2); } break; } case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN): { unsigned int pos=first+1; for(GLsizei i=2;ioperator()(first,pos,pos+1); } break; } case(GL_POINTS): case(GL_LINES): case(GL_LINE_STRIP): case(GL_LINE_LOOP): default: // can't be converted into to triangles. break; } } virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) { if (indices==0 || count==0) return; typedef const GLubyte* IndexPointer; switch(mode) { case(GL_TRIANGLES): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices;iptroperator()(*iptr,*(iptr+1),*(iptr+2)); break; } case(GL_TRIANGLE_STRIP): { IndexPointer iptr = indices; for(GLsizei i=2;ioperator()(*(iptr),*(iptr+2),*(iptr+1)); else this->operator()(*(iptr),*(iptr+1),*(iptr+2)); } break; } case(GL_QUADS): { IndexPointer iptr = indices; for(GLsizei i=3;ioperator()(*(iptr),*(iptr+1),*(iptr+2)); this->operator()(*(iptr),*(iptr+2),*(iptr+3)); } break; } case(GL_QUAD_STRIP): { IndexPointer iptr = indices; for(GLsizei i=3;ioperator()(*(iptr),*(iptr+1),*(iptr+2)); this->operator()(*(iptr+1),*(iptr+3),*(iptr+2)); } break; } case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN): { IndexPointer iptr = indices; unsigned int first = *iptr; ++iptr; for(GLsizei i=2;ioperator()(first,*(iptr),*(iptr+1)); } break; } case(GL_POINTS): case(GL_LINES): case(GL_LINE_STRIP): case(GL_LINE_LOOP): default: // can't be converted into to triangles. break; } } virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) { if (indices==0 || count==0) return; typedef const GLushort* IndexPointer; switch(mode) { case(GL_TRIANGLES): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices;iptroperator()(*iptr,*(iptr+1),*(iptr+2)); break; } case(GL_TRIANGLE_STRIP): { IndexPointer iptr = indices; for(GLsizei i=2;ioperator()(*(iptr),*(iptr+2),*(iptr+1)); else this->operator()(*(iptr),*(iptr+1),*(iptr+2)); } break; } case(GL_QUADS): { IndexPointer iptr = indices; for(GLsizei i=3;ioperator()(*(iptr),*(iptr+1),*(iptr+2)); this->operator()(*(iptr),*(iptr+2),*(iptr+3)); } break; } case(GL_QUAD_STRIP): { IndexPointer iptr = indices; for(GLsizei i=3;ioperator()(*(iptr),*(iptr+1),*(iptr+2)); this->operator()(*(iptr+1),*(iptr+3),*(iptr+2)); } break; } case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN): { IndexPointer iptr = indices; unsigned int first = *iptr; ++iptr; for(GLsizei i=2;ioperator()(first,*(iptr),*(iptr+1)); } break; } case(GL_POINTS): case(GL_LINES): case(GL_LINE_STRIP): case(GL_LINE_LOOP): default: // can't be converted into to triangles. break; } } virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) { if (indices==0 || count==0) return; typedef const GLuint* IndexPointer; switch(mode) { case(GL_TRIANGLES): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices;iptroperator()(*iptr,*(iptr+1),*(iptr+2)); break; } case(GL_TRIANGLE_STRIP): { IndexPointer iptr = indices; for(GLsizei i=2;ioperator()(*(iptr),*(iptr+2),*(iptr+1)); else this->operator()(*(iptr),*(iptr+1),*(iptr+2)); } break; } case(GL_QUADS): { IndexPointer iptr = indices; for(GLsizei i=3;ioperator()(*(iptr),*(iptr+1),*(iptr+2)); this->operator()(*(iptr),*(iptr+2),*(iptr+3)); } break; } case(GL_QUAD_STRIP): { IndexPointer iptr = indices; for(GLsizei i=3;ioperator()(*(iptr),*(iptr+1),*(iptr+2)); this->operator()(*(iptr+1),*(iptr+3),*(iptr+2)); } break; } case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN): { IndexPointer iptr = indices; unsigned int first = *iptr; ++iptr; for(GLsizei i=2;ioperator()(first,*(iptr),*(iptr+1)); } break; } case(GL_POINTS): case(GL_LINES): case(GL_LINE_STRIP): case(GL_LINE_LOOP): default: // can't be converted into to triangles. break; } } GLenum _modeCache; std::vector _indexCache; }; } #endif