1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSG_TRIANGLEINDEXFUNCTOR
15#define OSG_TRIANGLEINDEXFUNCTOR 1
17#include <osg/PrimitiveSet>
23class TriangleIndexFunctor : public PrimitiveIndexFunctor, public T
27 virtual void setVertexArray(unsigned int,const Vec2*) {}
28 virtual void setVertexArray(unsigned int ,const Vec3*) {}
29 virtual void setVertexArray(unsigned int,const Vec4*) {}
30 virtual void setVertexArray(unsigned int,const Vec2d*) {}
31 virtual void setVertexArray(unsigned int ,const Vec3d*) {}
32 virtual void setVertexArray(unsigned int,const Vec4d*) {}
34 virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
40 unsigned int pos=first;
41 for(GLsizei i=2;i<count;i+=3,pos+=3)
43 this->operator()(pos,pos+1,pos+2);
47 case(GL_TRIANGLE_STRIP):
49 unsigned int pos=first;
50 for(GLsizei i=2;i<count;++i,++pos)
52 if ((i%2)) this->operator()(pos,pos+2,pos+1);
53 else this->operator()(pos,pos+1,pos+2);
59 unsigned int pos=first;
60 for(GLsizei i=3;i<count;i+=4,pos+=4)
62 this->operator()(pos,pos+1,pos+2);
63 this->operator()(pos,pos+2,pos+3);
69 unsigned int pos=first;
70 for(GLsizei i=3;i<count;i+=2,pos+=2)
72 this->operator()(pos,pos+1,pos+2);
73 this->operator()(pos+1,pos+3,pos+2);
77 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
78 case(GL_TRIANGLE_FAN):
80 unsigned int pos=first+1;
81 for(GLsizei i=2;i<count;++i,++pos)
83 this->operator()(first,pos,pos+1);
92 // can't be converted into to triangles.
97 virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices)
99 if (indices==0 || count==0) return;
101 typedef GLubyte Index;
102 typedef const Index* IndexPointer;
108 IndexPointer ilast = &indices[count];
109 for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
110 this->operator()(*iptr,*(iptr+1),*(iptr+2));
113 case(GL_TRIANGLE_STRIP):
115 IndexPointer iptr = indices;
116 for(GLsizei i=2;i<count;++i,++iptr)
118 if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
119 else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
125 IndexPointer iptr = indices;
126 for(GLsizei i=3;i<count;i+=4,iptr+=4)
128 this->operator()(*(iptr),*(iptr+1),*(iptr+2));
129 this->operator()(*(iptr),*(iptr+2),*(iptr+3));
135 IndexPointer iptr = indices;
136 for(GLsizei i=3;i<count;i+=2,iptr+=2)
138 this->operator()(*(iptr),*(iptr+1),*(iptr+2));
139 this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
143 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
144 case(GL_TRIANGLE_FAN):
146 IndexPointer iptr = indices;
149 for(GLsizei i=2;i<count;++i,++iptr)
151 this->operator()(first,*(iptr),*(iptr+1));
160 // can't be converted into to triangles.
165 virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices)
167 if (indices==0 || count==0) return;
169 typedef GLushort Index;
170 typedef const Index* IndexPointer;
176 IndexPointer ilast = &indices[count];
177 for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
178 this->operator()(*iptr,*(iptr+1),*(iptr+2));
181 case(GL_TRIANGLE_STRIP):
183 IndexPointer iptr = indices;
184 for(GLsizei i=2;i<count;++i,++iptr)
186 if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
187 else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
193 IndexPointer iptr = indices;
194 for(GLsizei i=3;i<count;i+=4,iptr+=4)
196 this->operator()(*(iptr),*(iptr+1),*(iptr+2));
197 this->operator()(*(iptr),*(iptr+2),*(iptr+3));
203 IndexPointer iptr = indices;
204 for(GLsizei i=3;i<count;i+=2,iptr+=2)
206 this->operator()(*(iptr),*(iptr+1),*(iptr+2));
207 this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
211 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
212 case(GL_TRIANGLE_FAN):
214 IndexPointer iptr = indices;
217 for(GLsizei i=2;i<count;++i,++iptr)
219 this->operator()(first,*(iptr),*(iptr+1));
228 // can't be converted into to triangles.
233 virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices)
235 if (indices==0 || count==0) return;
237 typedef GLuint Index;
238 typedef const Index* IndexPointer;
244 IndexPointer ilast = &indices[count];
245 for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
246 this->operator()(*iptr,*(iptr+1),*(iptr+2));
249 case(GL_TRIANGLE_STRIP):
251 IndexPointer iptr = indices;
252 for(GLsizei i=2;i<count;++i,++iptr)
254 if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
255 else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
261 IndexPointer iptr = indices;
262 for(GLsizei i=3;i<count;i+=4,iptr+=4)
264 this->operator()(*(iptr),*(iptr+1),*(iptr+2));
265 this->operator()(*(iptr),*(iptr+2),*(iptr+3));
271 IndexPointer iptr = indices;
272 for(GLsizei i=3;i<count;i+=2,iptr+=2)
274 this->operator()(*(iptr),*(iptr+1),*(iptr+2));
275 this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
279 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
280 case(GL_TRIANGLE_FAN):
282 IndexPointer iptr = indices;
285 for(GLsizei i=2;i<count;++i,++iptr)
287 this->operator()(first,*(iptr),*(iptr+1));
296 // can't be converted into to triangles.