2

我解释我的问题:

我有三个 png 图像,第一个有通道 alpha,另外两个是 RGB:

框架 面具 背景

            frame                           mask                    background

我必须把它混合起来才能得到这个结果

在此处输入图像描述

现在我知道我必须以这种方式掩盖第三个纹理:

if mask_pixel=white:
    blend_pixel=pixel with alpha 0
else if mask_pixel=black:
    blend_pixel=backgroud_pixel

从现在开始我写了这个:

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, background);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mask);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glActiveTexture(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, frame);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    glEnable (GL_BLEND);
        glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE2, 0.0, 0.0);
            glVertex3f(-0.7, 0.0, 0.0);

            glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE2, 1.0, 0.0);
            glVertex3f(0.7, 0.0, 0.0);

            glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE2, 1.0, 1.0);
            glVertex3f(0.7, 2.6, 0.0);

            glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE2, 0.0, 1.0);
            glVertex3f(-0.7, 2.6, 0.0);
        glEnd();
    glDisable(GL_BLEND);
glActiveTexture (GL_TEXTURE2);
glDisable (GL_TEXTURE_2D);
glActiveTexture (GL_TEXTURE1);
glDisable (GL_TEXTURE_2D);
glActiveTexture (GL_TEXTURE0);
glDisable (GL_TEXTURE_2D);

但我不确定如何使用 glTexEnvi 来获得所需的效果,有什么建议吗?

4

1 回答 1

1

是的!我自己得出了答案:

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mask);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT);
glActiveTexture(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_open);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    glEnable (GL_BLEND);
        glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE2, 0.0, 0.0);
            glVertex3f(-0.7, 0.0, 0.0);

            glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
            glMultiTexCoord2f(GL_TEXTURE2, 1.0, 0.0);
            glVertex3f(0.7, 0.0, 0.0);

            glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE2, 1.0, 1.0);
            glVertex3f(0.7, 2.6, 0.0);

            glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
            glMultiTexCoord2f(GL_TEXTURE2, 0.0, 1.0);
            glVertex3f(-0.7, 2.6, 0.0);
        glEnd();
    glDisable(GL_BLEND);
glActiveTexture (GL_TEXTURE2);
glDisable (GL_TEXTURE_2D);
glActiveTexture (GL_TEXTURE1);
glDisable (GL_TEXTURE_2D);
glActiveTexture (GL_TEXTURE0);
glDisable (GL_TEXTURE_2D);

在蒙版图像中,我用白色替换了黑色,用透明度替换了白色

于 2013-01-29T22:19:53.403 回答