Tutorial 3D Scenes

1. Examples

If you prefer directly jumping to example code, see:

  • Example:opengl_objects_demo
  • Example: display3D.
    See C++ code

2. Classes

The main class behind 3D scenes is mrpt::opengl::COpenGLScene, which allows the user to create, load, save, and render 3D scenes using OpenGL primitives. The class can be understood as a program to be run on the OpenGL system, containing a sequence of viewport definitions, rendering primitives, etc…

An “OpenGL scene” contains from 1 up to any number of Viewports, each one associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes. An object of COpenGLScene always contains at least one viewport (mrpt::opengl::COpenGLViewport), named “main” (if you don’t provide a viewport name, “main” will be used always as default, so don’t worry on remembering that name). Optionally, any number of other viewports may exist.

Viewports are referenced by their names, case-sensitive strings. Each viewport contains a different 3D scene (i.e. they render different objects), though a mechanism exists to share the same 3D scene by a number of viewports so memory is not wasted replicating the same smart pointers (seeCOpenGLViewport::setCloneView).

The main rendering method, COpenGLScene::render(), assumes that a viewport has been set-up for the entire target window. That method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate member in COpenGLViewport.

A typical user will not need to invoke COpenGLScene::render() manually, but use instead the 3D standalone viewer (SceneVieweror the runtime 3D display windows (mrpt::gui::CDisplayWindow3D).

An object COpenGLScene can be saved to a “.3Dscene” file using COpenGLScene::saveToFile(), for posterior visualization from the standalone application SceneViewer.

Below follows a table with the preview of most rendering primitive classes (the most up-to-date version should be always found here):

3. Viewports

Each scene can comprise of any number of viewports, as described above. The following snapshot shows a scene with two viewports: The “main” viewport, whose extension is the whole window. A second, smaller one, in the bottom-left corner. In this example, this viewport clones the contents of the main one, but this behavior is optional.

Another example of the usage of viewports is this program.


Search C++ API:



Latest blog entries

Recent activity



web stats