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
00034
00035
00036
00037
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
00090 TwEnumVal drawmodes[6] = { {GLW::DMSmooth, "Smooth"}, {GLW::DMPoints, "Per Points"}, {GLW::DMWire, "Wire"}, {GLW::DMFlatWire, "FlatWire"},{GLW::DMHidden, "Hidden"},{GLW::DMFlat, "Flat"}};
00091
00092 TwType drawMode = TwDefineEnum("DrawMode", drawmodes, 6);
00093
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 }