PDA

View Full Version : Concave Polygon via Stencil Buffer



pseudosig
03-08-2011, 02:00 PM
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...

_arts_
03-08-2011, 03:09 PM
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.

trinitrotoluene
03-08-2011, 10:51 PM
Look at this (http://fly.cc.fer.hr/~unreal/theredbook/chapter13.html) web page and find the Drawing Filled, Concave Polygons Using the Stencil Buffer section.

pseudosig
03-10-2011, 07:40 AM
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...

http://i55.tinypic.com/2b5g7q.png

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();

pseudosig
03-10-2011, 08:35 AM
And I should further mention that I'm using the idea behind trinitrotoluene's URL post. If you click this (http://fly.cc.fer.hr/~unreal/theredbook/chapter13.html), and scroll down to the Drawing Filled, Concave Polygons Using the Stencil Buffer area, I'm "trying" to implement the idea behind the image:

http://fly.cc.fer.hr/~unreal/theredbook/figures/concave.gif

Thanks again...

pseudosig
03-10-2011, 12:32 PM
So I figured it out. My code does infact work... when you disable GL_CULL_FACE :-)