OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TriangleIndexFunctor.h
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_TRIANGLEINDEXFUNCTOR
15 #define OSG_TRIANGLEINDEXFUNCTOR 1
16 
17 #include <osg/PrimitiveSet>
18 #include <osg/Notify>
19 
20 namespace osg {
21 
22 template<class T>
24 {
25 public:
26 
27 
28  virtual void setVertexArray(unsigned int,const Vec2*)
29  {
30  }
31 
32  virtual void setVertexArray(unsigned int ,const Vec3* )
33  {
34  }
35 
36  virtual void setVertexArray(unsigned int,const Vec4* )
37  {
38  }
39 
40  virtual void setVertexArray(unsigned int,const Vec2d*)
41  {
42  }
43 
44  virtual void setVertexArray(unsigned int ,const Vec3d* )
45  {
46  }
47 
48  virtual void setVertexArray(unsigned int,const Vec4d* )
49  {
50  }
51 
52  virtual void begin(GLenum mode)
53  {
54  _modeCache = mode;
55  _indexCache.clear();
56  }
57 
58  virtual void vertex(unsigned int vert)
59  {
60  _indexCache.push_back(vert);
61  }
62 
63  virtual void end()
64  {
65  if (!_indexCache.empty())
66  {
68  }
69  }
70 
71  virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
72  {
73  switch(mode)
74  {
75  case(GL_TRIANGLES):
76  {
77  unsigned int pos=first;
78  for(GLsizei i=2;i<count;i+=3,pos+=3)
79  {
80  this->operator()(pos,pos+1,pos+2);
81  }
82  break;
83  }
84  case(GL_TRIANGLE_STRIP):
85  {
86  unsigned int pos=first;
87  for(GLsizei i=2;i<count;++i,++pos)
88  {
89  if ((i%2)) this->operator()(pos,pos+2,pos+1);
90  else this->operator()(pos,pos+1,pos+2);
91  }
92  break;
93  }
94  case(GL_QUADS):
95  {
96  unsigned int pos=first;
97  for(GLsizei i=3;i<count;i+=4,pos+=4)
98  {
99  this->operator()(pos,pos+1,pos+2);
100  this->operator()(pos,pos+2,pos+3);
101  }
102  break;
103  }
104  case(GL_QUAD_STRIP):
105  {
106  unsigned int pos=first;
107  for(GLsizei i=3;i<count;i+=2,pos+=2)
108  {
109  this->operator()(pos,pos+1,pos+2);
110  this->operator()(pos+1,pos+3,pos+2);
111  }
112  break;
113  }
114  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
115  case(GL_TRIANGLE_FAN):
116  {
117  unsigned int pos=first+1;
118  for(GLsizei i=2;i<count;++i,++pos)
119  {
120  this->operator()(first,pos,pos+1);
121  }
122  break;
123  }
124  case(GL_POINTS):
125  case(GL_LINES):
126  case(GL_LINE_STRIP):
127  case(GL_LINE_LOOP):
128  default:
129  // can't be converted into to triangles.
130  break;
131  }
132  }
133 
134  virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices)
135  {
136  if (indices==0 || count==0) return;
137 
138  typedef GLubyte Index;
139  typedef const Index* IndexPointer;
140 
141  switch(mode)
142  {
143  case(GL_TRIANGLES):
144  {
145  IndexPointer ilast = &indices[count];
146  for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
147  this->operator()(*iptr,*(iptr+1),*(iptr+2));
148  break;
149  }
150  case(GL_TRIANGLE_STRIP):
151  {
152  IndexPointer iptr = indices;
153  for(GLsizei i=2;i<count;++i,++iptr)
154  {
155  if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
156  else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
157  }
158  break;
159  }
160  case(GL_QUADS):
161  {
162  IndexPointer iptr = indices;
163  for(GLsizei i=3;i<count;i+=4,iptr+=4)
164  {
165  this->operator()(*(iptr),*(iptr+1),*(iptr+2));
166  this->operator()(*(iptr),*(iptr+2),*(iptr+3));
167  }
168  break;
169  }
170  case(GL_QUAD_STRIP):
171  {
172  IndexPointer iptr = indices;
173  for(GLsizei i=3;i<count;i+=2,iptr+=2)
174  {
175  this->operator()(*(iptr),*(iptr+1),*(iptr+2));
176  this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
177  }
178  break;
179  }
180  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
181  case(GL_TRIANGLE_FAN):
182  {
183  IndexPointer iptr = indices;
184  Index first = *iptr;
185  ++iptr;
186  for(GLsizei i=2;i<count;++i,++iptr)
187  {
188  this->operator()(first,*(iptr),*(iptr+1));
189  }
190  break;
191  }
192  case(GL_POINTS):
193  case(GL_LINES):
194  case(GL_LINE_STRIP):
195  case(GL_LINE_LOOP):
196  default:
197  // can't be converted into to triangles.
198  break;
199  }
200  }
201 
202  virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices)
203  {
204  if (indices==0 || count==0) return;
205 
206  typedef GLushort Index;
207  typedef const Index* IndexPointer;
208 
209  switch(mode)
210  {
211  case(GL_TRIANGLES):
212  {
213  IndexPointer ilast = &indices[count];
214  for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
215  this->operator()(*iptr,*(iptr+1),*(iptr+2));
216  break;
217  }
218  case(GL_TRIANGLE_STRIP):
219  {
220  IndexPointer iptr = indices;
221  for(GLsizei i=2;i<count;++i,++iptr)
222  {
223  if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
224  else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
225  }
226  break;
227  }
228  case(GL_QUADS):
229  {
230  IndexPointer iptr = indices;
231  for(GLsizei i=3;i<count;i+=4,iptr+=4)
232  {
233  this->operator()(*(iptr),*(iptr+1),*(iptr+2));
234  this->operator()(*(iptr),*(iptr+2),*(iptr+3));
235  }
236  break;
237  }
238  case(GL_QUAD_STRIP):
239  {
240  IndexPointer iptr = indices;
241  for(GLsizei i=3;i<count;i+=2,iptr+=2)
242  {
243  this->operator()(*(iptr),*(iptr+1),*(iptr+2));
244  this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
245  }
246  break;
247  }
248  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
249  case(GL_TRIANGLE_FAN):
250  {
251  IndexPointer iptr = indices;
252  Index first = *iptr;
253  ++iptr;
254  for(GLsizei i=2;i<count;++i,++iptr)
255  {
256  this->operator()(first,*(iptr),*(iptr+1));
257  }
258  break;
259  }
260  case(GL_POINTS):
261  case(GL_LINES):
262  case(GL_LINE_STRIP):
263  case(GL_LINE_LOOP):
264  default:
265  // can't be converted into to triangles.
266  break;
267  }
268  }
269 
270  virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices)
271  {
272  if (indices==0 || count==0) return;
273 
274  typedef GLuint Index;
275  typedef const Index* IndexPointer;
276 
277  switch(mode)
278  {
279  case(GL_TRIANGLES):
280  {
281  IndexPointer ilast = &indices[count];
282  for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
283  this->operator()(*iptr,*(iptr+1),*(iptr+2));
284  break;
285  }
286  case(GL_TRIANGLE_STRIP):
287  {
288  IndexPointer iptr = indices;
289  for(GLsizei i=2;i<count;++i,++iptr)
290  {
291  if ((i%2)) this->operator()(*(iptr),*(iptr+2),*(iptr+1));
292  else this->operator()(*(iptr),*(iptr+1),*(iptr+2));
293  }
294  break;
295  }
296  case(GL_QUADS):
297  {
298  IndexPointer iptr = indices;
299  for(GLsizei i=3;i<count;i+=4,iptr+=4)
300  {
301  this->operator()(*(iptr),*(iptr+1),*(iptr+2));
302  this->operator()(*(iptr),*(iptr+2),*(iptr+3));
303  }
304  break;
305  }
306  case(GL_QUAD_STRIP):
307  {
308  IndexPointer iptr = indices;
309  for(GLsizei i=3;i<count;i+=2,iptr+=2)
310  {
311  this->operator()(*(iptr),*(iptr+1),*(iptr+2));
312  this->operator()(*(iptr+1),*(iptr+3),*(iptr+2));
313  }
314  break;
315  }
316  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
317  case(GL_TRIANGLE_FAN):
318  {
319  IndexPointer iptr = indices;
320  Index first = *iptr;
321  ++iptr;
322  for(GLsizei i=2;i<count;++i,++iptr)
323  {
324  this->operator()(first,*(iptr),*(iptr+1));
325  }
326  break;
327  }
328  case(GL_POINTS):
329  case(GL_LINES):
330  case(GL_LINE_STRIP):
331  case(GL_LINE_LOOP):
332  default:
333  // can't be converted into to triangles.
334  break;
335  }
336  }
337 
338  GLenum _modeCache;
339  std::vector<GLuint> _indexCache;
340 };
341 
342 }
343 
344 #endif
virtual void setVertexArray(unsigned int, const Vec3d *)
virtual void setVertexArray(unsigned int, const Vec3 *)
std::vector< GLuint > _indexCache
virtual void setVertexArray(unsigned int, const Vec2 *)
virtual void setVertexArray(unsigned int, const Vec4 *)
virtual void drawElements(GLenum mode, GLsizei count, const GLushort *indices)
virtual void setVertexArray(unsigned int, const Vec4d *)
virtual void drawElements(GLenum mode, GLsizei count, const GLubyte *indices)
virtual void setVertexArray(unsigned int, const Vec2d *)
virtual void vertex(unsigned int vert)
virtual void drawArrays(GLenum mode, GLint first, GLsizei count)
Definition: AlphaFunc.h:19
virtual void drawElements(GLenum mode, GLsizei count, const GLuint *indices)
virtual void begin(GLenum mode)