Colouring a mesh

Firstly, I want to thank everyone who contributes to this board, I’ve learnt a lot in the short time I’ve been coding from searching here whenever I have an issue. Unfortunately, I haven’t been able to figure this one out, and I would greatly appreciate any guidance.

I am trying to colour this mesh. In the code, I’m just adding red at each vertex, but the whole mesh isn’t being filled. When I run the code, it flickers between the two images below.

My hunch is there’s something wrong with my line, or I need to add indices?

I was trying to implement this piece of code to create a mesh along a path:

Here is my code:

ofPolyline bezierPolyline;
ofPolyline resampledBezier;

void ofApp::setup(){
    resampledBezier = bezierPolyline.getResampledBySpacing(5);

void ofApp::draw(){
    ofMesh lineMesh;
    float widthSmooth = 10;
    float angleSmooth;
    for (int i = 0; i < resampledBezier.getVertices().size(); i++) {
        int me_m_one = i - 1 ;
        int me_p_one = i + 1;
        if (me_m_one < 0) me_m_one = 0;
        if (me_p_one == resampledBezier.getVertices().size()) me_p_one = resampledBezier.getVertices().size()-1;

        ofPoint diff = resampledBezier.getVertices()[me_p_one] - resampledBezier.getVertices()[me_m_one];
        float angle = atan2(diff.y, diff.x);

        if (i == 0) angleSmooth = angle;
        else {
            angleSmooth = ofLerpDegrees(angleSmooth, angle, 1.0);
        ofPoint offset;
        offset.x = cos(angleSmooth + PI/2) * widthSmooth;
        offset.y = sin(angleSmooth + PI/2) * widthSmooth;
        lineMesh.addColor(ofFloatColor(1.0, 0.0, 0.0));
        lineMesh.addVertex(resampledBezier.getVertices()[i] +  offset);
        lineMesh.addVertex(resampledBezier.getVertices()[i] -  offset);



The sketch flickers between these two states:

1 Like

You should add a color for each vertex:

        lineMesh.addColor(ofFloatColor(1.0, 0.0, 0.0));
        lineMesh.addVertex(resampledBezier.getVertices()[i] +  offset);
        lineMesh.addColor(ofFloatColor(1.0, 0.0, 0.0));
        lineMesh.addVertex(resampledBezier.getVertices()[i] -  offset);

addColor is adding an attribute for each vertex, it is not like setColor


Well, that was easy. Thank you!