Example 5: Objects positioning

Now that we know how displaying several objects by uses GLC_World class which contains a GLC_3DViewInstance class and a GLC_StructOccurence which the root assembly. This example show how to construct an assembly and how to positioning objects by using transformation with GLC_Matrix4x4 class.
Only the difference compared to the example04 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>
// Light
#include <GLC_Light>
// The Viewport with a default camera
#include <GLC_Viewport>
// The world which manage product structure
#include <GLC_World>
// 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_Light m_Light;
QColor m_DefaultColor;
GLC_World m_World;
GLC_Viewport m_GlView;
GLC_MoverController m_MoverController;
//////////////////////////End GLC specific/////////////////////////////////////
};

#endif /*GLWIDGET_H_*/

1.2. glwidget.cpp

The main change is in the method CreateScene().

// Create GLC_Object to display
void GLWidget::CreateScene()
{
    GLC_StructOccurence* pRoot= m_World.rootOccurence();
    GLC_StructInstance* pInstance= NULL;

    // Create a box at the center of the scene
    GLC_3DRep box(GLC_Factory::instance()->createBox(0.1, 0.1, 0.1));
    box.geomAt(0)->replaceMasterMaterial(new GLC_Material(m_DefaultColor));
    pRoot->addChild(new GLC_StructInstance(box.clone()));

    // create Z axis representation
    GLC_3DRep cylinder(GLC_Factory::instance()->createCylinder(0.02, 2.0));
    cylinder.geomAt(0)->addMaterial(new GLC_Material(Qt::blue));
    pRoot->addChild(new GLC_StructInstance(cylinder.clone()));

    // create X axis representation
    cylinder= GLC_Factory::instance()->createCylinder(0.02, 2.0);
    cylinder.geomAt(0)->replaceMasterMaterial(new GLC_Material(Qt::red));
    GLC_Matrix4x4 matRot(glc::Y_AXIS, glc::PI/2); //Create a rotation matrix
    pInstance= new GLC_StructInstance(cylinder.clone());
    pInstance->move(matRot); // move the cylinder
    pRoot->addChild(pInstance);

    // create Y axis representation
    cylinder= GLC_Factory::instance()->createCylinder(0.02, 2.0);
    cylinder.geomAt(0)->replaceMasterMaterial(new GLC_Material(Qt::green));
    matRot.setMatRot(glc::X_AXIS, -glc::PI/2); // Set rotation matrix
    pInstance= new GLC_StructInstance(cylinder.clone());
    pInstance->move(matRot); // move the cylinder
    pRoot->addChild(pInstance);

    // Create 3 circles around axis
    GLC_3DRep circle(GLC_Factory::instance()->createCircle(2.5));
    circle.geomAt(0)->setWireColor(Qt::white);
    pRoot->addChild(new GLC_StructInstance(circle.clone()));

    circle= GLC_Factory::instance()->createCircle(2.5);
    circle.geomAt(0)->setWireColor(Qt::white);
    matRot.setMatRot(glc::Y_AXIS, glc::PI/2);
    pInstance= new GLC_StructInstance(circle.clone());
    pInstance->move(matRot); // move the circle
    pRoot->addChild(pInstance);

    circle= GLC_Factory::instance()->createCircle(2.5);
    circle.geomAt(0)->setWireColor(Qt::white);
    matRot.setMatRot(glc::X_AXIS, glc::PI/2);
    pInstance= new GLC_StructInstance(circle.clone());
    pInstance->move(matRot); // move the circle
    pRoot->addChild(pInstance);
}

In this example we create all representations with the GLC_Factory class. The 3D representation created by GLC_Factory is included in the GLC_3DRep class.
GLC_3DRep contains a list of GLC_Geometry (It's possible to have more than one body in a 3DRep).GLC_Geometry is the base class of all GLC_Lib 3D geometries.
The geometries included in GLC_3DRep can be access by the method GLC_3DRep::geomAt(int).
In GLC_Lib the assembly structure is made of GLC_StructOccurence class. See Documentation for more informations.

Tags: