Analyses, Musings

Just another WordPress.com weblog

Archive for July 2012

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;
}

Written by 001041a

July 13, 2012 at 6:45 am

Posted in Uncategorized

A working stencil test OpenGL sample

leave a comment »

Written by 001041a

July 11, 2012 at 9:33 pm

Posted in Uncategorized