OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tessellator.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 OSGUTIL_Tessellator
15 #define OSGUTIL_Tessellator
16 
17 #include <osg/Geometry>
18 
19 #include <osgUtil/Export>
20 
21 #include <osg/GLU>
22 
23 #include <vector>
24 
25 #ifndef CALLBACK
26  /* Win32 calling conventions. (or a least thats what the GLUT example tess.c uses.)*/
27  #define CALLBACK
28 #endif
29 
30 namespace osgUtil {
31 
37 {
38  public:
39 
40  Tessellator();
41  ~Tessellator();
42 
45  TESS_WINDING_ODD = GLU_TESS_WINDING_ODD,
46  TESS_WINDING_NONZERO = GLU_TESS_WINDING_NONZERO,
47  TESS_WINDING_POSITIVE = GLU_TESS_WINDING_POSITIVE,
48  TESS_WINDING_NEGATIVE = GLU_TESS_WINDING_NEGATIVE,
49  TESS_WINDING_ABS_GEQ_TWO = GLU_TESS_WINDING_ABS_GEQ_TWO
50  } ;
51 
55  TESS_TYPE_GEOMETRY, // tessellate everything in the geometry object
56  TESS_TYPE_DRAWABLE, // tessellate each polygon, triangles & quads drawables in geometry separately
57  TESS_TYPE_POLYGONS // tessellate ONLY polygon drawables in geometry separately
58  };
59 
61  void setBoundaryOnly (const bool tt) { _boundaryOnly=tt;}
62  inline bool getBoundaryOnly ( ) { return _boundaryOnly;}
63 
65  void setWindingType (const WindingType wt) { _wtype=wt;}
66  inline WindingType getWindingType ( ) { return _wtype;}
67 
69  void setTessellationType (const TessellationType tt) { _ttype=tt;}
70  inline TessellationType getTessellationType ( ) { return _ttype;}
71 
77  void retessellatePolygons(osg::Geometry &cxgeom);
78 
85  void setTessellationNormal(const osg::Vec3 norm) { tessNormal=norm;}
86 
88 
89  struct Prim : public osg::Referenced
90  {
91  Prim(GLenum mode):_mode(mode) {}
92 
93  typedef std::vector<osg::Vec3*> VecList;
94 
95  GLenum _mode;
96  VecList _vertices;
97  };
98 
99  virtual void beginTessellation();
100 
101  void beginContour();
102 
108  void addVertex(osg::Vec3* vertex);
109 
110  void endContour();
111 
112  void endTessellation();
113 
114  typedef std::vector< osg::ref_ptr<Prim> > PrimList;
115 
116  PrimList& getPrimList() { return _primList; }
117 
118  void reset();
119 
120  protected:
121 
125  void reduceArray(osg::Array * cold, const unsigned int nnu);
126 
127  void collectTessellation(osg::Geometry &cxgeom, unsigned int originalIndex);
128 
129  typedef std::map<osg::Vec3*,unsigned int> VertexPtrToIndexMap;
130  void addContour(GLenum mode, unsigned int first, unsigned int last, osg::Vec3Array* vertices);
131  void addContour(osg::PrimitiveSet* primitive, osg::Vec3Array* vertices);
132  void handleNewVertices(osg::Geometry& geom,VertexPtrToIndexMap &vertexPtrToIndexMap);
133 
134  void begin(GLenum mode);
135  void vertex(osg::Vec3* vertex);
136  void combine(osg::Vec3* vertex,void* vertex_data[4],GLfloat weight[4]);
137  void end();
138  void error(GLenum errorCode);
139 
140 
141  static void CALLBACK beginCallback(GLenum which, void* userData);
142  static void CALLBACK vertexCallback(GLvoid *data, void* userData);
143  static void CALLBACK combineCallback(GLdouble coords[3], void* vertex_data[4],
144  GLfloat weight[4], void** outData,
145  void* useData);
146  static void CALLBACK endCallback(void* userData);
147  static void CALLBACK errorCallback(GLenum errorCode, void* userData);
148 
149 
150  struct Vec3d
151  {
152  double _v[3];
153  };
154 
155 
156  struct NewVertex
157  {
158 
160  _vpos(0),
161  _f1(0),
162  _v1(0),
163  _f2(0),
164  _v2(0),
165  _f3(0),
166  _v3(0),
167  _f4(0),
168  _v4(0) {}
169 
170  NewVertex(const NewVertex& nv):
171  _vpos(nv._vpos),
172  _f1(nv._f1),
173  _v1(nv._v1),
174  _f2(nv._f2),
175  _v2(nv._v2),
176  _f3(nv._f3),
177  _v3(nv._v3),
178  _f4(nv._f4),
179  _v4(nv._v4) {}
180 
182  float f1,osg::Vec3* v1,
183  float f2,osg::Vec3* v2,
184  float f3,osg::Vec3* v3,
185  float f4,osg::Vec3* v4):
186  _vpos(vx),
187  _f1(f1),
188  _v1(v1),
189  _f2(f2),
190  _v2(v2),
191  _f3(f3),
192  _v3(v3),
193  _f4(f4),
194  _v4(v4) {}
195 
196  osg::Vec3 *_vpos; // added gwm Jan 2004 the vertex coords s.t. NewVertex can be used in a std::vector
197 
198  float _f1;
200 
201  float _f2;
203 
204  float _f3;
206 
207  float _f4;
209 
210  };
211 
212  //change NewVertexList from std::map<osg::Vec3*,NewVertex> NewVertexList;
213  // because this has undefined order of insertion for new vertices.
214  // which occasionally corrupted the texture mapping.
215  typedef std::vector<NewVertex> NewVertexList;
216  typedef std::vector<Vec3d*> Vec3dList;
217 
218  osg::GLUtesselator* _tobj;
219 
220  PrimList _primList;
221  Vec3dList _coordData;
222  NewVertexList _newVertexList;
223  GLenum _errorCode;
224 
227 
230 
231  bool _boundaryOnly; // see gluTessProperty - if true: make the boundary edges only.
232 
234  unsigned int _numberVerts;
235 
238 
240  unsigned int _index;
241 
244 
246  unsigned int _extraPrimitives;
247 };
248 
249 }
250 
251 #endif
#define CALLBACK
Definition: Tessellator.h:27
std::vector< ref_ptr< PrimitiveSet > > PrimitiveSetList
Definition: Geometry.h:102
std::map< osg::Vec3 *, unsigned int > VertexPtrToIndexMap
Definition: Tessellator.h:129
#define GLU_TESS_WINDING_NONZERO
Definition: GLU.h:156
unsigned int _numberVerts
Definition: Tessellator.h:234
#define GLU_TESS_WINDING_ODD
Definition: GLU.h:155
std::vector< osg::ref_ptr< Prim > > PrimList
Definition: Tessellator.h:114
PrimList & getPrimList()
Definition: Tessellator.h:116
#define GLU_TESS_WINDING_POSITIVE
Definition: GLU.h:157
TessellationType _ttype
Definition: Tessellator.h:229
void setTessellationType(const TessellationType tt)
Definition: Tessellator.h:69
osg::GLUtesselator * _tobj
Definition: Tessellator.h:218
osg::Geometry::PrimitiveSetList getContours()
Definition: Tessellator.h:87
#define GLU_TESS_WINDING_ABS_GEQ_TWO
Definition: GLU.h:159
void setWindingType(const WindingType wt)
Definition: Tessellator.h:65
WindingType _wtype
Definition: Tessellator.h:226
double GLdouble
Definition: GL.h:159
void setBoundaryOnly(const bool tt)
Definition: Tessellator.h:61
std::vector< osg::Vec3 * > VecList
Definition: Tessellator.h:93
std::vector< NewVertex > NewVertexList
Definition: Tessellator.h:215
std::vector< Vec3d * > Vec3dList
Definition: Tessellator.h:216
NewVertex(osg::Vec3 *vx, float f1, osg::Vec3 *v1, float f2, osg::Vec3 *v2, float f3, osg::Vec3 *v3, float f4, osg::Vec3 *v4)
Definition: Tessellator.h:181
osg::Geometry::PrimitiveSetList _Contours
Definition: Tessellator.h:237
WindingType getWindingType()
Definition: Tessellator.h:66
#define OSGUTIL_EXPORT
Definition: Export.h:40
unsigned int _index
Definition: Tessellator.h:240
#define GLU_TESS_WINDING_NEGATIVE
Definition: GLU.h:158
unsigned int _extraPrimitives
Definition: Tessellator.h:246
void setTessellationNormal(const osg::Vec3 norm)
Definition: Tessellator.h:85
TessellationType getTessellationType()
Definition: Tessellator.h:70
NewVertexList _newVertexList
Definition: Tessellator.h:222
NewVertex(const NewVertex &nv)
Definition: Tessellator.h:170
Shader generator framework.
Definition: RenderInfo.h:20