Debian Codegraph @

vcg/wrap/gl/trimesh.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: trimesh.h,v $
27 Revision 1.28 2008/01/28 08:49:58 cignoni
28 added sanity checks when deleting buffers
29
30 Revision 1.27 2007/12/05 11:08:16 mischitelli
31 Renamed some unmeaningful variable names (b -> array_buffers ; h -> curr_hints)
32
33 Revision 1.26 2007/12/04 17:59:41 mischitelli
34 - Fixed DrawFill method, which required the hint 'HNUseVArray' enabled in order to render the mesh with VBO. This was also causing extra overhead in the Update method since HNUseVArray has to be enabled and therefore extra calculation were done to copy vertices in VArrays even if the user was using only VBOs.
35
36 Revision 1.25 2007/09/12 16:20:24 m_di_benedetto
37 *** empty log message ***
38
39 Revision 1.24 2007/09/12 14:48:50 m_di_benedetto
40 Corrected indexing for non textured faces.
41
42 Revision 1.23 2007/01/18 01:26:23 cignoni
43 Added cast for mac compiling
44
45 Revision 1.22 2006/12/12 11:06:58 cignoni
46 Slightly changed the colormaterial mode for the flatwire
47
48 Revision 1.21 2006/10/14 16:26:26 ponchio
49 Aggiunti un paio di typename... al solito.
50
51 Revision 1.20 2006/05/25 09:40:14 cignoni
52 gcc dislike doubleline comments; removed.
53
54 Revision 1.19 2006/02/13 13:05:05 cignoni
55 Removed glew inclusion
56
57 Revision 1.18 2006/02/09 10:00:39 cignoni
58 Switched from rough zoffset to glpolygonoffset for hiddenline and flatlines modes. Less zfighting...
59
60 Revision 1.17 2005/12/15 14:05:59 spinelli
61 add test (tm==TMPerWedgeMulti)
62
63 Revision 1.16 2005/12/14 00:18:43 cignoni
64 multiple texture support
65
66 Revision 1.15 2005/12/02 10:38:07 cignoni
67 Changed a wrong uppercase in the include
68
69 Revision 1.14 2005/12/02 00:03:22 cignoni
70 Added support for one texture mode (perwedge)
71 Changed texturemapid array into a safer vector
72
73 Revision 1.13 2005/11/24 08:06:50 cignoni
74 Added bound checking in texture access
75
76 Revision 1.12 2005/11/22 23:57:28 cignoni
77 Added a missing colormaterial for flatwire.
78
79 Revision 1.11 2005/10/12 18:24:30 ponchio
80 another bunch of typenames.
81
82 Revision 1.10 2005/10/12 17:19:03 ponchio
83 Added gazillions typenames, commented out old broken functions,
84 added unsigned int TextureMapID[128], and unsigned int & TMId(int i).
85
86 Revision 1.9 2005/05/09 11:28:48 spinelli
87 ho tolto 2 warning del tipo unreferenced formal parameter, commentando le var che producevano tale warning.
88
89 Revision 1.8 2005/04/22 15:16:48 turini
90 Minor Changes To Compile With List Containers.
91
92 Revision 1.7 2005/02/26 12:45:23 spinelli
93 ripristinata la modalita' di render bbox....
94
95 Revision 1.6 2005/01/12 14:39:41 tommyfranken
96 constructor name was wrong (old class name)
97
98 Revision 1.5 2004/12/15 18:45:07 tommyfranken
99 *** empty log message ***
100
101 Revision 1.4 2004/09/30 01:40:39 ponchio
102 <gl/glew.h> --> <GL/glew.h>
103
104 Revision 1.3 2004/07/13 11:25:57 pietroni
105 changed order of initial include ( it had problems with extension of openGL)
106
107 Revision 1.2 2004/07/12 15:57:33 ganovelli
108 first draft: it includes glew !
109
110
111
112
113 ****************************************************************************/
114
115 #ifndef __VCG_GLTRIMESH
116 #define __VCG_GLTRIMESH
117
118 #include <queue>
119 #include <vector>
120
121 //#include <GL/glew.h>
122 #include <wrap/gl/space.h>
123 #include <vcg/space/color4.h>
124
125 namespace vcg {
126
127
128 // classe base di glwrap usata solo per poter usare i vari drawmode, normalmode senza dover
129 // specificare tutto il tipo (a volte lunghissimo)
130 // della particolare classe glwrap usata.
131 class GLW
132 {
133 public:
134 enum DrawMode {DMNone, DMBox, DMPoints, DMWire, DMHidden, DMFlat, DMSmooth, DMFlatWire, DMRadar, DMLast} ;
135 enum NormalMode {NMNone, NMPerVert, NMPerFace, NMPerWedge, NMLast};
136 enum ColorMode {CMNone, CMPerMesh, CMPerFace, CMPerVert, CMLast};
137 enum TextureMode{TMNone, TMPerVert, TMPerWedge, TMPerWedgeMulti};
138 enum Hint {
139 HNUseTriStrip = 0x0001, // ha bisogno che ci sia la fftopology gia calcolata!
140 // HNUseEdgeStrip = 0x0002, //
141 HNUseDisplayList = 0x0004,
142 HNCacheDisplayList = 0x0008, // Each mode has its dl;
143 HNLazyDisplayList = 0x0010, // Display list are generated only when requested
144 HNIsTwoManifold = 0x0020, // There is no need to make DetachComplex before .
145 HNUsePerWedgeNormal = 0x0040, //
146 HNHasFFTopology = 0x0080, // E' l'utente che si preoccupa di tenere aggiornata la topologia ff
147 HNHasVFTopology = 0x0100, // E' l'utente che si preoccupa di tenere aggiornata la topologia vf
148 HNHasVertNormal = 0x0200, // E' l'utente che si preoccupa di tenere aggiornata le normali per faccia
149 HNHasFaceNormal = 0x0400, // E' l'utente che si preoccupa di tenere aggiornata le normali per vertice
150 HNUseVArray = 0x0800,
151 HNUseLazyEdgeStrip = 0x1000, // Edge Strip are generated only when requested
152 HNUseVBO = 0x2000 // Use Vertex Buffer Object
153 };
154
155 enum Change {
156 CHVertex = 0x01,
157 CHNormal = 0x02,
158 CHColor = 0x04,
159 CHFace = 0x08,
160 CHFaceNormal = 0x10,
161 CHRender = 0x20,
162 CHAll = 0xff
163 };
164 enum HintParami {
165 HNPDisplayListSize =0
166 };
167 enum HintParamf {
168 HNPCreaseAngle =0, // crease angle in radians
169 HNPZTwist = 1 // Z offset used in Flatwire and hiddenline modality
170 };
171
172 template<class MESH_TYPE>
173 class VertToSplit
174 {
175 public:
176 typename MESH_TYPE::face_base_pointer f;
177 char z;
178 char edge;
179 bool newp;
180 typename MESH_TYPE::vertex_pointer v;
181 };
182
183 // GL Array Elemet
184 class GLAElem {
185 public :
186 int glmode;
187 int len;
188 int start;
189 };
190
191
192 };
193
194 template <class MESH_TYPE, bool partial = false , class FACE_POINTER_CONTAINER = std::vector<typename MESH_TYPE::FacePointer> >
195 class GlTrimesh : public GLW
196 {
197 public:
198
199 typedef MESH_TYPE mesh_type;
200 FACE_POINTER_CONTAINER face_pointers;
201
202
203 std::vector<unsigned int> TMId;
204 unsigned int array_buffers[3];
205
206 int curr_hints; // the current hints
207
208 // The parameters of hints
209 int HNParami[8];
210 float HNParamf[8];
211
212 MESH_TYPE *m;
213 GlTrimesh()
214 {
215 m=0;
216 dl=0xffffffff;
217 curr_hints=HNUseLazyEdgeStrip;
218 cdm=DMNone;
219 ccm=CMNone;
220 cnm=NMNone;
221
222 SetHintParamf(HNPCreaseAngle,float(M_PI/5));
223 SetHintParamf(HNPZTwist,0.00005f);
224
225 }
226
227 ~GlTrimesh()
228 {
229 //Delete the VBOs
230 if(curr_hints&HNUseVBO)
231 {
232 for(int i=0;i<3;++i)
233 if(glIsBuffer(array_buffers[i]))
234 glDeleteBuffersARB(1, (GLuint *)(array_buffers+i));
235 }
236 }
237
238 void SetHintParami(const HintParami hip, const int value)
239 {
240 HNParami[hip]=value;
241 }
242 int GetHintParami(const HintParami hip) const
243 {
244 return HNParami[hip];
245 }
246 void SetHintParamf(const HintParamf hip, const float value)
247 {
248 HNParamf[hip]=value;
249 }
250 float GetHintParamf(const HintParamf hip) const
251 {
252 return HNParamf[hip];
253 }
254 void SetHint(Hint hn)
255 {
256 curr_hints |= hn;
257 }
258 void ClearHint(Hint hn)
259 {
260 curr_hints&=(~hn);
261 }
262
263 unsigned int dl;
264 std::vector<unsigned int> indices;
265
266 DrawMode cdm; // Current DrawMode
267 NormalMode cnm; // Current NormalMode
268 ColorMode ccm; // Current ColorMode
269
270 void Update(/*Change c=CHAll*/)
271 {
272 if(m==0) return;
273
274 if(curr_hints&HNUseVArray || curr_hints&HNUseVBO)
275 {
276 typename MESH_TYPE::FaceIterator fi;
277 indices.clear();
278 for(fi = m->face.begin(); fi != m->face.end(); ++fi)
279 {
280 indices.push_back((unsigned int)((*fi).V(0) - &(*m->vert.begin())));
281 indices.push_back((unsigned int)((*fi).V(1) - &(*m->vert.begin())));
282 indices.push_back((unsigned int)((*fi).V(2) - &(*m->vert.begin())));
283 }
284
285 if(curr_hints&HNUseVBO)
286 {
287 if(!glIsBuffer(array_buffers[1]))
288 glGenBuffers(2,(GLuint*)array_buffers);
289 glBindBuffer(GL_ARRAY_BUFFER,array_buffers[0]);
290 glBufferData(GL_ARRAY_BUFFER_ARB, m->vn * sizeof(typename MESH_TYPE::VertexType),
291 (char *)&(m->vert[0].P()), GL_STATIC_DRAW_ARB);
292
293 glBindBuffer(GL_ARRAY_BUFFER,array_buffers[1]);
294 glBufferData(GL_ARRAY_BUFFER_ARB, m->vn * sizeof(typename MESH_TYPE::VertexType),
295 (char *)&(m->vert[0].N()), GL_STATIC_DRAW_ARB);
296 }
297
298 glVertexPointer(3,GL_FLOAT,sizeof(typename MESH_TYPE::VertexType),0);
299 glNormalPointer(GL_FLOAT,sizeof(typename MESH_TYPE::VertexType),0);
300 }
301
302 //int C=c;
303 //if((C&CHVertex) || (C&CHFace)) {
304 // ComputeBBox(*m);
305 // if(!(curr_hints&HNHasFaceNormal)) m->ComputeFaceNormal();
306 // if(!(curr_hints&HNHasVertNormal)) m->ComputeVertexNormal();
307 // C= (C | CHFaceNormal);
308 //}
309 //if((C&CHFace) && (curr_hints&HNUseEdgeStrip)) ComputeEdges();
310 //if((C&CHFace) && (curr_hints&HNUseLazyEdgeStrip)) ClearEdges();
311 //if(MESH_TYPE::HasFFTopology())
312 // if((C&CHFace) && (curr_hints&HNUseTriStrip)) {
313 // if(!(curr_hints&HNHasFFTopology)) m->FFTopology();
314 // ComputeTriStrip();
315 // }
316 //if((C&CHFaceNormal) && (curr_hints&HNUsePerWedgeNormal)) {
317 // if(!(curr_hints&HNHasVFTopology)) m->VFTopology();
318 // CreaseWN(*m,MESH_TYPE::scalar_type(GetHintParamf(HNPCreaseAngle)));
319 //}
320 //if(C!=0) { // force the recomputation of display list
321 // cdm=DMNone;
322 // ccm=CMNone;
323 // cnm=NMNone;
324 //}
325 //if((curr_hints&HNUseVArray) && (curr_hints&HNUseTriStrip))
326 // {
327 // ConvertTriStrip<MESH_TYPE>(*m,TStrip,TStripF,TStripVED,TStripVEI);
328 // }
329 }
330
331 void Draw(DrawMode dm ,ColorMode cm, TextureMode tm)
332 {
333 switch(dm)
334 {
335 case DMNone : Draw<DMNone >(cm,tm); break;
336 case DMBox : Draw<DMBox >(cm,tm); break;
337 case DMPoints : Draw<DMPoints >(cm,tm); break;
338 case DMWire : Draw<DMWire >(cm,tm); break;
339 case DMHidden : Draw<DMHidden >(cm,tm); break;
340 case DMFlat : Draw<DMFlat >(cm,tm); break;
341 case DMSmooth : Draw<DMSmooth >(cm,tm); break;
342 case DMFlatWire: Draw<DMFlatWire>(cm,tm); break;
343 default : break;
344 }
345 }
346
347 template< DrawMode dm >
348 void Draw(ColorMode cm, TextureMode tm)
349 {
350 switch(cm)
351 {
352 case CMNone : Draw<dm,CMNone >(tm); break;
353 case CMPerMesh : Draw<dm,CMPerMesh>(tm); break;
354 case CMPerFace : Draw<dm,CMPerFace>(tm); break;
355 case CMPerVert : Draw<dm,CMPerVert>(tm); break;
356 default : break;
357 }
358 }
359
360 template< DrawMode dm, ColorMode cm >
361 void Draw(TextureMode tm)
362 {
363 switch(tm)
364 {
365 case TMNone : Draw<dm,cm,TMNone >(); break;
366 case TMPerVert : Draw<dm,cm,TMPerVert >(); break;
367 case TMPerWedge : Draw<dm,cm,TMPerWedge >(); break;
368 case TMPerWedgeMulti : Draw<dm,cm,TMPerWedgeMulti >(); break;
369 default : break;
370 }
371 }
372
373
374
375 template< DrawMode dm, ColorMode cm, TextureMode tm>
376 void Draw()
377 {
378 if(!m) return;
379 if((curr_hints & HNUseDisplayList)){
380 if (cdm==dm && ccm==cm){
381 glCallList(dl);
382 return;
383 }
384 else {
385 if(dl==0xffffffff) dl=glGenLists(1);
386 glNewList(dl,GL_COMPILE);
387 }
388 }
389
390 glPushMatrix();
391 switch(dm)
392 {
393 case DMNone : break;
394 case DMBox : DrawBBox(cm);break;
395 case DMPoints : DrawPoints<NMPerVert,cm>();break;
396 case DMHidden : DrawHidden();break;
397 case DMFlat : DrawFill<NMPerFace,cm,tm>();break;
398 case DMFlatWire : DrawFlatWire<NMPerFace,cm,tm>();break;
399 case DMRadar : DrawRadar<NMPerFace,cm>();break;
400 case DMWire : DrawWire<NMPerVert,cm>();break;
401 case DMSmooth : DrawFill<NMPerVert,cm,tm>();break;
402 default : break;
403 }
404 glPopMatrix();
405
406 if((curr_hints & HNUseDisplayList)){
407 cdm=dm;
408 ccm=cm;
409 glEndList();
410 glCallList(dl);
411 }
412 }
413
414
415 /*********************************************************************************************/
416 /*********************************************************************************************/
417
418 template <NormalMode nm, ColorMode cm, TextureMode tm>
419 void DrawFill()
420 {
421 typename FACE_POINTER_CONTAINER::iterator fp;
422
423 typename MESH_TYPE::FaceIterator fi;
424
425
426 typename std::vector<typename MESH_TYPE::FaceType*>::iterator fip;
427 short curtexname=-1;
428
429 if(cm == CMPerMesh)
430 glColor(m->C());
431
432 if(tm == TMPerWedge || tm == TMPerWedgeMulti )
433 glDisable(GL_TEXTURE_2D);
434
435 if(curr_hints&HNUseVBO)
436 {
437 if( (cm==CMNone) || (cm==CMPerMesh) )
438 {
439 if (nm==NMPerVert)
440 glEnableClientState (GL_NORMAL_ARRAY);
441 glEnableClientState (GL_VERTEX_ARRAY);
442
443 if (nm==NMPerVert)
444 {
445 glBindBuffer(GL_ARRAY_BUFFER,array_buffers[1]);
446 glNormalPointer(GL_FLOAT,sizeof(typename MESH_TYPE::VertexType),0);
447 }
448 glBindBuffer(GL_ARRAY_BUFFER,array_buffers[0]);
449 glVertexPointer(3,GL_FLOAT,sizeof(typename MESH_TYPE::VertexType),0);
450
451 glDrawElements(GL_TRIANGLES ,m->fn*3,GL_UNSIGNED_INT, &(*indices.begin()) );
452 glDisableClientState (GL_VERTEX_ARRAY);
453 if (nm==NMPerVert)
454 glDisableClientState (GL_NORMAL_ARRAY);
455
456 glBindBuffer(GL_ARRAY_BUFFER, 0);
457
458 return;
459
460 }
461 }
462
463 if(curr_hints&HNUseVArray)
464 {
465 if( (cm==CMNone) || (cm==CMPerMesh) )
466 {
467 if (nm==NMPerVert)
468 glEnableClientState (GL_NORMAL_ARRAY);
469 glEnableClientState (GL_VERTEX_ARRAY);
470
471 if (nm==NMPerVert)
472 glNormalPointer(GL_FLOAT,sizeof(typename MESH_TYPE::VertexType),&(m->vert.begin()->N()[0]));
473 glVertexPointer(3,GL_FLOAT,sizeof(typename MESH_TYPE::VertexType),&(m->vert.begin()->P()[0]));
474
475 glDrawElements(GL_TRIANGLES ,m->fn*3,GL_UNSIGNED_INT, &(*indices.begin()) );
476 glDisableClientState (GL_VERTEX_ARRAY);
477 if (nm==NMPerVert)
478 glDisableClientState (GL_NORMAL_ARRAY);
479
480 return;
481 }
482 }
483 else
484
485 if(curr_hints&HNUseTriStrip)
486 {
487 //if( (nm==NMPerVert) && ((cm==CMNone) || (cm==CMPerMesh)))
488 // if(curr_hints&HNUseVArray){
489 // glEnableClientState (GL_NORMAL_ARRAY );
490 // glNormalPointer(GL_FLOAT,sizeof(MESH_TYPE::VertexType),&(m->vert[0].cN()));
491 // glEnableClientState (GL_VERTEX_ARRAY);
492 // glVertexPointer(3,GL_FLOAT,sizeof(MESH_TYPE::VertexType),&(m->vert[0].cP()));
493 // std::vector<GLAElem>::iterator vi;
494 // for(vi=TStripVED.begin();vi!=TStripVED.end();++vi)
495 // glDrawElements(vi->glmode ,vi->len,GL_UNSIGNED_SHORT,&TStripVEI[vi->start] );
496 //
497 // glDisableClientState (GL_NORMAL_ARRAY );
498 // glDisableClientState (GL_VERTEX_ARRAY);
499 // return;
500 // }
501
502 //std::vector< MESH_TYPE::VertexType *>::iterator vi;
503 //glBegin(GL_TRIANGLE_STRIP);
504 //if(nm == NMPerFace) fip=TStripF.begin();
505
506 //for(vi=TStrip.begin();vi!=TStrip.end(); ++vi){
507 // if((*vi)){
508 // if(nm==NMPerVert) glNormal((*vi)->cN());
509 // if(nm==NMPerFace) glNormal((*fip)->cN());
510 // glVertex((*vi)->P());
511 // }
512 // else
513 // {
514 // glEnd();
515 // glBegin(GL_TRIANGLE_STRIP);
516 // }
517 // if(nm == NMPerFace) ++fip;
518 // }
519 //glEnd();
520 }
521 else
522 {
523 if(partial)
524 fp = face_pointers.begin();
525 else
526 fi = m->face.begin();
527
528 if(tm==TMPerWedgeMulti)
529 {
530 curtexname=(*fi).WT(0).n();
531 if (curtexname >= 0)
532 {
533 glEnable(GL_TEXTURE_2D);
534 glBindTexture(GL_TEXTURE_2D,TMId[curtexname]);
535 }
536 else
537 {
538 glDisable(GL_TEXTURE_2D);
539 }
540 }
541
542 glBegin(GL_TRIANGLES);
543
544 while( (partial)?(fp!=face_pointers.end()):(fi!=m->face.end()))
545 {
546 typename MESH_TYPE::FaceType & f = (partial)?(*(*fp)): *fi;
547
548 if(!f.IsD())
549 {
550 if(tm==TMPerWedgeMulti)
551 if(f.WT(0).n() != curtexname)
552 {
553 curtexname=(*fi).WT(0).n();
554 glEnd();
555
556 if (curtexname >= 0)
557 {
558 glEnable(GL_TEXTURE_2D);
559 glBindTexture(GL_TEXTURE_2D,TMId[curtexname]);
560 }
561 else
562 {
563 glDisable(GL_TEXTURE_2D);
564 }
565
566 glBegin(GL_TRIANGLES);
567 }
568
569 if(nm == NMPerFace) glNormal(f.cN());
570 if(nm == NMPerVert) glNormal(f.V(0)->cN());
571 if(nm == NMPerWedge)glNormal(f.WN(0));
572
573 if(cm == CMPerFace) glColor(f.C());
574 if(cm == CMPerVert) glColor(f.V(0)->C());
575 // if(tm==TMPerVert) glTexCoord(f.V(0)->T());
576 if( (tm==TMPerWedge)||(tm==TMPerWedgeMulti) )glTexCoord(f.WT(0).t(0));
577 glVertex(f.V(0)->P());
578
579 if(nm == NMPerVert) glNormal(f.V(1)->cN());
580 if(nm == NMPerWedge)glNormal(f.WN(1));
581 if(cm == CMPerVert) glColor(f.V(1)->C());
582 // if(tm==TMPerVert) glTexCoord(f.V(1)->T());
583 if( (tm==TMPerWedge)|| (tm==TMPerWedgeMulti)) glTexCoord(f.WT(1).t(0));
584 glVertex(f.V(1)->P());
585
586 if(nm == NMPerVert) glNormal(f.V(2)->cN());
587 if(nm == NMPerWedge)glNormal(f.WN(2));
588 if(cm == CMPerVert) glColor(f.V(2)->C());
589 // if(tm==TMPerVert) glTexCoord(f.V(2)->T());
590 if( (tm==TMPerWedge)|| (tm==TMPerWedgeMulti)) glTexCoord(f.WT(2).t(0));
591 glVertex(f.V(2)->P());
592 }
593
594 if(partial)
595 ++fp;
596 else
597 ++fi;
598 }
599
600 glEnd();
601
602 }
603
604 }
605
606 template<NormalMode nm, ColorMode cm>
607 void DrawPoints()
608 {
609 typename MESH_TYPE::VertexIterator vi;
610 glBegin(GL_POINTS);
611 if(cm==CMPerMesh) glColor(m->C());
612
613 for(vi=m->vert.begin();vi!=m->vert.end();++vi)if(!(*vi).IsD())
614 {
615 if(nm==NMPerVert) glNormal((*vi).cN());
616 if(cm==CMPerVert) glColor((*vi).C());
617 glVertex((*vi).P());
618 }
619 glEnd();
620 }
621
622
623 void DrawHidden()
624 {
625 //const float ZTWIST=HNParamf[HNPZTwist];
626 glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT );
627 glEnable(GL_POLYGON_OFFSET_FILL);
628 glPolygonOffset(1.0, 1);
629 //glDepthRange(ZTWIST,1.0f);
630 glDisable(GL_LIGHTING);
631 glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
632 DrawFill<NMNone,CMNone,TMNone>();
633 glDisable(GL_POLYGON_OFFSET_FILL);
634 glEnable(GL_LIGHTING);
635 glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
636 //glDepthRange(0.0f,1.0f-ZTWIST);
637 DrawWire<NMPerVert,CMNone>();
638 glPopAttrib();
639 // glDepthRange(0,1.0f);
640 }
641
642 template <NormalMode nm, ColorMode cm, TextureMode tm>
643 void DrawFlatWire()
644 {
645 //const float ZTWIST=HNParamf[HNPZTwist];
646 //glDepthRange(ZTWIST,1.0f);
647 glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT );
648 glEnable(GL_POLYGON_OFFSET_FILL);
649 glPolygonOffset(1.0, 1);
650 DrawFill<nm,cm,tm>();
651 glDisable(GL_POLYGON_OFFSET_FILL);
652 //glDepthRange(0.0f,1.0f-ZTWIST);
653 glEnable(GL_COLOR_MATERIAL);
654 glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
655 //glColorMaterial(GL_FRONT,GL_DIFFUSE);
656 glColor3f(.3f,.3f,.3f);
657 DrawWire<NMPerVert,CMNone>();
658 glPopAttrib();
659 //glDepthRange(0,1.0f);
660 }
661
662 template <NormalMode nm, ColorMode cm>
663 void DrawRadar()
664 {
665 const float ZTWIST=HNParamf[HNPZTwist];
666 glEnable(GL_BLEND);
667 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
668 glDepthMask(0);
669 glDepthRange(ZTWIST,1.0f);
670
671 if (cm == CMNone)
672 glColor4f(0.2f, 1.0f, 0.4f, 0.2f);
673 // DrawFill<nm,cm,TMNone>();
674 Draw<DMFlat,CMNone,TMNone>();
675
676 glDepthMask(1);
677 glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
678 // DrawFill<nm,cm,TMNone>();
679 Draw<DMFlat,CMNone,TMNone>();
680
681 glDepthRange(0.0f,1.0f-ZTWIST);
682 glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
683 glColor4f(0.1f, 1.0f, 0.2f, 0.6f);
684 Draw<DMWire,CMNone,TMNone>();
685 glDisable(GL_BLEND);
686 glDepthRange(0,1.0f);
687
688 }
689
690
691
692 #ifdef GL_TEXTURE0_ARB
693 // Multitexturing nel caso voglia usare due texture unit.
694 void DrawTexture_NPV_TPW2()
695 {
696 unsigned int texname=(*(m->face.begin())).WT(0).n(0);
697 glBindTexture(GL_TEXTURE_2D,TMId[texname]);
698 typename MESH_TYPE::FaceIterator fi;
699 glBegin(GL_TRIANGLES);
700 for(fi=m->face.begin();fi!=m->face.end();++fi)if(!(*fi).IsD()){
701 if(texname!=(*fi).WT(0).n(0)) {
702 texname=(*fi).WT(0).n(0);
703 glEnd();
704 glBindTexture(GL_TEXTURE_2D,TMId[texname]);
705 glBegin(GL_TRIANGLES);
706 }
707 glMultiTexCoordARB(GL_TEXTURE0_ARB, (*fi).WT(0).t(0));
708 glMultiTexCoordARB(GL_TEXTURE1_ARB, (*fi).WT(0).t(0));
709 glNormal((*fi).V(0)->N());
710 glVertex((*fi).V(0)->P());
711
712 glMultiTexCoordARB(GL_TEXTURE0_ARB, (*fi).WT(1).t(0));
713 glMultiTexCoordARB(GL_TEXTURE1_ARB, (*fi).WT(1).t(0));
714 glNormal((*fi).V(1)->N());
715 glVertex((*fi).V(1)->P());
716
717 glMultiTexCoordARB(GL_TEXTURE0_ARB, (*fi).WT(2).t(0));
718 glMultiTexCoordARB(GL_TEXTURE1_ARB, (*fi).WT(2).t(0));
719 glNormal((*fi).V(2)->N());
720 glVertex((*fi).V(2)->P());
721 }
722 glEnd();
723 }
724
725 #endif
726
727
728 /*int MemUsed()
729 {
730 int tot=sizeof(GlTrimesh);
731 tot+=sizeof(mesh_type::edge_type)*edge.size();
732 tot+=sizeof(MESH_TYPE::VertexType *) * EStrip.size();
733 tot+=sizeof(MESH_TYPE::VertexType *) * TStrip.size();
734 tot+=sizeof(MESH_TYPE::FaceType *) * TStripF.size();
735 return tot;
736 }*/
737
738 private:
739
740 template <NormalMode nm, ColorMode cm>
741 void DrawWire()
742 {
743 //if(!(curr_hints & (HNUseEdgeStrip | HNUseLazyEdgeStrip) ) )
744 // {
745 glPushAttrib(GL_POLYGON_BIT);
746 glPolygonMode(GL_FRONT_AND_BACK ,GL_LINE);
747 DrawFill<nm,cm,TMNone>();
748 glPopAttrib();
749 // }
750 //else
751 // {
752 // if(!HasEdges()) ComputeEdges();
753
754 //if(cm==CMPerMesh) glColor(m->C());
755 //std::vector< MESH_TYPE::VertexType *>::iterator vi;
756 //glBegin(GL_LINE_STRIP);
757 //for(vi=EStrip.begin();vi!=EStrip.end(); ++vi){
758 // if((*vi)){
759 // glNormal((*vi)->N());
760 // glVertex((*vi)->P());
761 // }
762 // else
763 // {
764 // glEnd();
765 // glBegin(GL_LINE_STRIP);
766 // }
767 //}
768 //glEnd();
769 // }
770 }
771
772 void DrawBBox(ColorMode cm)
773 {
774 if(cm==CMPerMesh) glColor(m->C());
775 glBoxWire(m->bbox);
776 }
777
778
779 };// end class
780
781 /*
782 Crease Angle
783 Assume che:
784 la mesh abbia la topologia ff
785 la mesh non abbia complex (o se li aveva fossero stati detached)
786 Abbia le normali per faccia normalizzate!!
787
788
789 Prende una mesh e duplica tutti gli edge le cui normali nelle facce incidenti formano un angolo maggiore
790 di <angle> (espresso in rad).
791 foreach face
792 foreach unvisited vert vi
793 scan the star of triangles around vi duplicating vi each time we encounter a crease angle.
794
795 the new (and old) vertexes are put in a std::vector that is swapped with the original one at the end.
796 */
797 // uncomment one of the following line to enable the Verbose Trace for Crease
798 #define VCTRACE (void)0
799 //#define VCTRACE TRACE
800
801 template<class MESH_TYPE>
802 void Crease(MESH_TYPE &m, typename MESH_TYPE::scalar_type angleRad)
803 {
804 assert(m.HasFFTopology());
805 typename MESH_TYPE::scalar_type cosangle=Cos(angleRad);
806
807 std::vector<GLW::VertToSplit<MESH_TYPE> > SPL;
808 std::vector<typename MESH_TYPE::VertexType> newvert;
809 newvert.reserve(m.fn*3);
810 // indica se un il vertice z della faccia e' stato processato
811 enum {VISITED_0= MESH_TYPE::FaceType::USER0,
812 VISITED_1= MESH_TYPE::FaceType::USER0<<1,
813 VISITED_2= MESH_TYPE::FaceType::USER0<<2} ;
814 int vis[3]={VISITED_0,VISITED_1,VISITED_2};
815
816 int _t2=clock();
817 typename MESH_TYPE::FaceIterator fi;
818 for(fi=m.face.begin();fi!=m.face.end();++fi)
819 if(!(*fi).IsD()) (*fi).Supervisor_Flags()&= (~(VISITED_0 | VISITED_1 | VISITED_2));
820
821 for(fi=m.face.begin();fi!=m.face.end();++fi)
822 if(!(*fi).IsD())
823 for(int j=0;j<3;++j)
824 if(!((*fi).Supervisor_Flags() & (vis[j])))
825 {
826 //VCTRACE("Face %i Spinning around vertex %i\n",fi-m.face.begin(), (*fi).V(j)-m.vert.begin());
827 //(*fi).Supervisor_Flags() |= vis[j];
828 typename MESH_TYPE::hedgepos_type he(&*fi,j,(*fi).V(j));
829 typename MESH_TYPE::hedgepos_type she=he;
830 typename MESH_TYPE::face_base_pointer nextf;
831 GLW::VertToSplit<MESH_TYPE> spl;
832 spl.newp=false;
833 spl.edge=-1;
834
835 //Primo giro per trovare un bordo da cui partire
836 do {
837 he.FlipF();
838 he.FlipE();
839 if(he.IsBorder()) break;
840 } while(he!=she);
841 if(he==she) // non c'e'bordi allora si cerca un crease
842 {
843 do {
844 he.FlipF();
845 he.FlipE();
846 nextf=he.f->F(he.z);
847 typename MESH_TYPE::scalar_type ps=nextf->N()*he.f->N();
848 if(ps<cosangle) break;
849 int vz=0;
850 if(he.v == he.f->V(he.z)) vz=he.z;
851 if(he.v == he.f->V((he.z+1)%3)) vz=(he.z+1)%3;
852 assert((he.f->Supervisor_Flags() & vis[vz] )==0);
853 } while(he!=she);
854 }
855 he.FlipE();
856
857 she=he;
858 newvert.push_back(*(*fi).V(j));
859 typename MESH_TYPE::vertex_pointer curvert=&newvert.back();
860 // VCTRACE("Starting from face %i edge %i vert %i \n",he.f-m.face.begin(), he.z, he.v-m.vert.begin());
861
862 // Secondo giro in cui si riempie il vettore SPL con tutte le info per fare i nuovi vertici
863 do{
864 //TRACE(" -- spinning face %i edge %i vert %i\n",he.f-m.face.begin(), he.z, he.v-m.vert.begin());
865 spl.v=curvert;
866 spl.f=he.f;
867 spl.z=-1;
868 if(he.v == he.f->V(he.z)) spl.z=he.z;
869 if(he.v == he.f->V((he.z+1)%3)) spl.z=(he.z+1)%3;
870 assert(spl.z>=0);
871 //VCTRACE(" -- spinning face vert %i Adding spl face %i vert %i\n",
872 // he.v-m.vert.begin(), spl.f-m.face.begin(), spl.z );
873 assert((spl.f->Supervisor_Flags() & vis[spl.z] )==0);
874 spl.f->Supervisor_Flags() |= vis[spl.z];
875 SPL.push_back(spl);
876 spl.newp=false;
877 spl.edge=-1;
878 if(he.IsBorder()) break;
879 nextf=he.f->F(he.z);
880 if(nextf==she.f) break;
881 typename MESH_TYPE::scalar_type ps=nextf->N()*he.f->N();
882 if(ps<cosangle){
883 // VCTRACE("splitting faces %i-%i edge %i vert %i\n",nextf-m.face.begin(),he.f-m.face.begin(), he.z, he.v-m.vert.begin());
884 newvert.push_back(*(he.v));
885 curvert=&newvert.back();
886 spl.newp=true;
887 //spl.edge=he.z;
888 }
889 he.FlipF();
890 if(spl.newp) spl.edge=he.z;
891 he.FlipE();
892
893 }while(he!=she);
894 }
895 assert(SPL.size()==m.fn*3);
896
897 typename std::vector<GLW::VertToSplit<MESH_TYPE> >::iterator vsi;
898 for(vsi=SPL.begin();vsi!=SPL.end();++vsi)
899 {
900 (*vsi).f->V((*vsi).z)=(*vsi).v;
901 if((*vsi).newp){
902 assert((*vsi).edge>=0 && (*vsi).edge<3);
903 if(!(*vsi).f->IsBorder( (*vsi).edge) )
904 (*vsi).f->Detach((*vsi).edge);
905
906 }
907 }
908
909 m.vert.math::Swap(newvert);
910 m.vn=m.vert.size();
911 }
912
913 /*
914 Secondo tipo di crease angle. ha bisogno del per wedge normal
915 e delle adiacence per vertice faccia gia fatte;
916 Assume che le normali per faccia siano gia'state fatte (se ci sono)
917 */
918
919 /*template<class MESH_TYPE>
920 void CreaseWN(MESH_TYPE &m, typename MESH_TYPE::scalar_type angle)
921 {
922 if(!(MESH_TYPE::FaceType::OBJ_TYPE & MESH_TYPE::FaceType::OBJ_TYPE_WN) )
923 {
924 assert(0); // You needs a mesh with faces having per wedge normals
925 return;
926 }
927
928 typename MESH_TYPE::scalar_type cosangle=Cos(angle);
929
930 typename MESH_TYPE::FaceIterator fi;
931
932 // Clear the per wedge normals
933 for(fi=m.face.begin();fi!=m.face.end();++fi) if(!(*fi).IsD())
934 {
935 (*fi).WN(0)=MESH_TYPE::vectorial_type(0,0,0);
936 (*fi).WN(1)=MESH_TYPE::vectorial_type(0,0,0);
937 (*fi).WN(2)=MESH_TYPE::vectorial_type(0,0,0);
938 }
939
940 typename MESH_TYPE::FaceType::vectorial_type nn;
941
942 for(fi=m.face.begin();fi!=m.face.end();++fi) if(!(*fi).IsD())
943 {
944 nn=(*fi).cN();
945 for(int i=0;i<3;++i)
946 {
947 VEdgePosB<MESH_TYPE::FaceType::face_base> x;
948 for(x.f = (*fi).V(i)->Fp(), x.z = (*fi).V(i)->Zp(); x.f!=0; x.NextF() ) {
949 assert(x.f->V(x.z)==(*fi).V(i));
950 if(x.f->cN()*nn > cosangle) x.f->WN(x.z)+=nn;
951 }
952 }
953 }
954
955 }*/
956
957 } // end namespace
958
959 #endif