相信你们有些人对OpenGL的模板缓冲区不是很理解,包括我最开始也是,OpenGL的模板缓冲区其实就是采用过滤的技术来控制那些颜色能够绘制,那些不能进行绘制。这里的过滤技术也就是咱们的一个控制方法,主要体如今以下两个函数glStencilFunc(GLenum func,GLint ref,GLuint mask)和glStencilOp(GLenum fail,GLenum zfail, GLenum zpass),其中 ios
1.glStencilFunc中的第一个参数指的是过滤函数,(如何来进行过滤),过滤函数有以下几种类型 函数
GL_NEVER 历来不能经过 oop
GL_ALWAYS 永远能够经过(默认值) 测试
GL_LESS 小于参考值能够经过 ui
GL_LEQUAL 小于或者等于能够经过 spa
GL_EQUAL 等于经过 .net
GL_GEQUAL 大于等于经过 code
GL_GREATER 大于经过 blog
GL_NOTEQUAL 不等于经过 ip
在这里“经过”的意思指的是,咱们在将图元绘制到帧缓冲区的时候在片断进行测试的时候是能够彻底透过去的,不然的话这个片断就没法绘制到对应的颜色帧缓冲区,那么咱们所绘制的内容也就显示不出来。经过这种控制方法来控制显示,其实这种操做在咱们实际的生活中也是很常见的,例如给汽车喷漆,盖章(只会显示刻了的内容)。
2.经过模板操做glStencil()来控制模板结果值的操做,例如,若是失败了对模板值进行加1,减1等处理。等待下一次片断处理的时候再进行新的比较,对值的过滤作新的控制。
3.在这里我想经过这样一个例子来讲明一下:
- #include <math.h>
- #include <iostream>
- #include <assert.h>
-
- #include <GL/glut.h>
- #pragma comment(lib, "glut32.lib")
- // #include <GL/glew.h>
- // #pragma comment(lib, "glew32.lib")
-
- void init()
- {
- glClearColor(0.0, 0.0, 1.0, 0.0);
- glClearStencil(0);
- glEnable(GL_STENCIL_TEST);
- }
-
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-
- glLoadIdentity();
- glTranslatef(0.0, 0.0, -20.0);
-
- glStencilFunc(GL_ALWAYS, 0, 0x00);
- //glStencilFunc(GL_NEVER, 0x0, 0x0);
- //glStencilOp(GL_INCR, GL_INCR, GL_INCR);//
-
- glColor3f(1.0f, 1.0f, 1.0f);
-
- float dRadius = 5.0 * (sqrt(2.0) / 2.0);
- glBegin(GL_LINE_STRIP);
- for (float dAngel = 0; dAngel < 380.0; dAngel += 0.1)
- {
- glVertex2d(dRadius * cos(dAngel), dRadius * sin(dAngel));
- dRadius *= 1.003;
- }
- glEnd();
-
- //glStencilFunc(GL_NOTEQUAL,0x1,0x1);
- //glStencilOp(GL_INCR,GL_INCR,GL_INCR);//
-
- glColor3f(1.0f, 0.0f, 0.0f);
- glRectf(-5, -5, 5, 5);
-
- glutSwapBuffers();
- }
-
- void reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- float aspect = (w * 1.0) / h;
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60, aspect, 1, 100);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
-
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_STENCIL);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(600,600);
- glutCreateWindow(argv[0]);
-
- // assert(GLEW_NO_ERROR == glewInit());
-
- init();
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutMainLoop();
-
- return 0;
- }
加入模板控制以后的结果:
- #include <math.h>
- #include <iostream>
- #include <assert.h>
-
- #include <GL/glut.h>
- #pragma comment(lib, "glut32.lib")
- // #include <GL/glew.h>
- // #pragma comment(lib, "glew32.lib")
-
- void init()
- {
- glClearColor(0.0, 0.0, 1.0, 0.0);
- glClearStencil(0);
- glEnable(GL_STENCIL_TEST);
- }
-
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-
- glLoadIdentity();
- glTranslatef(0.0, 0.0, -20.0);
-
- //glStencilFunc(GL_ALWAYS, 0, 0x00);
- glStencilFunc(GL_NEVER, 0x0, 0x0);
- glStencilOp(GL_INCR, GL_INCR,GL_INCR);//
-
- glColor3f(1.0f, 1.0f, 1.0f);
-
- float dRadius = 5.0 * (sqrt(2.0) / 2.0);
- glBegin(GL_LINE_STRIP);
- for (float dAngel = 0; dAngel < 380.0; dAngel += 0.1)
- {
- glVertex2d(dRadius * cos(dAngel), dRadius * sin(dAngel));
- dRadius *= 1.003;
- }
- glEnd();
-
- glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
- glStencilOp(GL_INCR, GL_INCR, GL_INCR);//
-
- glColor3f(1.0f, 0.0f, 0.0f);
- glRectf(-5.0, -5.0, 5.0, 5.0);
-
- glutSwapBuffers();
- }
-
- void reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- float aspect = (w * 1.0) / h;
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, aspect, 1.0, 100.0);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
-
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL);
- glutInitWindowPosition(200, 200);
- glutInitWindowSize(600, 600);
- glutCreateWindow(argv[0]);
-
- // assert(GLEW_NO_ERROR == glewInit());
-
- init();
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutMainLoop();
-
- return 0;
- }