如何融合RGB& BGRA原始图像由opengl?

时间:2018-05-29 09:04:41

标签: opengl freeglut blending

我是OpenGL的新手

尝试融合RGB& BGRA原始图像。

  • RGB是背景图像
  • RGBA是foregroubng image

以下源只获得输出," osd_raw" image.Blending没有发生。

在下面的代码中加载2个原始图像。 创建2个纹理&与它绑定。 使用glBlendFunc进行混合。 输出没有混合。

请告诉我哪里出错了。

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

2 个答案:

答案 0 :(得分:2)

首先,请注意,使用glBegin / glEnd序列进行绘图已超过10年。 阅读Fixed Function Pipeline并查看Vertex Specification了解最先进的渲染方式。

如果要混合2个纹理,则必须绑定第一个纹理并使用适当的纹理坐标属性集绘制四边形。 之后,您必须绑定第二个纹理并再次绘制四边形,并启用混合(请参阅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();

进一步说明,Depth Test必须被禁用或设置为例如GL_LEQUAL,绘制第二个四边形时。否则,深度测试将丢弃第二个四边形。

由于混合函数为glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);,因此第二个textue的某些alpha通道必须小于1.0。

答案 1 :(得分:0)

根据Rabbid76,修改后的&以下是工作来源供参考。

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

        glDisable(GL_BLEND);

        glBindTexture(GL_TEXTURE_2D, texture[0]);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, 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);

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

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