00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
00120
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
00130
00131 updateGL();
00132 }
00133
00134 if (pressedright) {
00135
00136
00137 zoom += ((y - beginy) / size().height()) * 40;
00138 if (zoom < 5) zoom = 5;
00139 if (zoom > 120) zoom = 120;
00140
00141 updateGL();
00142 }
00143 beginx = x;
00144 beginy = y;
00145 }
00146
00147 void QVGLCanvas::wheelEvent(QWheelEvent *event)
00148 {
00149
00150 zoom -= event->delta()/32;
00151 if (zoom < 5) zoom = 5;
00152 if (zoom > 120) zoom = 120;
00153
00154 updateGL();
00155 }
00156
00157
00158
00159 void QVGLCanvas::keyPressEvent(QKeyEvent *event)
00160 {
00161
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
00183 updateGL();
00184 }
00185
00186 void QVGLCanvas::closeEvent(QCloseEvent * event)
00187 {
00188 Q_UNUSED(event);
00189 emit closed();
00190 }