glwidget.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the examples of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:BSD$
00010 ** You may use this file under the terms of the BSD license as follows:
00011 **
00012 ** "Redistribution and use in source and binary forms, with or without
00013 ** modification, are permitted provided that the following conditions are
00014 ** met:
00015 **   * Redistributions of source code must retain the above copyright
00016 **     notice, this list of conditions and the following disclaimer.
00017 **   * Redistributions in binary form must reproduce the above copyright
00018 **     notice, this list of conditions and the following disclaimer in
00019 **     the documentation and/or other materials provided with the
00020 **     distribution.
00021 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00022 **     the names of its contributors may be used to endorse or promote
00023 **     products derived from this software without specific prior written
00024 **     permission.
00025 **
00026 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00037 ** $QT_END_LICENSE$
00038 **
00039 ****************************************************************************/
00040 
00041 #include "glwidget.h"
00042 #include <wrap/qt/trackball.h>
00043 #include <wrap/gl/picking.h>
00044 #include <wrap/qt/anttweakbarMapper.h>
00045 
00046 TwBar *bar;
00047 char * filename;
00048 CMesh mesh;     
00049 vcg::GlTrimesh<CMesh> glWrap;    
00050 vcg::Trackball track;     
00051 GLW::DrawMode drawmode=GLW::DMFlatWire;     
00052 
00053 void  TW_CALL loadTetrahedron(void *){
00054     vcg::tri::Tetrahedron(mesh);
00055     vcg::tri::UpdateBounding<CMesh>::Box(mesh);
00056     vcg::tri::UpdateNormal<CMesh>::PerVertexNormalizedPerFace(mesh);
00057     vcg::tri::UpdateNormal<CMesh>::PerFaceNormalized(mesh);
00058     glWrap.m = &mesh;
00059     glWrap.Update();
00060 }
00061 
00062 void TW_CALL loadMesh(void *)
00063 {
00064   if(filename==0) return;
00065   int err=vcg::tri::io::ImporterPLY<CMesh>::Open(mesh,(char*)filename);
00066   if(err==ply::E_NOERROR)
00067   {
00068     vcg::tri::UpdateBounding<CMesh>::Box(mesh);
00069     vcg::tri::UpdateNormal<CMesh>::PerVertexNormalizedPerFace(mesh);
00070     vcg::tri::UpdateNormal<CMesh>::PerFaceNormalized(mesh);
00071     glWrap.m = &mesh;
00072     glWrap.Update();
00073   }
00074 }
00075 
00076 GLWidget::GLWidget(QWidget *parent)
00077     : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
00078 {
00079   filename=0;
00080   hasToPick=false;
00081   setWindowTitle(tr("Hello GL"));
00082   bar = TwNewBar("TweakBar");
00083   TwCopyCDStringToClientFunc (CopyCDStringToClient);
00084 
00085   TwAddVarRW(bar,"Input",TW_TYPE_CDSTRING, &filename," label='Filepath' group=SetMesh help=` Name of the file to load` ");
00086   TwAddButton(bar,"Load from file",loadMesh,0,  " label='Load Mesh' group=SetMesh help=`load the mesh` ");
00087   TwAddButton(bar,"Use tetrahedron",loadTetrahedron,0,  " label='Make Tetrahedron' group=SetMesh help=`use tetrahedron.` ");
00088 
00089   // ShapeEV associates Shape enum values with labels that will be displayed instead of enum values
00090   TwEnumVal drawmodes[6] = { {GLW::DMSmooth, "Smooth"}, {GLW::DMPoints, "Per Points"}, {GLW::DMWire, "Wire"}, {GLW::DMFlatWire, "FlatWire"},{GLW::DMHidden, "Hidden"},{GLW::DMFlat, "Flat"}};
00091   // Create a type for the enum shapeEV
00092   TwType drawMode = TwDefineEnum("DrawMode", drawmodes, 6);
00093   // add 'g_CurrentShape' to 'bar': this is a variable of type ShapeType. Its key shortcuts are [<] and [>].
00094   TwAddVarRW(bar, "Draw Mode", drawMode, &drawmode, " keyIncr='<' keyDecr='>' help='Change draw mode.' ");
00095 }
00096 
00097 void GLWidget::initializeGL ()
00098 {
00099   glewInit();
00100   glClearColor(0, 0, 0, 0);
00101   glEnable(GL_LIGHTING);
00102   glEnable(GL_LIGHT0);
00103   glEnable(GL_NORMALIZE);
00104   glEnable(GL_COLOR_MATERIAL);
00105   glEnable(GL_CULL_FACE);
00106   glEnable(GL_DEPTH_TEST);
00107 }
00108 
00109 void GLWidget::resizeGL (int w, int h)
00110 {
00111   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
00112   TwWindowSize(w, h);
00113   initializeGL();
00114  }
00115 
00116 void GLWidget::paintGL ()
00117 {
00118     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00119     glMatrixMode(GL_PROJECTION);
00120     glLoadIdentity();
00121     gluPerspective(40, GLWidget::width()/(float)GLWidget::height(), 0.1, 100);
00122     glMatrixMode(GL_MODELVIEW);
00123     glLoadIdentity();
00124     gluLookAt(0,0,3.5f,   0,0,0,   0,1,0);
00125     track.center=vcg::Point3f(0, 0, 0);
00126     track.radius= 1;
00127     track.GetView();
00128     glPushMatrix();
00129     track.Apply();
00130     glPushMatrix();
00131     if(mesh.vert.size()>0)
00132     {
00133       vcg::glScale(2.0f/mesh.bbox.Diag());
00134       glTranslate(-mesh.bbox.Center());
00135       glWrap.Draw(GLW::DrawMode(drawmode),GLW::CMNone,GLW::TMNone);
00136     }
00137     glPopMatrix();
00138     track.DrawPostApply();
00139     glPopMatrix();
00140     if(hasToPick)
00141     {
00142       hasToPick=false;
00143       Point3f pp;
00144       if(Pick<Point3f>(pointToPick[0],pointToPick[1],pp))
00145       {
00146         track.Translate(-pp);
00147         track.Scale(1.25f);
00148         QCursor::setPos(mapToGlobal(QPoint(width()/2+2,height()/2+2)));
00149       }
00150     }
00151     TwDraw();
00152 }
00153 
00154 void GLWidget::keyReleaseEvent (QKeyEvent * e)
00155 {
00156   e->ignore ();
00157   if (e->key () == Qt::Key_Control)  track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ControlModifier));
00158   if (e->key () == Qt::Key_Shift)  track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ShiftModifier));
00159   if (e->key () == Qt::Key_Alt) track.ButtonUp (QT2VCG (Qt::NoButton, Qt::AltModifier));
00160   updateGL ();
00161 }
00162 
00163 
00164 void GLWidget::keyPressEvent (QKeyEvent * e)
00165 {
00166   e->ignore ();
00167   if (e->key () == Qt::Key_Control) track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ControlModifier));
00168   if (e->key () == Qt::Key_Shift)  track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ShiftModifier));
00169   if (e->key () == Qt::Key_Alt)  track.ButtonDown (QT2VCG (Qt::NoButton, Qt::AltModifier));
00170 
00171   TwKeyPressQt(e);
00172   updateGL ();
00173 }
00174 
00175 void GLWidget::mousePressEvent (QMouseEvent * e)
00176 {
00177   if(!TwMousePressQt(e))
00178   {
00179   e->accept ();
00180   setFocus ();
00181   track.MouseDown (e->x (), height () - e->y (), QT2VCG (e->button (), e->modifiers ()));
00182   }
00183   updateGL ();
00184 }
00185 
00186 void GLWidget::mouseMoveEvent (QMouseEvent * e)
00187 {
00188   if (e->buttons ()) {
00189     track.MouseMove (e->x (), height () - e->y ());
00190     updateGL ();
00191   }
00192   TwMouseMotion(e->x (), e->y ());
00193 }
00194 
00195 void GLWidget::mouseDoubleClickEvent (QMouseEvent * e)
00196 {
00197   hasToPick=true;
00198   pointToPick=Point2i(e->x(),height()-e->y());
00199   updateGL ();
00200 }
00201 
00202 
00203 void GLWidget::mouseReleaseEvent (QMouseEvent * e)
00204 {
00205   track.MouseUp (e->x (), height () - e->y (), QT2VCG (e->button (), e->modifiers ()));
00206   TwMouseReleaseQt(e);
00207   updateGL ();
00208 }
00209 
00210 void GLWidget::wheelEvent (QWheelEvent * e)
00211 {
00212   const int WHEEL_STEP = 120;
00213   track.MouseWheel (e->delta () / float (WHEEL_STEP), QTWheel2VCG (e->modifiers ()));
00214   updateGL ();
00215 }


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