| 1 |
#ifndef __GLWRAPTETRA__ |
| 2 |
#define __GLWRAPTETRA__ |
| 3 |
#include <GL/glew.h> |
| 4 |
#include <GL/GL.h> |
| 5 |
#include <vcg/space/color4.h> |
| 6 |
#include <vcg/space/tetra3.h> |
| 7 |
#include <wrap/gui/view.h> |
| 8 |
#include <wrap/gl/space.h> |
| 9 |
#include <wrap/gl/math.h> |
| 10 |
|
| 11 |
namespace vcg { |
| 12 |
|
| 13 |
namespace tetra { |
| 14 |
|
| 15 |
class GLW { |
| 16 |
public: |
| 17 |
enum DrawMode {DMNone, DMSmallTetra,DMFlat,DMWire, DMHidden,DMTransparent,DMFlatWire} ; |
| 18 |
enum NormalMode{NMFlat,NMSmooth, NMUser, NMPerMesh}; |
| 19 |
enum ColorMode {CMNone, CMPerMesh,CMUser,CMPerTetraF,CMPerVertexF,CMPerVertex}; |
| 20 |
enum Hint {HShrinkFactor}; |
| 21 |
}; |
| 22 |
|
| 23 |
template <typename CONT_TETRA> |
| 24 |
class GlTetramesh:public GLW{ |
| 25 |
|
| 26 |
|
| 27 |
public: |
| 28 |
|
| 29 |
typedef typename CONT_TETRA::value_type TetraType; |
| 30 |
typedef typename TetraType::VertexType VertexType; |
| 31 |
typedef typename VertexType::ScalarType ScalarType; |
| 32 |
typedef typename VertexType::CoordType Point3x; |
| 33 |
|
| 34 |
|
| 35 |
class ClipPlane |
| 36 |
{ |
| 37 |
private: |
| 38 |
Point3x D; |
| 39 |
Point3x D0; |
| 40 |
GLdouble eqn[4]; |
| 41 |
vcg::Matrix44<float> TR; |
| 42 |
|
| 43 |
Point3x pp0; |
| 44 |
Point3x pp1; |
| 45 |
Point3x pp2; |
| 46 |
Point3x pp3; |
| 47 |
|
| 48 |
public: |
| 49 |
|
| 50 |
bool active; |
| 51 |
|
| 52 |
Point3x P; |
| 53 |
|
| 54 |
ClipPlane (){active=false;} |
| 55 |
|
| 56 |
~ClipPlane (){} |
| 57 |
|
| 58 |
ClipPlane(Point3x p0, Point3x p1,Point3x p2) |
| 59 |
{ |
| 60 |
Point3x N=((p1-p0)^(p2-p0)).Normalize(); |
| 61 |
N.Normalize(); |
| 62 |
D=N; |
| 63 |
D0=D; |
| 64 |
P=(p0+p1+p2)/3.f; |
| 65 |
|
| 66 |
Point3x v0=N; |
| 67 |
Point3x v1=(P-p0); |
| 68 |
v1.Normalize(); |
| 69 |
Point3x v2=(v0^v1); |
| 70 |
v2.Normalize(); |
| 71 |
|
| 72 |
v0=v0*2; |
| 73 |
v1=v1*2; |
| 74 |
v2=v2*2; |
| 75 |
|
| 76 |
pp0=-v1-v2; |
| 77 |
pp1=-v1+v2; |
| 78 |
pp2=v1+v2; |
| 79 |
pp3=v1-v2; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
void SetD(Point3x d) |
| 84 |
{ |
| 85 |
D=d; |
| 86 |
} |
| 87 |
|
| 88 |
void SetP(Point3x p) |
| 89 |
{ |
| 90 |
P=p; |
| 91 |
} |
| 92 |
|
| 93 |
void GlClip() |
| 94 |
{ |
| 95 |
if (active){ |
| 96 |
GLdouble d=-(D.V(0)*P.V(0)+D.V(1)*P.V(1)+D.V(2)*P.V(2)); |
| 97 |
eqn[0]=-D.V(0); |
| 98 |
eqn[1]=-D.V(1); |
| 99 |
eqn[2]=-D.V(2); |
| 100 |
eqn[3]=-d; |
| 101 |
glClipPlane(GL_CLIP_PLANE0, eqn); |
| 102 |
glEnable(GL_CLIP_PLANE0); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void GlDraw() |
| 107 |
{ |
| 108 |
glPushMatrix(); |
| 109 |
glPushAttrib(0xffffffff); |
| 110 |
glDisable(GL_CLIP_PLANE0); |
| 111 |
|
| 112 |
glEnable(GL_LIGHTING); |
| 113 |
glEnable(GL_NORMALIZE); |
| 114 |
|
| 115 |
glTranslate(P); |
| 116 |
glMultMatrix(TR); |
| 117 |
glLineWidth(0.5); |
| 118 |
glColor3d(0.7,0,0.7); |
| 119 |
glBegin(GL_LINE_LOOP); |
| 120 |
glVertex(pp0); |
| 121 |
glVertex(pp1); |
| 122 |
glVertex(pp2); |
| 123 |
glVertex(pp3); |
| 124 |
glEnd(); |
| 125 |
|
| 126 |
glPopAttrib(); |
| 127 |
glPopMatrix(); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
void Transform(vcg::Matrix44<float> Tr) |
| 132 |
{ |
| 133 |
|
| 134 |
|
| 135 |
Point3f p=Point3f((float)D0.V(0),(float)D0.V(1),(float)D0.V(2)); |
| 136 |
TR=Tr; |
| 137 |
p=TR*p; |
| 138 |
D=Point3x((ScalarType) p.V(0),(ScalarType) p.V(1),(ScalarType) p.V(2)); |
| 139 |
} |
| 140 |
|
| 141 |
void Translate(float L) |
| 142 |
{ |
| 143 |
Point3x D1=D*L; |
| 144 |
P+=D1; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
}; |
| 149 |
|
| 150 |
GlTetramesh(CONT_TETRA * _t):tetra(_t){} |
| 151 |
GlTetramesh( ) {} |
| 152 |
|
| 153 |
CONT_TETRA * tetra; |
| 154 |
ClipPlane section; |
| 155 |
|
| 156 |
private: |
| 157 |
ScalarType shrink_factor; |
| 158 |
|
| 159 |
|
| 160 |
public: |
| 161 |
|
| 162 |
void SetHint(Hint h, double value){ |
| 163 |
switch(h){ |
| 164 |
case HShrinkFactor: shrink_factor = value; break; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void AddClipSection(Point3x p0,Point3x p1,Point3x p2) |
| 169 |
{ |
| 170 |
section=ClipPlane(p0,p1,p2); |
| 171 |
section.active=true; |
| 172 |
} |
| 173 |
|
| 174 |
void ClearClipSection() |
| 175 |
{ |
| 176 |
section.active=false; |
| 177 |
} |
| 178 |
|
| 179 |
typedef Color4b (*color_func_vertex)(VertexType&v); |
| 180 |
color_func_vertex color_vertex; |
| 181 |
|
| 182 |
typedef Color4b (*color_func_tetra)(TetraType&v); |
| 183 |
color_func_tetra color_tetra; |
| 184 |
|
| 185 |
|
| 186 |
template <DrawMode dm,NormalMode nm,ColorMode cm > |
| 187 |
|
| 188 |
void Draw(){ |
| 189 |
switch (dm){ |
| 190 |
case DMNone: break; |
| 191 |
case DMSmallTetra:_DrawSmallTetra<cm>();break; |
| 192 |
case DMFlat:_DrawSurface<dm,nm,cm>();break; |
| 193 |
case DMWire:_DrawSurface<dm,nm,cm>();break; |
| 194 |
case DMHidden:_DrawSurface<dm,nm,cm>();break; |
| 195 |
case DMFlatWire:_DrawFlatWire<nm,cm>(); break; |
| 196 |
case DMTransparent:break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
private: |
| 201 |
template <ColorMode cm > |
| 202 |
void _DrawSmallTetra(){ |
| 203 |
Point3x p[4],br; |
| 204 |
typename CONT_TETRA::iterator it; |
| 205 |
glPushAttrib(0xffffffff); |
| 206 |
glEnable(GL_COLOR_MATERIAL); |
| 207 |
glEnable(GL_LIGHT0); |
| 208 |
glEnable(GL_LIGHTING); |
| 209 |
glEnable(GL_NORMALIZE); |
| 210 |
glPolygonMode(GL_FRONT,GL_FILL); |
| 211 |
if (section.active) |
| 212 |
{ |
| 213 |
section.GlClip(); |
| 214 |
section.GlDraw(); |
| 215 |
} |
| 216 |
|
| 217 |
for( it = tetra->begin(); it != tetra->end(); ++it) |
| 218 |
if((!it->IsD())&&(!(it->IsS()))) |
| 219 |
{ |
| 220 |
_DrawSmallTetra<cm>(*it); |
| 221 |
} |
| 222 |
else |
| 223 |
if((!it->IsD())&&((it->IsS()))) |
| 224 |
{ |
| 225 |
_DrawSelectedTetra(*it); |
| 226 |
} |
| 227 |
|
| 228 |
glPopAttrib(); |
| 229 |
} |
| 230 |
|
| 231 |
template <NormalMode nm,ColorMode cm > |
| 232 |
void _DrawFlatWire(){ |
| 233 |
glPushAttrib(0xffff); |
| 234 |
glEnable(GL_COLOR_MATERIAL); |
| 235 |
glEnable(GL_DEPTH); |
| 236 |
glDepthRange(0.001,1.0); |
| 237 |
Draw<DMFlat,nm,cm>(); |
| 238 |
glDisable(GL_LIGHTING); |
| 239 |
glColor3f(0.0,0.0,0.0); |
| 240 |
glDepthRange(0.0,0.999); |
| 241 |
Draw<DMHidden,nm,cm>(); |
| 242 |
glPopAttrib(); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
template <DrawMode dm,NormalMode nm,ColorMode cm > |
| 247 |
void _DrawSurface(){ |
| 248 |
typename CONT_TETRA::iterator it; |
| 249 |
|
| 250 |
glPushAttrib(0xffff); |
| 251 |
glEnable(GL_COLOR_MATERIAL); |
| 252 |
if((dm == DMWire)||(dm ==DMHidden)) |
| 253 |
{ |
| 254 |
glDisable(GL_LIGHTING); |
| 255 |
glDisable(GL_NORMALIZE); |
| 256 |
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
glEnable(GL_LIGHTING); |
| 261 |
glEnable(GL_NORMALIZE); |
| 262 |
glPolygonMode(GL_FRONT,GL_FILL); |
| 263 |
} |
| 264 |
|
| 265 |
for( it = tetra->begin(); it != tetra->end(); ++it) |
| 266 |
_DrawTetra<dm,nm,cm>((*it)); |
| 267 |
|
| 268 |
glPopAttrib(); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
void _DrawSelectedTetra(TetraType &t) |
| 273 |
{ |
| 274 |
glPushMatrix(); |
| 275 |
glPushAttrib(0xffff); |
| 276 |
glDisable(GL_CLIP_PLANE0); |
| 277 |
glDisable(GL_BLEND); |
| 278 |
glDisable(GL_LIGHTING); |
| 279 |
glDisable(GL_NORMALIZE); |
| 280 |
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); |
| 281 |
|
| 282 |
glColor3d(1,0,0); |
| 283 |
|
| 284 |
glBegin(GL_TRIANGLES); |
| 285 |
for (int face=0;face<4;face++) |
| 286 |
{ |
| 287 |
glVertex(t.V(Tetra::VofF(face,0))->P()); |
| 288 |
glVertex(t.V(Tetra::VofF(face,1))->P()); |
| 289 |
glVertex(t.V(Tetra::VofF(face,2))->P()); |
| 290 |
} |
| 291 |
glEnd(); |
| 292 |
|
| 293 |
|
| 294 |
glPopAttrib(); |
| 295 |
glPopMatrix(); |
| 296 |
} |
| 297 |
|
| 298 |
template <DrawMode dm,NormalMode nm,ColorMode cm > |
| 299 |
void _DrawTetra(TetraType &t) |
| 300 |
{ |
| 301 |
if((!t.IsD())&&(!t.IsS())) |
| 302 |
{ |
| 303 |
if ((dm!=DMWire)&&(dm!=DMHidden)) |
| 304 |
_ChooseColorTetra<cm>(t); |
| 305 |
for(int i = 0; i < 4; ++i){ |
| 306 |
if (dm == DMWire) |
| 307 |
_DrawFace<cm>(t,i); |
| 308 |
else |
| 309 |
{ |
| 310 |
if (t.IsBorderF(i)) |
| 311 |
{ |
| 312 |
if(nm==NMSmooth) |
| 313 |
_DrawFaceSmooth<cm>(t,i); |
| 314 |
else |
| 315 |
if(nm==NMFlat) |
| 316 |
_DrawFace<cm>(t,i); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
else |
| 322 |
if((!t.IsD())&&(t.IsS())) |
| 323 |
_DrawSelectedTetra(t); |
| 324 |
} |
| 325 |
|
| 326 |
template <ColorMode cm > |
| 327 |
void _ChooseColorTetra(TetraType &t) |
| 328 |
{ |
| 329 |
if (cm==CMNone) |
| 330 |
{ |
| 331 |
if (t.IsS()) |
| 332 |
glColor3d(1,0,0); |
| 333 |
else |
| 334 |
glColor3d(0.8f,0.8f,0.8f); |
| 335 |
} |
| 336 |
else |
| 337 |
if(cm == CMPerTetraF) |
| 338 |
{ |
| 339 |
Color4b c; |
| 340 |
c = color_tetra(t); |
| 341 |
GLint ic[4]; ic[0] = c[0];ic[1] = c[1];ic[2] = c[2];ic[3] = c[3]; |
| 342 |
glMaterialiv(GL_FRONT,GL_DIFFUSE ,ic); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
template <ColorMode cm > |
| 347 |
void _ChooseColorVertex(VertexType &v) |
| 348 |
{ |
| 349 |
if (cm!=CMNone) |
| 350 |
{ |
| 351 |
if(cm == CMPerVertexF) |
| 352 |
{ |
| 353 |
Color4b c; |
| 354 |
c = color_vertex(v); |
| 355 |
GLint ic[4]; ic[0] = c[0];ic[1] = c[1];ic[2] = c[2];ic[3] = c[3]; |
| 356 |
glMaterialiv(GL_FRONT,GL_DIFFUSE ,ic); |
| 357 |
} |
| 358 |
else |
| 359 |
if(cm == CMPerVertex) |
| 360 |
glColor3f(v.C()[0],v.C()[1],v.C()[2]); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
template <ColorMode cm > |
| 365 |
void _DrawFaceSmooth(TetraType &t,int face) |
| 366 |
{ |
| 367 |
|
| 368 |
VertexType *v0=t.V(Tetra::VofF(face,0)); |
| 369 |
VertexType *v1=t.V(Tetra::VofF(face,1)); |
| 370 |
VertexType *v2=t.V(Tetra::VofF(face,2)); |
| 371 |
|
| 372 |
glBegin(GL_TRIANGLES); |
| 373 |
_ChooseColorVertex<cm>(*v0); |
| 374 |
glNormal(v0->N()); |
| 375 |
glVertex(v0->P()); |
| 376 |
_ChooseColorVertex<cm>(*v1); |
| 377 |
glNormal(v1->N()); |
| 378 |
glVertex(v1->P()); |
| 379 |
_ChooseColorVertex<cm>(*v2); |
| 380 |
glNormal(v2->N()); |
| 381 |
glVertex(v2->P()); |
| 382 |
glEnd(); |
| 383 |
} |
| 384 |
|
| 385 |
template <ColorMode cm > |
| 386 |
void _DrawFace(TetraType &t,int face) |
| 387 |
{ |
| 388 |
glBegin(GL_TRIANGLES); |
| 389 |
glNormal(t.N(face)); |
| 390 |
VertexType *v0=t.V(Tetra::VofF(face,0)); |
| 391 |
VertexType *v1=t.V(Tetra::VofF(face,1)); |
| 392 |
VertexType *v2=t.V(Tetra::VofF(face,2)); |
| 393 |
_ChooseColorVertex<cm>(*v0); |
| 394 |
glVertex(v0->P()); |
| 395 |
_ChooseColorVertex<cm>(*v1); |
| 396 |
glVertex(v1->P()); |
| 397 |
_ChooseColorVertex<cm>(*v2); |
| 398 |
glVertex(v2->P()); |
| 399 |
glEnd(); |
| 400 |
} |
| 401 |
|
| 402 |
template <ColorMode cm > |
| 403 |
void _DrawSmallTetra(TetraType &t) |
| 404 |
{ |
| 405 |
Tetra3<ScalarType> T=Tetra3<ScalarType>(); |
| 406 |
T.P0(0)=t.V(0)->cP(); |
| 407 |
T.P1(0)=t.V(1)->cP(); |
| 408 |
T.P2(0)=t.V(2)->cP(); |
| 409 |
T.P3(0)=t.V(3)->cP(); |
| 410 |
Point3x p[4], br; |
| 411 |
br=T.ComputeBarycenter(); |
| 412 |
for(int i = 0; i < 4; ++i) |
| 413 |
p[i] = t.V(i)->P()* shrink_factor + br *(1- shrink_factor); |
| 414 |
_ChooseColorTetra<cm>(t); |
| 415 |
|
| 416 |
glBegin(GL_TRIANGLES); |
| 417 |
for(int i = 0; i < 4; ++i) |
| 418 |
{ |
| 419 |
glNormal(t.N(i)); |
| 420 |
VertexType *v0=t.V(Tetra::VofF(i,0)); |
| 421 |
VertexType *v1=t.V(Tetra::VofF(i,1)); |
| 422 |
VertexType *v2=t.V(Tetra::VofF(i,2)); |
| 423 |
_ChooseColorVertex<cm>(*v0); |
| 424 |
glVertex(p[Tetra::VofF(i,0)]); |
| 425 |
_ChooseColorVertex<cm>(*v1); |
| 426 |
glVertex(p[Tetra::VofF(i,1)]); |
| 427 |
_ChooseColorVertex<cm>(*v2); |
| 428 |
glVertex(p[Tetra::VofF(i,2)]); |
| 429 |
} |
| 430 |
glEnd(); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
}; |
| 435 |
|
| 436 |
} |
| 437 |
} |
| 438 |
#endif |