Example 2 : Point of view handling

From the Example 1 (Display of a circle) add some code lines in order to handle the point of view with the mouse.
Only the difference compared to the exemple01 is developed.

1. GLWidget class

1.1. glwidget.h

/****************************************************************************

This file is part of the GLC-lib library.
Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
http://glc-lib.sourceforge.net

GLC-lib is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

GLC-lib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GLC-lib; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*****************************************************************************/

#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 *pParent);
~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_3DViewInstance m_Circle;
GLC_Viewport m_GlView;
// Exemple02 New
GLC_MoverController m_MoverController;
// End Exemple02 New
//////////////////////////End GLC specific/////////////////////////////////////
};

#endif /*GLWIDGET_H_*/

1.1.1. Changes :

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.

1.2. glwidget.cpp

/****************************************************************************

This file is part of the GLC-lib library.
Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
http://glc-lib.sourceforge.net

GLC-lib is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

GLC-lib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GLC-lib; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*****************************************************************************/

#include <QtDebug>

#include <GLC_UserInput>
#include <GLC_Context>

#include "glwidget.h"


GLWidget::GLWidget(QWidget *pParent)
: QGLWidget(new GLC_Context(QGLFormat(QGL::SampleBuffers)), pParent)
, m_Circle(GLC_Factory::instance()->createCircle(0.3)) // Circle radius
, m_GlView()
, m_MoverController()
{
    connect(&m_GlView, SIGNAL(updateOpenGL()), this, SLOT(updateGL()));
m_Circle.geomAt(0)->setWireColor(Qt::white);

// Color of mover representation (Trackball)
QColor repColor;
repColor.setRgbF(1.0, 0.11372, 0.11372, 1.0);
// Contruct default mover controller
m_MoverController= GLC_Factory::instance()->createDefaultMoverController(repColor, &m_GlView);

}

GLWidget::~GLWidget()
{

}

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
    GLC_Context::current()->glcLoadIdentity();
m_GlView.setDistMinAndMax(m_Circle.boundingBox());
//////////////////////////// GLC specific///////////////////////////////////////
// define view matrix
m_GlView.glExecuteCam();

// Display the circle
m_Circle.render();

// 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, GLC_UserInput(e->x(), e->y()));
updateGL();
break;
case (Qt::LeftButton):
// Set Pan mover
m_MoverController.setActiveMover(GLC_MoverController::Pan, GLC_UserInput(e->x(), e->y()));
updateGL();
break;
case (Qt::MidButton):
// Set Zoom mover
m_MoverController.setActiveMover(GLC_MoverController::Zoom, GLC_UserInput(e->x(), e->y()));
updateGL();
break;

default:
break;
}
}

void GLWidget::mouseMoveEvent(QMouseEvent * e)
{
if (m_MoverController.hasActiveMover())
{
// Move with the active mover
m_MoverController.move(GLC_UserInput(e->x(), e->y()));
updateGL();
}
}

void GLWidget::mouseReleaseEvent(QMouseEvent*)
{
if (m_MoverController.hasActiveMover())
{
// Set No mover
m_MoverController.setNoMover();
updateGL();
}
}
// End Exemple02 New

1.2.1. PaintGL method

Call m_MoverController.drawActiveMoverRep() to display the active mover representation.
Active mover representation can be :

  • Track Ball center.
  • Track Ball.
  • Track Ball arcs.

For more information see GLC_MoverController reference.

1.2.2. MousePressEvent method

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.

1.2.3. MousseMoveEvent method

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.

1.2.4. MousseReleaseEvent method

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

Tags: