Search code examples
openglfreeglutblending

How to blend RGB & BGRA raw images by opengl?


I am very new to OpenGL

Trying to blend RGB & BGRA raw images.

  • RGB is backgroung image
  • RGBA is foregroubng image

With the below source getting the output only ,"osd_raw" image .Blending is not happening.

In the below code loading the 2 raw images . Creating 2 textures & binding with it. Used glBlendFunc for blending. The output is not getting blended.

Please tell me where it's getting wrong.

void display()
{

    GLuint texture[2];
    int width = 960;
    int height = 540;
    unsigned char *osd_raw = loadFile("./osd.raw");
    unsigned char *video_raw = loadFile("./video.raw");

    glClearColor(0.1, 0.1, 0.1, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glGenTextures(2, texture);

    glEnable( GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, video_raw);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, osd_raw);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex3f(-1, -1, 0);
    glTexCoord2f(0, 1);
    glVertex3f(-1, 1, 0);
    glTexCoord2f(1, 1);
    glVertex3f(1, 1, 0);
    glTexCoord2f(1, 0);
    glVertex3f(1, -1, 0);
    glEnd();

    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);

    glutInitWindowSize(960, 540);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("glut test");

    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Solution

  • First of all, note that drawing with glBegin/glEnd sequences is deprecated since more than 10 years. Read about Fixed Function Pipeline and see Vertex Specification for a state of the art way of rendering.


    If you want to blend 2 textures, then you have to bind the first texture and to draw the quad with the proper texture coordinate attributes set. After that you have to bind the 2nd texture and to draw the quad again, with enabled blending (see Blending):

    glDisable( GL_BLEND);
    
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex3f(-1, -1, 0);
    glTexCoord2f(0, 1);
    glVertex3f(-1, 1, 0);
    glTexCoord2f(1, 1);
    glVertex3f(1, 1, 0);
    glTexCoord2f(1, 0);
    glVertex3f(1, -1, 0);
    glEnd();
    
    glEnable( GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex3f(-1, -1, 0);
    glTexCoord2f(0, 1);
    glVertex3f(-1, 1, 0);
    glTexCoord2f(1, 1);
    glVertex3f(1, 1, 0);
    glTexCoord2f(1, 0);
    glVertex3f(1, -1, 0);
    glEnd();
    

    Further note, that the Depth Test has to be disabled or set to e.g. GL_LEQUAL, when the 2nd quad is drawn. Otherwise the 2nd quad would be discarded by the depth test.

    Since the blend function is glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);, of course some alpha channels of the 2nd textue have to be less than 1.0.