trimesh.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 #ifndef __VCG_GLTRIMESH
00025 #define __VCG_GLTRIMESH
00026 
00027 #include <queue>
00028 #include <vector>
00029 #include <map>
00030 #include <algorithm>
00031 #include <iostream>
00032 
00033 //#include <GL/glew.h>
00034 #include <wrap/gl/space.h>
00035 #include <wrap/gl/math.h>
00036 #include <vcg/space/color4.h>
00037 
00038 
00039 namespace vcg {
00040 
00041         template <typename T>
00042         class GL_TYPE_NM
00043         {public:
00044         static GLenum SCALAR() { assert(0); return 0;}
00045         };
00046         template <> class GL_TYPE_NM<float>
00047         { public:
00048         typedef GLfloat ScalarType;
00049         static GLenum SCALAR() { return GL_FLOAT; }
00050         };
00051         template <> class GL_TYPE_NM<double>
00052         {public:
00053         typedef GLdouble ScalarType;
00054         static GLenum SCALAR() { return GL_DOUBLE; }
00055         };
00056 
00057         //template <> GL_TYPE::SCALAR<double>() { return GL_DOUBLE; }
00058 
00059         // classe base di glwrap usata solo per poter usare i vari drawmode, normalmode senza dover
00060         // specificare tutto il tipo (a volte lunghissimo)
00061         // della particolare classe glwrap usata.
00062         class GLW
00063         {
00064         public:
00065                 enum DrawMode   {DMNone, DMBox, DMPoints, DMWire, DMHidden, DMFlat, DMSmooth, DMFlatWire, DMRadar, DMLast} ;
00066                 enum NormalMode {NMNone, NMPerVert, NMPerFace, NMPerWedge, NMLast};
00067                 enum ColorMode  {CMNone, CMPerMesh, CMPerFace, CMPerVert, CMLast};
00068                 enum TextureMode{TMNone, TMPerVert, TMPerWedge, TMPerWedgeMulti};
00069                 enum Hint {
00070                         HNUseTriStrip             = 0x0001,                             // ha bisogno che ci sia la fftopology gia calcolata!
00071                         //              HNUseEdgeStrip            = 0x0002,                     //
00072                         HNUseDisplayList          = 0x0004,
00073                         HNCacheDisplayList        = 0x0008,             // Each mode has its dl;
00074                         HNLazyDisplayList         = 0x0010,                     // Display list are generated only when requested
00075                         HNIsTwoManifold           = 0x0020,                     // There is no need to make DetachComplex before .
00076                         HNUsePerWedgeNormal       = 0x0040,             //
00077                         HNHasFFTopology       = 0x0080,         // E' l'utente che si preoccupa di tenere aggiornata la topologia ff
00078                         HNHasVFTopology       = 0x0100,         // E' l'utente che si preoccupa di tenere aggiornata la topologia vf
00079                         HNHasVertNormal       = 0x0200,         // E' l'utente che si preoccupa di tenere aggiornata le normali per faccia
00080                         HNHasFaceNormal       = 0x0400,         // E' l'utente che si preoccupa di tenere aggiornata le normali per vertice
00081                         HNUseVArray           = 0x0800,
00082                         HNUseLazyEdgeStrip        = 0x1000,             // Edge Strip are generated only when requested
00083                         HNUseVBO              = 0x2000,         // Use Vertex Buffer Object
00084                         HNIsPolygonal         = 0x4000    // In wireframe modes, hide faux edges
00085                 };
00086 
00087                 enum Change {
00088                         CHVertex                = 0x01,
00089                         CHNormal                = 0x02,
00090                         CHColor                 = 0x04,
00091                         CHFace                  = 0x08,
00092                         CHFaceNormal    = 0x10,
00093                         CHRender        = 0x20,
00094                         CHAll                   = 0xff
00095                 };
00096                 enum HintParami {
00097                         HNPDisplayListSize =0,
00098                         HNPPointDistanceAttenuation =1,
00099                         HNPPointSmooth = 2
00100                 };
00101                 enum HintParamf {
00102                         HNPCreaseAngle =0,      // crease angle in radians
00103                         HNPZTwist = 1,                          // Z offset used in Flatwire and hiddenline modality
00104                         HNPPointSize = 2                // the point size used in point rendering
00105                 };
00106 
00107                 template<class MeshType>
00108                 class VertToSplit
00109                 {
00110                 public:
00111                         typename MeshType::face_base_pointer f;
00112                         char z;
00113                         char edge;
00114                         bool newp;
00115                         typename MeshType::vertex_pointer v;
00116                 };
00117 
00118                 // GL Array Elemet
00119                 class GLAElem {
00120                 public :
00121                         int glmode;
00122                         int len;
00123                         int start;
00124                 };
00125 
00126                 std::vector<unsigned int> TMId;
00127         };
00128 
00129         template <class MeshType,  bool partial = false , class FACE_POINTER_CONTAINER = std::vector<typename MeshType::FacePointer> >
00130         class GlTrimesh : public GLW
00131         {
00132         public:
00133 
00134                 typedef typename MeshType::VertexType VertexType;
00135                 typedef typename MeshType::FaceType FaceType;
00136                 typedef typename MeshType::VertexType::CoordType CoordType;
00137                 typedef typename MeshType::VertexType::ScalarType ScalarType;
00138                 typedef typename MeshType::VertexIterator VertexIterator;
00139                 typedef typename MeshType::EdgeIterator EdgeIterator;
00140                 typedef typename MeshType::FaceIterator FaceIterator;
00141 
00142                 FACE_POINTER_CONTAINER face_pointers;
00143 
00144                 MeshType *m;
00145                 unsigned int array_buffers[3];
00146 
00147                 int curr_hints;      // the current hints
00148 
00149                 // The parameters of hints
00150                 int   HNParami[8];
00151                 float HNParamf[8];
00152 
00153                 DrawMode cdm; // Current DrawMode
00154                 NormalMode cnm; // Current NormalMode
00155                 ColorMode ccm; // Current ColorMode
00156 
00157                 static NormalMode convertDrawModeToNormalMode(DrawMode dm)
00158                 {
00159                         switch(dm)
00160                         {
00161                         case(DMFlat):
00162                         case(DMFlatWire):
00163                         case(DMRadar):
00164                                 return NMPerFace;
00165                         case(DMPoints):
00166                         case(DMWire):
00167                         case(DMSmooth):
00168                                 return NMPerVert;
00169                         default:
00170                                 return NMNone;
00171                         }
00172                         return NMNone;
00173                 }
00174 
00175                 GlTrimesh()
00176                 {
00177                         m=0;
00178                         dl=0xffffffff;
00179                         curr_hints=HNUseLazyEdgeStrip;
00180                         cdm=DMNone;
00181                         ccm=CMNone;
00182                         cnm=NMNone;
00183 
00184                         SetHintParamf(HNPCreaseAngle,float(M_PI/5));
00185                         SetHintParamf(HNPZTwist,0.00005f);
00186                         SetHintParamf(HNPPointSize,1.0f);
00187                         SetHintParami(HNPPointDistanceAttenuation, 1);
00188                         SetHintParami(HNPPointSmooth, 0);
00189                 }
00190 
00191                 ~GlTrimesh()
00192                 {
00193                         //Delete the VBOs
00194                         if(curr_hints&HNUseVBO)
00195                         {
00196                                 for(int i=0;i<3;++i)
00197                                         if(glIsBuffer(GLuint(array_buffers[i])))
00198                                                 glDeleteBuffersARB(1, (GLuint *)(array_buffers+i));
00199                         }
00200                 }
00201 
00202                 unsigned int dl;
00203                 std::vector<unsigned int> indices;
00204 
00205                 void SetHintParami(const HintParami hip, const int value)
00206                 {
00207                         HNParami[hip]=value;
00208                 }
00209                 int GetHintParami(const HintParami hip) const
00210                 {
00211                         return HNParami[hip];
00212                 }
00213                 void SetHintParamf(const HintParamf hip, const float value)
00214                 {
00215                         HNParamf[hip]=value;
00216                 }
00217                 float GetHintParamf(const HintParamf hip) const
00218                 {
00219                         return HNParamf[hip];
00220                 }
00221                 void SetHint(Hint hn)
00222                 {
00223                         curr_hints |= hn;
00224                 }
00225                 void ClearHint(Hint hn)
00226                 {
00227                         curr_hints&=(~hn);
00228                 }
00229 
00230                 void Update(/*Change c=CHAll*/)
00231                 {
00232                         if(m==0) return;
00233 
00234                         if(curr_hints&HNUseVArray || curr_hints&HNUseVBO)
00235                         {
00236                                 indices.clear();
00237                                 for(FaceIterator fi = m->face.begin(); fi != m->face.end(); ++fi)
00238                                 {
00239                                         indices.push_back((unsigned int)((*fi).V(0) - &(*m->vert.begin())));
00240                                         indices.push_back((unsigned int)((*fi).V(1) - &(*m->vert.begin())));
00241                                         indices.push_back((unsigned int)((*fi).V(2) - &(*m->vert.begin())));
00242                                 }
00243 
00244                                 if(curr_hints&HNUseVBO)
00245                                 {
00246                                         if(!glIsBuffer(array_buffers[1]))
00247                                                 glGenBuffers(2,(GLuint*)array_buffers);
00248                                         glBindBuffer(GL_ARRAY_BUFFER,array_buffers[0]);
00249                                         glBufferData(GL_ARRAY_BUFFER_ARB, m->vn * sizeof(VertexType),
00250                                                 (char *)&(m->vert[0].P()), GL_STATIC_DRAW_ARB);
00251 
00252                                         glBindBuffer(GL_ARRAY_BUFFER,array_buffers[1]);
00253                                         glBufferData(GL_ARRAY_BUFFER_ARB, m->vn * sizeof(VertexType),
00254                                                 (char *)&(m->vert[0].N()), GL_STATIC_DRAW_ARB);
00255                                 }
00256 
00257                                 glVertexPointer(3,GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(VertexType),0);
00258                                 glNormalPointer(GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(VertexType),0);
00259                         }
00260 
00261                         //int C=c;
00262                         //if((C&CHVertex) || (C&CHFace)) {
00263                         //      ComputeBBox(*m);
00264                         //      if(!(curr_hints&HNHasFaceNormal)) m->ComputeFaceNormal();
00265                         //      if(!(curr_hints&HNHasVertNormal)) m->ComputeVertexNormal();
00266                         //      C= (C | CHFaceNormal);
00267                         //}
00268                         //if((C&CHFace) && (curr_hints&HNUseEdgeStrip))                  ComputeEdges();
00269                         //if((C&CHFace) && (curr_hints&HNUseLazyEdgeStrip)) ClearEdges();
00270                         //if(MeshType::HasFFTopology())
00271                         //      if((C&CHFace) && (curr_hints&HNUseTriStrip))            {
00272                         //                      if(!(curr_hints&HNHasFFTopology)) m->FFTopology();
00273                         //                      ComputeTriStrip();
00274                         //              }
00275                         //if((C&CHFaceNormal) && (curr_hints&HNUsePerWedgeNormal))              {
00276                         //        if(!(curr_hints&HNHasVFTopology)) m->VFTopology();
00277                         //              CreaseWN(*m,MeshType::scalar_type(GetHintParamf(HNPCreaseAngle)));
00278                         //}
00279                         //if(C!=0) { // force the recomputation of display list
00280                         //      cdm=DMNone;
00281                         //      ccm=CMNone;
00282                         //      cnm=NMNone;
00283                         //}
00284                         //if((curr_hints&HNUseVArray) && (curr_hints&HNUseTriStrip))
00285                         //      {
00286                         //       ConvertTriStrip<MeshType>(*m,TStrip,TStripF,TStripVED,TStripVEI);
00287                         //      }
00288                 }
00289 
00290                 void Draw(DrawMode dm ,ColorMode cm, TextureMode tm)
00291                 {
00292                         switch(dm)
00293                         {
00294                         case    DMNone    : Draw<DMNone    >(cm,tm); break;
00295                         case    DMBox     : Draw<DMBox     >(cm,tm); break;
00296                         case    DMPoints  : Draw<DMPoints  >(cm,tm); break;
00297                         case    DMWire    : Draw<DMWire    >(cm,tm); break;
00298                         case    DMHidden  : Draw<DMHidden  >(cm,tm); break;
00299                         case    DMFlat    : Draw<DMFlat    >(cm,tm); break;
00300                         case    DMSmooth  : Draw<DMSmooth  >(cm,tm); break;
00301                         case    DMFlatWire: Draw<DMFlatWire>(cm,tm); break;
00302                         default : break;
00303                         }
00304                 }
00305 
00306                 template< DrawMode dm >
00307                 void Draw(ColorMode cm, TextureMode tm)
00308                 {
00309                         switch(cm)
00310                         {
00311                         case    CMNone    : Draw<dm,CMNone   >(tm); break;
00312                         case    CMPerMesh : Draw<dm,CMPerMesh>(tm); break;
00313                         case    CMPerFace : Draw<dm,CMPerFace>(tm); break;
00314                         case    CMPerVert : Draw<dm,CMPerVert>(tm); break;
00315                         default : break;
00316                         }
00317                 }
00318 
00319                 template< DrawMode dm, ColorMode cm >
00320                 void Draw(TextureMode tm)
00321                 {
00322                         switch(tm)
00323                         {
00324                         case    TMNone          : Draw<dm,cm,TMNone          >(); break;
00325                         case    TMPerVert       : Draw<dm,cm,TMPerVert       >(); break;
00326                         case    TMPerWedge      : Draw<dm,cm,TMPerWedge      >(); break;
00327                         case    TMPerWedgeMulti : Draw<dm,cm,TMPerWedgeMulti >(); break;
00328                         default : break;
00329                         }
00330                 }
00331 
00332                 template< DrawMode dm, ColorMode cm, TextureMode tm>
00333                 void Draw()
00334                 {
00335                         if(!m) return;
00336                         if((curr_hints & HNUseDisplayList)){
00337                                 if (cdm==dm && ccm==cm){
00338                                         glCallList(dl);
00339                                         return;
00340                                 }
00341                                 else {
00342                                         if(dl==0xffffffff) dl=glGenLists(1);
00343                                         glNewList(dl,GL_COMPILE);
00344                                 }
00345                         }
00346 
00347                         glPushMatrix();
00348                         switch(dm)
00349                         {
00350                         case DMNone               : break;
00351                         case DMBox                : DrawBBox(cm);break;
00352                         case DMPoints   : DrawPoints<NMPerVert,cm>();break;
00353                         case DMHidden           :       DrawHidden();break;
00354                         case DMFlat                     :       DrawFill<NMPerFace,cm,tm>();break;
00355                         case DMFlatWire :       DrawFlatWire<NMPerFace,cm,tm>();break;
00356                         case DMRadar            :       DrawRadar<NMPerFace,cm>();break;
00357                         case DMWire               :     DrawWire<NMPerVert,cm>();break;
00358                         case DMSmooth   : DrawFill<NMPerVert,cm,tm>();break;
00359                         default : break;
00360                         }
00361                         glPopMatrix();
00362 
00363                         if((curr_hints & HNUseDisplayList)){
00364                                 cdm=dm;
00365                                 ccm=cm;
00366                                 glEndList();
00367                                 glCallList(dl);
00368                         }
00369                 }
00370 
00371 
00372                 /*********************************************************************************************/
00373                 /*********************************************************************************************/
00374 
00375 
00376                 template <NormalMode nm, ColorMode cm, TextureMode tm>
00377                 void DrawFill()
00378                 {
00379                         if(m->fn==0) return;
00380 
00381                         if(cm == CMPerMesh)
00382                                 glColor(m->C());
00383 
00384                         if(tm == TMPerWedge || tm == TMPerWedgeMulti )
00385                                 glDisable(GL_TEXTURE_2D);
00386 
00387                         if(curr_hints&HNUseVBO)
00388                         {
00389                                 if( (cm==CMNone) || (cm==CMPerMesh) )
00390                                 {
00391                                         if (nm==NMPerVert)
00392                                                 glEnableClientState (GL_NORMAL_ARRAY);
00393                                         glEnableClientState (GL_VERTEX_ARRAY);
00394 
00395                                         if (nm==NMPerVert)
00396                                         {
00397                                                 glBindBuffer(GL_ARRAY_BUFFER,array_buffers[1]);
00398                                                 glNormalPointer(GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(typename MeshType::VertexType),0);
00399                                         }
00400                                         glBindBuffer(GL_ARRAY_BUFFER,array_buffers[0]);
00401                                         glVertexPointer(3,GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(typename MeshType::VertexType),0);
00402 
00403                                         glDrawElements(GL_TRIANGLES ,m->fn*3,GL_UNSIGNED_INT, &(*indices.begin()) );
00404                                         glDisableClientState (GL_VERTEX_ARRAY);
00405                                         if (nm==NMPerVert)
00406                                                 glDisableClientState (GL_NORMAL_ARRAY);
00407 
00408                                         glBindBuffer(GL_ARRAY_BUFFER, 0);
00409 
00410                                         return;
00411 
00412                                 }
00413                         }
00414 
00415                         if(curr_hints&HNUseVArray)
00416                         {
00417                                 if( (cm==CMNone) || (cm==CMPerMesh) )
00418                                 {
00419                                         if (nm==NMPerVert)
00420                                                 glEnableClientState (GL_NORMAL_ARRAY);
00421                                         glEnableClientState (GL_VERTEX_ARRAY);
00422 
00423                                         if (nm==NMPerVert)
00424                                                 glNormalPointer(GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(typename MeshType::VertexType),&(m->vert.begin()->N()[0]));
00425                                         glVertexPointer(3,GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(typename MeshType::VertexType),&(m->vert.begin()->P()[0]));
00426 
00427                                         glDrawElements(GL_TRIANGLES ,m->fn*3,GL_UNSIGNED_INT, &(*indices.begin()) );
00428                                         glDisableClientState (GL_VERTEX_ARRAY);
00429                                         if (nm==NMPerVert)
00430                                                 glDisableClientState (GL_NORMAL_ARRAY);
00431 
00432                                         return;
00433                                 }
00434                         }
00435                         else
00436 
00437                                 if(curr_hints&HNUseTriStrip)
00438                                 {
00439                                         //if( (nm==NMPerVert) && ((cm==CMNone) || (cm==CMPerMesh)))
00440                                         //      if(curr_hints&HNUseVArray){
00441                                         //              glEnableClientState (GL_NORMAL_ARRAY  );
00442                                         //              glNormalPointer(GL_FLOAT,sizeof(MeshType::VertexType),&(m->vert[0].cN()));
00443                                         //              glEnableClientState (GL_VERTEX_ARRAY);
00444                                         //              glVertexPointer(3,GL_FLOAT,sizeof(MeshType::VertexType),&(m->vert[0].cP()));
00445                                         //              std::vector<GLAElem>::iterator vi;
00446                                         //              for(vi=TStripVED.begin();vi!=TStripVED.end();++vi)
00447                                         //                                      glDrawElements(vi->glmode ,vi->len,GL_UNSIGNED_SHORT,&TStripVEI[vi->start] );
00448                                         //
00449                                         //              glDisableClientState (GL_NORMAL_ARRAY  );
00450                                         //              glDisableClientState (GL_VERTEX_ARRAY);
00451                                         //              return;
00452                                         //      }
00453 
00454                                         //std::vector< MeshType::VertexType *>::iterator vi;
00455                                         //glBegin(GL_TRIANGLE_STRIP);
00456                                         //if(nm == NMPerFace) fip=TStripF.begin();
00457 
00458                                         //for(vi=TStrip.begin();vi!=TStrip.end(); ++vi){
00459                                         //      if((*vi)){
00460                                         //              if(nm==NMPerVert) glNormal((*vi)->cN());
00461                                         //              if(nm==NMPerFace) glNormal((*fip)->cN());
00462                                         //              glVertex((*vi)->P());
00463                                         //              }
00464                                         //      else
00465                                         //              {
00466                                         //                      glEnd();
00467                                         //                      glBegin(GL_TRIANGLE_STRIP);
00468                                         //              }
00469                                         //      if(nm == NMPerFace) ++fip;
00470                                         //      }
00471                                         //glEnd();
00472                                 }
00473                                 else
00474                                 {
00475                                         typename FACE_POINTER_CONTAINER::iterator fp;
00476                                         FaceIterator fi;
00477 
00478                                         short curtexname=-1;
00479                                         if(partial)
00480                                                 fp = face_pointers.begin();
00481                                         else
00482                                                 fi = m->face.begin();
00483 
00484                                         if(tm==TMPerWedgeMulti)
00485                                         {
00486                                                 curtexname=(*fi).WT(0).n();
00487                                                 if ((curtexname >= 0) && (curtexname < (int)TMId.size()))
00488                                                 {
00489                                                         glEnable(GL_TEXTURE_2D);
00490                                                         glBindTexture(GL_TEXTURE_2D,TMId[curtexname]);
00491                                                 }
00492                                                 else
00493                                                 {
00494                                                         glDisable(GL_TEXTURE_2D);
00495                                                 }
00496                                         }
00497 
00498                                         if(tm==TMPerWedge)
00499                                                 glEnable(GL_TEXTURE_2D);
00500 
00501                                         if(tm==TMPerVert && !TMId.empty()) // in the case of per vertex tex coord we assume that we have a SINGLE texture.
00502                                         {
00503                                                 curtexname = 0;
00504                                                 glEnable(GL_TEXTURE_2D);
00505                                                 glBindTexture(GL_TEXTURE_2D,TMId[curtexname]);
00506                                         }
00507                                         glBegin(GL_TRIANGLES);
00508 
00509                                         while( (partial)?(fp!=face_pointers.end()):(fi!=m->face.end()))
00510                                         {
00511                                                 FaceType & f = (partial)?(*(*fp)): *fi;
00512 
00513                                                 if(!f.IsD())
00514                                                 {
00515                                                         if(tm==TMPerWedgeMulti)
00516                                                                 if(f.WT(0).n() != curtexname)
00517                                                                 {
00518                                                                         curtexname=(*fi).WT(0).n();
00519                                                                         glEnd();
00520 
00521                                                                         if (curtexname >= 0)
00522                                                                         {
00523                                                                                 glEnable(GL_TEXTURE_2D);
00524                                                                                 if(!TMId.empty())
00525                                                                                         glBindTexture(GL_TEXTURE_2D,TMId[curtexname]);
00526                                                                         }
00527                                                                         else
00528                                                                         {
00529                                                                                 glDisable(GL_TEXTURE_2D);
00530                                                                         }
00531 
00532                                                                         glBegin(GL_TRIANGLES);
00533                                                                 }
00534 
00535                                                                 if(nm == NMPerFace)     glNormal(f.cN());
00536                                                                 if(nm == NMPerVert)     glNormal(f.V(0)->cN());
00537                                                                 if(nm == NMPerWedge)glNormal(f.WN(0));
00538 
00539                                                                 if(cm == CMPerFace)     glColor(f.C());
00540                                                                 if(cm == CMPerVert)     glColor(f.V(0)->C());
00541                                                                 if(tm==TMPerVert) glTexCoord(f.V(0)->T().P());
00542                                                                 if( (tm==TMPerWedge)||(tm==TMPerWedgeMulti) )glTexCoord(f.WT(0).t(0));
00543                                                                 glVertex(f.V(0)->P());
00544 
00545                                                                 if(nm == NMPerVert)     glNormal(f.V(1)->cN());
00546                                                                 if(nm == NMPerWedge)glNormal(f.WN(1));
00547                                                                 if(cm == CMPerVert)     glColor(f.V(1)->C());
00548                                                                 if(tm==TMPerVert) glTexCoord(f.V(1)->T().P());
00549                                                                 if( (tm==TMPerWedge)|| (tm==TMPerWedgeMulti)) glTexCoord(f.WT(1).t(0));
00550                                                                 glVertex(f.V(1)->P());
00551 
00552                                                                 if(nm == NMPerVert)     glNormal(f.V(2)->cN());
00553                                                                 if(nm == NMPerWedge)glNormal(f.WN(2));
00554                                                                 if(cm == CMPerVert) glColor(f.V(2)->C());
00555                                                                 if(tm==TMPerVert) glTexCoord(f.V(2)->T().P());
00556                                                                 if( (tm==TMPerWedge)|| (tm==TMPerWedgeMulti)) glTexCoord(f.WT(2).t(0));
00557                                                                 glVertex(f.V(2)->P());
00558                                                 }
00559 
00560                                                 if(partial)
00561                                                         ++fp;
00562                                                 else
00563                                                         ++fi;
00564                                         }
00565 
00566                                         glEnd();
00567 
00568                                 }
00569                 }
00570 
00571                 // A draw wireframe that hides faux edges
00572                 template <NormalMode nm, ColorMode cm>
00573                 void DrawWirePolygonal()
00574                 {
00575                         if(cm == CMPerMesh)   glColor(m->C());
00576                         FaceIterator fi;
00577                         typename FACE_POINTER_CONTAINER::iterator fp;
00578 
00579                         if(partial)
00580                                 fp = face_pointers.begin();
00581                         else
00582                                 fi = m->face.begin();
00583 
00584                         glBegin(GL_LINES);
00585 
00586                         while( (partial)?(fp!=face_pointers.end()):(fi!=m->face.end()))
00587                         {
00588                                 typename MeshType::FaceType & f = (partial)?(*(*fp)): *fi;
00589 
00590                                 if(!f.IsD())
00591                                 {
00592 
00593                                         if(nm == NMPerFace)     glNormal(f.cN());
00594                                         if(cm == CMPerFace)     glColor(f.C());
00595 
00596                                         if (!f.IsF(0)) {
00597                                                 if(nm == NMPerVert)     glNormal(f.V(0)->cN());
00598                                                 if(nm == NMPerWedge)glNormal(f.WN(0));
00599                                                 if(cm == CMPerVert)     glColor(f.V(0)->C());
00600                                                 glVertex(f.V(0)->P());
00601 
00602                                                 if(nm == NMPerVert)     glNormal(f.V(1)->cN());
00603                                                 if(nm == NMPerWedge)glNormal(f.WN(1));
00604                                                 if(cm == CMPerVert)     glColor(f.V(1)->C());
00605                                                 glVertex(f.V(1)->P());
00606                                         }
00607 
00608                                         if (!f.IsF(1)) {
00609                                                 if(nm == NMPerVert)     glNormal(f.V(1)->cN());
00610                                                 if(nm == NMPerWedge)glNormal(f.WN(1));
00611                                                 if(cm == CMPerVert)     glColor(f.V(1)->C());
00612                                                 glVertex(f.V(1)->P());
00613 
00614                                                 if(nm == NMPerVert)     glNormal(f.V(2)->cN());
00615                                                 if(nm == NMPerWedge)glNormal(f.WN(2));
00616                                                 if(cm == CMPerVert)     glColor(f.V(2)->C());
00617                                                 glVertex(f.V(2)->P());
00618                                         }
00619 
00620                                         if (!f.IsF(2)) {
00621                                                 if(nm == NMPerVert)     glNormal(f.V(2)->cN());
00622                                                 if(nm == NMPerWedge)glNormal(f.WN(2));
00623                                                 if(cm == CMPerVert)     glColor(f.V(2)->C());
00624                                                 glVertex(f.V(2)->P());
00625 
00626                                                 if(nm == NMPerVert)     glNormal(f.V(0)->cN());
00627                                                 if(nm == NMPerWedge)glNormal(f.WN(0));
00628                                                 if(cm == CMPerVert)     glColor(f.V(0)->C());
00629                                                 glVertex(f.V(0)->P());
00630                                         }
00631 
00632                                 }
00633 
00634                                 if(partial)
00635                                         ++fp;
00636                                 else
00637                                         ++fi;
00638                         }
00639 
00640                         glEnd();
00641                 }
00642 
00644                 // works also for mesh with deleted vertices
00645                 template<NormalMode nm, ColorMode cm>
00646                 void DrawPointsBase()
00647                 {
00648                         glBegin(GL_POINTS);
00649                         if(cm==CMPerMesh) glColor(m->C());
00650 
00651                         for(VertexIterator vi=m->vert.begin();vi!=m->vert.end();++vi)if(!(*vi).IsD())
00652                         {
00653                                 if(nm==NMPerVert) glNormal((*vi).cN());
00654                                 if(cm==CMPerVert) glColor((*vi).C());
00655                                 glVertex((*vi).P());
00656                         }
00657                         glEnd();
00658                 }
00659 
00661                 double CameraDistance(){
00662                         CoordType res;
00663                         Matrix44<ScalarType> mm;
00664                         glGetv(GL_MODELVIEW_MATRIX,mm);
00665                         CoordType  c=m->bbox.Center();
00666                         res=mm*c;
00667                         return Norm(res);
00668                 }
00669 
00670                 template<NormalMode nm, ColorMode cm>
00671                 void DrawPoints()
00672                 {
00673                         glPushAttrib(GL_ENABLE_BIT | GL_POINT_BIT);
00674                         if(GetHintParami(HNPPointSmooth)>0) glEnable(GL_POINT_SMOOTH);
00675                         else glDisable(GL_POINT_SMOOTH);
00676                         glPointSize(GetHintParamf(HNPPointSize));
00677                         if(GetHintParami(HNPPointDistanceAttenuation)>0)
00678                         {
00679                                 float camDist = (float)CameraDistance();
00680                                 float quadratic[] = { 0.0f, 0.0f, 1.0f/(camDist*camDist) , 0.0f };
00681                                 glPointParameterfv( GL_POINT_DISTANCE_ATTENUATION, quadratic );
00682                                 glPointParameterf( GL_POINT_SIZE_MAX, 16.0f );
00683                                 glPointParameterf( GL_POINT_SIZE_MIN, 1.0f );
00684                         }
00685                         else
00686                         {
00687                                 float quadratic[] = { 1.0f, 0.0f, 0.0f};
00688                                 glPointParameterfv( GL_POINT_DISTANCE_ATTENUATION, quadratic );
00689                                 glPointSize(GetHintParamf(HNPPointSize));
00690                         }
00691 
00692                         if(m->vn!=(int)m->vert.size())
00693                         {
00694                                 DrawPointsBase<nm,cm>();
00695                         }
00696                         else
00697                         {
00698                                 if(cm==CMPerMesh)
00699                                         glColor(m->C());
00700                                 if (m->vert.size() != 0)
00701                                 {
00702                                         // Perfect case, no deleted stuff,
00703                                         // draw the vertices using vertex arrays
00704                                         if (nm==NMPerVert)
00705                                         {
00706                                                 glEnableClientState (GL_NORMAL_ARRAY);
00707                                                 glNormalPointer(GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(VertexType),&(m->vert.begin()->N()[0]));
00708                                         }
00709                                         if (cm==CMPerVert)
00710                                         {
00711                                                 glEnableClientState (GL_COLOR_ARRAY);
00712                                                 glColorPointer(4,GL_UNSIGNED_BYTE,sizeof(typename MeshType::VertexType),&(m->vert.begin()->C()[0]));
00713                                         }
00714 
00715                                         glEnableClientState (GL_VERTEX_ARRAY);
00716                                         glVertexPointer(3,GL_TYPE_NM<ScalarType>::SCALAR(),sizeof(VertexType),&(m->vert.begin()->P()[0]));
00717 
00718                                         glDrawArrays(GL_POINTS,0,m->vn);
00719 
00720                                         glDisableClientState (GL_VERTEX_ARRAY);
00721                                         if (nm==NMPerVert)  glDisableClientState (GL_NORMAL_ARRAY);
00722                                         if (cm==CMPerVert)  glDisableClientState (GL_COLOR_ARRAY);
00723                                 }
00724                         }
00725                         glPopAttrib();
00726                         return;
00727                 }
00728 
00729                 void DrawHidden()
00730                 {
00731                         //const float ZTWIST=HNParamf[HNPZTwist];
00732                         glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT );
00733                         glEnable(GL_POLYGON_OFFSET_FILL);
00734                         glPolygonOffset(1.0, 1);
00735                         //glDepthRange(ZTWIST,1.0f);
00736                         glDisable(GL_LIGHTING);
00737                         glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
00738                         DrawFill<NMNone,CMNone,TMNone>();
00739                         glDisable(GL_POLYGON_OFFSET_FILL);
00740                         glEnable(GL_LIGHTING);
00741                         glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
00742                         //glDepthRange(0.0f,1.0f-ZTWIST);
00743                         DrawWire<NMPerVert,CMNone>();
00744                         glPopAttrib();
00745                         //      glDepthRange(0,1.0f);
00746                 }
00747 
00748                 template <NormalMode nm, ColorMode cm, TextureMode tm>
00749                 void DrawFlatWire()
00750                 {
00751                         //const float ZTWIST=HNParamf[HNPZTwist];
00752                         //glDepthRange(ZTWIST,1.0f);
00753                         glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT );
00754                         glEnable(GL_POLYGON_OFFSET_FILL);
00755                         glPolygonOffset(1.0, 1);
00756                         DrawFill<nm,cm,tm>();
00757                         glDisable(GL_POLYGON_OFFSET_FILL);
00758                         //glDepthRange(0.0f,1.0f-ZTWIST);
00759                         glEnable(GL_COLOR_MATERIAL);
00760                         glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
00761                         //glColorMaterial(GL_FRONT,GL_DIFFUSE);
00762                         glColor3f(.3f,.3f,.3f);
00763                         DrawWire<nm,CMNone>();
00764                         glPopAttrib();
00765                         //glDepthRange(0,1.0f);
00766                 }
00767 
00768                 template <NormalMode nm, ColorMode cm>
00769                 void DrawRadar()
00770                 {
00771                         const float ZTWIST=HNParamf[HNPZTwist];
00772                         glEnable(GL_BLEND);
00773                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00774                         glDepthMask(0);
00775                         glDepthRange(ZTWIST,1.0f);
00776 
00777                         if (cm == CMNone)
00778                                 glColor4f(0.2f, 1.0f, 0.4f, 0.2f);
00779                         //      DrawFill<nm,cm,TMNone>();
00780                         Draw<DMFlat,CMNone,TMNone>();
00781 
00782                         glDepthMask(1);
00783                         glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
00784                         //      DrawFill<nm,cm,TMNone>();
00785                         Draw<DMFlat,CMNone,TMNone>();
00786 
00787                         glDepthRange(0.0f,1.0f-ZTWIST);
00788                         glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
00789                         glColor4f(0.1f, 1.0f, 0.2f, 0.6f);
00790                         Draw<DMWire,CMNone,TMNone>();
00791                         glDisable(GL_BLEND);
00792                         glDepthRange(0,1.0f);
00793 
00794                 }
00795 
00796 
00797 
00798 #ifdef GL_TEXTURE0_ARB
00799                 // Multitexturing nel caso voglia usare due texture unit.
00800                 void DrawTexture_NPV_TPW2()
00801                 {
00802                         unsigned int texname=(*(m->face.begin())).WT(0).n(0);
00803                         glBindTexture(GL_TEXTURE_2D,TMId[texname]);
00804                         typename MeshType::FaceIterator fi;
00805                         glBegin(GL_TRIANGLES);
00806                         for(fi=m->face.begin();fi!=m->face.end();++fi)if(!(*fi).IsD()){
00807                                 if(texname!=(*fi).WT(0).n(0))   {
00808                                         texname=(*fi).WT(0).n(0);
00809                                         glEnd();
00810                                         glBindTexture(GL_TEXTURE_2D,TMId[texname]);
00811                                         glBegin(GL_TRIANGLES);
00812                                 }
00813                                 glMultiTexCoordARB(GL_TEXTURE0_ARB, (*fi).WT(0).t(0));
00814                                 glMultiTexCoordARB(GL_TEXTURE1_ARB, (*fi).WT(0).t(0));
00815                                 glNormal((*fi).V(0)->N());
00816                                 glVertex((*fi).V(0)->P());
00817 
00818                                 glMultiTexCoordARB(GL_TEXTURE0_ARB, (*fi).WT(1).t(0));
00819                                 glMultiTexCoordARB(GL_TEXTURE1_ARB, (*fi).WT(1).t(0));
00820                                 glNormal((*fi).V(1)->N());
00821                                 glVertex((*fi).V(1)->P());
00822 
00823                                 glMultiTexCoordARB(GL_TEXTURE0_ARB, (*fi).WT(2).t(0));
00824                                 glMultiTexCoordARB(GL_TEXTURE1_ARB, (*fi).WT(2).t(0));
00825                                 glNormal((*fi).V(2)->N());
00826                                 glVertex((*fi).V(2)->P());
00827                         }
00828                         glEnd();
00829                 }
00830 
00831 #endif
00832 
00833 
00834                 /*int MemUsed()
00835                 {
00836                 int tot=sizeof(GlTrimesh);
00837                 tot+=sizeof(mesh_type::edge_type)*edge.size();
00838                 tot+=sizeof(MeshType::VertexType *) * EStrip.size();
00839                 tot+=sizeof(MeshType::VertexType *) * TStrip.size();
00840                 tot+=sizeof(MeshType::FaceType *)   * TStripF.size();
00841                 return tot;
00842                 }*/
00843 
00844         private:
00845 
00846                 template <NormalMode nm, ColorMode cm>
00847                 void DrawWire()
00848                 {
00849                         //if(!(curr_hints & (HNUseEdgeStrip | HNUseLazyEdgeStrip) ) )
00850                         if ( (curr_hints & HNIsPolygonal) )
00851                         {
00852                                 DrawWirePolygonal<nm,cm>();
00853                         }
00854                         else
00855                         {
00856                                 glPushAttrib(GL_POLYGON_BIT);
00857                                 glPolygonMode(GL_FRONT_AND_BACK ,GL_LINE);
00858                                 DrawFill<nm,cm,TMNone>();
00859                                 glPopAttrib();
00860                         }
00861                         if(m->fn==0 && m->en>0)
00862                         {
00863                                 glPushAttrib(GL_ENABLE_BIT);
00864                                 glDisable(GL_LIGHTING);
00865                                 glBegin(GL_LINES);
00866                                 for(EdgeIterator ei=m->edge.begin();ei!=m->edge.end(); ++ei)
00867                                 {
00868                                         glVertex((*ei).V(0)->P());
00869                                         glVertex((*ei).V(1)->P());
00870                                 }
00871                                 glEnd();
00872                                 glPopAttrib();
00873                         }
00874                         //      {
00875                         //                      if(!HasEdges()) ComputeEdges();
00876 
00877                         //if(cm==CMPerMesh)     glColor(m->C());
00878                         //std::vector< MeshType::VertexType *>::iterator vi;
00879                         //glBegin(GL_LINE_STRIP);
00880                         //for(vi=EStrip.begin();vi!=EStrip.end(); ++vi){
00881                         //      if((*vi)){
00882                         //                      glNormal((*vi)->N());
00883                         //                      glVertex((*vi)->P());
00884                         //              }
00885                         //      else
00886                         //              {
00887                         //                      glEnd();
00888                         //                      glBegin(GL_LINE_STRIP);
00889                         //              }
00890                         //}
00891                         //glEnd();
00892                         //      }
00893                 }
00894 
00895                 void DrawBBox(ColorMode cm)
00896                 {
00897                         if(cm==CMPerMesh) glColor(m->C());
00898                         glBoxWire(m->bbox);
00899                 }
00900 
00901 
00902         };// end class
00903 } // end namespace
00904 
00905 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:13