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