Debian Codegraph @

vcg/wrap/gl/glu_tesselator.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25
26 $Log: glu_tesselator.h,v $
27 Revision 1.5 2008/02/15 08:19:51 cignoni
28 removed unused vars
29
30 Revision 1.4 2007/12/01 18:09:14 m_di_benedetto
31 Added cast to prevent pointer truncation warning/error messages.
32
33 Revision 1.3 2007/08/28 05:43:56 cignoni
34 Added ifdef needed for mac gcc compilation
35
36 Revision 1.2 2007/08/25 08:43:33 cignoni
37 moved here callback def and added some needed opengl related casts
38
39 Revision 1.1 2007/05/14 22:23:36 m_di_benedetto
40 First Commit.
41
42
43
44 ****************************************************************************/
45
46 #ifndef __VCGLIB_GLU_TESSELATOR_H
47 #define __VCGLIB_GLU_TESSELATOR_H
48
49 #include <vector>
50 //#include <GL/glu.h>
51
52 #ifndef CALLBACK
53 #ifdef WIN32
54 #define CALLBACK __stdcall
55 #else
56 #define CALLBACK
57 #endif
58 #endif
59
60 namespace vcg
61 {
62
63 class glu_tesselator
64 {
65 public:
66
67 typedef glu_tesselator this_type;
68
69 /*
70 Works with Point2 and Point3;
71
72 sample usage:
73
74 // tesselation input: each outline represents a polygon contour
75 std::vector< std::vector<point_type> > outlines = ...;
76
77 // tesselation output (triangles indices)
78 std::vector<int> indices;
79
80 // compute triangles indices
81 glu_tesselator::tesselate(outlines, indices);
82
83 // unroll input contours points
84 std::vector<point_type> points;
85
86 for (size_t i=0; i<outlines.size(); ++i)
87 {
88 for (size_t j=0; j<outlines[i].size(); ++j)
89 {
90 points.push_back(outlines[i][j]);
91 }
92 }
93 // or simply call glu_tesselator::unroll(outlines, points);
94
95 // create triangles
96 for (size_t i=0; i<indices.size(); i+=3)
97 {
98 create_triangle(
99 points[ indices[i+0] ],
100 points[ indices[i+1] ],
101 points[ indices[i+2] ]);
102 }
103 */
104
105 template <class point_type>
106 static inline void unroll(const std::vector< std::vector<point_type> > & outlines, std::vector<point_type> & points)
107 {
108 for (size_t i=0; i<outlines.size(); ++i)
109 {
110 for (size_t j=0; j<outlines[i].size(); ++j)
111 {
112 points.push_back(outlines[i][j]);
113 }
114 }
115 }
116
117 template <class point_type>
118 static inline void tesselate(const std::vector< std::vector<point_type> > & outlines, std::vector<int> & indices)
119 {
120 tess_prim_data_vec t_data;
121
122 this_type::do_tesselation(outlines, t_data);
123
124 //int k = 0;
125 for (size_t i=0; i<t_data.size(); ++i)
126 {
127 const size_t st = t_data[i].indices.size();
128 if (st < 3) continue;
129
130 switch (t_data[i].type)
131 {
132 case GL_TRIANGLES:
133 for (size_t j=0; j<st; ++j)
134 {
135 indices.push_back(t_data[i].indices[j]);
136 }
137 break;
138
139 case GL_TRIANGLE_STRIP:
140 {
141 int i0 = t_data[i].indices[0];
142 int i1 = t_data[i].indices[1];
143
144 bool ccw = true;
145
146 for (size_t j=2; j<st; ++j)
147 {
148 const int i2 = t_data[i].indices[j];
149
150 indices.push_back(i0);
151 indices.push_back(i1);
152 indices.push_back(i2);
153
154 if (ccw) i0 = i2;
155 else i1 = i2;
156
157 ccw = !ccw;
158 } }
159 break;
160
161 case GL_TRIANGLE_FAN:
162 {
163 const int first = t_data[i].indices[0];
164 int prev = t_data[i].indices[1];
165
166 for (size_t j=2; j<st; ++j)
167 {
168 const int curr = t_data[i].indices[j];
169
170 indices.push_back(first);
171 indices.push_back(prev);
172 indices.push_back(curr);
173
174 prev = curr;
175 }
176 }
177 break;
178
179 default:
180 break;
181 }
182 }
183 }
184
185 protected:
186
187 class tess_prim_data
188 {
189 public:
190
191 typedef tess_prim_data this_type;
192
193 GLenum type;
194 std::vector<int> indices;
195
196 tess_prim_data(void) { }
197 tess_prim_data(GLenum t) : type(t) { }
198 };
199
200 typedef std::vector<tess_prim_data> tess_prim_data_vec;
201
202 static void CALLBACK begin_cb(GLenum type, void * polygon_data)
203 {
204 tess_prim_data_vec * t_data = (tess_prim_data_vec *)polygon_data;
205 t_data->push_back(tess_prim_data(type));
206 }
207
208 static void CALLBACK end_cb(void * polygon_data)
209 {
210 (void)polygon_data;
211 }
212
213 static void CALLBACK vertex_cb(void * vertex_data, void * polygon_data)
214 {
215 tess_prim_data_vec * t_data = (tess_prim_data_vec *)polygon_data;
216 t_data->back().indices.push_back((int)((size_t)vertex_data));
217 }
218
219 template <class point_type>
220 static void do_tesselation(const std::vector< std::vector<point_type> > & outlines, tess_prim_data_vec & t_data)
221 {
222 GLUtesselator * tess = gluNewTess();
223 #ifdef __APPLE__
224 gluTessCallback(tess, GLU_TESS_BEGIN_DATA, (GLvoid (CALLBACK *)(...))(this_type::begin_cb));
225 gluTessCallback(tess, GLU_TESS_END_DATA, (GLvoid (CALLBACK *)(...))(this_type::end_cb));
226 gluTessCallback(tess, GLU_TESS_VERTEX_DATA, (GLvoid (CALLBACK *)(...))(this_type::vertex_cb));
227 #else
228 gluTessCallback(tess, GLU_TESS_BEGIN_DATA, (GLvoid (CALLBACK *)())(this_type::begin_cb));
229 gluTessCallback(tess, GLU_TESS_END_DATA, (GLvoid (CALLBACK *)())(this_type::end_cb));
230 gluTessCallback(tess, GLU_TESS_VERTEX_DATA, (GLvoid (CALLBACK *)())(this_type::vertex_cb));
231 #endif
232 void * polygon_data = (void *)(&t_data);
233
234 GLdouble vertex[3];
235
236 int k = 0;
237 gluTessBeginPolygon(tess, polygon_data);
238 for (size_t i=0; i<outlines.size(); ++i)
239 {
240 gluTessBeginContour(tess);
241 for (size_t j=0; j<outlines[i].size(); ++j)
242 {
243 this_type::get_position(outlines[i][j], vertex);
244 gluTessVertex(tess, vertex, (void *)k);
245 ++k;
246 }
247 gluTessEndContour(tess);
248 }
249 gluTessEndPolygon(tess);
250
251 gluDeleteTess(tess);
252 }
253
254 template <class scalar_type>
255 static inline void get_position(const vcg::Point2<scalar_type> & p, GLdouble * d)
256 {
257 d[0] = (GLdouble)(p[0]);
258 d[1] = (GLdouble)(p[1]);
259 d[2] = (GLdouble)(0);
260 }
261
262 template <class scalar_type>
263 static inline void get_position(const vcg::Point3<scalar_type> & p, GLdouble * d)
264 {
265 d[0] = (GLdouble)(p[0]);
266 d[1] = (GLdouble)(p[1]);
267 d[2] = (GLdouble)(p[2]);
268 }
269 };
270
271 } // end namespace vcg
272
273 #endif // __VCGLIB_GLU_TESSELATOR_H