Information about vertex normals / specular reflections

Dear OF community,

I’m trying to recreate in OF a project I did with a friend in 2005 using OGRE game engine

For this I need access to the per-vertex normal data. Could anyone please suggest what is the best way of achieving this in OF?

Thank you!

Hi @konstantin, you’ve probably figured this out by now, but I thought I’d reply to your post. Have a look at the ofMesh and ofMeshFace sections of the 3d page in the documentation. You can add a normal with .addNormal(), get a normal with .getNormal(), and set an existing one with .setNormal(). You can also get a vector of ofMeshFace (a triangle) with ofMesh::getUniqueFaces(), modify the normals (and anything else), and then update the ofMesh with .setFromTriangles().

1 Like

Hi @konstantin,
adding to what @TimChi said, the ofMesh will not generate normals by default. You have to add these. There is an example in examples/3d/normalsexample which shows how to calculate and add normals.
In case you already have the normals and what you want is to access all the normals you can call getNormals(); which will return a reference to the vector containing the normals, thus if you modify these, you’ll be modifying the ones in the mesh. Typically there should be a direct relationship between the vertices and the normals. I mean, vertex at index i will use normal at index i.

Keep in mind that when calling getNormals() or getVertices() you’ll get a reference, which will modify the ones contained in the mesh, yet it is quite easy to generate a copy instead.

ofMesh mesh;

// the following will generate a reference. Thus anything you do to n will  modify the mesh normals.
auto &n = mesh.getNormals();
// yet the following will make a copy and anything you do to n WILL NOT modify the mesh normals.
// The big difference is the &
auto n = mesh.getNormals();

/// As well, you can use range loops, where the same applies

// the following will normalize the mesh normals
for(auto& n : mesh.getNormals())
    n = glm::normalize(n);

// the following will NOT normalize the mesh normals
for(auto n : mesh.getNormals())
    n = glm::normalize(n);
1 Like