我正在尝试使用带有 alpha 掩码的模板缓冲区来防止绘制部分纹理。
初始化代码
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(stderr,"%s:%d\n SDL_Init call failed.\n",__FILE__,__LINE__);
return false;
}
// Request use of the stencil buffer
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 1 );
if((Surf_Display = SDL_SetVideoMode(WWIDTH, WHEIGHT, 32, SDL_GL_DOUBLEBUFFER | SDL_OPENGL | SDL_RESIZABLE)) == NULL) {
fprintf(stderr,"%s:%d\n SDL_SetVideoMode call failed.\n",__FILE__,__LINE__);
return false;
}
// Init GLDebug system
GLDebug::Init();
// Init GL system
glClearColor(0, 0, 0, 1);
glClearDepth(1.0f);
glViewport(0, 0, WWIDTH, WHEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WWIDTH, WHEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glLoadIdentity();
资源
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Enable use of textures
glEnable(GL_TEXTURE_2D);
// Disable writing to any of the color fields
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glStencilFunc(GL_ALWAYS, 0,0);
glEnable(GL_ALPHA_TEST);
glEnable(GL_STENCIL_TEST);
glBindTexture(GL_TEXTURE_2D,(&StencilTex)[0]);
// Draw our stencil buffer texture
glBegin(GL_POLYGON);
glTexCoord2d( 0, 0 ); glVertex2f( 50, 50 );
glTexCoord2d( 0, 1 ); glVertex2f( 50, 50+128 );
glTexCoord2d( 1, 1 ); glVertex2f( 50+128, 50+128 );
glTexCoord2d( 1, 0 ); glVertex2f( 50+128, 50 );
glEnd();
glDisable(GL_ALPHA_TEST);
// Re enable drawing of colors
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_GREATER, 0, -1);
// Bind desired texture for drawing
glBindTexture(GL_TEXTURE_2D,(&texture)[0]);
// Draw the box with colors (this should be masked off)
glBegin(GL_QUADS);
glTexCoord2d( 0, 0 ); glVertex2f( 50, 50 );
glTexCoord2d( 0, 1 ); glVertex2f( 50, 50+128 );
glTexCoord2d( 1, 1 ); glVertex2f( 50+128, 50+128 );
glTexCoord2d( 1, 0 ); glVertex2f( 50+128, 50 );
glEnd();
glDisable(GL_STENCIL_TEST);
// Swap buffers and display!
SDL_GL_SwapBuffers();
我将从蒂姆发布的这个问题中删除参考。我的设置适用于像素的原始绘图,但我不能 A)使这个过程应该如何工作或 B)让它从 alpha 纹理工作。我得到的只是黑屏或完整的纹理。
有人愿意将其分解为合理的步骤吗?我对 glStencilOp 和 glStencilFunc 选项的工作原理有一个模糊的理解,但是它们与使用 alpha 测试和模板测试的关系让我感到困惑。我们如何决定只保留纹理中的 alpha 层?
回顾:
- 我有一个我想在场景中绘制的纹理,但我需要将非常特定的区域挡住。
- 无法在运行时修改对象的 alpha 纹理以实现此效果。
- 我正在使用固定管道,我计划稍后学习如何使用 VBO。
- 调用第二个纹理时,给定的代码不会绘制任何内容。
[编辑 0]
公认的解决方案有效!这是我所期望的最终代码。
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// // Enable use of textures
glEnable(GL_TEXTURE_2D);
// Disable writing to any of the color fields
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glAlphaFunc(GL_GREATER, 0.05);
glStencilFunc(GL_ALWAYS, 0,0);
glEnable(GL_STENCIL_TEST);
glEnable(GL_ALPHA_TEST);
glBindTexture(GL_TEXTURE_2D,(&StencilTex)[0]);
// Draw our blocking poly
glBegin(GL_POLYGON);
glTexCoord2d( 0, 0 ); glVertex2f( 50, 50 );
glTexCoord2d( 0, 1 ); glVertex2f( 50, 50+128 );
glTexCoord2d( 1, 1 ); glVertex2f( 50+128, 50+128 );
glTexCoord2d( 1, 0 ); glVertex2f( 50+128, 50 );
glEnd();
glDisable(GL_ALPHA_TEST);
// Re enable drawing of colors
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_LESS, 0, -1);
// Bind desired texture for drawing
glBindTexture(GL_TEXTURE_2D,(&texture)[0]);
// Draw the box with colors
glBegin(GL_QUADS);
glTexCoord2d( 0, 0 ); glVertex2f( 50, 50 );
glTexCoord2d( 0, 1 ); glVertex2f( 50, 50+128 );
glTexCoord2d( 1, 1 ); glVertex2f( 50+128, 50+128 );
glTexCoord2d( 1, 0 ); glVertex2f( 50+128, 50 );
glEnd();
glDisable(GL_STENCIL_TEST);
// Swap buffers and display!
SDL_GL_SwapBuffers();