Hi!

I have a spline loop and I have to color out the bordered area and the border as well.

I using shaders for this job, but I don’t know how to specify this area.

This is the spline:

Hi!

I have a spline loop and I have to color out the bordered area and the border as well.

I using shaders for this job, but I don’t know how to specify this area.

This is the spline:

The easiest way to fill it is to flatten it (convert it to a list of line segments approximating the curve to a given tolerance, typically half a pixel) and fill the result as a non-convex polygon using stencilling.

It’s possible to fill an area bounded by a single cubic curve without flattening by converting the curve to implicit form and filling its convex hull, testing each fragment against the implicit function. But that’s somewhat more involved.

How can I make a non-convex polygon? I tired to use a simple polygon but it is not filled.

```
glBindVertexArray(vao);
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(polyPoints) * sizeof(float), polyPoints, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(vao);
glDrawArrays(GL_POLYGON, 0, vecPoints.size());
```

I store the curve points in the polyPoints array.

Filling a non-convex polygon using stencilling boils down to:

- Enable stencil writes (
`glStencilMask`

), set the stencil operation (`glStencilOp`

) to`GL_INVERT`

for all three cases, disable stencil tests (`glDisable(GL_STENCIL_TEST)`

), disable writes to the colour buffer (`glColorMask`

). Clear the stencil buffer if it hasn’t already been cleared. - Draw a triangle fan. The first vertex can be anything, but ideally shouldn’t be too far from the polygon being drawn for performance reasons. The remaining vertices are the vertices of the polygon, with the first vertex duplicated to the last vertex (so the exterior of the triangle fan forms a closed loop).
- Disable stencil writes, enable stencil tests, set the stencil function (
`glStencilFunc`

) to`GL_NOTEQUAL`

with a`ref`

of zero, enable writes to the colour buffer. - Draw one or more triangles (or other filled primitives, if supported) to completely cover the area of the non-convex polygon. Typically you’d draw either the bounding rectangle or the convex hull.

After step 2, the stencil buffer is non-zero for pixels which are inside the shape and zero for pixels which are outside. Essentially, the triangle fan will cover interior pixels an odd number of times and exterior pixels an even number of times, meaning that exterior pixels will have zero in the stencil buffer. The stencil test enabled in step 3 causes step 4 to only affect interior pixels.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.