GLC_lib Logo
      GLC_lib
OpenGL Library Class Support This Project

Home

Help
Examples
Screenshots
Download
Link
Forum
News

Example02 : Point of view handling

 

exemple2_img

 

From the example01 (Display of a circle) add some code lines in order to handle the point of view with the mouse.

 

Sources of the example02:

Download Source and binaries of this example.

 

Only the difference compared to the exemple01 is developed.

GLWidget class

 

glwidget.h

#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_*/

Changes :

Reimplementation of these QT4 QWidget mouse event handler :

Use the class GLC_MoverController to manage viewpoint navigation.

glwidget.cpp

#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

PaintGl method

Call m_MoverController.drawActiveMoverRep() to display the active mover representation.

Active mover representation can be :

For more information see  GLC_MoverController reference.

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.

MousseMoveEvent method

Catch mouse move event and send it to the MoverController in order to carry out one of the following actions:

For more information see  GLC_MoverController reference.

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.

 

SourceForge.net Logo

©2005-2011 Laurent Ribon