1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2011 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
30 #include "OSGFastTriangleIterator.h"
34 FastTriangleIterator::FastTriangleIterator(void) :
45 FastTriangleIterator::FastTriangleIterator(const FastTriangleIterator
&source
) :
47 _triIndex (source
._triIndex
),
48 _actPrimIndex(source
._actPrimIndex
),
51 _triPntIndex
[0] = source
._triPntIndex
[0];
52 _triPntIndex
[1] = source
._triPntIndex
[1];
53 _triPntIndex
[2] = source
._triPntIndex
[2];
56 FastTriangleIterator::FastTriangleIterator(const Geometry
*geo
) :
69 FastTriangleIterator::FastTriangleIterator(const Node
*node
) :
83 FastTriangleIterator::~FastTriangleIterator(void)
88 FastTriangleIterator::setToBegin(void)
90 Inherited::setToBegin();
97 FastTriangleIterator::setToEnd(void)
99 Inherited::setToEnd();
103 FastTriangleIterator
&
104 FastTriangleIterator::operator++(void)
111 if(_actPrimIndex
>= getLength())
113 ++(static_cast<Inherited
&>(*this));
122 _triPntIndex
[0] = _actPrimIndex
++;
123 _triPntIndex
[1] = _actPrimIndex
++;
124 _triPntIndex
[2] = _actPrimIndex
++;
128 case GL_TRIANGLE_STRIP
:
130 if(_actPrimIndex
& 1)
132 _triPntIndex
[0] = _triPntIndex
[2];
136 _triPntIndex
[1] = _triPntIndex
[2];
138 _triPntIndex
[2] = _actPrimIndex
++;
143 case GL_TRIANGLE_FAN
:
144 _triPntIndex
[1] = _triPntIndex
[2];
145 _triPntIndex
[2] = _actPrimIndex
++;
149 if(_actPrimIndex
& 1)
151 _triPntIndex
[1] = _triPntIndex
[2];
152 _triPntIndex
[2] = _actPrimIndex
++;
156 _triPntIndex
[0] = _actPrimIndex
++;
157 _triPntIndex
[1] = _actPrimIndex
++;
158 _triPntIndex
[2] = _actPrimIndex
++;
164 SWARNING
<< "TriangleIterator::++: encountered "
165 << "unknown primitive type "
166 << getType() << ", ignoring!" << std::endl
;
175 FastTriangleIterator
&
176 FastTriangleIterator::seek(Int32 index
)
178 if(index
< getIndex())
181 if(index
> getIndex())
183 UInt32 tri
= getTriInActPrim ();
184 UInt32 numTris
= getTrisInActPrim();
188 ++(static_cast<Inherited
&>(*this));
191 tri
= getTriInActPrim ();
192 numTris
= getTrisInActPrim();
195 Int32 leftTris
= numTris
- tri
- 1;
197 // skip whole primitives
198 while(index
> _triIndex
+ leftTris
)
200 _triIndex
+= leftTris
+ 1;
201 ++(static_cast<Inherited
&>(*this));
205 leftTris
= getTrisInActPrim() - 1;
208 tri
+= index
- getIndex();
212 if(!isAtEnd() && tri
> 0)
217 _actPrimIndex
= 3 * tri
+ 3;
218 _triPntIndex
[0] = _actPrimIndex
- 3;
219 _triPntIndex
[1] = _actPrimIndex
- 2;
220 _triPntIndex
[2] = _actPrimIndex
- 1;
226 _actPrimIndex
= 2 * tri
+ 2;
227 _triPntIndex
[0] = _actPrimIndex
- 4;
228 _triPntIndex
[1] = _actPrimIndex
- 2;
229 _triPntIndex
[2] = _actPrimIndex
- 1;
233 _actPrimIndex
= 2 * tri
+ 3;
234 _triPntIndex
[0] = _actPrimIndex
- 3;
235 _triPntIndex
[1] = _actPrimIndex
- 2;
236 _triPntIndex
[2] = _actPrimIndex
- 1;
241 case GL_TRIANGLE_STRIP
:
244 _actPrimIndex
= tri
+ 3;
245 _triPntIndex
[0] = _actPrimIndex
- 2;
246 _triPntIndex
[1] = _actPrimIndex
- 3;
247 _triPntIndex
[2] = _actPrimIndex
- 1;
251 _actPrimIndex
= tri
+ 3;
252 _triPntIndex
[0] = _actPrimIndex
- 3;
253 _triPntIndex
[1] = _actPrimIndex
- 2;
254 _triPntIndex
[2] = _actPrimIndex
- 1;
258 case GL_TRIANGLE_FAN
:
260 _actPrimIndex
= tri
+ 3;
262 _triPntIndex
[1] = _actPrimIndex
- 2;
263 _triPntIndex
[2] = _actPrimIndex
- 1;
268 SWARNING
<< "FastTriangleIterator::seek: "
269 << "encountered unknown primitive type "
270 << getType() << ", ignoring!"
282 FastTriangleIterator::startPrim(void)
296 case GL_POINTS
: // non-polygon types: ignored
302 case GL_TRIANGLES
: // polygon types
303 case GL_TRIANGLE_STRIP
:
304 case GL_TRIANGLE_FAN
:
314 SWARNING
<< "FastTriangleIterator::startPrim: "
315 << "encountered unknown primitive type "
316 << getType() << ", ignoring!"
322 ++(static_cast<Inherited
&>(*this));
327 FastTriangleIterator::getTrisInActPrim(void) const
329 UInt32 returnValue
= 0;
334 assert(getLength() % 3 == 0);
335 returnValue
= getLength() / 3;
339 assert(getLength() % 4 == 0);
340 returnValue
= getLength() / 2;
344 case GL_TRIANGLE_STRIP
:
345 case GL_TRIANGLE_FAN
:
347 assert(getLength() >= 3);
348 returnValue
= getLength() - 2;
353 SWARNING
<< "FastTriangleIterator::getTrisInActPrim: "
354 << "encountered unknown primitive type "
355 << getType() << ", ignoring!"
365 FastTriangleIterator::getTriInActPrim(void) const
367 UInt32 returnValue
= 0;
372 assert(_actPrimIndex
% 3 == 0);
373 returnValue
= _actPrimIndex
/ 3 - 1;
377 if(_actPrimIndex
% 1)
379 returnValue
= (_actPrimIndex
- 3) / 2;
383 returnValue
= (_actPrimIndex
- 2) / 2;
388 case GL_TRIANGLE_STRIP
:
389 case GL_TRIANGLE_FAN
:
391 returnValue
= _actPrimIndex
- 3;
396 SWARNING
<< "FastTriangleIterator::getTriInActPrim: "
397 << "encountered unknown primitive type "
398 << getType() << ", ignoring!"