Home Documentation Download Screenshots Developer

libQGLViewer examples

Many small pedagogical examples illustrate some functionalities of the viewer and are provided for understanding and cut and paste. New users should start by the simpleViewer to discover some of the features of QGLViewer. All the examples are installed in /usr/share/doc/QGLViewer/examples/ by the rpm.

Also read the introduction page and the commented example to understand the philosophy of the library.

Short description of all the examples

Compilation

With the RPM, the example source files are in /usr/share/doc/QGLViewer/examples and you have to copy the desired example(s) directories to a place where you have write permission before compiling it. If you have downloaded the sources (.tar.gz, .zip) instead, simply go to the examples directory.

Use the following code to compile all the examples (can also be done in a specific example directory to compile only this example) :
> cd examples
> qmake [INCLUDE_DIR=...] [LIB_DIR=...]
> make
The optional INCLUDE_DIR and LIB_DIR parameters are needed if you customized your make install.

You may prompted an "error while loading shared libraries" message when trying to execute an example. In that case, you probably forgot to add the directory of the library to your default library path. On unix, this is done like this (on SGI IRIX, replace LD_LIBRARY_PATH by LD_LIBRARYN32_PATH):

[bash] > export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${HOME}/PathToQGLViewer/lib
[tcsh] > setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${HOME}/PathToQGLViewer/lib

Note that this should not be necessary when the library has been installed in the standard directory with make install (see the download page) or using the RPM.

Windows users have to compile each example separately, using the Open (or Generate) Qt project icon. Check that the libQGLViewer files can be found in the Tools/Options/Directories/ Include Files and Library Files directories. Qt 2.3 users have to edit the examples .pro files as is detailed in these files.

Detailed description

3dsViewer 3dsViewer

The LGPL lib3ds library is used to load and display a 3ds scene.

You need to install the lib3ds library (version 1.2) in order to compile this file. See http://lib3ds.sourceforge.net/.

The current version (Ver 1.2, Dec 2002) of the lib3ds library is flawed and a patched version is available on the libQGLViewer rpm page.

Alternately, you can add the following line in lib3ds/file.h : extern LIB3DSAPI void lib3ds_file_bounding_box(Lib3dsFile *file, Lib3dsVector min, Lib3dsVector max);

Once this is done, edit 3dsViewer.pro, set LIB3DS_INSTALLED to yes and set INCLUDEPATH and LIBS to the path where you installed lib3ds.

Press 'L' (load) to load a new 3DS scene.

animation

The animate() function illustrated by a water particle simulation.

When animation is activated (the Return key toggles animation), the animate() and then the draw() functions are called in an infinite loop.

You can tune the frequency of your animation (default is 25Hz) using setAnimationInterval(). The frame rate will then be fixed, provided that your animation loop function is fast enough.

animation
callback callback

Using the QT callback signal-slot to connect a QGLViewer and your scene.

This very simple application uses the QT callback signal-slot mechanism to link the QGLViewer and your Scene. The drawNeeded() QGLViewer signal is connected to the Scene drawScene() slot.

constrainedCamera

Constraints applied on the Camera Frame to limit its translation and/or rotation.

Try the different possible constraints using the T (translate) and R (rotate) keys. G and D change the constraint directions. Press Space to change the coordinate system (World or Camera) which defines the constraint directions.

Note that in order to apply a constraint to a camera, you need to apply it to the camera()->frame().

constrainedCamera
constrainedFrame constrainedFrame

Constraints can limit the translation and/or rotation of a (Manipulated)Frame.

Try the different possible constraints using the T (translate) and R (rotate) keys. G and D change the constraint directions. Press Space to change the coordinate system (World, Camera or Local) which defines the constraint directions.

Press the Control key while moving the mouse to move the camera.

draw3DText

An icosahedron is used to illustrate the draw3DText() function.

Use this function to draw text as if it was a normal object of your 3D scene. You simply have to define the text position and its normal.

draw3DText
drawLight drawLight

The drawLight() function is a light debugging tool.

drawLight() takes as an argument the GL index of the lamp : GL_LIGHT0, GL_LIGHT1... and displays a symbolic representation of the light. This function is usefull for debugging your light setup.

fastDraw

The fastDraw() function keeps interactivity even with large scenes.

This example demonstrates the use of the fastDraw() function, which is called when the camera moves. This function is usefull for displaying very complex scene, while keeping an interactive camera motion.

fastDraw
frameTransform frameTransform

Coordinate transformation between different Frames.

This example illustrates the camera and world coordinate systems relationship. The position of the camera in the world coordinate system is printed from Camera::position(). Symetrically, the position of the world origin is given in the camera coordinate system by camera()->cameraCoordinatesOf(0,0,0).

Three sets of lines (red, green, blue) are drawned. They have different starting points, but common end points, located on a circle in the XY plane.

All the red lines start from the camera position, and will hence always be aligned with the viewing direction, making them invisible.

The green lines starting points' positions are determined from the camera coordinate system, using camera()->worldCoordinatesOf(). As a result, these points will seem to be fixed on the screen, even when the camera is moved

Finally, the blue lines are classically defined in the world coordinate system, and will move with the camera.

Beautiful Moire pattern can be obtained with a proper rotation.

interface

Adding a graphical user interface around a QGLViewer.

The same example as simpleViewer with an interface designed with designer. The interface.ui file is the description of the interface resulting of designer. The interface.cw file is the description of the signals and slots available. Here we use three slots and three signals to connect to and from the interface and the viewer.

The designer cw files (custom widget), describing all the available signals and slots of the libQGLViewer classes are available in the QGLViewer include directory.

interface
keyFrames keyFrames

The KeyFrameInterpolator test example.

KeyFrameInterpolator smoothly interpolate their attached Frame over time on a path defined by Frames. The interpolation can be started/stopped/reset, played in loop, played at a different speed, etc...

In this examples, the path is defined by four ManipulatedFrame which all can be moved with the mouse. The interpolating path is updated accordingly.

The path and the interpolating axis are drawn using KeyFrameInterpolator::drawPath().

By default, the Camera holds 12 KeyFrameInterpolator, binded to the F1-12 keys. Use Alt-Fx to define a new keyFrame for path x, and simply press Fx to play/pause the path x. See the shortcuts page for details.

luxo

A more complex exemple that combines manipulatedFrames, selection and constraints.

This exemple illustrates different functions of the viewer. It displays a famous luxo lamp (Pixar) that can be interactively manipulated with the mouse. It illustrates the use of several ManipulatedFrames in the same scene.

Shift click on any part of the lamp to select it, and then move it with the mouse. To move the camera select the background or press the Control key (default modifier keys are switched).

A simpler object selection example is given in select.
A simpler frame displacement example is given in manipulatedFrame.
A simpler constrained frame example is given in constrainedFrame.

luxo
manipulatedFrame manipulatedFrame

A ManipulatedFrame can be moved with the mouse in the scene.

A manipulatedFrame converts mouse gestures into Frame displacements. Adding two lines of code will allow you to move any object of your scene using the mouse. The button bindings of the ManipulatedFrame are the same as for the camera. Spinning is possible.

The frame is drawned at its position using a glMultMatrix of the frame associated matrix(). This matrix is changed when the mouse moves, thus changing the position of the frame.

mouseGrabber

Illustration of the MouseGrabber class.

A MouseGrabber is an abstract class which defines objects that are sensitive to the mouse motion. When activated, they grab the mouse events. Possible applications are interactive 2D or 3D GUI, object auto-selection, auto drop-down menu and much more.

In this example, MouseGrabbers are associated with Camera keyFrame paths or position. Clicking a MouseGrabber will play (resp. restore) the Camera path (resp. position).

mouseGrabber
multiView multiView

A multi-view application, with constrained camera displacements.

Four viewers are created, each displaying the same scene. The camera displacements are constrained for three of the viewers to create the classical top, front, side views. The last viewer is a classical 3D viewer.

screenCoordSystem

A saucers control viewer that illustrates the screen coordinate system feature.

Use startScreenCoordinatesSystem() and stopScreenCoordinatesSystem() to set this mode. Once this mode has been activated in draw(), the X,Y coordinates correspond to the pixel units (origin in the lower left corner). Combined with the camera()->projectedCoordinatesOf(), this feature enable the mix of 2D and 3D drawing.

In this example, the arrows that designate the saucers seem to be attached to the object.

screenCoordSystem
select select

Selection of objects of the scene using select() and an OpenGL GL_SELECT render mode.

Use the select() callback function to implement your object selection function. This examples is based on a generic GL_SELECT implementation that can easily be cut and pasted in your applications.

Analytic intersection computations are also possible once the screen coordinates have be converted to a half line using convertClickToLine(). Make a selection and then move the camera to see a representation of the intersection line.

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

Illustration of the SpinningFrame class.

A SpinningFrame is useful to animate Frames that have a rotation motion. An optional rotationCenter can be provided to create excentered motions.

stereoViewer

Simply press S to toggle stereo display (architecture dependent).

This is a simple stereo application inspired from simpleViewer. The constructor is called with a QFormat stereo option to enable stereo. Press 'S' to toggle stereo display.

stereoViewer
textureViewer textureViewer

Loads image files and textures map polygons with it.

Pedagogical example that shows how to apply a texture on a 3D object. Inpired from the QT texture example.

The Qt QImage class and its convertToGLFormat() function are used to load any image format. The image is resized so that its dimensions are powers of two if needed. Feel free to cut and paste.

triSetConstraint

A TriangleSetConstraint constrains the camera displacement.

The camera is constrained to stay above a given 'road' defined by triangles. Camera is in FLY mode.

The example is a little bit complex : the computations needed to get the intersection of the Z-direction and the triangle are cut and pasted from the triangleSetConstraint class, in order to draw interesting debugging informations.

triSetConstraint
x3dViewer x3dViewer

The X3D Toolkit library is used to load and display a x3d scene.

You need to install the X3D library in order to compile this file. See the Yannick Le Goc web site (or this page).

Once this is done, edit x3dViewer.pro, set LIBX3D_INSTALLED to yes and set INCLUDEPATH and LIBS to the path where you installed libX3D.

Press L (load) to load a new 3DS scene.

Valid XHTML 1.0! Valid CSS!