TriangleLinePointIndexFunctor
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) Sketchfab
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commercial and non commercial
* applications, as long as this copyright notice is maintained.
*
* This application 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.
*
*/
#ifndef TRIANGLE_LINE_POINT_INDEX_FUNCTOR
#define TRIANGLE_LINE_POINT_INDEX_FUNCTOR
#include <osg/PrimitiveSet>
#include <osg/Array>
namespace osg {
template<class T>
class TriangleLinePointIndexFunctor : public osg::PrimitiveIndexFunctor, public T
{
public:
virtual void setVertexArray(unsigned int,const osg::Vec2*)
{}
virtual void setVertexArray(unsigned int ,const osg::Vec3* )
{}
virtual void setVertexArray(unsigned int,const osg::Vec4* )
{}
virtual void setVertexArray(unsigned int,const osg::Vec2d*)
{}
virtual void setVertexArray(unsigned int ,const osg::Vec3d* )
{}
virtual void setVertexArray(unsigned int,const osg::Vec4d* )
{}
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 ; i < count ; i += 3, pos += 3) {
this->operator()(pos, pos + 1, pos + 2);
}
break;
}
case(GL_TRIANGLE_STRIP):
{
unsigned int pos = first;
for(GLsizei i = 2 ; i < count ; ++ i, ++ pos) {
if ((i%2)) this->operator()(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 ; i < count ; i += 4, pos += 4) {
this->operator()(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 ; i < count ; i += 2, pos += 2) {
this->operator()(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 ; i < count ; ++ i, ++ pos) {
this->operator()(first, pos, pos + 1);
}
break;
}
case(GL_LINES):
{
unsigned int pos = first;
for(GLsizei i = 0 ; i < count ; i += 2, pos += 2) {
this->operator()(pos, pos + 1);
}
break;
}
case(GL_LINE_STRIP):
{
unsigned int pos = first;
for(GLsizei i = 0 ; i < count - 1 ; i += 1, pos += 1) {
this->operator()(pos, pos + 1);
}
break;
}
case(GL_LINE_LOOP):
{
unsigned int pos = first;
for(GLsizei i = 0 ; i < count - 1 ; i += 1, pos += 1) {
this->operator()(pos, pos + 1);
}
this->operator()(pos, first);
break;
}
case(GL_POINTS):
{
unsigned int pos=first;
for(GLsizei i = 0 ; i < count ; ++ i) {
this->operator()(pos + i);
}
break;
}
default:
break;
}
}
template<typename I>
void drawElements(GLenum mode, GLsizei count, const I* indices)
{
typedef I Index;
typedef const I* IndexPointer;
if (indices == 0 || count == 0) {
return;
}
switch(mode)
{
case(GL_TRIANGLES):
{
IndexPointer ilast = &indices[count];
for(IndexPointer iptr = indices ; iptr < ilast ; iptr += 3) {
this->operator()(*iptr, *(iptr + 1), *(iptr + 2));
}
break;
}
case(GL_TRIANGLE_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i = 2 ; i < count ; ++ i, ++ iptr) {
if ((i%2)) this->operator()(*(iptr), *(iptr + 2), *(iptr + 1));
else this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
}
break;
}
case(GL_QUADS):
{
IndexPointer iptr = indices;
for(GLsizei i = 3 ; i < count ; i += 4, iptr += 4) {
this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
this->operator()(*(iptr), *(iptr + 2), *(iptr + 3));
}
break;
}
case(GL_QUAD_STRIP):
{
IndexPointer iptr = indices;
for(GLsizei i = 3 ; i < count ; i += 2, iptr += 2) {
this->operator()(*(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;
Index first = *iptr;
++iptr;
for(GLsizei i = 2 ; i < count ; ++ i, ++ iptr) {
this->operator()(first, *(iptr), *(iptr + 1));
}
break;
}
case(GL_LINES):
{
const I* iptr = indices;
for(GLsizei i = 0 ; i < count ; i += 2, iptr += 2) {
this->operator()(*iptr, *(iptr + 1));
}
break;
}
case(GL_LINE_STRIP):
{
const I* iptr = indices;
for(GLsizei i = 0 ; i < count - 1 ; i += 1, iptr += 1) {
this->operator()(*iptr, *(iptr + 1));
}
break;
}
case(GL_LINE_LOOP):
{
const I* iptr = indices;
I first = *iptr;
for(GLsizei i = 0 ; i < count - 1 ; i += 1, iptr += 1) {
this->operator()(*iptr, *(iptr + 1));
}
this->operator()(*iptr, first);
break;
}
case GL_POINTS:
{
IndexPointer ilast = &indices[count];
for(IndexPointer iptr = indices ; iptr < ilast ; iptr += 1) {
this->operator()(*iptr);
}
break;
}
default:
break;
}
}
virtual void drawElements(GLenum mode, GLsizei count, const GLubyte* indices) {
drawElements<GLubyte>(mode, count, indices);
}
virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) {
drawElements<GLushort>(mode, count, indices);
}
virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) {
drawElements<GLuint>(mode, count, indices);
}
GLenum _modeCache;
std::vector<GLuint> _indexCache;
std::vector<unsigned int> _remap;
};
}
#endif
Array
PrimitiveSet
osg::PrimitiveIndexFunctor
Definition PrimitiveSet:103
osg::TriangleLinePointIndexFunctor
Definition TriangleLinePointIndexFunctor:23
osg::TriangleLinePointIndexFunctor::_modeCache
GLenum _modeCache
Definition TriangleLinePointIndexFunctor:253
osg::TriangleLinePointIndexFunctor::drawArrays
virtual void drawArrays(GLenum mode, GLint first, GLsizei count)
Definition TriangleLinePointIndexFunctor:58
osg::TriangleLinePointIndexFunctor::drawElements
virtual void drawElements(GLenum mode, GLsizei count, const GLushort *indices)
Definition TriangleLinePointIndexFunctor:244
osg::TriangleLinePointIndexFunctor::vertex
virtual void vertex(unsigned int vert)
Definition TriangleLinePointIndexFunctor:48
osg::TriangleLinePointIndexFunctor::drawElements
virtual void drawElements(GLenum mode, GLsizei count, const GLuint *indices)
Definition TriangleLinePointIndexFunctor:248
osg::TriangleLinePointIndexFunctor::end
virtual void end()
Definition TriangleLinePointIndexFunctor:52
osg::TriangleLinePointIndexFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const osg::Vec4 *)
Definition TriangleLinePointIndexFunctor:31
osg::TriangleLinePointIndexFunctor::_indexCache
std::vector< GLuint > _indexCache
Definition TriangleLinePointIndexFunctor:254
osg::TriangleLinePointIndexFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const osg::Vec3d *)
Definition TriangleLinePointIndexFunctor:37
osg::TriangleLinePointIndexFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const osg::Vec2d *)
Definition TriangleLinePointIndexFunctor:34
osg::TriangleLinePointIndexFunctor::drawElements
virtual void drawElements(GLenum mode, GLsizei count, const GLubyte *indices)
Definition TriangleLinePointIndexFunctor:240
osg::TriangleLinePointIndexFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const osg::Vec3 *)
Definition TriangleLinePointIndexFunctor:28
osg::TriangleLinePointIndexFunctor::drawElements
void drawElements(GLenum mode, GLsizei count, const I *indices)
Definition TriangleLinePointIndexFunctor:144
osg::TriangleLinePointIndexFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const osg::Vec4d *)
Definition TriangleLinePointIndexFunctor:40
osg::TriangleLinePointIndexFunctor::_remap
std::vector< unsigned int > _remap
Definition TriangleLinePointIndexFunctor:255
osg::TriangleLinePointIndexFunctor::begin
virtual void begin(GLenum mode)
Definition TriangleLinePointIndexFunctor:43
osg::TriangleLinePointIndexFunctor::setVertexArray
virtual void setVertexArray(unsigned int, const osg::Vec2 *)
Definition TriangleLinePointIndexFunctor:25
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::Vec3
Vec3f Vec3
Definition Vec3:21
osg::Vec4
Vec4f Vec4
Definition Vec4:21