Home Documentation Download Screenshots Developper

simpleViewer

simpleViewer

The simplest application example: 20 lines of code and yet all the power !

A Viewer class is derived from QGLViewer and its draw() function is overloaded to specify the user defined OpenGL orders that describe the scene.

This is the first example you should try, as it explains some of the default keyboard shortcuts and the mouse behavior of the viewer.

This example can be cut and pasted to start the development of a new application.

simpleViewer.h

#include "qglviewer.h"

class Viewer : public QGLViewer
{
protected :
  void draw();
};

main.cpp

#include "simpleViewer.h"
#include <qapplication.h>

using namespace std;

static void help()
{
  cout << endl << "\t\t- -  S i m p l e V i e w e r  - -" << endl << endl;
  cout << "Use the mouse to move the camera around the object." << endl;
  cout << "You can revolve around, zoom and translate with the three buttons." << endl << endl;
  cout << "Pressing Alt and one of the function key (F1..F12) defines a camera keyFrame." << endl;
  cout << "Simply press the function key again to restore it. Several keyFrames define a" << endl;
  cout << "camera path. Paths are saved when you quit the application." << endl << endl;
  cout << "'A' displays a world axis, 'G' displays a grid, 'C' draws the camera paths and" << endl;
  cout << "'F' shows the frame rate. Press Control-S to save a screenshot in various formats." << endl << endl;
  cout << "A double click aligns the camera with the axis (left), fits the zoom (middle) or" << endl;
  cout << "re-center the scene (right). The complete mouse and keyboard bindinds are described" << endl;
  cout << "in the mouse.html and shortcuts.html documentation pages." << endl << endl;
  cout << "Press Escape to quit." << endl;
}

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();

  // Display a short help
  help();
  
  // Set the viewer as the application main widget.
  application.setMainWidget(&v);
  
  // Run main loop.
  return application.exec();
}

simpleViewer.cpp

#include "simpleViewer.h"
#include <math.h>

// Draws a spiral
void Viewer::draw()
{
  const float nbSteps = 500.0;

  glBegin(GL_QUAD_STRIP);
  for (float i=0; i<nbSteps; ++i)
    {
      float ratio = i/nbSteps;
      float angle = 20.0*ratio;
      float c = cos(angle);
      float s = sin(angle);
      float r1 = 1.0 - 0.8*ratio;
      float r2 = 0.8 - 0.8*ratio;
      float alt = ratio - 0.5;
      const float nor = .5;
      const float up = sqrt(1.0-nor*nor);
      glColor3f(1-ratio, .2 , ratio);
      glNormal3f(nor*c, up, nor*s);
      glVertex3f(r1*c, alt, r1*s);
      glVertex3f(r2*c, alt+0.05, r2*s);
    }
  glEnd();
}

Back to the main page

Valid XHTML 1.0! Valid CSS!