glTexImage2D多个图像

Ope*_*bie 4 c++ opengl image-processing

我正在从openCV全屏绘制图像,这是一个60fps的大图像所以我需要比openCV gui更快的方式.

使用OpenGL我做:

void paintGL() {
    glClear (GL_COLOR_BUFFER_BIT);
    glClearColor (0.0,0.0,0.0,1.0);

    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,width,height,0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, the_image_data );
    glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex2i(0,height);
    glTexCoord2i(0,1); glVertex2i(0,0);
    glTexCoord2i(1,1); glVertex2i(width,0);
    glTexCoord2i(1,0); glVertex2i(width,height);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}
Run Code Online (Sandbox Code Playgroud)

现在我想绘制两个图像:侧面 - 使用openGL硬件来缩放它们.
我可以通过改变四边形尺寸缩小图像我不明白如何用glTexImage2()加载两个图像,因为没有与图像相关联的句柄或id.

Mik*_*son 11

您无法看到如何添加其他纹理的原因是因为您在所发布的代码中缺少两个关键函数:glGenTexturesglBindTexture.第一个将在OpenGL上下文中生成纹理对象(纹理的位置存在于图形硬件上).第二个"选择"其中一个纹理对象用于后续调用(glTex ..)以影响它.

首先,glTexParameteri和glTexImage2D等函数不需要在每个渲染循环中再次调用......但我想在你的情况下,你应该这样做,因为图像总是在变化.默认情况下,在您的代码中,使用的纹理对象是第0个对象(默认保留的对象).您应该创建两个纹理对象并将它们一个接一个地绑定以获得所需的结果:

GLuint tex_obj[2]; //create two names for the texture (should not be global variables, but just for sake of this example).

void initGL() {
    glClearColor (0.0,0.0,0.0,1.0);

    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,width,height,0);

    glEnable(GL_TEXTURE_2D);
    glGenTextures(2,tex_obj); //generate 2 texture objects with names tex_obj[0] and [1]

    glBindTexture(GL_TEXTURE_2D, tex_obj[0]); //bind the first texture
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //set its parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glBindTexture(GL_TEXTURE_2D, tex_obj[1]); //bind the second texture
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //set its parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}

void paintGL() {
    glClear (GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindTexture(GL_TEXTURE_2D,tex_obj[0]); //bind the first texture.
    //then load it into the graphics hardware:
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width0, height0, 0, GL_RGB, GL_UNSIGNED_BYTE, the_image_data0 );
    glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex2i(0,height);  //you should probably change these vertices.
    glTexCoord2i(0,1); glVertex2i(0,0);
    glTexCoord2i(1,1); glVertex2i(width,0);
    glTexCoord2i(1,0); glVertex2i(width,height);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, tex_obj[1]); //bind the second texture.
    //then load it into the graphics hardware:
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, the_image_data1 );
    glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex2i(0,height);  //you should probably change these vertices.
    glTexCoord2i(0,1); glVertex2i(0,0);
    glTexCoord2i(1,1); glVertex2i(width,0);
    glTexCoord2i(1,0); glVertex2i(width,height);
    glEnd();
}
Run Code Online (Sandbox Code Playgroud)

这基本上就是这样做的.但是我必须警告你,我对OpenGL的了解有点过时了,所以可能有更有效的方法来做到这一点(至少我知道glBegin/glEnd在C++中被弃用,取而代之的是VBO).

  • 创建纹理后,只需使用glTexSubImage2D更新数据.这比glTexImage快得多,后者每次都会经历完全重新初始化.实际上你可以将NULL传递给glTexImage的data参数告诉你只想初始化然后通过glTexSubImage提供数据 (2认同)