src/qvgui/qvglcanvas.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <qvgui/qvglcanvas.h>
00026 #include <qvgui/trackball.h>
00027 #include <QGLWidget>
00028 #include <QMouseEvent>
00029 
00030 #define GL_VIEW_ASPECT   1.333
00031 
00032 QVGLCanvas::QVGLCanvas(const QString &title, QWidget* parent = 0):
00033         QGLWidget(parent), maxwide(2.0), cx(0.0), cy(0.0), cz(0.0),
00034         zoom(45), pressedleft(FALSE), pressedright(FALSE)
00035         {
00036         /* Trackball initialization: */
00037         trackball(quat,0.0,0.0,0.0,0.0);
00038         resize(400,(int)(400/GL_VIEW_ASPECT));
00039         setWindowTitle(title);
00040         show();
00041         }
00042 
00043 QVGLCanvas::~QVGLCanvas ()
00044         {
00045         // FIXME: is it needed?: makeCurrent(); 
00046         }
00047 
00051 
00052 void QVGLCanvas::initializeGL()
00053         {
00054         init();
00055         }
00056 
00057 void QVGLCanvas::paintGL()
00058         {
00059         GLfloat m[4][4];
00060         
00061         glMatrixMode(GL_PROJECTION);
00062 
00063         glLoadIdentity();
00064         gluPerspective(zoom,(float)size().width()/(float)size().height(),1,100);
00065         glMatrixMode(GL_MODELVIEW);
00066 
00067         glLoadIdentity();
00068         
00069         glTranslatef(0,0,-4); 
00070         glScaled((GLdouble)1/maxwide,(GLdouble)1/maxwide,(GLdouble)1/maxwide);
00071         build_rotmatrix(m,quat);
00072         glMultMatrixf(&m[0][0]);
00073         
00074         glRotatef(270,1,0,0); 
00075         
00076         glTranslatef(-cx,-cy,-cz); 
00077 
00078         display();
00079         }
00080 
00081 void QVGLCanvas::resizeGL( int w, int h )
00082         {
00083         glViewport(0,0,w,h); 
00084         reshape(w,h);
00085         }
00086 
00090 
00091 void QVGLCanvas::mousePressEvent(QMouseEvent *event)
00092 {
00093     beginx = event->x();
00094     beginy = event->y();
00095     if(event->button() == Qt::LeftButton) {
00096         pressedleft = TRUE;
00097     } else if(event->button() == Qt::RightButton) {
00098         pressedright = TRUE;
00099     }
00100 }
00101 
00102 void QVGLCanvas::mouseReleaseEvent(QMouseEvent *event)
00103 {
00104     if(event->button() == Qt::LeftButton) {
00105         pressedleft = FALSE;
00106     } else if(event->button() == Qt::RightButton) {
00107         pressedright = FALSE;
00108     }
00109 }
00110 
00111 void QVGLCanvas::mouseMoveEvent(QMouseEvent *event)
00112 {
00113     int x,y;
00114 
00115     x = (int) event->x();
00116     y = (int) event->y();
00117 
00118     if (pressedleft) {
00119         /* drag in progress, simulate trackball */
00120         //printf("MouseMove left button\n");
00121         float spin_quat[4];
00122         trackball(spin_quat,
00123                     (2.0*beginx -             size().width()) / size().width(),
00124                     (     size().height()       - 2.0*beginy) / size().height(),
00125                     (           2.0*x -       size().width()) / size().width(),
00126                     (     size().height() -            2.0*y) / size().height());
00127 
00128         add_quats(spin_quat, quat, quat);
00129         /* orientation has changed, redraw */
00130         //gtk_widget_draw(widget, &area);
00131         updateGL();
00132     }
00133 
00134     if (pressedright) {
00135         /* zooming drag */
00136         //printf("MouseMove right button\n");
00137         zoom += ((y - beginy) / size().height()) * 40;
00138         if (zoom < 5) zoom = 5;
00139         if (zoom > 120) zoom = 120;
00140         /* zoom has changed, redraw  */
00141         updateGL();
00142     }
00143     beginx = x;
00144     beginy = y;
00145 }
00146 
00147 void QVGLCanvas::wheelEvent(QWheelEvent *event)
00148 {
00149     /* zooming wheel */
00150     zoom -= event->delta()/32;
00151     if (zoom < 5) zoom = 5;
00152     if (zoom > 120) zoom = 120;
00153     /* zoom has changed, redraw  */
00154     updateGL();
00155 }
00156 
00157 
00158 
00159 void QVGLCanvas::keyPressEvent(QKeyEvent *event)
00160 {
00161     //printf("KeyPress\n");
00162     switch(event->key()) {
00163       case Qt::Key_Left:
00164         cx -= maxwide/20;
00165         break;
00166       case Qt::Key_Right:
00167         cx += maxwide/20;
00168         break;
00169       case Qt::Key_Up:
00170         cy += maxwide/20;
00171         break;
00172       case Qt::Key_Down:
00173         cy -= maxwide/20;
00174         break;
00175       case Qt::Key_PageUp:
00176         cz += maxwide/20;
00177         break;
00178       case Qt::Key_PageDown:
00179         cz -= maxwide/20;
00180         break;
00181     }
00182     /* Ha cambiado el centro, redibujar: */
00183     updateGL();
00184 }
00185 
00186 void QVGLCanvas::closeEvent(QCloseEvent * event)
00187 {
00188         Q_UNUSED(event);
00189     emit closed();
00190 }

Generated on Thu Mar 13 19:18:16 2008 for QVision by  doxygen 1.5.3