0
\$\begingroup\$

I am trying to render three textures: Background, Black/White Foreground Mask and Foreground.

I have used this OpenGL - mask with multiple textures, but I cannot get it to work. I only get the last rendered texture, in this case the Foreground Texture.

This is my code:

#include <GL/glew.h>
#include <GL/glfw.h>
#include <IL/il.h>
#include <IL/ilut.h>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>

void DibujaCuadro(GLuint idtextura);

int main(){
    bool running = true;

    ilInit();
    glfwInit();

    if(!glfwOpenWindow(640,480,8,8,8,8,8,8,GLFW_WINDOW) ){
        glfwTerminate();
        return 0;
    }

    glfwSetWindowTitle("GLFW Application");
    glewInit();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,640,0,480,-1,1);

    glMatrixMode(GL_MODELVIEW);
    glClearColor(0.0,0.0,0.5,1.0);   

    ILuint idimg;
    ilGenImages(1,&idimg);
    ilBindImage(idimg);
    ILboolean imagen;
    ILint ilbpp;
    ILint ilancho;
    ILint ilalto;
    ILint ilformato;
    ILubyte *ildata;

    imagen = ilLoadImage("graficos/cfondo.png");

    if(imagen){
      imagen = ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE);
      if(!imagen)return GL_FALSE;
    }
    else return GL_FALSE;

    ilbpp     = ilGetInteger(IL_IMAGE_BPP);
    ilancho   = ilGetInteger(IL_IMAGE_WIDTH);
    ilalto    = ilGetInteger(IL_IMAGE_HEIGHT);
    ilformato = ilGetInteger(IL_IMAGE_FORMAT);
    ildata    = ilGetData();

    GLuint textura[3];

    glGenTextures(1,&textura[0]);
    glBindTexture(GL_TEXTURE_2D,textura[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glTexImage2D(GL_TEXTURE_2D,0,ilbpp,ilancho,ilalto,0,ilformato,GL_UNSIGNED_BYTE,ildata);

    imagen = ilLoadImage("graficos/mascara.png");

    if(imagen){
      imagen = ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE);
      if(!imagen)return GL_FALSE;
    }
    else return GL_FALSE;

    ilbpp     = ilGetInteger(IL_IMAGE_BPP);
    ilancho   = ilGetInteger(IL_IMAGE_WIDTH);
    ilalto    = ilGetInteger(IL_IMAGE_HEIGHT);
    ilformato = ilGetInteger(IL_IMAGE_FORMAT);
    ildata    = ilGetData();

    glGenTextures(1,&textura[1]);
    glBindTexture(GL_TEXTURE_2D,textura[1]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glTexImage2D(GL_TEXTURE_2D,0,ilbpp,ilancho,ilalto,0,ilformato,GL_UNSIGNED_BYTE,ildata);

    imagen = ilLoadImage("graficos/cfrente.png");

    if(imagen){
      imagen = ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE);
      if(!imagen)return GL_FALSE;
    }
    else return GL_FALSE;

    ilbpp     = ilGetInteger(IL_IMAGE_BPP);
    ilancho   = ilGetInteger(IL_IMAGE_WIDTH);
    ilalto    = ilGetInteger(IL_IMAGE_HEIGHT);
    ilformato = ilGetInteger(IL_IMAGE_FORMAT);
    ildata    = ilGetData();

    glGenTextures(1,&textura[2]);
    glBindTexture(GL_TEXTURE_2D,textura[2]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glTexImage2D(GL_TEXTURE_2D,0,ilbpp,ilancho,ilalto,0,ilformato,GL_UNSIGNED_BYTE,ildata);    

    while(running){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glEnable(GL_BLEND);

        glBlendFunc(GL_ONE,GL_ZERO);
        DibujaCuadro(textura[0]); //Background

        glBlendFuncSeparate(GL_ZERO,GL_ONE,GL_SRC_COLOR,GL_ZERO);
        DibujaCuadro(textura[1]); //Mask

        glBlendFunc(GL_DST_ALPHA,GL_ONE_MINUS_DST_ALPHA);
        DibujaCuadro(textura[2]); //Foreground

        glDisable(GL_BLEND);

        glfwSwapBuffers();

        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
    }

    glDeleteTextures(1,&textura[0]);
    glDeleteTextures(1,&textura[1]);
    glDeleteTextures(1,&textura[2]);

    glfwTerminate();

    return 0;
}

void DibujaCuadro(GLuint idtextura){
  glLoadIdentity();
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D,idtextura);
  glBegin( GL_QUADS );
    glTexCoord2f(0.0,0.0);
    glVertex2f(-180.0f,110.0f);
    glTexCoord2f(0.0,1.0);
    glVertex2f(-180.0f,  0.0f);
    glTexCoord2f(1.0,1.0);
    glVertex2f( 820.0f,  0.0f);
    glTexCoord2f(1.0,0.0);
    glVertex2f( 820.0f,110.0f);
  glEnd();
  glDisable(GL_TEXTURE_2D);
}

Can anyone see what am I doing wrong?

Textures:

Background

Background

Mask

Mask

Foreground

Foreground

Result

This is what I want to get.

\$\endgroup\$
2
  • \$\begingroup\$ Could you share the mask picture? \$\endgroup\$ Jan 1, 2016 at 17:43
  • \$\begingroup\$ These are my textures. \$\endgroup\$ Jan 1, 2016 at 18:06

2 Answers 2

2
\$\begingroup\$

It seems that your mask texture is alpha 1.0 everywhere. In order for this to work, your mask should have alpha set 1.0 for "HOLA" and alpha set to zero everywhere else.

Hope it helps!

\$\endgroup\$
2
  • \$\begingroup\$ De nada, amigo ;) \$\endgroup\$ Jan 1, 2016 at 20:15
  • \$\begingroup\$ ¿Hablas español? me alegra mucho y de nuevo muchas gracias. \$\endgroup\$ Jan 1, 2016 at 20:18
0
\$\begingroup\$

On some graphic systems the window's back buffer has no alpha component. You should verify that it does have alpha bits by calling GLint alpha_bits=0; glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); and check the value in alpha_bits after the call.

If that is the case you will have to render to a frame buffer object (FBO) then copy this to the window's back buffer before calling glfwSwapBuffers().

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for answering. The value obtained is 8. Now what can I do? \$\endgroup\$ Jan 1, 2016 at 17:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .