OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TriangleLinePointIndexFunctor.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) Sketchfab
2  *
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.
6  *
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.
10  *
11  */
12 
13 #ifndef TRIANGLE_LINE_POINT_INDEX_FUNCTOR
14 #define TRIANGLE_LINE_POINT_INDEX_FUNCTOR
15 
16 #include <osg/PrimitiveSet>
17 #include <osg/Array>
18 
19 
20 template<class T>
22 {
23 public:
24  virtual void setVertexArray(unsigned int,const osg::Vec2*)
25  {}
26 
27  virtual void setVertexArray(unsigned int ,const osg::Vec3* )
28  {}
29 
30  virtual void setVertexArray(unsigned int,const osg::Vec4* )
31  {}
32 
33  virtual void setVertexArray(unsigned int,const osg::Vec2d*)
34  {}
35 
36  virtual void setVertexArray(unsigned int ,const osg::Vec3d* )
37  {}
38 
39  virtual void setVertexArray(unsigned int,const osg::Vec4d* )
40  {}
41 
42  virtual void begin(GLenum mode) {
43  _modeCache = mode;
44  _indexCache.clear();
45  }
46 
47  virtual void vertex(unsigned int vert) {
48  _indexCache.push_back(vert);
49  }
50 
51  virtual void end() {
52  if (!_indexCache.empty()) {
54  }
55  }
56 
57  virtual void drawArrays(GLenum mode, GLint first, GLsizei count) {
58  switch(mode)
59  {
60  case(GL_TRIANGLES):
61  {
62  unsigned int pos=first;
63  for(GLsizei i = 2 ; i < count ; i += 3, pos += 3) {
64  this->operator()(pos, pos + 1, pos + 2);
65  }
66  break;
67  }
68  case(GL_TRIANGLE_STRIP):
69  {
70  unsigned int pos = first;
71  for(GLsizei i = 2 ; i < count ; ++ i, ++ pos) {
72  if ((i%2)) this->operator()(pos, pos + 2, pos + 1);
73  else this->operator()(pos, pos + 1, pos + 2);
74  }
75  break;
76  }
77  case(GL_QUADS):
78  {
79  unsigned int pos = first;
80  for(GLsizei i = 3 ; i < count ; i += 4, pos += 4) {
81  this->operator()(pos,pos + 1, pos + 2);
82  this->operator()(pos,pos + 2, pos + 3);
83  }
84  break;
85  }
86  case(GL_QUAD_STRIP):
87  {
88  unsigned int pos = first;
89  for(GLsizei i = 3 ; i < count ; i += 2, pos += 2) {
90  this->operator()(pos, pos + 1,pos + 2);
91  this->operator()(pos + 1,pos + 3,pos + 2);
92  }
93  break;
94  }
95  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
96  case(GL_TRIANGLE_FAN):
97  {
98  unsigned int pos = first + 1;
99  for(GLsizei i = 2 ; i < count ; ++ i, ++ pos) {
100  this->operator()(first, pos, pos + 1);
101  }
102  break;
103  }
104  case(GL_LINES):
105  {
106  unsigned int pos = first;
107  for(GLsizei i = 0 ; i < count ; i += 2, pos += 2) {
108  this->operator()(pos, pos + 1);
109  }
110  break;
111  }
112  case(GL_LINE_STRIP):
113  {
114  unsigned int pos = first;
115  for(GLsizei i = 0 ; i < count - 1 ; i += 1, pos += 1) {
116  this->operator()(pos, pos + 1);
117  }
118  break;
119  }
120  case(GL_LINE_LOOP):
121  {
122  unsigned int pos = first;
123  for(GLsizei i = 0 ; i < count - 1 ; i += 1, pos += 1) {
124  this->operator()(pos, pos + 1);
125  }
126  this->operator()(pos, first);
127  break;
128  }
129  case(GL_POINTS):
130  {
131  unsigned int pos=first;
132  for(GLsizei i = 0 ; i < count ; ++ i) {
133  this->operator()(pos + i);
134  }
135  break;
136  }
137  default:
138  break;
139  }
140  }
141 
142  template<typename I>
143  void drawElements(GLenum mode, GLsizei count, const I* indices)
144  {
145  typedef I Index;
146  typedef const I* IndexPointer;
147 
148  if (indices == 0 || count == 0) {
149  return;
150  }
151 
152  switch(mode)
153  {
154  case(GL_TRIANGLES):
155  {
156  IndexPointer ilast = &indices[count];
157  for(IndexPointer iptr = indices ; iptr < ilast ; iptr += 3) {
158  this->operator()(*iptr, *(iptr + 1), *(iptr + 2));
159  }
160  break;
161  }
162  case(GL_TRIANGLE_STRIP):
163  {
164  IndexPointer iptr = indices;
165  for(GLsizei i = 2 ; i < count ; ++ i, ++ iptr) {
166  if ((i%2)) this->operator()(*(iptr), *(iptr + 2), *(iptr + 1));
167  else this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
168  }
169  break;
170  }
171  case(GL_QUADS):
172  {
173  IndexPointer iptr = indices;
174  for(GLsizei i = 3 ; i < count ; i += 4, iptr += 4) {
175  this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
176  this->operator()(*(iptr), *(iptr + 2), *(iptr + 3));
177  }
178  break;
179  }
180  case(GL_QUAD_STRIP):
181  {
182  IndexPointer iptr = indices;
183  for(GLsizei i = 3 ; i < count ; i += 2, iptr += 2) {
184  this->operator()(*(iptr), *(iptr + 1), *(iptr + 2));
185  this->operator()(*(iptr + 1), *(iptr + 3), *(iptr + 2));
186  }
187  break;
188  }
189  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
190  case(GL_TRIANGLE_FAN):
191  {
192  IndexPointer iptr = indices;
193  Index first = *iptr;
194  ++iptr;
195  for(GLsizei i = 2 ; i < count ; ++ i, ++ iptr) {
196  this->operator()(first, *(iptr), *(iptr + 1));
197  }
198  break;
199  }
200  case(GL_LINES):
201  {
202  const I* iptr = indices;
203  for(GLsizei i = 0 ; i < count ; i += 2, iptr += 2) {
204  this->operator()(*iptr, *(iptr + 1));
205  }
206  break;
207  }
208  case(GL_LINE_STRIP):
209  {
210  const I* iptr = indices;
211  for(GLsizei i = 0 ; i < count - 1 ; i += 1, iptr += 1) {
212  this->operator()(*iptr, *(iptr + 1));
213  }
214  break;
215  }
216  case(GL_LINE_LOOP):
217  {
218  const I* iptr = indices;
219  I first = *iptr;
220  for(GLsizei i = 0 ; i < count - 1 ; i += 1, iptr += 1) {
221  this->operator()(*iptr, *(iptr + 1));
222  }
223  this->operator()(*iptr, first);
224  break;
225  }
226  case GL_POINTS:
227  {
228  IndexPointer ilast = &indices[count];
229  for(IndexPointer iptr = indices ; iptr < ilast ; iptr += 1) {
230  this->operator()(*iptr);
231  }
232  break;
233  }
234  default:
235  break;
236  }
237  }
238 
239  virtual void drawElements(GLenum mode, GLsizei count, const GLubyte* indices) {
240  drawElements<GLubyte>(mode, count, indices);
241  }
242 
243  virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) {
244  drawElements<GLushort>(mode, count, indices);
245  }
246 
247  virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) {
248  drawElements<GLuint>(mode, count, indices);
249  }
250 
251 
252  GLenum _modeCache;
253  std::vector<GLuint> _indexCache;
254  std::vector<unsigned int> _remap;
255 };
256 
257 #endif
virtual void setVertexArray(unsigned int, const osg::Vec2d *)
virtual void setVertexArray(unsigned int, const osg::Vec3 *)
virtual void vertex(unsigned int vert)
virtual void drawElements(GLenum mode, GLsizei count, const GLushort *indices)
void drawElements(GLenum mode, GLsizei count, const I *indices)
virtual void setVertexArray(unsigned int, const osg::Vec2 *)
virtual void drawElements(GLenum mode, GLsizei count, const GLubyte *indices)
virtual void drawElements(GLenum mode, GLsizei count, const GLuint *indices)
virtual void setVertexArray(unsigned int, const osg::Vec4d *)
virtual void drawArrays(GLenum mode, GLint first, GLsizei count)
virtual void setVertexArray(unsigned int, const osg::Vec4 *)
virtual void setVertexArray(unsigned int, const osg::Vec3d *)