![]() |
GLC_lib | |||||||
| OpenGL Library Class | ||||||||
From the example01 (Display of a circle) add some code lines in order to handle the point of view with the mouse.
Download Source and binaries of this example.
Only the difference compared to the exemple01 is developed.
#ifndef GLWIDGET_H_ #define GLWIDGET_H_ #include <QGLWidget> //////////////////////////// GLC specific/////////////////////////////////////// // The factory #include <GLC_Factory> // The Viewport with a default camera #include <GLC_Viewport> // The Mover controller is used to change the point of view #include <GLC_MoverController> //////////////////////////End GLC specific///////////////////////////////////// class GLWidget : public QGLWidget { public: GLWidget(QWidget *p_parent); ~GLWidget(); private: void initializeGL(); void paintGL(); void resizeGL(int width, int height); // Exemple02 New //Mouse events void mousePressEvent(QMouseEvent * e); void mouseMoveEvent(QMouseEvent * e); void mouseReleaseEvent(QMouseEvent * e); // End Exemple02 New private: //////////////////////////// GLC specific/////////////////////////////////////// GLC_Factory* m_pFactory; GLC_3DViewInstance m_Circle; GLC_Viewport m_GlView; // Exemple02 New GLC_MoverController m_MoverController; // End Exemple02 New //////////////////////////End GLC specific///////////////////////////////////// }; #endif /*GLWIDGET_H_*/
Reimplementation of these QT4 QWidget mouse event handler :
Mousse press event.
Mousse release event.
Mouse move event
Use the class GLC_MoverController to manage viewpoint navigation.
#include <QtDebug> #include "glwidget.h" GLWidget::GLWidget(QWidget *p_parent) : QGLWidget(p_parent) , m_pFactory(GLC_Factory::instance(this->context())) , m_Circle(m_pFactory->createCircle(0.3)) // Circle radius , m_GlView(this) , m_MoverController() { // Min and max view distances m_GlView.setDistMax(10); m_GlView.setDistMin(0.01); // Color of mover representation (Trackball) QColor repColor; repColor.setRgbF(1.0, 0.11372, 0.11372, 0.0); // Contruct default mover controller m_MoverController= m_pFactory->createDefaultMoverController(repColor, &m_GlView); } GLWidget::~GLWidget() { delete m_pFactory; } void GLWidget::initializeGL() { // OpenGL initialisation from NEHE production m_GlView.initGl(); } void GLWidget::paintGL() { // Clear screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Load identity matrix glLoadIdentity(); //////////////////////////// GLC specific/////////////////////////////////////// // define view matrix m_GlView.glExecuteCam(); // Display the circle m_Circle.glExecute(); // Exemple02 New // Display the active mover representation m_MoverController.drawActiveMoverRep(); // End Exemple02 New //////////////////////////End GLC specific///////////////////////////////////// } void GLWidget::resizeGL(int width, int height) { //////////////////////////// GLC specific/////////////////////////////////////// m_GlView.setWinGLSize(width, height); // Compute window aspect ratio //////////////////////////End GLC specific///////////////////////////////////// } // Exemple02 New void GLWidget::mousePressEvent(QMouseEvent *e) { if (m_MoverController.hasActiveMover()) return; switch (e->button()) { case (Qt::RightButton): // Set track ball mover m_MoverController.setActiveMover(GLC_MoverController::TrackBall, e->x(), e->y()); updateGL(); break; case (Qt::LeftButton): // Set Pan mover m_MoverController.setActiveMover(GLC_MoverController::Pan, e->x(), e->y()); updateGL(); break; case (Qt::MidButton): // Set Zoom mover m_MoverController.setActiveMover(GLC_MoverController::Zoom, e->x(), e->y()); updateGL(); break; default: break; } } void GLWidget::mouseMoveEvent(QMouseEvent * e) { if (not m_MoverController.hasActiveMover()) return; // Move with the active mover m_MoverController.move(e->x(), e->y()); updateGL(); } void GLWidget::mouseReleaseEvent(QMouseEvent*) { if (not m_MoverController.hasActiveMover()) return; // Set No mover m_MoverController.setNoMover(); updateGL(); } // End Exemple02 New
Call m_MoverController.drawActiveMoverRep() to display the active mover representation.
Active mover representation can be :
For more information see GLC_MoverController reference.
Catch mouse press event to prepare point of view handling by recording mouse position and display view handling user interface.
For more information see GLC_MoverController reference.
Catch mouse move event and send it to the MoverController in order to carry out one of the following actions:
Track Ball.
Pan.
Zoom.
For more information see GLC_MoverController reference.
Catch mouse release event to unset the current MoverController
Then redraw the view to overwrite old view handling user interface.
For more information see GLC_MoverController reference.
| ©2005-2010 Laurent Ribon |