如何使用带alpha掩码的模板缓冲区

时间:2012-08-15 05:39:23

标签: c++ opengl sdl

我正在尝试使用带有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();

我将从Tim发布的this问题中删除参考文献。设置我用于原始绘制像素,但我不能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();

1 个答案:

答案 0 :(得分:2)

我看到你启用了alpha测试,但我没有看到任何设置alpha函数的调用(glAlphaFunc)?

如果您未设置alpha功能,则alpha测试始终通过。您必须告诉状态机丢弃哪些像素。

通常我会将其设置为:

glAlphaFunc(GL_GREATER, 0.05); //reject any pixels with alpha less than 0.05

如果您不清楚我之前的回答,我很抱歉,但如果您对模板功能的工作方式感到困惑,可以随时查阅参考页glStencilFunc glStencilOp