PDA

View Full Version : Please help! Need quick alternative to stencilling



phalaris
09-01-2009, 08:32 AM
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&amp;lt;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(&amp;argc, argv);
glutInitWindowSize(450,450);
glutCreateWindow("test");
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

ZbuffeR
09-01-2009, 09:18 AM
a third party MATLAB library..
problem is I just discovered that stencilling is not properly supported!
Which one, so that others can avoid it ?
'quick alternative'... you need at least GLSL shaders, multipass rendering, ... quite hard.

phalaris
09-01-2009, 09:29 AM
Accelereyes Jacket. Let me say, it is quite useful, and I've made good use of it. I don't think others should avoid it.

This problem is quite annoying thouogh...

I don't know about GLSL shaders or multipass rendering... is there something simpler I could do?

kyle_
09-01-2009, 10:13 AM
Add this line after glutInit:

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_STENCIL);

phalaris
09-01-2009, 10:24 AM
Thank you for the suggestion,
Unfortunately I already tried that.

Like I said, the program above works fine, even without
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_STENCIL);

It's the ported version that's not working properly and that doesn't have a working glutInitDisplayMode either.

What exactly does glutInitDisplayMode do? Is there a more low-level way I can do this? (like toggle some bits or something?)

kyle_
09-01-2009, 11:07 AM
For me your application was producing garbage (as it didnt pick pixel format).

More low level way of glutInitDisplayMode it to SetPixelFormat (on Windows) with correct pixel format with attributes needed by you.

If your actual application isnt extremely big (with regard to emited ogl API calls) i suggest using GLIntercept to inspect your issue.

soconne
09-01-2009, 05:44 PM
First, your call to glEnable(GL_STENCIL) is wrong. Change it to GL_STENCIL_TEST.

MaxH
09-01-2009, 06:24 PM
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.
Blending.

phalaris
09-02-2009, 05:18 AM
OK thanks for the replies,

I've tried all the suggestions.
The problems still persists, and it seems to be a limitation of Jacket.