OpenGL: 模板缓冲区


        相信你们有些人对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.在这里我想经过这样一个例子来讲明一下:

[cpp]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. #include <math.h>  
  2. #include <iostream>  
  3. #include <assert.h>  
  4.   
  5. #include <GL/glut.h>  
  6. #pragma comment(lib, "glut32.lib")  
  7. // #include <GL/glew.h>  
  8. // #pragma comment(lib, "glew32.lib")  
  9.   
  10. void init()  
  11. {  
  12.     glClearColor(0.0, 0.0, 1.0, 0.0);  
  13.     glClearStencil(0);  
  14.     glEnable(GL_STENCIL_TEST);  
  15. }  
  16.   
  17. void display()  
  18. {  
  19.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);  
  20.   
  21.     glLoadIdentity();  
  22.     glTranslatef(0.0, 0.0, -20.0);  
  23.   
  24.     glStencilFunc(GL_ALWAYS, 0, 0x00);  
  25.     //glStencilFunc(GL_NEVER, 0x0, 0x0);  
  26.     //glStencilOp(GL_INCR, GL_INCR, GL_INCR);//  
  27.   
  28.     glColor3f(1.0f, 1.0f, 1.0f);  
  29.   
  30.     float dRadius = 5.0 * (sqrt(2.0) / 2.0);  
  31.     glBegin(GL_LINE_STRIP);  
  32.     for (float dAngel = 0; dAngel < 380.0; dAngel += 0.1)  
  33.     {  
  34.         glVertex2d(dRadius * cos(dAngel), dRadius * sin(dAngel));  
  35.         dRadius *= 1.003;  
  36.     }  
  37.     glEnd();  
  38.   
  39.     //glStencilFunc(GL_NOTEQUAL,0x1,0x1);  
  40.     //glStencilOp(GL_INCR,GL_INCR,GL_INCR);//  
  41.   
  42.     glColor3f(1.0f, 0.0f, 0.0f);  
  43.     glRectf(-5, -5, 5, 5);  
  44.   
  45.     glutSwapBuffers();  
  46. }  
  47.   
  48. void reshape(int w, int h)  
  49. {  
  50.     glViewport(0, 0, w, h);  
  51.     float aspect = (w * 1.0) / h;  
  52.   
  53.     glMatrixMode(GL_PROJECTION);  
  54.     glLoadIdentity();  
  55.     gluPerspective(60, aspect, 1, 100);  
  56.   
  57.     glMatrixMode(GL_MODELVIEW);  
  58.     glLoadIdentity();  
  59. }  
  60.   
  61. int main(int argc, char* argv[])  
  62. {  
  63.     glutInit(&argc, argv);  
  64.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_STENCIL);  
  65.     glutInitWindowPosition(200,200);  
  66.     glutInitWindowSize(600,600);  
  67.     glutCreateWindow(argv[0]);  
  68.   
  69. //  assert(GLEW_NO_ERROR == glewInit());  
  70.   
  71.     init();  
  72.     glutReshapeFunc(reshape);  
  73.     glutDisplayFunc(display);  
  74.     glutMainLoop();  
  75.   
  76.     return 0;  
  77. }  

加入模板控制以后的结果:

[cpp]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. #include <math.h>  
  2. #include <iostream>  
  3. #include <assert.h>  
  4.   
  5. #include <GL/glut.h>  
  6. #pragma comment(lib, "glut32.lib")  
  7. // #include <GL/glew.h>  
  8. // #pragma comment(lib, "glew32.lib")  
  9.   
  10. void init()  
  11. {  
  12.     glClearColor(0.0, 0.0, 1.0, 0.0);  
  13.     glClearStencil(0);  
  14.     glEnable(GL_STENCIL_TEST);  
  15. }  
  16.   
  17. void display()  
  18. {  
  19.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);  
  20.   
  21.     glLoadIdentity();  
  22.     glTranslatef(0.0, 0.0, -20.0);  
  23.   
  24.     //glStencilFunc(GL_ALWAYS, 0, 0x00);  
  25.     glStencilFunc(GL_NEVER, 0x0, 0x0);  
  26.     glStencilOp(GL_INCR, GL_INCR,GL_INCR);//  
  27.   
  28.     glColor3f(1.0f, 1.0f, 1.0f);  
  29.   
  30.     float dRadius = 5.0 * (sqrt(2.0) / 2.0);  
  31.     glBegin(GL_LINE_STRIP);  
  32.     for (float dAngel = 0; dAngel < 380.0; dAngel += 0.1)  
  33.     {  
  34.         glVertex2d(dRadius * cos(dAngel), dRadius * sin(dAngel));  
  35.         dRadius *= 1.003;  
  36.     }  
  37.     glEnd();  
  38.   
  39.     glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);  
  40.     glStencilOp(GL_INCR, GL_INCR, GL_INCR);//  
  41.   
  42.     glColor3f(1.0f, 0.0f, 0.0f);  
  43.     glRectf(-5.0, -5.0, 5.0, 5.0);  
  44.   
  45.     glutSwapBuffers();  
  46. }  
  47.   
  48. void reshape(int w, int h)  
  49. {  
  50.     glViewport(0, 0, w, h);  
  51.     float aspect = (w * 1.0) / h;  
  52.   
  53.     glMatrixMode(GL_PROJECTION);  
  54.     glLoadIdentity();  
  55.     gluPerspective(60.0, aspect, 1.0, 100.0);  
  56.   
  57.     glMatrixMode(GL_MODELVIEW);  
  58.     glLoadIdentity();  
  59. }  
  60.   
  61. int main(int argc, char* argv[])  
  62. {  
  63.     glutInit(&argc, argv);  
  64.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL);  
  65.     glutInitWindowPosition(200, 200);  
  66.     glutInitWindowSize(600, 600);  
  67.     glutCreateWindow(argv[0]);  
  68.   
  69. //  assert(GLEW_NO_ERROR == glewInit());  
  70.   
  71.     init();  
  72.     glutReshapeFunc(reshape);  
  73.     glutDisplayFunc(display);  
  74.     glutMainLoop();  
  75.   
  76.     return 0;  
  77. }  
相关文章
相关标签/搜索