Draw hundreds of different 2d shapes the fastest way

Hi there!

I’m a complete noob at this OpenFrameworks stuff and am trying to learn as much from these forums and other resources as I can.

This is a general and not so general question about performance.

I’m trying to develop an application with kinect where I get the contour from the depth map. These contours I store them as ofPaths in a vector and remove them after they’ve been there for 5 seconds. This means that the vector can contain up to approx. 80 ofPaths.

In the draw function I loop over the vector and draw all these vectors and obviously performance goes down a bunch depending of the number of paths and complexity of them.

I was wondering what would be the best way, performance wise, to draw these paths or maybe if there was some way to put together all of them (which overlap most of the times) to draw them in one call.

I also thought about comparing the new path with the last one to see if its the same (or really similar) and not include it in the vector, but I don’t know how to compare them without looping the vertices.

Appending the new path to one path doesn’t work because of the overlapping. I’ve already tried several things that didn’t work but at least served me to learn about shaders, and some of OF and cpp.

Thank you so much in advance!

Is there any reasons while are you converting the contour, that if I remember correctly it is an ofPolyline in, ofxCv at least, to an ofPath?

One thing that you can do, is to store all your vertices in an vbo mesh of type OF_PRIMITIVES_LINES, so that you can have a single draw call that displays all your contours.

Hi Edapx!

I converted it to an ofPath in order to draw the silhouette with a fill and an outline with different opacity. What I tried before was converting to a mesh instead of an ofPath and drawing it to a texture that I passed to a shader in order to do the outline and fill, but I noticed ofPath is a bit faster when drawing a lot of them.

In the case of storing the polylines in a vbo mesh, which I’ll have to research further, would there be a way to remove previously stored vertices?

Thank you for your response and time!

I see. ofPath offers those styling options you were mentioning, like the fill, and the outline with different opacity. If you are going to use a vboMesh, you will not have those options, and you will have to create your own shader to style the contour. It is not a trivial task, specially if you are new to shaders, you can read more about the topic here.

How bad are the performance by the way? how many FPS do you have?

By the way, an ofPath, as it is written in the documentation, can be composed by multiple paths, see the method append, https://openframeworks.cc/documentation/graphics/ofPath/#show_append, there is no need to store them in a vector. But I do not think that using multiple paths is going to save you fps, as at the end internally is doing the same thing, but maybe worth to give a try.

I know, I almost cried writing my first shader. The problem I couldn’t find a solution to was the overlapping. I couldn’t figure out how to store the vertices from several contours in the same mesh and outline them individually, at least in the shader I wrote, where I take a texture with the mesh drawn and check the surrounding pixels looking for alpha.

Performance can go from 60fps to 20 or even 15 fps and application starts to feel laggy.

I tried what you say, appending subpaths to the a single path, but again the overlapping created weird effects and colors inverted in these areas.

Are you running the application in release mode? drawing 80 paths or so shouldn’t be so slow

Actually no. I was running in debug mode. I didn’t know this made a difference :sweat_smile:

When I run it in release mode it plays quite fast! Sorry for my lack of knowledge!!

Thank you for the advice!