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_TERMPLATEPRIMITIVEFUNCTOR
15#define OSG_TERMPLATEPRIMITIVEFUNCTOR 1
17#include <osg/PrimitiveSet>
23/** Provides access to the primitives that compose an \c osg::Drawable.
24 * <p>Notice that \c TemplatePrimitiveFunctor is a class template, and that it inherits
25 * from its template parameter \c T. This template parameter must implement
26 * <tt>operator()(const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3
27 * v3, bool treatVertexDataAsTemporary)</tt>,
28 * <tt>operator()(const osg::Vec3 v1, const osg::Vec3 v2, bool
29 * treatVertexDataAsTemporary)</tt>, <tt>operator()(const osg::Vec3 v1,
30 * const osg::Vec3 v2, const osg::Vec3 v3, bool treatVertexDataAsTemporary)</tt>,
31 * and <tt>operator()(const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3 v3,
32 * const osg::Vec3 v4, bool treatVertexDataAsTemporary)</tt> which will be called
33 * for the matching primitive when the functor is applied to a \c Drawable.
34 * Parameters \c v1, \c v2, \c v3, and \c v4 are the vertices of the primitive.
35 * The last parameter, \c treatVertexDataAsTemporary, indicates whether these
36 * vertices are coming from a "real" vertex array, or from a temporary vertex array,
37 * created by the \c TemplatePrimitiveFunctor from some other geometry representation.
38 * @see \c PrimitiveFunctor for general usage hints.
41class TemplatePrimitiveFunctor : public PrimitiveFunctor, public T
45 TemplatePrimitiveFunctor()
51 virtual ~TemplatePrimitiveFunctor() {}
53 virtual void setVertexArray(unsigned int,const Vec2*)
55 notify(WARN)<<"Triangle Functor does not support Vec2* vertex arrays"<<std::endl;
58 virtual void setVertexArray(unsigned int count,const Vec3* vertices)
60 _vertexArraySize = count;
61 _vertexArrayPtr = vertices;
64 virtual void setVertexArray(unsigned int,const Vec4* )
66 notify(WARN)<<"Triangle Functor does not support Vec4* vertex arrays"<<std::endl;
69 virtual void setVertexArray(unsigned int,const Vec2d*)
71 notify(WARN)<<"Triangle Functor does not support Vec2d* vertex arrays"<<std::endl;
74 virtual void setVertexArray(unsigned int,const Vec3d*)
76 notify(WARN)<<"Triangle Functor does not support Vec3d* vertex arrays"<<std::endl;
79 virtual void setVertexArray(unsigned int,const Vec4d* )
81 notify(WARN)<<"Triangle Functor does not support Vec4d* vertex arrays"<<std::endl;
85 virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
87 if (_vertexArrayPtr==0 || count==0) return;
92 const Vec3* vlast = &_vertexArrayPtr[first+count];
93 for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=3)
94 this->operator()(*(vptr),*(vptr+1),*(vptr+2),false);
97 case(GL_TRIANGLE_STRIP): {
98 const Vec3* vptr = &_vertexArrayPtr[first];
99 for(GLsizei i=2;i<count;++i,++vptr)
101 if ((i%2)) this->operator()(*(vptr),*(vptr+2),*(vptr+1),false);
102 else this->operator()(*(vptr),*(vptr+1),*(vptr+2),false);
107 const Vec3* vptr = &_vertexArrayPtr[first];
108 for(GLsizei i=3;i<count;i+=4,vptr+=4)
110 this->operator()(*(vptr),*(vptr+1),*(vptr+2),*(vptr+3),false);
114 case(GL_QUAD_STRIP): {
115 const Vec3* vptr = &_vertexArrayPtr[first];
116 for(GLsizei i=3;i<count;i+=2,vptr+=2)
118 this->operator()(*(vptr),*(vptr+1),*(vptr+3),*(vptr+2),false);
122 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
123 case(GL_TRIANGLE_FAN): {
124 const Vec3* vfirst = &_vertexArrayPtr[first];
125 const Vec3* vptr = vfirst+1;
126 for(GLsizei i=2;i<count;++i,++vptr)
128 this->operator()(*(vfirst),*(vptr),*(vptr+1),false);
133 const Vec3* vlast = &_vertexArrayPtr[first+count];
134 for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
135 this->operator()(*(vptr),false);
139 const Vec3* vlast = &_vertexArrayPtr[first+count-1];
140 for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=2)
141 this->operator()(*(vptr),*(vptr+1),false);
144 case(GL_LINE_STRIP): {
145 const Vec3* vlast = &_vertexArrayPtr[first+count-1];
146 for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
147 this->operator()(*(vptr),*(vptr+1),false);
150 case(GL_LINE_STRIP_ADJACENCY): {
151 const Vec3* vlast = &_vertexArrayPtr[first+count-2];
152 for(const Vec3* vptr=&_vertexArrayPtr[first+1];vptr<vlast;vptr+=1)
153 this->operator()(*(vptr),*(vptr+1),false);
156 case(GL_LINE_LOOP): {
157 const Vec3* vlast = &_vertexArrayPtr[first+count-1];
158 for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
159 this->operator()(*(vptr),*(vptr+1),false);
160 this->operator()(*(vlast),_vertexArrayPtr[first],false);
168 template<class IndexType>
169 void drawElementsTemplate(GLenum mode,GLsizei count,const IndexType* indices)
171 if (indices==0 || count==0) return;
173 typedef const IndexType* IndexPointer;
177 case(GL_TRIANGLES): {
178 IndexPointer ilast = &indices[count];
179 for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
180 this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)],false);
183 case(GL_TRIANGLE_STRIP): {
184 IndexPointer iptr = indices;
185 for(GLsizei i=2;i<count;++i,++iptr)
187 if ((i%2)) this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],
188 _vertexArrayPtr[*(iptr+1)],false);
189 else this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
190 _vertexArrayPtr[*(iptr+2)],false);
195 IndexPointer iptr = indices;
196 for(GLsizei i=3;i<count;i+=4,iptr+=4)
198 this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
199 _vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)],
204 case(GL_QUAD_STRIP): {
205 IndexPointer iptr = indices;
206 for(GLsizei i=3;i<count;i+=2,iptr+=2)
208 this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
209 _vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)],
214 case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
215 case(GL_TRIANGLE_FAN): {
216 IndexPointer iptr = indices;
217 const Vec3& vfirst = _vertexArrayPtr[*iptr];
219 for(GLsizei i=2;i<count;++i,++iptr)
221 this->operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
227 IndexPointer ilast = &indices[count];
228 for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
229 this->operator()(_vertexArrayPtr[*iptr],false);
233 IndexPointer ilast = &indices[count-1];
234 for(IndexPointer iptr=indices;iptr<ilast;iptr+=2)
235 this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
239 case(GL_LINE_STRIP): {
240 IndexPointer ilast = &indices[count-1];
241 for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
242 this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
246 case(GL_LINE_STRIP_ADJACENCY): {
247 IndexPointer ilast = &indices[count-2];
248 for(IndexPointer iptr=&indices[1];iptr<ilast;iptr+=1)
249 this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
253 case(GL_LINE_LOOP): {
254 IndexPointer ilast = &indices[count-1];
255 for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
256 this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
258 this->operator()(_vertexArrayPtr[*(ilast)],_vertexArrayPtr[indices[0]],
268 virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices)
270 drawElementsTemplate(mode, count, indices);
273 virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices)
275 drawElementsTemplate(mode, count, indices);
278 virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices)
280 drawElementsTemplate(mode, count, indices);
285 unsigned int _vertexArraySize;
286 const Vec3* _vertexArrayPtr;