Convert path to image?


Is there a way to rasterize an ofPath or an ofMesh ?
I would like to save a shape (from an ofPath for example) into a png.

You can grab a screen shot using!show_ofSaveScreen.

Or you could render to an ofFBO and readback the FBO’s ofPixels using!show_ofSaveImage


I would like to generate a shape and save it in setup si the FBO trick would be the best. But i can’t find the way to read the pixels from the fbo…


ofPixels pixels;

if you just need this to draw as a texture you don’t even need the pixels, you can just bind the fbo texture directly. if you are going to use it for the point sprites from the other thread, you should set the target of the fbo to GL_TEXTURE_2D when allocating it.

Thanks arturo.
Yes this is for using as a point sprite.

Sorry, consider me as a noob concerning openGL thing :wink:
What do you mean by “setting the target of the fbo to GL_TEXTURE_2D when allocating it” ?
I think i found a way to set it (see below) but i don’t have any clue in what it is useful !

This is what i have for now, but the saved image is messed up : the circle seems ok, but with a noisy background.

Edit : ok seems noisy background was because i didn’t set a background color. So i added ofBackground(0, 0, 0, 0) with alpha set to 0 to avoid the square shape.

// Generate sprite
ofPath path;
path.arc(0, 0, radius, radius, 0, 360);
ofFbo fbo;
ofFbo::Settings settings;
settings.width = radius * 2;
settings.height = radius * 2;
settings.textureTarget = GL_TEXTURE_2D;
ofBackground(0, 0, 0, 0);
path.draw(radius, radius);
ofPixels sprite;
sprite.allocate(radius * 2, radius * 2, 4);
ofSaveImage(sprite, "sprite.png");

What is the point of GL_TEXTURE_2D by the way ?

GL_TEXTURE_2D is the most usual target and supports all the features (like point sprites among others) but OF by default uses GL_TEXTURE_RECT_ARB which on old hardware allowed to use textures which didn’t have power of 2 width and height.

anyway, in this case you need to enable it to be able to use point sprites

ok thanks for clarifying.
Thinking of it, this is more the notion of “target” i don’t really understand here.

Also, the png i get is really sharp and aliased. I think this is because video card settings doesn’t affect FBO.
How can i get a smooth png ?

try using anti aliased fbo’s. Add this to your settings
settings.numSamples = 4; //or even 8 if you want

thanks for the tip @Kj1

a bit better, but not soft enough yet


numSamples = 10

you can also try to use


just before or even just after the fbo.begin() call.

Did something similar, had also crispy png exports from fbo, lots of trial & error to get it right.

some other things:
i was using settings.internalformat = GL_RGBA32F; and numsamples 8 or 16.
also, ofPAths have circle resolutions I think, setCircleResolution() & setCurveResolution(); try to use before adding the arc.

what usually works nicely too is to render at a bigger size than you are going to draw

1 Like

Yep, tried all of this, nothing changes !
by the way, numsamples seems limited to 8 !

@arturo what i did is create the path and draw to the fbo at twice the size and then set
glPointSize to half the size. Is that right ? nothing changes anyway.

I also tried to scale up and down the sprite before saving it… no more luck…

keep trying…