Example 3: Cylinder display

From example02 (Point of view handling) replace the circle by a cylinder GLC_Cylinder and add a light GLC_Light to the scene.
Only the difference compared to the example02 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 Mover controller is used to change the point of view
#include <GLC_MoverController>

#include <GLC_3DViewCollection>

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

class GLWidget : public QGLWidget
{
public:
GLWidget(QWidget *p_parent);
~GLWidget();

private:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
//Mouse events
void mousePressEvent(QMouseEvent * e);
void mouseMoveEvent(QMouseEvent * e);
void mouseReleaseEvent(QMouseEvent * e);
private:
//////////////////////////// GLC specific///////////////////////////////////////
GLC_3DViewInstance m_Cylinder;
    GLC_3DViewCollection m_Collection;
GLC_Light m_light;
GLC_Viewport m_GlView;
GLC_MoverController m_MoverController;
//////////////////////////End GLC specific/////////////////////////////////////
};

#endif /*GLWIDGET_H_*/

1.1.1. Private members

  • GLC_Cylinder : The cylinder to display.
  • GLC_Light : The light of the scene.

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 "glwidget.h"

#include <GLC_UserInput>
#include <GLC_Context>

// GLC_Lib Exception
#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(new GLC_Context(QGLFormat(QGL::SampleBuffers)),p_parent)
    , m_Cylinder(GLC_Factory::instance()->createCylinder(1.0, 2.0)) // Cylinder definition.
    , m_Collection()
    , m_light()
    , m_GlView()
    , m_MoverController()
{
    connect(&m_GlView, SIGNAL(updateOpenGL()), this, SLOT(updateGL()));
    // Set cylinder material
    QColor matBlue;
    matBlue.setRgbF(0.5, 0.8, 1.0, 1.0);
    m_Cylinder.geomAt(0)->addMaterial(new GLC_Material(matBlue));
   
    m_Collection.add(m_Cylinder);
   
    // Set up mover controller
    QColor repColor;
    repColor.setRgbF(1.0, 0.11372, 0.11372, 1.0);
    m_MoverController= GLC_Factory::instance()->createDefaultMoverController(repColor, &m_GlView);
   
    // Set iso view
    m_GlView.cameraHandle()->setIsoView();
}

GLWidget::~GLWidget()
{
   
}

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
    m_GlView.initGl();
    // Reframe to the cylinder bounding Box
    m_GlView.reframe(m_Collection.boundingBox());
}

void GLWidget::paintGL()
{
    // Clear screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
    // Load identity matrix
    GLC_Context::current()->glcLoadIdentity();
    try
    {
        // Set the opengl clipping plane
        m_GlView.setDistMinAndMax(m_Cylinder.boundingBox());
       
        // define the light
        m_light.glExecute();
       
        // define view matrix
        m_GlView.glExecuteCam();
       
        // Display the cylinder
        m_Collection.render(0, glc::ShadingFlag);
       
        // Display UI Info (orbit circle)
        m_MoverController.drawActiveMoverRep();
    }
    catch (GLC_Exception &e)
    {
        qDebug() << e.what();
    }
}

void GLWidget::resizeGL(int width, int height)
{
    m_GlView.setWinGLSize(width, height); // Compute window aspect ratio
   
}
void GLWidget::mousePressEvent(QMouseEvent *e)
{
    if (m_MoverController.hasActiveMover()) return;
    switch (e->button())
    {
    case (Qt::RightButton):
        m_MoverController.setActiveMover(GLC_MoverController::TrackBall, GLC_UserInput(e->x(), e->y()));
        updateGL();
        break;
    case (Qt::LeftButton):
        m_MoverController.setActiveMover(GLC_MoverController::Pan, GLC_UserInput(e->x(), e->y()));
        updateGL();
        break;
    case (Qt::MidButton):
        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())
    {
        m_MoverController.move(GLC_UserInput(e->x(), e->y()));
        // Update opengl clipping plane
        m_GlView.setDistMinAndMax(m_Cylinder.boundingBox());
        updateGL();
    }
}

void GLWidget::mouseReleaseEvent(QMouseEvent*)
{
    if (m_MoverController.hasActiveMover())
    {
        m_MoverController.setNoMover();
        updateGL();
    }
}

1.2.1. Constructor

Initialization :

  • m_Cylinder an instance of a GLC_Cylinder construct by the factory.
  • m_Collection is a part of GLC_lib scene graph. It contains the list of instances to render.
  • m_Light is initialized with default value : see GLC_Light.

Body :

  • Set matBlue as m_pCylinder's diffuse color.
  • Set m_GlView.cameraHandle() to Iso View

1.2.2. initializeGL

  • Reframe the scene: see GLC_Viewport::reframe.
  • Set near and far clipping plane : see GLC_Viewport::setDistMinAndMax.

1.2.3. mouseMouveEvent

In Zoom and Orbit mode set near and far clipping plane : see GLC_Viewport::setDistMinAndMax.

Tags: