GLC_lib Logo
      GLC_lib
OpenGL Library Class Support This Project

Home

Help
Examples
Screenshots
Download
Link
Forum
News

Example08 : Selection (Christmas tree decoration)

 

exemple08_img

 

Now we will see how to select object with GLC_Viewport class. In this example, we can change ball color by left click on it.

 

Sources of the example08:

Download Source and binaries of this example.

Only the code related to selection developed.

GLWidget class

glwidget.h

#ifndef GLWIDGET_H_
#define GLWIDGET_H_

#include <QGLWidget>

//////////////////////////// GLC specific///////////////////////////////////////
// The factory

#include <GLC_Factory>
// Light
#include <GLC_Light>
// The Viewport with a default camera
#include <GLC_Viewport>
// The collection which manage GLC_object
#include <GLC_World>
#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);

	// Create GLC_Object to display
	void CreateScene();

	// Selection function
	void select(const int x, const int y);

	//Mouse events
	void mousePressEvent(QMouseEvent * e);
	void mouseMoveEvent(QMouseEvent * e);

	void mouseReleaseEvent(QMouseEvent * e);
private:
//////////////////////////// GLC specific///////////////////////////////////////
	GLC_Light m_Light;

	GLC_Factory* m_pFactory;
	GLC_World m_World;
	GLC_Viewport m_GlView;
	QList<GLC_uint> m_TreeId;

	GLC_MoverController m_MoverController;
//////////////////////////End GLC specific/////////////////////////////////////
};

#endif /*GLWIDGET_H_*/

Methods :

Private members :

glwidget.cpp

void GLWidget::mousePressEvent(QMouseEvent *e)
{

	if (m_MoverController.hasActiveMover()) return;
	switch (e->button())
	{

	case (Qt::RightButton):
		m_MoverController.setActiveMover(GLC_MoverController::TrackBall, e->x(), e->y());

		updateGL();
		break;
	case (Qt::LeftButton):

		select(e->x(), e->y());
		break;

	case (Qt::MidButton):
		m_MoverController.setActiveMover(GLC_MoverController::Zoom, e->x(), e->y());

		updateGL();
		break;

	default:
		break;
	}
}

void GLWidget::select(const int x, const int y)
{

	setAutoBufferSwap(false);
	GLC_uint SelectionID= m_GlView.select(this, x, y);

	setAutoBufferSwap(true);

	if (SelectionID != 0)
	{

		GLC_3DViewInstance instance(*m_World.collection()->instanceHandle(SelectionID));
		if ((!instance.isEmpty()) && (not m_TreeId.contains(instance.id())))
		{	// Instance is in the collection and is not the tree

			if (instance.geomAt(0)->firstMaterial()->ambientColor() == Qt::blue)
			{

				instance.geomAt(0)->firstMaterial()->setAmbientColor(Qt::red);
				instance.geomAt(0)->firstMaterial()->setDiffuseColor(QColor::fromRgbF(0.8, 0.2, 0.2, 1.0));
			}

			else
			{
				instance.geomAt(0)->firstMaterial()->setAmbientColor(Qt::blue);

				instance.geomAt(0)->firstMaterial()->setDiffuseColor(QColor::fromRgbF(0.2, 0.2, 0.8, 1.0));
			}

		}
	}

	updateGL();
}

mousePressEvent :

If the button is the left button, call method select.

select :

All GLC_3DViewInstance have an unique ID, therefore it's easy to identify the selected object.

SourceForge.net Logo

©2005-2011 Laurent Ribon