How to draw unfilled ofPath through ofMesh?

Hi, I’m trying to draw unfilled ofPath through ofMesh.
But the following code below does not draw anything.

void ofApp::draw(){

ofPath path;
path.setFilled(false);
path.setStrokeWidth(2);
path.rectangle(ofGetWidth()/2, ofGetHeight()/2, 100, 100);

ofMesh mesh = path.getTessellation();
mesh.draw();

}

If I set “path.setFilled(true)”, then it draws a filled rectangle, but nothing appears if I set “path.setFilled(false)”.
Could anyone please give me any advice? Thanks!

when you use only stroke then ofPath uses a set of ofPolylines to draw not ofMesh. you can get them with getPolylines

Thanks Arturo.

But I can’t find where the ‘getPolylines’ method is… Is it part of ofPath?
I can’t find it here http://openframeworks.cc/documentation/graphics/ofPath/

oh sorry, it’s getOutlines

Thank you!

So, is this a correct way to do it?

//in ofApp.h
vector< ofPolyline > polylines;

//in setup()
ofPath path;
path.setFilled(false);
path.setStrokeWidth(2);
path.rectangle(ofGetWidth()/2, ofGetHeight()/2, 100, 100);
polylines = path.getOutline();

//in draw()
for (int i=0; i < polylines.size(); ++i)
polylines[i].draw();

It seems like it’s more CPU efficient than drawing directly with ofPath although I don’t know why.
Also, it’s way slower than drawing a filled ofPath through ofMesh.

you can also just do path.draw() and it’ll choose path or polylines depending on what it’s using. and yes creating the path in setup and then drawing it everytime is way faster than recreating the path every frame, 60 times per second.

usually drawing lines can be slower than drawing triangles so that might be why drawing the outlines is slower than drawing the mesh

Thanks!

What I meant was the code above is faster (when drawing) than the one below although this one looks simpler.

//in ofApp.h
ofPath path;

//in setup()
path.setFilled(false);
path.setStrokeWidth(2);
path.rectangle(ofGetWidth()/2, ofGetHeight()/2, 100, 100);

//in draw()
path.draw();

path.draw is also setting the color and the line width every time it draws which might slow things down a bit but apart from that it’s doing the same as the example you posted

1 Like

I see! Thank you so much!
Btw, there was about 10% CPU usage difference between the two when drawing 10,000 times at once.

yeah if you are drawing that many it can really make a difference to get the polylines first and draw them later to avoid that state change.