TriangleFunctor
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_TRIANGLEFUNCTOR
#define OSG_TRIANGLEFUNCTOR 1
#include <osg/PrimitiveSet>
#include <osg/Notify>
namespace osg {
template<class T>
class TriangleFunctor : public PrimitiveFunctor, public T
{
public:
TriangleFunctor()
{
_vertexArraySize=0;
_vertexArrayPtr=0;
}
virtual ~TriangleFunctor() {}
virtual void setVertexArray(unsigned int,const Vec2*)
{
notify(WARN)<<"Triangle Functor does not support Vec2* vertex arrays"<<std::endl;
}
virtual void setVertexArray(unsigned int count,const Vec3* vertices)
{
_vertexArraySize = count;
_vertexArrayPtr = vertices;
}
virtual void setVertexArray(unsigned int,const Vec4* )
{
notify(WARN)<<"Triangle Functor does not support Vec4* vertex arrays"<<std::endl;
}
virtual void setVertexArray(unsigned int,const Vec2d*)
{
notify(WARN)<<"Triangle Functor does not support Vec2d* vertex arrays"<<std::endl;
}
virtual void setVertexArray(unsigned int,const Vec3d*)
{
notify(WARN)<<"Triangle Functor does not support Vec3d* vertex arrays"<<std::endl;
}
virtual void setVertexArray(unsigned int,const Vec4d* )
{
notify(WARN)<<"Triangle Functor does not support Vec4d* vertex arrays"<<std::endl;
}
virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
{
if (_vertexArrayPtr==0 || count==0) return;
switch(mode)
{
case(GL_TRIANGLES):
{
const Vec3* vlast = &_vertexArrayPtr[first+count];
for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=3)
this->operator()(*(vptr),*(vptr+1),*(vptr+2));
break;
}
case(GL_TRIANGLE_STRIP):
{
const Vec3* vptr = &_vertexArrayPtr[first];
for(GLsizei i=2;i<count;++i,++vptr)
{
if ((i%2)) this->operator()(*(vptr),*(vptr+2),*(vptr+1));
else this->operator()(*(vptr),*(vptr+1),*(vptr+2));
}
break;
}
case(GL_QUADS):
{
const Vec3* vptr = &_vertexArrayPtr[first];
for(GLsizei i=3;i<count;i+=4,vptr+=4)
{
this->operator()(*(vptr),*(vptr+1),*(vptr+2));
this->operator()(*(vptr),*(vptr+2),*(vptr+3));
}
break;
}
case(GL_QUAD_STRIP):
{
const Vec3* vptr = &_vertexArrayPtr[first];
for(GLsizei i=3;i<count;i+=2,vptr+=2)
{
this->operator()(*(vptr),*(vptr+1),*(vptr+2));
this->operator()(*(vptr+1),*(vptr+3),*(vptr+2));
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
const Vec3* vfirst = &_vertexArrayPtr[first];
const Vec3* vptr = vfirst+1;
for(GLsizei i=2;i<count;++i,++vptr)
{
this->operator()(*(vfirst),*(vptr),*(vptr+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;iptr<ilast;iptr+=3)
this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
break;
}
case(GL_TRIANGLE_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=2;i<count;++i,++iptr)
{
if ((i%2)) this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+1)]);
else this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
}
break;
}
case(GL_QUADS):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=4,iptr+=4)
{
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)]);
}
break;
}
case(GL_QUAD_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=2,iptr+=2)
{
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
this->operator()(_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)]);
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
IndexPointer iptr = indices;
const Vec3& vfirst = _vertexArrayPtr[*iptr];
++iptr;
for(GLsizei i=2;i<count;++i,++iptr)
{
this->operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(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;iptr<ilast;iptr+=3)
{
this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
}
break;
}
case(GL_TRIANGLE_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=2;i<count;++i,++iptr)
{
if ((i%2)) this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+1)]);
else this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
}
break;
}
case(GL_QUADS):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=4,iptr+=4)
{
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)]);
}
break;
}
case(GL_QUAD_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=2,iptr+=2)
{
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
this->operator()(_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)]);
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
IndexPointer iptr = indices;
const Vec3& vfirst = _vertexArrayPtr[*iptr];
++iptr;
for(GLsizei i=2;i<count;++i,++iptr)
{
this->operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(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;iptr<ilast;iptr+=3)
this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
break;
}
case(GL_TRIANGLE_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=2;i<count;++i,++iptr)
{
if ((i%2)) this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+1)]);
else this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
}
break;
}
case(GL_QUADS):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=4,iptr+=4)
{
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)]);
}
break;
}
case(GL_QUAD_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i=3;i<count;i+=2,iptr+=2)
{
this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
this->operator()(_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)]);
}
break;
}
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
case(GL_TRIANGLE_FAN):
{
IndexPointer iptr = indices;
const Vec3& vfirst = _vertexArrayPtr[*iptr];
++iptr;
for(GLsizei i=2;i<count;++i,++iptr)
{
this->operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(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;
}
}
protected:
unsigned int _vertexArraySize;
const Vec3* _vertexArrayPtr;
};
}
#endif
Notify
PrimitiveSet
osg::PrimitiveFunctor
Definition PrimitiveSet:54
osg::TriangleFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const Vec3d *)
Definition TriangleFunctor:71
osg::TriangleFunctor::_vertexArraySize
unsigned int _vertexArraySize
Definition TriangleFunctor:351
osg::TriangleFunctor::_vertexArrayPtr
const Vec3 * _vertexArrayPtr
Definition TriangleFunctor:352
osg::TriangleFunctor::setVertexArray
virtual void setVertexArray(unsigned int count, const Vec3 *vertices)
Definition TriangleFunctor:55
osg::TriangleFunctor::drawElements
virtual void drawElements(GLenum mode, GLsizei count, const GLuint *indices)
Mimics the OpenGL glDrawElements() function.
Definition TriangleFunctor:281
osg::TriangleFunctor::~TriangleFunctor
virtual ~TriangleFunctor()
Definition TriangleFunctor:48
osg::TriangleFunctor::drawArrays
virtual void drawArrays(GLenum mode, GLint first, GLsizei count)
Mimics the OpenGL glDrawArrays() function.
Definition TriangleFunctor:81
osg::TriangleFunctor::TriangleFunctor
TriangleFunctor()
Definition TriangleFunctor:42
osg::TriangleFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const Vec2 *)
Definition TriangleFunctor:50
osg::TriangleFunctor::drawElements
virtual void drawElements(GLenum mode, GLsizei count, const GLushort *indices)
Mimics the OpenGL glDrawElements() function.
Definition TriangleFunctor:212
osg::TriangleFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const Vec4 *)
Definition TriangleFunctor:61
osg::TriangleFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const Vec4d *)
Definition TriangleFunctor:76
osg::TriangleFunctor::drawElements
virtual void drawElements(GLenum mode, GLsizei count, const GLubyte *indices)
Mimics the OpenGL glDrawElements() function.
Definition TriangleFunctor:145
osg::TriangleFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const Vec2d *)
Definition TriangleFunctor:66
osg::Vec2d
Definition Vec2d:29
osg::Vec3d
Definition Vec3d:30
osg::Vec4d
Definition Vec4d:29
osg
Definition AlphaFunc:19
osg::Vec2
Vec2f Vec2
Definition Vec2:21
osg::WARN
@ WARN
Definition Notify:33
osg::Vec3
Vec3f Vec3
Definition Vec3:21
osg::Vec4
Vec4f Vec4
Definition Vec4:21
osg::notify
std::ostream & notify(void)
Definition Notify:80