Good algorithm for flood fill?

@Ignatz Actually, you could use a shader for flood fill. You could get the neighbor pixel’s colors based on the current pixel (image width times vTexCoord.x (image width and height would have to be a uniform float)), and if the neighbor’s pixel’s color matches the interior, and it has a neighbor of the flood fill’s color, it could know to change.

I don’t think so. The pixel you get from cTexCoord will always be the original interior colour and not the replacement colour, because the shader doesn’t change anything in the texture image, so how will you know if the current pixel is connected to the first pixel selected?

Didn’t think of that… Splash screens it is.

@Ignatz if you look a few posts up you’ll see a shader version that I posted.

I’d be very grateful Andrew if you could further explain how I could use the shader you made to fill an image

In the old days (pre-windows) we’d make a list of LINES from the left to the right borders of the area needing filling.

Lines draw very fast.

I saw this in the scan line algorithm, could work I suppose but where do I find the borders? checking each pixel along the xaxis where I clicked?

depends on the shape you want to fill. Basically take your starting click point, move left/right to find the end points.
Assuming a rectangle you can figure the equation for the two sides (assuming you can figure out the starting/ending points) then just plug Y into the line equation to get the X.
Concave shapes are more complicated – and in all cases it really helps to know the equations for teh lines making up the sides.
It’s really worthwhile looking at old DOS code for these types of questions – when performance was a serious issue the old-time developers came up with a lot of tricks to do things quickly.
I think Mike Abrash discussed a lot of these algorithms in his two books on graphics programming (and in his Dr. Dobbs articles). His Graphics Programming Black book is a GREAT reference for 2D and 3D programming (Dated in some ways – but still very applicable) The Black Book is available as a free download from Dr. Dobbs

Brilliant help, thanks a lot! I never thought to look at the old DOS programs thats a great idea. Ill do that and post back with anything I find or make

Another option (don’t know if it’s good for your app) – do a vector-based drawing program instead of bit-mapped. Then you could use meshes for all your objects

(Think Illustrator instead of Photoshop)

@Andrew_Stacey - ok, so your shader fills one layer of pixels per draw, so it is always going to be pretty slow, and not a practical solution, as you implied. But fun, anyway.

@Luatee - I wrote a codea program to find the outline, worked almost all the time (it can get tricky with some images). Happy to share

I remember seeing that, for the physics bodies? I’ll take a look

I looked for the black book ressource you indicate. The book is huge. I randomly sampled chapter 65, http://twimgs.com/ddj/abrashblackbook/gpbb65.pdf , and read the introduction story: looks like it was written for forums like this one! Incredible that i hit that by random walk!

@Ignatz I wasn’t recommending it as a solution, simply as a proof that it could be done.

You could always do the draw/redraw loop several times before rendering to the screen (no idea what that would do to the frame rate). One disadvantage of using a shader is that there’s no easy way to know if the fill has stopped.

yep! I keep seeing questions (basic graphics/ 3D/ BSP/ path finding) that were developed to a fine science 20 years ago – but once machines got fast enough people stopped using them.

Not to mention Knuth’s Art of Computer Programming.

@Andrew_Stacey - the reason I commented on the shader was because your previous mentions of it hadn’t made it clear it wasn’t a practical solution, and at least one person was asking how to use it. So I was just clarifying, not criticising :wink:

@Ignatz Mind you, I’d be interested in seeing the speed comparison of a shader versus a CPU method. The main drawback of the shader that I can see is knowing when it has finished. To be absolutely sure you’d have to run it enough times that the path could travel from one corner to the opposite corner.

Also, for a drawing app then the speed might not be the issue. Given that it shows what it’s doing, it’s not unpleasant to watch it do the flood fill “in real time”.

(That said, I’m still not recommending it!)

@Andrew_Stacey - the shader would probably work much faster if it did a lot of iterations between drawing. I tried to modify it to do that, but didn’t succeed. I don’t know how you get round not knowing when to stop, though!