Opengl Multitexturing和混合

时间:2013-01-29 21:48:05

标签: opengl alphablending multitexturing

我解释了我的问题:

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

frame mask background

            frame                           mask                    background

我必须将它们全部混合才能获得此结果

enter image description here

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

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来获得所需的效果,任何建议?

1 个答案:

答案 0 :(得分: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);
掩码图像中的

我用白色代替黑色,用透明度代替白色