Concave Polygon via Stencil Buffer

Does anyone know of a simple “full” example where one is rending a simple (concave/convex) filled polygon using a stencil buffer. I google around, but I’m having a hard time finding actual code to step through. I’ve come across general algs describing what to do, but since I’m unfamiliar with stencil buffs altogether, they aren’t much help (yet).

Anyone have a simple code dump they can suggest?

Thanks in advance…

You can’t render concave polygons with GL. You must use several convex polygons to create your concave surface.

After, all depends on what you want to achieve with the stencil buffer. Generally you decide about which stencil test to use and which stencil operation to do, then render your objects, change your stencil operations, render same or other objects, then use the result in a second pass.

Look at this web page and find the Drawing Filled, Concave Polygons Using the Stencil Buffer section.

Thanks… but I’m still having issues with this. I can at least stencil out my triangle fan… but it’s not rending “only” the concave polygon.

In the below image, you can see the intended polygon (outlined in red), and that the green goes outside my polygon contour and fills my entire triangle fan. I just want to fill the outlined red area.

Does anyone see any glaring issues with my code? Thanks again everyone…

Here’s my code:


         glClearStencil(0);
         glEnable(GL_STENCIL_TEST);
         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
         glStencilFunc(GL_NEVER, 0, 1);
         glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);


         DrawPolyon(); // Just draws using triangle fan


         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
         glStencilFunc(GL_EQUAL, 1, 1);
         glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
         glColor3f(0.0, 1.0, 0.0);

         glBegin(GL_QUADS); // Draw big green box over polygon area 
         glVertex3f(-200.0f,-200.0f, -0.0f);
         glVertex3f( 200.0f,-200.0f, -0.0f);
         glVertex3f( 200.0f, 200.0f, -0.0f);
         glVertex3f(-200.0f, 200.0f, -0.0f);
         glEnd();

         glDisable(GL_STENCIL_TEST);

         
         glColor3f(1.0, 0.0, 0.0);
         glBegin(GL_QUADS);

         glVertex3f(xth-0.25f, yth-0.25f, 1.0f);
         glVertex3f(xth+0.25f, yth-0.25f, 1.0f);
         glVertex3f(xth+0.25f, yth+0.25f, 1.0f);
         glVertex3f(xth-0.25f, yth+0.25f, 1.0f);

         glEnd();

And I should further mention that I’m using the idea behind trinitrotoluene’s URL post. If you click this, and scroll down to the Drawing Filled, Concave Polygons Using the Stencil Buffer area, I’m “trying” to implement the idea behind the image:

Thanks again…

So I figured it out. My code does infact work… when you disable GL_CULL_FACE :slight_smile: