Colouring a mesh

#1

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(){
    
    bezierPolyline.addVertex(ofPoint(600.67,151.56));
    bezierPolyline.bezierTo(650.89,101.33,732.33,101.33,782.56,151.56);
    bezierPolyline.addVertex(ofPoint(782.56,151.56));
    bezierPolyline.bezierTo(832.78,201.78,832.78,283.22,782.56,333.44);
    bezierPolyline.addVertex(ofPoint(782.56,333.44));
    bezierPolyline.addVertex(ofPoint(600.02,515.98));
    
    resampledBezier = bezierPolyline.getResampledBySpacing(5);
    
}

void ofApp::draw(){
    
    ofMesh lineMesh;
    
    lineMesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
    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);
        
    }

    lineMesh.draw();

}

The sketch flickers between these two states:

1 Like
#2

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

2 Likes
#3

Well, that was easy. Thank you!