When creating a Cairo Graphics Backend we need to implement some operations, one of them is the fill operation.

Fill operation basically describe how a polygon is filled, and cairo has two fill rules:

Non-Zero Winding: takes into account the direction of the path. Being enclosed by a clockwise loop counts as +1 and being enclosed by a counter-clockwise loop counts as -1. When of the sum of these counts is zero, it is a hole otherwise it is filled.

Evend-Odd: Every area that is inside of an even number of “interiors” is a hole and every region that is inside a odd number of “interiors” is filled.

These operations are easily implemented using stencil buffer. About the Even-Odd we just need draw each of the polygons in turn, using the PIPE_STENCIL_OP_INVERT function in the stencil buffer. This flips the value between zero and a nonzero value every time a triangle is drawn that covers a pixel. After all the triangles are drawn, if a pixel is covered an even number of times, the value in the stencil buffers is zero; otherwise, it’s nonzero. Finally, draw a large polygon over the whole region.[1]

And the No n-Zero rule we need to know using the cull face if we are drawing the polygon into clockwise or counter-clockwise, we increment the stencil(PIPE_STENCIL_OP_INCR_WRAP) if is in clockwise and we decrement(PIPE_STENCIL_OP_INCR_DECR) the stencil if is in counter-clockwise. After we just need to draw a large polygon over the whole region.

So thinking about a star with the coordinates:

**(Remember the star is a self intersected polygon)**

cairo_move_to(cr, 0, 1.0);

cairo_line_to(cr, 0.5, 0.0);

cairo_line_to(cr, 1.0, 1.0);

cairo_line_to(cr, 0.0, 0.5);

cairo_line_to(cr, 1.0, 0.5);

cairo_line_to(cr, 0, 1.0);

Using Even odd rule this star is draw with a hole:

Using Winding rule we have the all the star filled:

So see you in the next post :).

[1] Drawing Filled, Concave Polygons Using the Stencil Buffer.

link:http://www.dei.isep.ipp.pt/~jpp/sgrai/livros/RedBook/chapter14.html

### Like this:

Like Loading...

This entry was posted on July 10, 2010 at 8:15 pm and is filed under Freedesktop, Gallium3D.

July 10, 2010 at 11:58 pm

And now you just need to teach the stencil buffer about antialiasing ;)