00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "glarea.h"
00034 #include <QMessageBox>
00035 #include <QKeyEvent>
00036 #include <QKeyEvent>
00037 #include <QWheelEvent>
00038 #include <wrap/qt/trackball.h>
00039
00040 GLArea::GLArea (QWidget * parent)
00041 :QGLWidget (parent)
00042 {
00043 drawmode= SMOOTH;
00044 GLArea::loadTetrahedron();
00045 }
00046
00047 void GLArea::selectDrawMode(int mode){
00048 drawmode=DrawMode(mode);
00049 updateGL();
00050 }
00051
00052 void GLArea::loadMesh(QString fileName)
00053 {
00054 int err=vcg::tri::io::ImporterPLY<CMesh>::Open(mesh,(fileName.toStdString()).c_str());
00055 if(err!=0){
00056 const char* errmsg=vcg::tri::io::ImporterPLY<CMesh>::ErrorMsg(err);
00057 QMessageBox::warning(this,tr("Error Loading Mesh"),QString(errmsg));
00058 }
00059 initMesh("Loaded \""+fileName+"\".");
00060 }
00061
00062 void GLArea::loadTetrahedron(){
00063 vcg::tri::Tetrahedron(mesh);
00064 initMesh(tr("Tethraedron [builtin]"));
00065 }
00066
00067 void GLArea::loadDodecahedron(){
00068 vcg::tri::Dodecahedron(mesh);
00069 initMesh(tr("Dodecahedron [builtin]"));
00070 }
00071
00072 void GLArea::initMesh(QString message)
00073 {
00074
00075 vcg::tri::UpdateBounding<CMesh>::Box(mesh);
00076
00077 vcg::tri::UpdateNormal<CMesh>::PerVertexNormalizedPerFace(mesh);
00078 vcg::tri::UpdateNormal<CMesh>::PerFaceNormalized(mesh);
00079
00080 glWrap.m = &mesh;
00081 glWrap.Update();
00082 updateGL();
00083 emit setStatusBar(message);
00084 }
00085
00086 void GLArea::initializeGL ()
00087 {
00088 glClearColor(0, 0, 0, 0);
00089 glEnable(GL_LIGHTING);
00090 glEnable(GL_LIGHT0);
00091 glEnable(GL_NORMALIZE);
00092 glEnable(GL_COLOR_MATERIAL);
00093 glEnable(GL_CULL_FACE);
00094 glEnable(GL_DEPTH_TEST);
00095 }
00096
00097 void GLArea::resizeGL (int w, int h)
00098 {
00099 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
00100 initializeGL();
00101 }
00102
00103 void GLArea::paintGL ()
00104 {
00105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00106 glMatrixMode(GL_PROJECTION);
00107 glLoadIdentity();
00108 gluPerspective(25, GLArea::width()/(float)GLArea::height(), 0.1, 100);
00109 glMatrixMode(GL_MODELVIEW);
00110 glLoadIdentity();
00111 gluLookAt(0,0,5, 0,0,0, 0,1,0);
00112 track.center=vcg::Point3f(0, 0, 0);
00113 track.radius= 1;
00114 track.GetView();
00115 track.Apply();
00116 glPushMatrix();
00117 float d=2.0f/mesh.bbox.Diag();
00118 vcg::glScale(d);
00119 glTranslate(-glWrap.m->bbox.Center());
00120
00121 switch(drawmode)
00122 {
00123 case SMOOTH:
00124 glWrap.Draw<vcg::GLW::DMSmooth, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00125 break;
00126 case POINTS:
00127 glWrap.Draw<vcg::GLW::DMPoints, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00128 break;
00129 case WIRE:
00130 glWrap.Draw<vcg::GLW::DMWire, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00131 break;
00132 case FLATWIRE:
00133 glWrap.Draw<vcg::GLW::DMFlatWire, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00134 break;
00135 case HIDDEN:
00136 glWrap.Draw<vcg::GLW::DMHidden, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00137 break;
00138 case FLAT:
00139 glWrap.Draw<vcg::GLW::DMFlat, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00140 break;
00141 default:
00142 break;
00143 }
00144
00145 glPopMatrix();
00146 track.DrawPostApply();
00147 }
00148
00149 void GLArea::keyReleaseEvent (QKeyEvent * e)
00150 {
00151 e->ignore ();
00152 if (e->key () == Qt::Key_Control)
00153 track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ControlModifier));
00154 if (e->key () == Qt::Key_Shift)
00155 track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ShiftModifier));
00156 if (e->key () == Qt::Key_Alt)
00157 track.ButtonUp (QT2VCG (Qt::NoButton, Qt::AltModifier));
00158 updateGL ();
00159 }
00160
00161 void GLArea::keyPressEvent (QKeyEvent * e)
00162 {
00163 e->ignore ();
00164 if (e->key () == Qt::Key_Control)
00165 track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ControlModifier));
00166 if (e->key () == Qt::Key_Shift)
00167 track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ShiftModifier));
00168 if (e->key () == Qt::Key_Alt)
00169 track.ButtonDown (QT2VCG (Qt::NoButton, Qt::AltModifier));
00170 updateGL ();
00171 }
00172
00173 void GLArea::mousePressEvent (QMouseEvent * e)
00174 {
00175 e->accept ();
00176 setFocus ();
00177 track.MouseDown (QT2VCG_X(this,e), QT2VCG_Y(this,e), QT2VCG (e->button (), e->modifiers ()));
00178 updateGL ();
00179 }
00180
00181 void GLArea::mouseMoveEvent (QMouseEvent * e)
00182 {
00183 if (e->buttons ()) {
00184 track.MouseMove (QT2VCG_X(this,e), QT2VCG_Y(this,e));
00185 updateGL ();
00186 }
00187 }
00188
00189 void GLArea::mouseReleaseEvent (QMouseEvent * e)
00190 {
00191 track.MouseUp (QT2VCG_X(this,e), QT2VCG_Y(this,e), QT2VCG (e->button (), e->modifiers ()));
00192 updateGL ();
00193 }
00194
00195 void GLArea::wheelEvent (QWheelEvent * e)
00196 {
00197 const int WHEEL_STEP = 120;
00198 track.MouseWheel (e->delta () / float (WHEEL_STEP), QTWheel2VCG (e->modifiers ()));
00199 updateGL ();
00200 }