views:

164

answers:

3

I have this algorithm that I found here, just one thing puzzles me about it:

    Clear the stencil buffer to 1.
    Pick an arbitrary vertex v0, probably somewhere near the polygon to reduce floating-point errors.
    For each vertex v[i] of the polygon in clockwise order:
        let s be the segment v[i]->v[i+1] (where i+1 will wrap to 0 when the last vertex is reached)
        if v0 is to the "right" of s:
            draw a triangle defined by s, v[i], v[i+1] that adds 1 to the stencil buffer
        else
            draw a triangle defined by s, v[i], v[i+1] that subtracts 1 from the stencil buffer
    end for
    fill the screen with the desired color/texture, testing for stencil buffer values >= 2.

By "right of s" I mean from the perspective of someone standing on v[i] and facing v[i+1]. This can be tested by using a cross product:

cross(v0 - v[i], v[i+1] - v[i]) > 0

The part that confused me was where I need to draw a triangle defined by S, V[i], V[i + 1]. If S is segment v[i]->v[i+1] then how is this possible?

Thanks

+2  A: 

If I recall correctly, the triangles you have to draw are v0 - v[i] - v[i+1]

Calvin1602
Perfect thanks!
Milo
A: 

If I read your example correctly, s is a segment between 2 vertices that are the edge of the triangle. So it "walks" around the triangle in clockwise fashion filling in the vertices.

The "winding" - clockwise or counter-clockwise - determines the normal of the triangle.

JustBoo
A: 

Yup, it looks like an error, since s is segment defined by v[i] and v[i+1]. That would make sense in context of drawing concave polygon. Go with v0, v[i], v[i+1].

Rekin