QGLViewer - Commented code

Simple example

In your applications, you will mainly use the QGLViewer class, which opens an OpenGL window and lets you draw 3D geometry inside. It handles the mouse to move the camera and defines some keyboard shortcuts, thus providing the classical functions of a 3D viewer (although everything can be redefined). QGLViewer actually inherits from the Qt's QGLWidget class (use the Qt assistant to see its documentation).

Header file

A very simple application will look like simpleViewer. Here is a typical header:
class Viewer :
public QGLViewer
{
protected :
  void draw();
};
In your header file, you declare a new class (Viewer) which publicly inherits from QGLViewer and you redefine the draw() method.

Implementation file

The draw() function is the heart of your program. Your Viewer implementation is as simple as this:
#include "viewer.h"

void Viewer::draw()
{
  // Your OpenGL 3D code goes here.
  // It only consists in geometry description, the
  // GL_MODELVIEW and GL_PROJECTION are handled by the QGLViewer.
}

Main file

You then create a main function which uses the Viewer class (most main functions will be like this, simply cut and paste):
#include <qapplication.h>
#include "viewer.h"

int main(int argc,
char** argv)
{
  // Read command lines arguments.
  QApplication application(argc,argv);

  // Instantiate the viewer.
  Viewer v;

  // Make the viewer window visible on screen.
  v.show();
  
  // Set the viewer as the application main widget.
  application.setMainWidget(&v);
  
  // Run main loop.
  return application.exec();
}

You just have to concentrate on the scene description and on the OpenGL orders that represent it. All the rest comes for free : camera displacement with the mouse in revolve or walk through mode, camera keyFrames paths, screenshots, frame per seconds display, objects of the scene that can be manipulated with the mouse, and much more.

The draw() method is actually one of the functions that can be redefined to change the viewer's behavior. Beginners should read the introduction page.

Heritage versus Signals-Slots

There are actually two ways of using the QGLViewer class in your applications : In order to offer those two mechanisms, the QGLViewer class emits different signals in its key methods. When you derive and overload these functions, you replace this behavior by your own implementation.

Valid XHTML 1.0! Valid CSS! Last modified on Friday, July 1, 2005.