Home | Documentation | Download | Screenshots | Developer |
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.
#include <QGLViewer/qglviewer.h> class Viewer : public QGLViewer { protected : virtual void draw(); virtual void init(); virtual QString helpString() const; };
#include "simpleViewer.h" #include <math.h> using namespace std; // 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 = 21.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.0-ratio, 0.2 , ratio); glNormal3f(nor*c, up, nor*s); glVertex3f(r1*c, alt, r1*s); glVertex3f(r2*c, alt+0.05, r2*s); } glEnd(); } void Viewer::init() { // Restore previous viewer state. restoreFromFile(); help(); } QString Viewer::helpString() const { QString text("<h2>S i m p l e V i e w e r</h2>"); text += "Use the mouse to move the camera around the object. "; text += "You can revolve around, zoom and translate with the three buttons. "; text += "Left and middle buttons pressed together rotate around the camera z axis<br><br>"; text += "Pressing <b>Alt</b> and one of the function key (<b>F1</b>..<b>F12</b>) defines a camera keyFrame. "; text += "Simply press the function key again to restore it. Several keyFrames define a "; text += "camera path. Paths are saved when you quit the application.<br><br>"; text += "Press <b>F</b> to display the frame rate, <b>A</b> for the world axis, "; text += "<b>Alt+Return</b> for full screen mode and <b>Control+S</b> to save a snapshot.<br><br>"; text += "A double click aligns the closer axis with the camera (left, if close enough), fits "; text += "the zoom of the camera (middle) or re-center the scene (right).<br>"; text += "A left double click while right button is pressed defines the <i>revolveAroundPoint</i>."; text += "With middle button pressed instead, it zooms on the pixel.<br><br>"; text += "See also the <b>Shortcuts</b> and <b>Mouse</b> tabs and the documentation web pages.<br>"; text += "Press <b>Escape</b> to exit the viewer."; return text; }
#include "simpleViewer.h" #include <qapplication.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(); }
Go back to the examples main page