openscenegraph
TemplatePrimitiveFunctor
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSG_TERMPLATEPRIMITIVEFUNCTOR
15#define OSG_TERMPLATEPRIMITIVEFUNCTOR 1
16
17#include <osg/PrimitiveSet>
18#include <osg/Notify>
19
20namespace osg {
21
22
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.
39 */
40template<class T>
41class TemplatePrimitiveFunctor : public PrimitiveFunctor, public T
42{
43public:
44
45 TemplatePrimitiveFunctor()
46 {
47 _vertexArraySize=0;
48 _vertexArrayPtr=0;
49 }
50
51 virtual ~TemplatePrimitiveFunctor() {}
52
53 virtual void setVertexArray(unsigned int,const Vec2*)
54 {
55 notify(WARN)<<"Triangle Functor does not support Vec2* vertex arrays"<<std::endl;
56 }
57
58 virtual void setVertexArray(unsigned int count,const Vec3* vertices)
59 {
60 _vertexArraySize = count;
61 _vertexArrayPtr = vertices;
62 }
63
64 virtual void setVertexArray(unsigned int,const Vec4* )
65 {
66 notify(WARN)<<"Triangle Functor does not support Vec4* vertex arrays"<<std::endl;
67 }
68
69 virtual void setVertexArray(unsigned int,const Vec2d*)
70 {
71 notify(WARN)<<"Triangle Functor does not support Vec2d* vertex arrays"<<std::endl;
72 }
73
74 virtual void setVertexArray(unsigned int,const Vec3d*)
75 {
76 notify(WARN)<<"Triangle Functor does not support Vec3d* vertex arrays"<<std::endl;
77 }
78
79 virtual void setVertexArray(unsigned int,const Vec4d* )
80 {
81 notify(WARN)<<"Triangle Functor does not support Vec4d* vertex arrays"<<std::endl;
82 }
83
84
85 virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
86 {
87 if (_vertexArrayPtr==0 || count==0) return;
88
89 switch(mode)
90 {
91 case(GL_TRIANGLES): {
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);
95 break;
96 }
97 case(GL_TRIANGLE_STRIP): {
98 const Vec3* vptr = &_vertexArrayPtr[first];
99 for(GLsizei i=2;i<count;++i,++vptr)
100 {
101 if ((i%2)) this->operator()(*(vptr),*(vptr+2),*(vptr+1),false);
102 else this->operator()(*(vptr),*(vptr+1),*(vptr+2),false);
103 }
104 break;
105 }
106 case(GL_QUADS): {
107 const Vec3* vptr = &_vertexArrayPtr[first];
108 for(GLsizei i=3;i<count;i+=4,vptr+=4)
109 {
110 this->operator()(*(vptr),*(vptr+1),*(vptr+2),*(vptr+3),false);
111 }
112 break;
113 }
114 case(GL_QUAD_STRIP): {
115 const Vec3* vptr = &_vertexArrayPtr[first];
116 for(GLsizei i=3;i<count;i+=2,vptr+=2)
117 {
118 this->operator()(*(vptr),*(vptr+1),*(vptr+3),*(vptr+2),false);
119 }
120 break;
121 }
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)
127 {
128 this->operator()(*(vfirst),*(vptr),*(vptr+1),false);
129 }
130 break;
131 }
132 case(GL_POINTS): {
133 const Vec3* vlast = &_vertexArrayPtr[first+count];
134 for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
135 this->operator()(*(vptr),false);
136 break;
137 }
138 case(GL_LINES): {
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);
142 break;
143 }
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);
148 break;
149 }
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);
154 break;
155 }
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);
161 break;
162 }
163 default:
164 break;
165 }
166 }
167
168 template<class IndexType>
169 void drawElementsTemplate(GLenum mode,GLsizei count,const IndexType* indices)
170 {
171 if (indices==0 || count==0) return;
172
173 typedef const IndexType* IndexPointer;
174
175 switch(mode)
176 {
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);
181 break;
182 }
183 case(GL_TRIANGLE_STRIP): {
184 IndexPointer iptr = indices;
185 for(GLsizei i=2;i<count;++i,++iptr)
186 {
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);
191 }
192 break;
193 }
194 case(GL_QUADS): {
195 IndexPointer iptr = indices;
196 for(GLsizei i=3;i<count;i+=4,iptr+=4)
197 {
198 this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
199 _vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)],
200 false);
201 }
202 break;
203 }
204 case(GL_QUAD_STRIP): {
205 IndexPointer iptr = indices;
206 for(GLsizei i=3;i<count;i+=2,iptr+=2)
207 {
208 this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
209 _vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)],
210 false);
211 }
212 break;
213 }
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];
218 ++iptr;
219 for(GLsizei i=2;i<count;++i,++iptr)
220 {
221 this->operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
222 false);
223 }
224 break;
225 }
226 case(GL_POINTS): {
227 IndexPointer ilast = &indices[count];
228 for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
229 this->operator()(_vertexArrayPtr[*iptr],false);
230 break;
231 }
232 case(GL_LINES): {
233 IndexPointer ilast = &indices[count-1];
234 for(IndexPointer iptr=indices;iptr<ilast;iptr+=2)
235 this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
236 false);
237 break;
238 }
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)],
243 false);
244 break;
245 }
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)],
250 false);
251 break;
252 }
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)],
257 false);
258 this->operator()(_vertexArrayPtr[*(ilast)],_vertexArrayPtr[indices[0]],
259 false);
260 break;
261 }
262 default:
263 break;
264 }
265 }
266
267
268 virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices)
269 {
270 drawElementsTemplate(mode, count, indices);
271 }
272
273 virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices)
274 {
275 drawElementsTemplate(mode, count, indices);
276 }
277
278 virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices)
279 {
280 drawElementsTemplate(mode, count, indices);
281 }
282
283protected:
284
285 unsigned int _vertexArraySize;
286 const Vec3* _vertexArrayPtr;
287};
288
289
290}
291
292#endif