OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TemplatePrimitiveFunctor.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_TERMPLATEPRIMITIVEFUNCTOR
15 #define OSG_TERMPLATEPRIMITIVEFUNCTOR 1
16 
17 #include <osg/PrimitiveSet>
18 #include <osg/Notify>
19 
20 namespace osg {
21 
22 
40  template<class T>
41  class TemplatePrimitiveFunctor : public PrimitiveFunctor, public T
42  {
43  public:
44 
46  {
49  _modeCache=0;
51  }
52 
54 
55  void setTreatVertexDataAsTemporary(bool treatVertexDataAsTemporary) { _treatVertexDataAsTemporary=treatVertexDataAsTemporary; }
57 
58  virtual void setVertexArray(unsigned int,const Vec2*)
59  {
60  notify(WARN)<<"Triangle Functor does not support Vec2* vertex arrays"<<std::endl;
61  }
62 
63  virtual void setVertexArray(unsigned int count,const Vec3* vertices)
64  {
65  _vertexArraySize = count;
66  _vertexArrayPtr = vertices;
67  }
68 
69  virtual void setVertexArray(unsigned int,const Vec4* )
70  {
71  notify(WARN)<<"Triangle Functor does not support Vec4* vertex arrays"<<std::endl;
72  }
73 
74  virtual void setVertexArray(unsigned int,const Vec2d*)
75  {
76  notify(WARN)<<"Triangle Functor does not support Vec2d* vertex arrays"<<std::endl;
77  }
78 
79  virtual void setVertexArray(unsigned int,const Vec3d*)
80  {
81  notify(WARN)<<"Triangle Functor does not support Vec3d* vertex arrays"<<std::endl;
82  }
83 
84  virtual void setVertexArray(unsigned int,const Vec4d* )
85  {
86  notify(WARN)<<"Triangle Functor does not support Vec4d* vertex arrays"<<std::endl;
87  }
88 
89 
90  virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
91  {
92  if (_vertexArrayPtr==0 || count==0) return;
93 
94  switch(mode)
95  {
96  case(GL_TRIANGLES): {
97  const Vec3* vlast = &_vertexArrayPtr[first+count];
98  for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=3)
99  this->operator()(*(vptr),*(vptr+1),*(vptr+2),_treatVertexDataAsTemporary);
100  break;
101  }
102  case(GL_TRIANGLE_STRIP): {
103  const Vec3* vptr = &_vertexArrayPtr[first];
104  for(GLsizei i=2;i<count;++i,++vptr)
105  {
106  if ((i%2)) this->operator()(*(vptr),*(vptr+2),*(vptr+1),_treatVertexDataAsTemporary);
107  else this->operator()(*(vptr),*(vptr+1),*(vptr+2),_treatVertexDataAsTemporary);
108  }
109  break;
110  }
111  case(GL_QUADS): {
112  const Vec3* vptr = &_vertexArrayPtr[first];
113  for(GLsizei i=3;i<count;i+=4,vptr+=4)
114  {
115  this->operator()(*(vptr),*(vptr+1),*(vptr+2),*(vptr+3),_treatVertexDataAsTemporary);
116  }
117  break;
118  }
119  case(GL_QUAD_STRIP): {
120  const Vec3* vptr = &_vertexArrayPtr[first];
121  for(GLsizei i=3;i<count;i+=2,vptr+=2)
122  {
123  this->operator()(*(vptr),*(vptr+1),*(vptr+3),*(vptr+2),_treatVertexDataAsTemporary);
124  }
125  break;
126  }
127  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
128  case(GL_TRIANGLE_FAN): {
129  const Vec3* vfirst = &_vertexArrayPtr[first];
130  const Vec3* vptr = vfirst+1;
131  for(GLsizei i=2;i<count;++i,++vptr)
132  {
133  this->operator()(*(vfirst),*(vptr),*(vptr+1),_treatVertexDataAsTemporary);
134  }
135  break;
136  }
137  case(GL_POINTS): {
138  const Vec3* vlast = &_vertexArrayPtr[first+count];
139  for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
140  this->operator()(*(vptr),_treatVertexDataAsTemporary);
141  break;
142  }
143  case(GL_LINES): {
144  const Vec3* vlast = &_vertexArrayPtr[first+count-1];
145  for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=2)
146  this->operator()(*(vptr),*(vptr+1),_treatVertexDataAsTemporary);
147  break;
148  }
149  case(GL_LINE_STRIP): {
150  const Vec3* vlast = &_vertexArrayPtr[first+count-1];
151  for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
152  this->operator()(*(vptr),*(vptr+1),_treatVertexDataAsTemporary);
153  break;
154  }
155  case(GL_LINE_LOOP): {
156  const Vec3* vlast = &_vertexArrayPtr[first+count-1];
157  for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
158  this->operator()(*(vptr),*(vptr+1),_treatVertexDataAsTemporary);
159  this->operator()(*(vlast),_vertexArrayPtr[first],_treatVertexDataAsTemporary);
160  break;
161  }
162  default:
163  break;
164  }
165  }
166 
167  template<class IndexType>
168  void drawElementsTemplate(GLenum mode,GLsizei count,const IndexType* indices)
169  {
170  if (indices==0 || count==0) return;
171 
172  typedef const IndexType* IndexPointer;
173 
174  switch(mode)
175  {
176  case(GL_TRIANGLES): {
177  IndexPointer ilast = &indices[count];
178  for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
179  this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)],_treatVertexDataAsTemporary);
180  break;
181  }
182  case(GL_TRIANGLE_STRIP): {
183  IndexPointer iptr = indices;
184  for(GLsizei i=2;i<count;++i,++iptr)
185  {
186  if ((i%2)) this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],
188  else this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
190  }
191  break;
192  }
193  case(GL_QUADS): {
194  IndexPointer iptr = indices;
195  for(GLsizei i=3;i<count;i+=4,iptr+=4)
196  {
197  this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
198  _vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)],
200  }
201  break;
202  }
203  case(GL_QUAD_STRIP): {
204  IndexPointer iptr = indices;
205  for(GLsizei i=3;i<count;i+=2,iptr+=2)
206  {
207  this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
208  _vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)],
210  }
211  break;
212  }
213  case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
214  case(GL_TRIANGLE_FAN): {
215  IndexPointer iptr = indices;
216  const Vec3& vfirst = _vertexArrayPtr[*iptr];
217  ++iptr;
218  for(GLsizei i=2;i<count;++i,++iptr)
219  {
220  this->operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
222  }
223  break;
224  }
225  case(GL_POINTS): {
226  IndexPointer ilast = &indices[count];
227  for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
228  this->operator()(_vertexArrayPtr[*iptr],_treatVertexDataAsTemporary);
229  break;
230  }
231  case(GL_LINES): {
232  IndexPointer ilast = &indices[count-1];
233  for(IndexPointer iptr=indices;iptr<ilast;iptr+=2)
234  this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
236  break;
237  }
238  case(GL_LINE_STRIP): {
239  IndexPointer ilast = &indices[count-1];
240  for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
241  this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
243  break;
244  }
245  case(GL_LINE_LOOP): {
246  IndexPointer ilast = &indices[count-1];
247  for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
248  this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
250  this->operator()(_vertexArrayPtr[*(ilast)],_vertexArrayPtr[indices[0]],
252  break;
253  }
254  default:
255  break;
256  }
257  }
258 
259 
260  virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices)
261  {
262  drawElementsTemplate(mode, count, indices);
263  }
264 
265  virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices)
266  {
267  drawElementsTemplate(mode, count, indices);
268  }
269 
270  virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices)
271  {
272  drawElementsTemplate(mode, count, indices);
273  }
274 
281  virtual void begin(GLenum mode)
282  {
283  _modeCache = mode;
284  _vertexCache.clear();
285  }
286 
287  virtual void vertex(const Vec2& vert) { _vertexCache.push_back(osg::Vec3(vert[0],vert[1],0.0f)); }
288  virtual void vertex(const Vec3& vert) { _vertexCache.push_back(vert); }
289  virtual void vertex(const Vec4& vert) { _vertexCache.push_back(osg::Vec3(vert[0],vert[1],vert[2])/vert[3]); }
290  virtual void vertex(float x,float y) { _vertexCache.push_back(osg::Vec3(x,y,0.0f)); }
291  virtual void vertex(float x,float y,float z) { _vertexCache.push_back(osg::Vec3(x,y,z)); }
292  virtual void vertex(float x,float y,float z,float w) { _vertexCache.push_back(osg::Vec3(x,y,z)/w); }
293  virtual void end()
294  {
295  if (!_vertexCache.empty())
296  {
297  setVertexArray(_vertexCache.size(),&_vertexCache.front());
300  }
301  }
302 
303  protected:
304 
305 
306  unsigned int _vertexArraySize;
308 
309  GLenum _modeCache;
310  };
311 
312 
313 }
314 
315 #endif
void drawElementsTemplate(GLenum mode, GLsizei count, const IndexType *indices)
std::vector< Vec3 > _vertexCache
Definition: PrimitiveSet.h:128
virtual void drawArrays(GLenum mode, GLint first, GLsizei count)
Mimics the OpenGL glDrawArrays() function.
virtual void end()
Mimics the OpenGL glEnd() function.
virtual void setVertexArray(unsigned int, const Vec4d *)
virtual void setVertexArray(unsigned int, const Vec4 *)
virtual void vertex(float x, float y)
Mimics the OpenGL glVertex() "family of functions".
virtual void setVertexArray(unsigned int, const Vec2d *)
virtual void setVertexArray(unsigned int count, const Vec3 *vertices)
virtual void drawElements(GLenum mode, GLsizei count, const GLuint *indices)
Mimics the OpenGL glDrawElements() function.
virtual void vertex(float x, float y, float z, float w)
Mimics the OpenGL glVertex() "family of functions".
void setTreatVertexDataAsTemporary(bool treatVertexDataAsTemporary)
virtual void drawElements(GLenum mode, GLsizei count, const GLubyte *indices)
Mimics the OpenGL glDrawElements() function.
virtual void vertex(const Vec4 &vert)
Mimics the OpenGL glVertex() "family of functions".
virtual void drawElements(GLenum mode, GLsizei count, const GLushort *indices)
Mimics the OpenGL glDrawElements() function.
Definition: AlphaFunc.h:19
OSG_EXPORT std::ostream & notify(const NotifySeverity severity)
virtual void vertex(const Vec2 &vert)
Mimics the OpenGL glVertex() "family of functions".
virtual void vertex(float x, float y, float z)
Mimics the OpenGL glVertex() "family of functions".
virtual void setVertexArray(unsigned int, const Vec3d *)
virtual void vertex(const Vec3 &vert)
Mimics the OpenGL glVertex() "family of functions".
virtual void setVertexArray(unsigned int, const Vec2 *)