| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#ifndef __VCGLIB_GLU_TESSELATOR_H |
| 47 |
#define __VCGLIB_GLU_TESSELATOR_H |
| 48 |
|
| 49 |
#include <vector> |
| 50 |
|
| 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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 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 |
} |
| 272 |
|
| 273 |
#endif |