Examples » Primitives

Importing mesh data, 3D transformations and input handling.


This example shows how to easily create a GL::Mesh from arbitrary external data and do basic mouse interaction in a 3D scene.

Setting up

The new includes are for a Primitives::cubeSolid(), which returns a Trade::MeshData, which we then turn into a GL mesh using MeshTools::compile(). And because we have a 3D scene now, we'll want to draw it with Shaders::PhongGL instead of plain flat shading.

#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/Math/Color.h>
#include <Magnum/Math/Matrix4.h>
#include <Magnum/MeshTools/Compile.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Primitives/Cube.h>
#include <Magnum/Shaders/PhongGL.h>
#include <Magnum/Trade/MeshData.h>

This time, for user interactivity we'll need to implement mouse event handlers and store transformation & projection Matrix4 plus the current Color3.

class PrimitivesExample: public Platform::Application {
        explicit PrimitivesExample(const Arguments& arguments);

        void drawEvent() override;
        void mousePressEvent(MouseEvent& event) override;
        void mouseReleaseEvent(MouseEvent& event) override;
        void mouseMoveEvent(MouseMoveEvent& event) override;

        GL::Mesh _mesh;
        Shaders::PhongGL _shader;

        Matrix4 _transformation, _projection;
        Color3 _color;

Because we are displaying a 3D scene, we need to enable depth test to have the cube rendered in proper Z order. The GL::Renderer::Feature::FaceCulling is not strictly needed, but for closed meshes it can save a lot of rasterization work by discarding triangles that face away from camera.

PrimitivesExample::PrimitivesExample(const Arguments& arguments):
    Platform::Application{arguments, Configuration{}
        .setTitle("Magnum Primitives Example")}

Preparing the mesh

Perhaps the biggest leap from previous examples is that setting up the mesh is now just a simple oneliner:

    _mesh = MeshTools::compile(Primitives::cubeSolid());

The main responsibility here lies on Trade::MeshData, which is a class capable of describing arbitrary meshes in arbitrary layouts. It's used as the backbone of the Primitives library as well as of Magnum's asset import pipeline, which will be shown in the next Model Viewer example.

The Primitives::cubeSolid() gives us an indexed triangle mesh with positions and normals. MeshTools::compile() then takes the Trade::MeshData, uploads it to the GPU, and pops out a GL::Mesh that owns the index and vertex buffers and has all attributes configured to be rendered with builtin shaders.

While this admittedly skipped quite a few intermediate steps compared to manual mesh setup from the previous examples, in practice you'll mostly either deal with raw attribute arrays or already have a Trade::MeshData to begin with. Nevertheless, it's not an opaque container — be sure to go through its documentation to understand how the data is represented, how the attributes can be accessed and how it all maps to a GL::Mesh.

Transformation and projection

As a final step in the constructor we specify the initial cube transformation, rotating it slightly for a 3D look. Earlier we used color literals from Math::Literals, here we use angle literals. Magnum has strongly-typed angles, which allows us to conveniently use degrees even though APIs such as Matrix4::rotationX() accept radians:

    _transformation =

Then we set up a perspective projection with an aspect ratio matching the window size and an initial color with an orange hue. For a more thorough introduction to Magnum's vector math library please see Operations with matrices and vectors and 2D and 3D transformations.

    _projection =
            35.0_degf, Vector2{windowSize()}.aspectRatio(), 0.01f, 100.0f)*
    _color = Color3::fromHsv({35.0_degf, 1.0f, 1.0f});


In the draw event we clear the framebuffer, now also clearing the depth buffer as we need it for the depth test. For the shader we hardcode a directional light source, set the cached color, transformation and projection matrices including a normal matrix and draw the mesh.

void PrimitivesExample::drawEvent() {

    _shader.setLightPositions({{1.4f, 1.0f, 0.75f, 0.0f}})
        .setAmbientColor(Color3::fromHsv({_color.hue(), 1.0f, 0.3f}))


Mouse event handling

On a click (press + release) we want to shift the color hue and on mouse drag we'll rotate the object using a bunch of vector and matrix operations.

To save resources, the application doesn't redraw constantly by default, which means we have to trigger a redraw() in order to see any effect from the events. In case you'd want to draw constantly — for example in order to perform an animation — you can call redraw() directly in the drawEvent().

void PrimitivesExample::mousePressEvent(MouseEvent& event) {
    if(event.button() != MouseEvent::Button::Left) return;


void PrimitivesExample::mouseReleaseEvent(MouseEvent& event) {
    _color = Color3::fromHsv({_color.hue() + 50.0_degf, 1.0f, 1.0f});


void PrimitivesExample::mouseMoveEvent(MouseMoveEvent& event) {
    if(!(event.buttons() & MouseMoveEvent::Button::Left)) return;

    Vector2 delta = 3.0f*Vector2{event.relativePosition()}/Vector2{windowSize()};

    _transformation =



The main function is just the macro call, as previously.



Compilation is pretty straigtforward, similar to the Triangle example. Again omitting the basic setup described in the Getting Started Guide, we now use also the MeshTools, Primitives and Trade libraries:

find_package(Corrade REQUIRED Main)
find_package(Magnum REQUIRED


add_executable(magnum-primitives WIN32 PrimitivesExample.cpp)
target_link_libraries(magnum-primitives PRIVATE

You can now try using different primitives from the Primitives namespace or render the mesh with a different shader from Shaders namespace. The full file content is linked below. Full source code is also available in the magnum-examples GitHub repository.

The ports branch contains additional patches for iOS, Android and Emscripten support that aren't present in master in order to keep the example code as simple as possible.