glwidget.cpp
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   History
00025 
00026 $Log: not supported by cvs2svn $
00027 Revision 1.4  2007/05/17 09:06:44  ganovelli
00028 gestione double click
00029 
00030 Revision 1.3  2006/12/10 23:29:57  ganovelli
00031 added VFIterator (Pos is disabled in this version)
00032 
00033 Revision 1.2  2006/12/10 22:17:18  ganovelli
00034 cvs problem during frist committ. repeated
00035 
00036 */
00037 
00038 #include <QtGui>
00039 #include <GL/glew.h>
00040 #include <QtOpenGL>
00041 
00042 #include <math.h>
00043 #include "glwidget.h"
00044 #include <wrap/io_trimesh/import_PLY.h>
00045 #include <wrap/gl/picking.h>
00046 #include <wrap/gl/space.h>
00047 #include <wrap/gl/pos.h>
00048 #include <vcg/complex/algorithms/update/bounding.h>
00049 #include <vcg/complex/algorithms/update/normal.h>
00050 #include <vcg/complex/algorithms/update/topology.h>
00051 
00052 
00053 GLWidget::GLWidget(QWidget *parent)
00054     : QGLWidget(parent)
00055 {
00056     object = 0;
00057 
00058     trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
00059     trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
00060                 track.SetPosition(vcg::Point3f(0.0,0.0,0.0));
00061                 track.SetIdentity();
00062                 track.radius = 0.4;
00063                 pos.f=NULL;
00064                 vfite.f = NULL;
00065                 doPickVfIte = false;
00066                 doPickPos = false;
00067 }
00068 
00069 GLWidget::~GLWidget()
00070 {
00071     makeCurrent();
00072     glDeleteLists(object, 1);
00073 }
00074 
00075 QSize GLWidget::minimumSizeHint() const
00076 {
00077     return QSize(50, 50);
00078 }
00079 
00080 QSize GLWidget::sizeHint() const
00081 {
00082     return QSize(400, 400);
00083 }
00084 
00085 void GLWidget::LoadTriMesh(QString &namefile)
00086 {
00087         vcg::tri::io::ImporterPLY<MyStraightMesh>::Open(mesh,namefile.toAscii());
00088         vcg::tri::UpdateBounding<MyStraightMesh>::Box(mesh);
00089         vcg::tri::UpdateNormals<MyStraightMesh>::PerFace(mesh);
00090         vcg::tri::UpdateNormals<MyStraightMesh>::PerVertex(mesh);
00091         vcg::tri::UpdateTopology<MyStraightMesh>::FaceFace(mesh);
00092         vcg::tri::UpdateTopology<MyStraightMesh>::VertexFace(mesh);
00093         pos.f=0;
00094         vfite.f=NULL;
00095 }
00096 
00097 void GLWidget::OpenFile(){
00098         QStringList filters;
00099         
00100 
00101         QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),".", filters.join("\n"));
00102         
00103         if (fileName.isEmpty()) return;
00104         else
00105          LoadTriMesh( fileName );
00106 
00107 }
00108 
00109 void GLWidget::flipV( ){
00110         if(pos.f) pos.FlipV();
00111         repaint();
00112 }
00113 void GLWidget::flipE( ){
00114         if(pos.f) pos.FlipE();
00115         repaint();
00116 }
00117 void GLWidget::flipF( ){
00118         if(pos.f) pos.FlipF();
00119         repaint();
00120 }
00121 void GLWidget::nextE( ){
00122         if(pos.f) pos.NextE();
00123         repaint();
00124 }
00125 void GLWidget::nextB( ){
00126         if(pos.f) pos.NextB();
00127         repaint();
00128 }
00129 
00130 void GLWidget::nextVfite( ){
00131         if(vfite.F()) ++vfite;
00132         repaint();
00133 }
00134 
00135 void GLWidget::initializeGL()
00136 {
00137     qglClearColor(trolltechPurple.dark());
00138     glShadeModel(GL_FLAT);
00139     glEnable(GL_DEPTH_TEST);
00140     glEnable(GL_CULL_FACE);
00141 }
00142 
00143 template <class VertexType>
00144 void drawVertex(VertexType & v){
00145         glPushAttrib(0xffffffff);
00146         glPointSize(2.0);
00147         glBegin(GL_POINTS);
00148         glVertex(v.P());
00149         glEnd();
00150         glPopAttrib();
00151 }
00152 
00153 void GLWidget::paintGL()
00154 {
00155     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00156     glMatrixMode(GL_MODELVIEW);
00157     glLoadIdentity();
00158     gluLookAt(0,0,2,   0,0,0,   0,1,0);    
00159     
00160                 track.GetView();
00161     track.Apply();
00162 
00163                 
00164                 glScalef(1/glWrap.m->bbox.Diag(),1/glWrap.m->bbox.Diag(),1/glWrap.m->bbox.Diag());
00165                 glTranslate(-glWrap.m->bbox.Center());  
00166 
00167                 // to do some picking
00168                  MyStraightMesh::FaceType* fp=NULL;
00169                  MyStraightMesh::VertexType* vp=NULL;
00170                         if(doPickPos)
00171                         {
00172                                 std::vector<MyStraightMesh::FaceType*> res;
00173                                 int yes = vcg::Pick<MyStraightMesh::FaceContainer>(pic_x,ScreenH-pic_y+1,mesh.face,res,vcg::glTriangle3<MyStraightMesh::FaceType>,1,1);
00174                                 if(yes) 
00175                                         {fp = res[0];
00176                                                 pos.Set(fp,0,fp->V(0));
00177                                         }
00178                                 doPickPos=false;
00179                   }else
00180                         if(doPickVfIte)
00181                         {
00182                                 std::vector<MyStraightMesh::VertexType*> res;
00183                                 int yes = vcg::Pick<MyStraightMesh::VertContainer>(pic_x,ScreenH-pic_y+1,mesh.vert,res,drawVertex<MyStraightMesh::VertexType>,3,3);
00184                                 if(yes) 
00185                                         {vp = res[0];
00186 MyStraightMesh::FaceType* g  = vp->VFp();
00187                                                 vfite=vcg::face::VFIterator<MyStraightMesh::FaceType>(vp);
00188                                         }
00189 
00190                                 doPickVfIte = false;
00191                         }
00192                 
00193                 glWrap.Draw<vcg::GLW::DMFlatWire,vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00194  
00195                 if(pos.f!=NULL) {
00196                         glPushAttrib(0xffffffff);
00197                         glDisable(GL_LIGHTING);
00198                         glColor3f(0.0,1.0,0.0);
00199                         glDepthRange(0.0,0.999);
00200                         vcg::GlPos<vcg::face::Pos<MyStraightMesh::FaceType> >::Draw(pos);
00201                         glPopAttrib();
00202                 }
00203                 if(vfite.F()!=NULL) {
00204                         glPushAttrib(0xffffffff);
00205                         glDisable(GL_LIGHTING);
00206                         glColor3f(0.0,1.0,0.0);
00207                         glDepthRange(0.0,0.99);
00208                         vcg::GlVfIterator<vcg::face::VFIterator<MyStraightMesh::FaceType> >::Draw(vfite);
00209                         glPopAttrib();
00210                 }
00211 
00212 }
00213 
00214 
00215 
00216 
00217 
00218 void GLWidget::mouseMoveEvent(QMouseEvent *e)
00219 {
00220                         track.MouseMove(e->x(),ScreenH-e->y()+1);
00221                         repaint();
00222 
00223     //if (event->buttons() & Qt::LeftButton) {
00224     //    setXRotation(xRot + 8 * dy);
00225     //} else if (event->buttons() & Qt::RightButton) {
00226     //    setXRotation(xRot + 8 * dy);
00227     //}
00228  //   lastPos = event->pos();
00229 }
00230  void GLWidget::keyPressEvent ( QKeyEvent * e ) {
00231                 if((keypress == Qt::Key_Control)&&(e->key()==Qt::Key_Control))
00232                         keypress = -1;
00233                 else
00234                         keypress = e->key();
00235  }
00236  void GLWidget::mouseDoubleClickEvent(QMouseEvent *e){
00237                                         if(e->button() == Qt::RightButton)
00238                                                 doPickPos=true;
00239 
00240  }
00241  void GLWidget:: mousePressEvent(QMouseEvent *e)
00242 {
00243                 if( (keypress==Qt::Key_Control) && (e->button() == Qt::LeftButton) )
00244                                         track.MouseDown(e->x(),ScreenH-e->y()+1,vcg::Trackball::KEY_CTRL|vcg::Trackball::BUTTON_LEFT );
00245                         else
00246                                 if(e->button() == Qt::LeftButton )
00247                                         track.MouseDown(e->x(),ScreenH-e->y()+1,vcg::Trackball::BUTTON_LEFT);
00248                                 else
00249                                         if(e->button() == Qt::RightButton)
00250                                         {
00251                                                 doPickVfIte=true;
00252                                                 pic_x = e->x();
00253                                                 pic_y = e->y();
00254                                         }
00255                         repaint();
00256                 }
00257 
00258  void GLWidget::mouseReleaseEvent ( QMouseEvent * e ){
00259                         
00260                         if( (keypress==Qt::Key_Control) && (e->button() == Qt::LeftButton) )
00261                                         track.MouseUp(e->x(),ScreenH-e->y()+1,vcg::Trackball::KEY_CTRL );
00262                         else
00263                                 if(e->button() == Qt::LeftButton )
00264                                         track.MouseUp(e->x(),ScreenH-e->y()+1,vcg::Trackball::BUTTON_LEFT);
00265                         repaint();
00266                 }
00267 
00268 
00269  void GLWidget::resizeGL(int w,int h){
00270                         ScreenW=w; ScreenH=h;
00271                         glViewport(0,0,w,h);
00272                         glMatrixMode(GL_PROJECTION);
00273                         glLoadIdentity();
00274                         gluPerspective(45,ScreenW/(float)ScreenH,0.01,5);
00275                 }
00276  void GLWidget::wheelEvent ( QWheelEvent * e ){
00277                         int v =  e->delta()/(float) 120;
00278                         track.MouseWheel(v);
00279                         repaint();
00280                 }


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