1/* -*-c++-*- OpenSceneGraph - Copyright (C) Sketchfab
3 * This application is open source and may be redistributed and/or modified
4 * freely and without restriction, both in commercial and non commercial
5 * applications, as long as this copyright notice is maintained.
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13#ifndef TRIANGLE_LINE_POINT_INDEX_FUNCTOR
14#define TRIANGLE_LINE_POINT_INDEX_FUNCTOR
16#include <osg/PrimitiveSet>
22class TriangleLinePointIndexFunctor : public osg::PrimitiveIndexFunctor, public T
25 virtual void setVertexArray(unsigned int,const osg::Vec2*)
28 virtual void setVertexArray(unsigned int ,const osg::Vec3* )
31 virtual void setVertexArray(unsigned int,const osg::Vec4* )
34 virtual void setVertexArray(unsigned int,const osg::Vec2d*)
37 virtual void setVertexArray(unsigned int ,const osg::Vec3d* )
40 virtual void setVertexArray(unsigned int,const osg::Vec4d* )
43 virtual void begin(GLenum mode) {
48 virtual void vertex(unsigned int vert) {
49 _indexCache.push_back(vert);
53 if (!_indexCache.empty()) {
54 drawElements(_modeCache,_indexCache.size(),&_indexCache.front());
58 virtual void drawArrays(GLenum mode, GLint first, GLsizei count) {
63 unsigned int pos=first;
64 for(GLsizei i = 2 ; i < count ; i += 3, pos += 3) {
65 this->operator()(pos, pos + 1, pos + 2);
69 case(GL_TRIANGLE_STRIP):
71 unsigned int pos = first;
72 for(GLsizei i = 2 ; i < count ; ++ i, ++ pos) {
73 if ((i%2)) this->operator()(pos, pos + 2, pos + 1);
74 else this->operator()(pos, pos + 1, pos + 2);
80 unsigned int pos = first;
81 for(GLsizei i = 3 ; i < count ; i += 4, pos += 4) {
82 this->operator()(pos,pos + 1, pos + 2);
83 this->operator()(pos,pos + 2, pos + 3);
89 unsigned int pos = first;
90 for(GLsizei i = 3 ; i < count ; i += 2, pos += 2) {
91 this->operator()(pos, pos + 1,pos + 2);
92 this->operator()(pos + 1,pos + 3,pos + 2);
96 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
97 case(GL_TRIANGLE_FAN):
99 unsigned int pos = first + 1;
100 for(GLsizei i = 2 ; i < count ; ++ i, ++ pos) {
101 this->operator()(first, pos, pos + 1);
107 unsigned int pos = first;
108 for(GLsizei i = 0 ; i < count ; i += 2, pos += 2) {
109 this->operator()(pos, pos + 1);
115 unsigned int pos = first;
116 for(GLsizei i = 0 ; i < count - 1 ; i += 1, pos += 1) {
117 this->operator()(pos, pos + 1);
123 unsigned int pos = first;
124 for(GLsizei i = 0 ; i < count - 1 ; i += 1, pos += 1) {
125 this->operator()(pos, pos + 1);
127 this->operator()(pos, first);
132 unsigned int pos=first;
133 for(GLsizei i = 0 ; i < count ; ++ i) {
134 this->operator()(pos + i);
144 void drawElements(GLenum mode, GLsizei count, const I* indices)
147 typedef const I* IndexPointer;
149 if (indices == 0 || count == 0) {
157 IndexPointer ilast = &indices[count];
158 for(IndexPointer iptr = indices ; iptr < ilast ; iptr += 3) {
159 this->operator()(*iptr, *(iptr + 1), *(iptr + 2));
163 case(GL_TRIANGLE_STRIP):
165 IndexPointer iptr = indices;
166 for(GLsizei i = 2 ; i < count ; ++ i, ++ iptr) {
167 if ((i%2)) this->operator()(*(iptr), *(iptr + 2), *(iptr + 1));
168 else this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
174 IndexPointer iptr = indices;
175 for(GLsizei i = 3 ; i < count ; i += 4, iptr += 4) {
176 this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
177 this->operator()(*(iptr), *(iptr + 2), *(iptr + 3));
183 IndexPointer iptr = indices;
184 for(GLsizei i = 3 ; i < count ; i += 2, iptr += 2) {
185 this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
186 this->operator()(*(iptr + 1), *(iptr + 3), *(iptr + 2));
190 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
191 case(GL_TRIANGLE_FAN):
193 IndexPointer iptr = indices;
196 for(GLsizei i = 2 ; i < count ; ++ i, ++ iptr) {
197 this->operator()(first, *(iptr), *(iptr + 1));
203 const I* iptr = indices;
204 for(GLsizei i = 0 ; i < count ; i += 2, iptr += 2) {
205 this->operator()(*iptr, *(iptr + 1));
211 const I* iptr = indices;
212 for(GLsizei i = 0 ; i < count - 1 ; i += 1, iptr += 1) {
213 this->operator()(*iptr, *(iptr + 1));
219 const I* iptr = indices;
221 for(GLsizei i = 0 ; i < count - 1 ; i += 1, iptr += 1) {
222 this->operator()(*iptr, *(iptr + 1));
224 this->operator()(*iptr, first);
229 IndexPointer ilast = &indices[count];
230 for(IndexPointer iptr = indices ; iptr < ilast ; iptr += 1) {
231 this->operator()(*iptr);
240 virtual void drawElements(GLenum mode, GLsizei count, const GLubyte* indices) {
241 drawElements<GLubyte>(mode, count, indices);
244 virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) {
245 drawElements<GLushort>(mode, count, indices);
248 virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) {
249 drawElements<GLuint>(mode, count, indices);
254 std::vector<GLuint> _indexCache;
255 std::vector<unsigned int> _remap;