c++openmesh

How to set the color of a face using OpenMesh?


I'm trying to just set the face color of a certain face and my code keeps throwing an error.

The line mesh.set_color(*f_it, clr); is throwing an error (something about a property error). I've tried changing it to mesh.set_color(f_it.handle(), clr); but that throws a dereferencing error.

Am I going about setting the color correctly?

typedef OpenMesh::TriMesh_ArrayKernelT<> myMesh;
myMesh * Mesh;
myMesh mesh;

void computeFaceNormals(myMesh mesh) {
    OpenMesh::Vec3f pointA, pointB, pointC;
    myMesh::VertexIter vlt, vBegin, vEnd;
    myMesh::ConstFaceVertexIter cfvlt;
    myMesh::Color clr;

    for (myMesh::FaceIter f_it = mesh.faces_begin(); f_it != mesh.faces_end(); f_it++) {
        cfvlt = mesh.cfv_iter(*f_it);
        pointA = mesh.point(*cfvlt);
        pointB = mesh.point((*cfvlt++));
        pointC = mesh.point((*cfvlt++));

        clr[0] = 0;
        clr[1] = 1;
        clr[2] = 0;

        mesh.set_color(*f_it, clr);
    }

}

Solution

  • The openmesh Mesh (OpenMesh::TriMesh_ArrayKernelT) can work with different properties: vertex-colors, face-colors, vertex-normals and so on. But you need to specify explicitly the properties that you want the Mesh to have.

    In your case, what you are missing is a mesh.request_face_colors();

    If you receive the mesh as an argument, you can check whether it already has a color proterty with: mesh.has_face_colors()

    You can also remove properties using: mesh.release_face_colors();

    Read the tutorial for more details. An important note you should consider (from the tutorial) that clarifies the usage of request/has/release:

    But, what happens if for example the vertex status property has been requested twice? Then the first release does nothing, but the second one will remove it. The standard properties have a reference counter, which is incremented by one for each request and decremented by one for each release. If the counter reaches 0 the property will be removed from memory.