Analyses, Musings

Just another WordPress.com weblog

Drawing Filled, Concave Polygons Using the Stencil Buffer

with 3 comments

Chapter 14

Here is a GLUT implementation. I use GL_INCR instead of GL_INVERT but it keeps the basic idea.

#include "glut.h"

float p[][2]={{0,0}
,{.1,0}
,{.4,.3}
,{0,.5}
,{.7,1}
,{.3,.5}
,{1,0}
,{.6,.1}};

void d(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS,1,1);
glStencilOp(GL_KEEP,GL_KEEP,GL_INCR);

glColorMask(0,0,0,0);

glColor3f(1,1,1);
glBegin(GL_TRIANGLES);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[2][0],p[2][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[7][0],p[7][1]);
glEnd();

glColorMask(1,1,1,1);

glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);

glStencilFunc(GL_EQUAL,1,255);
glColor3f(0,.5,0);
glBegin(GL_TRIANGLES);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[2][0],p[2][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[7][0],p[7][1]);
glEnd();

glStencilFunc(GL_EQUAL,3,255);
glColor3f(0,1,0);
glBegin(GL_TRIANGLES);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[2][0],p[2][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[7][0],p[7][1]);
glEnd();

glStencilFunc(GL_EQUAL,2,255);
glColor3f(0,0,.5);
glBegin(GL_TRIANGLES);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[2][0],p[2][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[7][0],p[7][1]);
glEnd();

glStencilFunc(GL_EQUAL,4,255);
glColor3f(0,0,1);
glBegin(GL_TRIANGLES);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[2][0],p[2][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[3][0],p[3][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[4][0],p[4][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[5][0],p[5][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[1][0],p[1][1]);
glVertex2f(p[6][0],p[6][1]);
glVertex2f(p[7][0],p[7][1]);
glEnd();

glFlush();
}

void init(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,1,0,1,0,1);
}

int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_STENCIL);
glutCreateWindow("");
init();
glutDisplayFunc(d);
glutMainLoop();
return 0;
}

Advertisements

Written by 001041a

July 13, 2012 at 6:45 am

Posted in Uncategorized

3 Responses

Subscribe to comments with RSS.

  1. If you use PIXELFORMATDESCRIPTOR, be sure to have non-zero stencil bits.

    analysesmusings

    July 13, 2012 at 5:53 pm

  2. The glClear() call should probably be glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT).

    analysesmusings

    July 19, 2012 at 5:38 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: