|
|
下面是书上(《OpenGL超级宝典》86面)关于模板缓冲的例子代码(我已上传了此程序的完整代码):
void RenderScene(void)
{
1 GLdouble dRadius = 0.1; // Initial radius of spiral
2 GLdouble dAngle; // Looping variable
// Clear blue window
3 glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
// Use 0 for clear stencil, enable stencil test
4 glClearStencil(0.0f);
5 glEnable(GL_STENCIL_TEST);
// Clear color and stencil buffer
6 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// All drawing commands fail the stencil test, and are not
// drawn, but increment the value in the stencil buffer.
7 glStencilFunc(GL_NEVER, 0x0, 0x0);
8 glStencilOp(GL_INCR, GL_INCR, GL_INCR);
// Spiral pattern will create stencil pattern
// Draw the spiral pattern with white lines. We
// make the lines white to demonstrate that the
// stencil function prevents them from being drawn
9 glColor3f(0.3f, 1.0f, 0.5f);
10 glBegin(GL_LINE_STRIP);
11 for(dAngle = 0; dAngle < 400.0; dAngle += 0.1)
12 {
13 glVertex2d(dRadius * cos(dAngle), dRadius * sin(dAngle));
14 dRadius *= 1.002;
15 }
16 glEnd();
// Now, allow drawing, except where the stencil pattern is 0x1
// and do not make any further changes to the stencil buffer
17 glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
18 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Now draw red bouncing square
// (x and y) are modified by a timer function
19 glColor3f(1.0f, 0.0f, 1.0f);
20 glRectf(x, y, x + rsize, y - rsize);
// All done, do the buffer swap
21 glutSwapBuffers();
}
提问开始,我实在是无法从这里明白模板缓冲是如何运作的:
4 glClearStencil(0.0f);//使用0清除模板有啥意义?
5 glEnable(GL_STENCIL_TEST);
7 glStencilFunc(GL_NEVER, 0x0, 0x0);//将模板缓冲中的值与参考值0x0进行比较,在前面已将模板缓冲清0了,即参考值与模板缓冲的值是相等的,但比较函数又设成GL_NEVER,将颜色缓冲区中的值全部丢弃?不明白!我的脑子真是不好使啊。
8 glStencilOp(GL_INCR, GL_INCR, GL_INCR); //这个也不是很明白,测试失败则增加模板缓冲区中的值?是什么意思?
17 glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
18 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
在这段程序中这是第二次调用这两个函数了。还是有点不明白,请大家指点一下,我好烦哪,拜托!求你们了。
|
|