Iterate trough ofMesh vertices

Hello everyone,

I’m drawing some meshes from polylines using tessellateToMesh().
After doing that, I want to iterate to all the vertices and set their color according to some calculated value (i.e.: the distance to the next vertex), but I’m constantly getting what I think is a bad pointer error:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

the loop I’m using is this one:

       ofMesh mesh;
        tess.tessellateToMesh(polyline, (ofPolyWindingMode) 1, mesh);
        for(unsigned int j = 0; j < mesh.getVertices().size(); j++) {
            mesh.setColor(j, ofColor(0.1,0.5,0.9));

Am I doing something clearly wrong that i just can’t see?

Yes, it means that you are trying to access by index the vertex of a mesh that does not exists. It is highly possible that your mesh is empty.

But doesn’t

tess.tessellateToMesh(polyline, (ofPolyWindingMode) 1, mesh);

create a mesh in my ofMesh object?
Later I draw the mesh and it works. I just can’t for the life of me set the vertex colors…

The mesh you create doesn’t necessarily have any colors yet. You can call ofMesh::getNumColors() to find out how many colors there are stored, and you can add new ones with ofMesh::addColor() or ofMesh::addColors().

The fact that there are vertices doesn’t imply there’s the same number of colors, indices, normals or texCoords.

Thanks @hamoid, now it’s a bit clearer.
Sorry to chain in with a question that is only just partially related. but do you know how to pass vertices colors to a vert shader?

Looking at the default shaders might shed some light:

But… I’m not sure what do you mean exactly. If you draw the mesh, and the mesh has vertex colors, they should be passed to the shader, right?

If you draw the mesh, and the mesh has vertex colors, they should be passed to the shader, right?

Yes, that’s true. I had a very complicated idea involving interpolation modes in the frag shader. But I think I have to set that aside for now :slight_smile: and go back to the basics!

The color get linearly interpolated during the rasterisation process.

Let’s say that you create a mesh composed by 3 vertices, and that you set as mode OF_PRIMITIVE_TRIANGLES. Let’s say that you also want to add the attribute color for each vertex, using “addColor” as you were doing. Let’s assume that vertex is red, one is green and one is blue. That means that each angle of the triangle will have a different color.

During the rasterisation process, the triangle originally composed by 3 vertices connected together becomes pixels on the screen. The attributes (and therefore the colors) of each vertex get interpolated. If a pixel, for example, end up being in the middle between the blue angle and the red angle, its color will be violet.
If you look for “rendering pipeline” you can find way more detailed information about what’s going on.

I will!
thanks a ton you for your help