GLC_lib Logo
      GLC_lib
OpenGL Library Class Support This Project

Home

Help
Examples
Screenshots
Download
Link
Forum
News

Example04 : Several objects display

 

exemple04_img

 

Now that we know how displaying a circle and a cylinder, we will use GLC_3DViewCollection class to display a set of objects enbeded in GLC_3DViewInstance class.

 

Sources of the example04:

Download Source and binaries of this example.

 

Only the difference compared to the example03 is 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_3DViewCollection>
// 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);

	// Create GLC_Object to display
	void CreateScene();

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

	void mouseMoveEvent(QMouseEvent * e);
	void mouseReleaseEvent(QMouseEvent * e);

private:
//////////////////////////// GLC specific///////////////////////////////////////
	GLC_Factory* m_pFactory;
	GLC_Light m_Light;
	GLC_3DViewCollection m_Collection;

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

#endif /*GLWIDGET_H_*/

Include :

To display several objects, the header to include is GLC_3DViewCollection

Method :

To create objects the method CreateScene() is added.

Private members :

glwidget.cpp

#include <GLC_Exception>

// For VSYNC problem under Mac OS X
#if defined(Q_OS_MAC)
#include <OpenGL.h>
#endif

GLWidget::GLWidget(QWidget *p_parent)
: QGLWidget(p_parent)
, m_pFactory(GLC_Factory::instance(this->context()))
, m_Light()
, m_Collection()
, m_GlView(this)
, m_MoverController()
{

//////////////////////////// GLC specific///////////////////////////////////////

	m_Light.setPosition(1.0, 1.0, 1.0);

	QColor repColor;
	repColor.setRgbF(1.0, 0.11372, 0.11372, 0.0);

	m_MoverController= m_pFactory->createDefaultMoverController(repColor, &m_GlView);

	// Set The Camera default Up Vector
	m_GlView.cameraHandle()->setDefaultUpVector(glc::Z_AXIS);

	m_GlView.cameraHandle()->setIsoView();

	// Create objects to display
	CreateScene();

//////////////////////////End GLC specific/////////////////////////////////////

}

GLWidget::~GLWidget()
{
	delete m_pFactory;
}

void GLWidget::initializeGL()
{

	// For VSYNC problem under Mac OS X
	#if defined(Q_OS_MAC)
	const GLint swapInterval = 1;
	CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);

	#endif

	// OpenGL initialisation from NEHE production
	m_GlView.initGl();
	m_GlView.reframe(m_Collection.boundingBox());

	// Set Opengl clipping planes
	m_GlView.setDistMinAndMax(m_Collection.boundingBox());

}

void GLWidget::paintGL()
{

	// Clear screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Load identity matrix
	glLoadIdentity();

//////////////////////////// GLC specific///////////////////////////////////////
	try
	{
		// define the light
		m_Light.enable();
		m_Light.glExecute();

		// define view matrix
		m_GlView.glExecuteCam();

		// Display the collection of GLC_Object
		m_Collection.glExecute(0, false);

		// Display UI Info (orbit circle)
		m_GlView.glExecute();
		m_MoverController.drawActiveMoverRep();
	}
	catch (GLC_Exception &e)
	{

		qDebug() << e.what();
	}
//////////////////////////End GLC specific/////////////////////////////////////

}

void GLWidget::resizeGL(int width, int height)
{

//////////////////////////// GLC specific///////////////////////////////////////
	m_GlView.setWinGLSize(width, height);	// Compute window aspect ratio
//////////////////////////End GLC specific/////////////////////////////////////

}

// Create GLC_Object to display
void GLWidget::CreateScene()
{
	QColor matBlue;
	matBlue.setRgbF(0.5, 0.8, 1.0, 1.0);

	// Create a new cylinder
	GLC_3DViewInstance instance(m_pFactory->createCylinder(1.0, 2.0));
	// Assign material to the cylinder

	instance.geomAt(0)->addMaterial(new GLC_Material(matBlue));
	// Add the cylinder to the collection

	m_Collection.add(instance);

	// Create 20 circles
	for (int i= 0; i < 20; ++i)
	{

		GLC_3DViewInstance instance(m_pFactory->createCircle(1.2 + static_cast<double>(i) / 10.0));

		m_Collection.add(instance);
	}
}

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):

		m_MoverController.setActiveMover(GLC_MoverController::Pan, e->x(), e->y());

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

		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;

	m_MoverController.move(e->x(), e->y());
	m_GlView.setDistMinAndMax(m_Collection.boundingBox());

	updateGL();
}

void GLWidget::mouseReleaseEvent(QMouseEvent *)
{
	if (not m_MoverController.hasActiveMover()) return;

	m_MoverController.setNoMover();
	updateGL();
}

Constructor:

createScene :

 

 

SourceForge.net Logo

©2005-2011 Laurent Ribon