Is there any way to achieve the effect of the code below without using stencilling? Essentially the two squares should be interlaced where they meet in the middle.
Here is an image of what it should look like:
www.maths.tcd.ie/~vapor/right.png
This is just a sample program, but I am trying to port a much bigger one for a third party MATLAB library…
problem is I just discovered that stencilling is not properly supported!
This is what;s happening in the ported example:
www.maths.tcd.ie/~vapor/wrong.png
I REALLY appreciate the help. We’ve invested a lot of time and money into this library and I don’t know what I’m going to do if I can’t find an alternative solution…
#include <GL/glut.h>
static GLint w,h;
GLint y;
void display(void)
{
glEnable(GL_STENCIL);
w = glutGet(GLUT_WINDOW_WIDTH);
h = glutGet(GLUT_WINDOW_HEIGHT);
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w-1,0,h-1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawBuffer(GL_BACK);
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glDisable(GL_DEPTH_TEST);
glStencilFunc(GL_ALWAYS,1,1);
glColor4f(0,0,0,0);
for (y=0; y<h; y+=2)
{
glLineWidth(1);
glBegin(GL_LINES);
glVertex2f(0,y);
glVertex2f(w,y);
glEnd();
}
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glFlush();
glDrawBuffer(GL_BACK);
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glStencilFunc(GL_NOTEQUAL,1,1);
glColor4f(1,0,0,1);
glBegin(GL_QUADS);
glVertex2f(100,100);
glVertex2f(100,300);
glVertex2f(300,300);
glVertex2f(300,100);
glEnd();
glStencilFunc(GL_EQUAL,1,1);
glColor4f(0,1,1,1);
glBegin(GL_QUADS);
glVertex2f(150,150);
glVertex2f(150,350);
glVertex2f(350,350);
glVertex2f(350,150);
glEnd();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(450,450);
glutCreateWindow("test");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}