OpenSceneGraph/include/osg/TriangleIndexFunctor

305 lines
9.7 KiB
Plaintext
Raw Normal View History

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2004-02-09 18:14:06 +08:00
*
* 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
2004-02-09 18:14:06 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
2004-02-09 18:14:06 +08:00
* 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
2004-02-09 18:14:06 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_TRIANGLEINDEXFUNCTOR
#define OSG_TRIANGLEINDEXFUNCTOR 1
2004-02-09 18:14:06 +08:00
#include <osg/PrimitiveSet>
2004-02-09 18:14:06 +08:00
#include <osg/Notify>
namespace osg {
template<class T>
class TriangleIndexFunctor : public PrimitiveIndexFunctor, public T
2004-02-09 18:14:06 +08:00
{
public:
virtual void setVertexArray(unsigned int,const Vec2*) {}
virtual void setVertexArray(unsigned int ,const Vec3*) {}
virtual void setVertexArray(unsigned int,const Vec4*) {}
virtual void setVertexArray(unsigned int,const Vec2d*) {}
virtual void setVertexArray(unsigned int ,const Vec3d*) {}
virtual void setVertexArray(unsigned int,const Vec4d*) {}
2004-02-09 18:14:06 +08:00
virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
{
switch(mode)
{
case(GL_TRIANGLES):
{
unsigned int pos=first;
for(GLsizei i=2;i<count;i+=3,pos+=3)
{
2004-08-01 16:26:44 +08:00
this->operator()(pos,pos+1,pos+2);
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_TRIANGLE_STRIP):
{
2004-02-09 18:14:06 +08:00
unsigned int pos=first;
for(GLsizei i=2;i<count;++i,++pos)
{
2005-11-18 01:44:48 +08:00
if ((i%2)) this->operator()(pos,pos+2,pos+1);
else this->operator()(pos,pos+1,pos+2);
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUADS):
{
unsigned int pos=first;
for(GLsizei i=3;i<count;i+=4,pos+=4)
{
2004-08-01 16:26:44 +08:00
this->operator()(pos,pos+1,pos+2);
this->operator()(pos,pos+2,pos+3);
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUAD_STRIP):
{
unsigned int pos=first;
for(GLsizei i=3;i<count;i+=2,pos+=2)
{
2004-08-01 16:26:44 +08:00
this->operator()(pos,pos+1,pos+2);
this->operator()(pos+1,pos+3,pos+2);
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
unsigned int pos=first+1;
for(GLsizei i=2;i<count;++i,++pos)
{
2004-08-01 16:26:44 +08:00
this->operator()(first,pos,pos+1);
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POINTS):
case(GL_LINES):
case(GL_LINE_STRIP):
case(GL_LINE_LOOP):
default:
// can't be converted into to triangles.
break;
}
}
2004-02-09 18:14:06 +08:00
virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices)
{
if (indices==0 || count==0) return;
typedef GLubyte Index;
typedef const Index* IndexPointer;
2004-02-09 18:14:06 +08:00
switch(mode)
{
case(GL_TRIANGLES):
{
IndexPointer ilast = &indices[count];
for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
2004-08-01 16:26:44 +08:00
this->operator()(*iptr,*(iptr+1),*(iptr+2));
2004-02-09 18:14:06 +08:00
break;
}
case(GL_TRIANGLE_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=2;i<count;++i,++iptr)
{
2005-11-18 01:44:48 +08:00
if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUADS):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=4,iptr+=4)
{
2004-08-01 16:26:44 +08:00
this->operator()(*(iptr),*(iptr+1),*(iptr+2));
this->operator()(*(iptr),*(iptr+2),*(iptr+3));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUAD_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=2,iptr+=2)
{
2004-08-01 16:26:44 +08:00
this->operator()(*(iptr),*(iptr+1),*(iptr+2));
this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
IndexPointer iptr = indices;
Index first = *iptr;
2004-02-09 18:14:06 +08:00
++iptr;
for(GLsizei i=2;i<count;++i,++iptr)
{
2004-08-01 16:26:44 +08:00
this->operator()(first,*(iptr),*(iptr+1));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POINTS):
case(GL_LINES):
case(GL_LINE_STRIP):
case(GL_LINE_LOOP):
default:
// can't be converted into to triangles.
break;
}
}
2004-02-09 18:14:06 +08:00
virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices)
{
if (indices==0 || count==0) return;
typedef GLushort Index;
typedef const Index* IndexPointer;
2004-02-09 18:14:06 +08:00
switch(mode)
{
case(GL_TRIANGLES):
{
IndexPointer ilast = &indices[count];
for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
2004-08-01 16:26:44 +08:00
this->operator()(*iptr,*(iptr+1),*(iptr+2));
2004-02-09 18:14:06 +08:00
break;
}
case(GL_TRIANGLE_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=2;i<count;++i,++iptr)
{
2005-11-18 01:44:48 +08:00
if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUADS):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=4,iptr+=4)
{
2004-08-01 16:26:44 +08:00
this->operator()(*(iptr),*(iptr+1),*(iptr+2));
this->operator()(*(iptr),*(iptr+2),*(iptr+3));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUAD_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=2,iptr+=2)
{
2004-08-01 16:26:44 +08:00
this->operator()(*(iptr),*(iptr+1),*(iptr+2));
this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
IndexPointer iptr = indices;
Index first = *iptr;
2004-02-09 18:14:06 +08:00
++iptr;
for(GLsizei i=2;i<count;++i,++iptr)
{
2004-08-01 16:26:44 +08:00
this->operator()(first,*(iptr),*(iptr+1));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POINTS):
case(GL_LINES):
case(GL_LINE_STRIP):
case(GL_LINE_LOOP):
default:
// can't be converted into to triangles.
break;
}
}
2004-02-09 18:14:06 +08:00
virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices)
{
if (indices==0 || count==0) return;
typedef GLuint Index;
typedef const Index* IndexPointer;
2004-02-09 18:14:06 +08:00
switch(mode)
{
case(GL_TRIANGLES):
{
IndexPointer ilast = &indices[count];
for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
2004-08-01 16:26:44 +08:00
this->operator()(*iptr,*(iptr+1),*(iptr+2));
2004-02-09 18:14:06 +08:00
break;
}
case(GL_TRIANGLE_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=2;i<count;++i,++iptr)
{
2005-11-18 01:44:48 +08:00
if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUADS):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=4,iptr+=4)
{
2004-08-01 16:26:44 +08:00
this->operator()(*(iptr),*(iptr+1),*(iptr+2));
this->operator()(*(iptr),*(iptr+2),*(iptr+3));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_QUAD_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=2,iptr+=2)
{
2004-08-01 16:26:44 +08:00
this->operator()(*(iptr),*(iptr+1),*(iptr+2));
this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
IndexPointer iptr = indices;
Index first = *iptr;
2004-02-09 18:14:06 +08:00
++iptr;
for(GLsizei i=2;i<count;++i,++iptr)
{
2004-08-01 16:26:44 +08:00
this->operator()(first,*(iptr),*(iptr+1));
2004-02-09 18:14:06 +08:00
}
break;
}
case(GL_POINTS):
case(GL_LINES):
case(GL_LINE_STRIP):
case(GL_LINE_LOOP):
default:
// can't be converted into to triangles.
break;
}
}
2004-02-09 18:14:06 +08:00
};
}
#endif