PDA

View Full Version : A stencil buffer question



hgxz
01-06-2004, 06:02 PM
Hello!I need help.
I'm puzzled and don't know how to make stencil buffer work.Hope you could give me some hints.
I have a rendering thread in my windows program,here's a piece of code cut from it:

...
//This while-loop renders the scene all the time,
//until the "game" stops running by other means.
while(game_status.bRunning )
{
glViewport(0,0,winWidth,winHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, winWidth, 0, winHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Here I want to draw a black rectangle in the lower part of my window.
//But the output on the screen doesn't fit my need--the black rectangle wasn't shown at all.
//What's wrong?
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

glColor3ub (0,0,0);
glRecti(0,0,windowWidth,windowHeight/10);

glStencilFunc(GL_NOTEQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glClearColor(0.4f, 0.4f, 0.6f, 0);
glClear(GL_COLOR_BUFFER_BIT);

glFlush();
::SwapBuffers(pDC->GetSafeHdc());//pDC is ready before,dont worry about it.
}
...

Relic
01-07-2004, 04:20 AM
You do glClear(GL_COLOR_BUFFER_BIT) after drawing the glRect. Look up what states affect glClear! Stencil is not one of them.
You could use glScissor here which affects glClear and is faster than your method.

Or do you mean you don't have a result in the stencil buffer?
Check if you really got stencil bits with
DescribePixelFormat or glGetIntegerv(GL_STENCIL_BITS, &i).

There's no need to glFlush or glFinish before SwapBuffers.

Oh, and make sure winWidth and winHeight in glOrtho is the same as windowWidth and windowHeight in the glRect command.
If those are integers beware that windowHeight < 10 will result in null area glRect.

[This message has been edited by Relic (edited 01-07-2004).]

hgxz
01-08-2004, 12:23 AM
Thank your for your suggestions,I'll make a hard work and try to modify my codes and test them later.
I'm a beginner.I am glad to learn more from you.