Example 1 : Circle Display

This example show how using GLC_lib to display a circle with the class GLC_Circle.

1. GLWidget class

GLWidget class inherits QGLWidget. This class is a Widget for rendering OpenGL graphics. (See QT4 reference)
This is the class in which we include code to display the circle.

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>
// The Viewport with a default camera
#include <GLC_Viewport>
//////////////////////////End GLC specific/////////////////////////////////////

class GLWidget : public QGLWidget
{

public:
GLWidget(QWidget *p_parent);
~GLWidget();


private:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);


private:
//////////////////////////// GLC specific///////////////////////////////////////
GLC_3DViewInstance m_Circle;
GLC_Viewport m_GlView;
//////////////////////////End GLC specific/////////////////////////////////////

};

#endif /*GLWIDGET_H_*/

1.1.1. Includes

The first header to include is GLC_Factory to built all GLC_lib geometry, material and texture.
The second header is GLC_Viewport to manage GLC_lib viewport.

1.1.2. Private members

After we have to declare these variables :

  • GLC_3DViewInstance (the circle to display)
  • GLC_Viewport (the application viewport)

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 <GLC_Context>

#include "glwidget.h"

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

GLWidget::GLWidget(QWidget *pParent)
: QGLWidget(new GLC_Context(QGLFormat(QGL::SampleBuffers)), pParent)
, m_Circle(GLC_Factory::instance()->createCircle(0.2))  // Circle radius
, m_GlView()
{
m_Circle.geomAt(0)->setWireColor(Qt::white);
}

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

    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.render();
//////////////////////////End GLC specific/////////////////////////////////////

}

void GLWidget::resizeGL(int width, int height)
{
//////////////////////////// GLC specific///////////////////////////////////////
m_GlView.setWinGLSize(width, height); // Compute window aspect ratio
//////////////////////////End GLC specific/////////////////////////////////////
}

1.2.1. Constructor

  • m_Circle variable is initialized with an instance of a GLC_Circle construct by the factory.

1.2.2. initializedGL

  • Call the m_GlView.initGL() to initialized the view with default values.

1.2.3. paintGL method

  • Clear the color buffer and the depth buffer of the view.
  • Set the current transformation matrix with the identity matrix.
  • Call m_GlView.glExecuteCam() to set OpenGL View to m_GlView's camera properties.
  • Call m_Circle.render() to draw the circle.

1.2.4. resizeGL

  • Call m_GlView.setWinGLSize to compute the aspect ratio and projection matrix of the view.

In this example we take the default camera position. Have a look to GLC_Camera constructor.

2. MainWindow Class

QT4 main application window.
Nothing specific to add for GLC_lib

2.1. mainwindow.h

#ifndef MAINWINDOW_H_
#define MAINWINDOW_H_

#include <QMainWindow>

class GLWidget;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

//private members
private:
GLWidget *p_GLWidget;

};

#endif /*MAINWINDOW_H_*/

2.2. mainwindow.cpp

#include <QtGui>
#include "mainwindow.h"
#include "glwidget.h"

MainWindow::MainWindow()
: p_GLWidget(NULL)
{
p_GLWidget= new GLWidget(this);

setCentralWidget(p_GLWidget);
}

3. Application

#include <QtGui>
#include "mainwindow.h"
#include "glwidget.h"

MainWindow::MainWindow()
: p_GLWidget(NULL)
{
p_GLWidget= new GLWidget(this);

setCentralWidget(p_GLWidget);
}

Tags: