The new GLM syntax

Note!: This page is useful only if you are working with the current master branch. You can skip this information if you are working with the version 0.9.3 that you have downloaded from this website.

The next version of openFrameworks will replace the internal math library with GLM. GLM is a solid C++ library used for all the math operations needed when doing vectors and matrices operations. The use of this library implies some change in the syntax used to declare vectors and to execute vector's operation. The legacy mode is still supported, but the new mode, enabled by default, uses the new glm syntax.

If you are not interested using this library and you want to continue using the syntax you were used to, or if you want to run an old project using the last openFrameworks master branch, you can define the OF_USE_LEGACY_MESH constant in ofConstants.h. Doing this, glm will be disabled for ofPolyline and ofMesh.

Instead, if you want to use GLM and prepare yourself for what will be the future syntax adopted by openFrameworks, these are the things that are changed:

Declaring the vectors

When declaring vectors in the header file, you have to change

ofVec3f myVector;
ofVec2f my2dVector;

To:

glm::vec3 myVector
glm::vec2 my2dVector;

When the vectors come, for example, from a mesh, you don't have to worry about the type declaration, since c++ 11 you have the wonderful auto keywords, that tells to the compiler to figure out the type for you. For example, if you have code like:

vector<ofVec3f> normals = mesh.getNormals();

And you want to migrate it to the new mode, just use auto

auto normals = mesh.getNormals();

Methods names

Methods are now plain c functions. Most methods have the exact same name but without camel case and others are slightly different:

v.length()

becomes

glm::length(v)

And

v.squaredLength()

becomes

glm::length2(v)

Containers

When using the pure glm mode (enabled by default right now) you'll usually need to change any container of ofVec to a container of glm::vec.

vector<ofVec3f> myContainer

becomes

vector<glm::vec3> myContainer

As said before, if it's in a function using auto is the best solution since the compiler can automatically detect the type, and it will work for both modes, the glm one and the legacy one.

Transitional typedefs

There's also some typedefs that can be used to make containers compatible with both the legacy and glm mode:

std::vector<ofDefaultVec3>

will be std::vector<ofVec3f> in legacy mode or std::vector<glm::vec3> in glm mode

Caveats when using ofVec

All the old ofVec (and matrices) classes autoconvert and can be created from glm so although everything in the core now returns or accepts as parameters glm you can still use ofVec even in pure glm mode:

ofVec3f pos = camera.getGlobalPosition();

will still work, although getGlobalPosition is now returning glm::vec3. but:

camera.getGlobalPosition().distance(node.getGlobalPosition());

won't work anymore you have to change it to:

glm::distance(camera.getGlobalPosition(), node.getGlobalPosition());

or

ofVec3f cameraPos = camera.getGlobalPosition();
cameraPos.distance(node.getGlobalPosition());