如何在OpenGL-ES 2.0中使用纹理(图像)创建模板缓冲区。

8

我可以在OpenGL 2.0中使用纹理(图像)准备模板吗?这样图像的某些部分将变为透明,结果会按原样传输到模板缓冲区,然后将使用此模板缓冲区进行进一步绘制。


由datenwolf编辑以考虑OP在答案中的更新:

来自@InfiniteLoop:

@datenwolf非常感谢您的回复,但没有成功 :( 这是我的代码

- (void)render 
{
    [EAGLContext setCurrentContext:context];
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    glUseProgram(program);
    glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
    glViewport(0, 0, backingWidth, backingHeight);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    // Set the stencil clear value
    glClearStencil ( 0x1 );

    // Set the depth clear value
    glClearDepthf( 0.75f );



    ////////
    ////
    GLfloat threshold = 0.5f;//half transparency
    /* the order in which orthogonal OpenGL state is set doesn't matter */
    glEnable(GL_STENCIL_TEST);
    glEnable(GL_ALPHA_TEST);

    /* don't write to color or depth buffer */
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glDepthMask(GL_FALSE);

    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

    /* first set alpha test so that fragments greater than the threshold
     will pass thus will set nonzero bits masked by 1 in stencil */
    glStencilFunc(GL_ALWAYS, 1, 1);//set one
    glAlphaFunc(GL_GREATER, threshold);//greater than half transparency

    glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, fullVertices);
    glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
    glVertexAttribPointer(textureLoc, 2, GL_FLOAT, GL_FALSE, 0, textureVertices);
    [self drawTexture:texture2DMaskImage];
    /* second pass of the fragments less or equal than the threshold
     will pass thus will set zero bits masked by 1 in stencil */
    glStencilFunc(GL_ALWAYS, 0, 1);//set zero
    glAlphaFunc(GL_LEQUAL, threshold);//Less than Half transparency
    glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, fullVertices);
    glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
    glVertexAttribPointer(textureLoc, 2, GL_FLOAT, GL_FALSE, 0, textureVertices);
    [self drawTexture:texture2DMaskImage];


// till here as suggested by u after this I have added to draw
// the next Image which will use the created Stencil
    glDepthMask(GL_TRUE);
    glDisable(GL_ALPHA_TEST);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    //
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);//stop further write to stencil buffer
    glStencilFunc(GL_EQUAL, 0, 1);//pass if 0
    glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, fullVertices);
    glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
    glVertexAttribPointer(textureLoc, 2, GL_FLOAT, GL_FALSE, 0, textureVertices);
    [self drawTexture:texture2D];
    ////
    ///////



    // Validate program before drawing. This is a good check, 
    // but only really necessary in a debug build.
    // DEBUG macro must be defined in your debug configurations
    //  if that's not already the case.
#if defined(DEBUG)
    if (![self validateProgram:program])
    {
        NSLog(@"Failed to validate program: %d", program);
        return;
    }
#endif
    // draw
    [context presentRenderbuffer:GL_RENDERBUFFER];//Present
}

- (void) drawTexture:(Texture2D*)texture
{
    // handle viewing matrices
    GLfloat proj[16], modelview[16], modelviewProj[16];
    // setup projection matrix (orthographic)
    mat4f_LoadOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, proj);
    //   glUniformMatrix4fv(uniforms[UNIFORM_PROJECTION], 1, 0, proj);
    // setup modelview matrix (rotate around z)
    mat4f_LoadIdentity(modelview);
    mat4f_LoadZRotation(0.0, modelview);
    mat4f_LoadTranslation3D(0.0, 0.0, 0.0, modelview);
    // projection matrix * modelview matrix
    mat4f_MultiplyMat4f(proj, modelview, modelviewProj);
    glUniformMatrix4fv(_modelViewUniform, 1, 0, modelviewProj);
    // update uniform values
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture.name);
    glUniform1i(_textureUniform, 0);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, cubeIndices);
}

请给我一些提示,我非常需要 :( 再次感谢。
2个回答

8

是的,您可以使用alpha测试(glEnable(GL_ALPHA_TEST); glAlphaFunc(比较模式,值);)来丢弃片段;被丢弃的片段将不会进行模板测试,因此可以使用通过的片段构建模板掩码。

编辑代码示例

/* the order in which orthogonal OpenGL state is set doesn't matter */
glEnable(GL_STENCIL_TEST);
glEnable(GL_ALPHA_TEST);

/* don't write to color or depth buffer */
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);

glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

/* first set alpha test so that fragments greater than the threshold
   will pass thus will set nonzero bits masked by 1 in stencil */
glStencilFunc(GL_ALWAYS, 1, 1);
glAlphaFunc(GL_GREATER, threshold);
draw_textured_primitive();


/* second pass of the fragments less or equal than the threshold
   will pass thus will set zero bits masked by 1 in stencil */
glStencilFunc(GL_ALWAYS, 0, 1);
glAlphaFunc(GL_LEQUAL, threshold);
draw_textured_primitive();

在绘制结束后不要忘记恢复OpenGL状态。

针对问题更新的编辑:

由于您实际上使用的是iOS,因此使用OpenGL-ES 2.0会有所不同。OpenGL-ES2中没有alpha测试。相反,您应该在片段着色器中使用discard关键字/语句来丢弃低于/高于选定阈值的片段。


谢谢您的回复,是的,我尝试过了,但是对流程感到困惑。像 1. 禁用深度 2. 启用 Alpha 测试 3. 设置 Alpha 测试函数 4. 设置模板测试函数 5. 设置模板操作(Operation)。6. 绘制纹理。对吗?????但是这并没有给出预期结果。应该设置什么 StencilOp 和 stencilFunc 值??以便如果 alpha 测试通过,则将“1”或“0”写入模板缓冲区,其余部分将保持为之前设置为清除模板缓冲区。请帮忙,再次感谢。 - infiniteLoop
@infiniteLoop:看看我的代码编辑,应该可以工作(我没有测试过)。 - datenwolf
@ChristianRau:问题标题提到了OpenGL 2.0(不是ES)。然而,由于在OpenGL-ES2中必须提供一个片段着色器,所以这个变化变得微不足道。 - datenwolf
@datenwolf 哦,是的,看起来Brad Larson已经完全改变了关于OpenGL ES的问题,并添加了代码示例。如果他不是同一个人或者与OP没有联系,那么这将是一种严重的滥用行为。 - Christian Rau
@ChristianRau:我将问题恢复到之前的版本。如果有人进行这样的更改/添加,那么应该清楚地指出。此外,由于原始问题是关于OpenGL的,但更改为OpenGL-ES2,因此涉及了不同的主题,这就需要提出一个单独的问题。 - datenwolf
显示剩余4条评论

1

最终我使用着色器中的discard关键字进行条件颜色传递,成功地完成了它。

我还在同一着色器中使用了2个纹理,并相应地将它们组合起来。

感谢大家。


2
请问,您能否添加有关如何组合这两种纹理的更多细节? - MatterGoal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接