2

I explain my problem:

I have three png image, the first has channel alpha, the other two are RGB:

frame mask background

            frame                           mask                    background

I have to blend it all to obtain this result

enter image description here

now i know that i have to mask the third texture in this way:

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

and since now i have written this:

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);

but I am not sure how to use glTexEnvi to obtain the desired efect, any suggestion?

Luca
  • 1,270
  • 1
  • 18
  • 34
  • 1
    Untested recommendation: put the mask in TEXTURE0 using REPLACE mode, the background in TEXTURE1 using DECAL, and then the frame in TEXTURE2 using MODULATE mode. You may get a bit of blending from the background into the blue border of the mask, unless the mask is only the size of the interior of the frame. – radical7 Jan 29 '13 at 22:00
  • What's your target audience? People owning a GPU older than 7 years? If so, glTexEnv juggling is avoidable. If you're targeting current system: *Use a fragment shader. It makes things like what you want so much easier!* – datenwolf Jan 30 '13 at 00:40
  • my target audience is the professor of my opengl exam :) – Luca Jan 30 '13 at 09:29

1 Answers1

1

yes! i have reached the answer on my own:

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);

in the mask image i have replaced the black with white and the white with trasparency

Luca
  • 1,270
  • 1
  • 18
  • 34