OpenGL 中的纹理错误

Texture in OpenGL wrong

我正在尝试在 OpenGL 中绘制一个带有纹理的矩形。在我将纹理写入矩形之前,我绘制了一个具有适当颜色的 OpenGL 图元。然后我使用下面给出的代码将纹理写入矩形,其中变量缓冲区包含 800x600 像素的 RGB 数据。但是执行一段代码会导致矩形内的颜色仍然填充有来自图元的颜色。 我做错了什么?

从 07.11.19 更新代码

一段代码:

    //glDisable(GL_COLOR_MATERIAL);

    glScalef(1,1,1);
    glPixelstorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures (1, @texID);
    glBindTexture (GL_TEXTURE_2D,texID);

    if schglobal=1 then
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer)
                   else
                   begin
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer)
                   end;

    glcolor3i(255,255,255);
    //glcolor3i(0,0,0);
    //glcolor3i(0,0,0);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    //glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
    glBindTexture (GL_TEXTURE_2D,texID);
    glEnable(GL_TEXTURE_2D);
    glBegin (GL_QUADS);
    glTexCoord2f (0, 0);
    glVertex3f (vertexes[4,1], vertexes[4,2], vertexes[4,3]);
    glTexCoord2f (1, 0);
    glVertex3f (vertexes[2,1], vertexes[2,2], vertexes[2,3]);
    glTexCoord2f (1, 1);
    glVertex3f (vertexes[1,1], vertexes[1,2], vertexes[1,3]);
    glTexCoord2f (0, 1);
    glVertex3f (vertexes[3,1], vertexes[3,2], vertexes[3,3]);
    glEnd;
    ans:=glGetError;
    //glFlush();
    //glBindTexture (GL_TEXTURE_2D,0);
    glDisable(GL_TEXTURE_2D);

从 08.11.19 更新代码(有效但如果 glTexSubImage2D 使用错误 1281 则无效)

    glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
    glScalef(1,1,1);
    glPixelstorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures (1, @texID);
    glBindTexture (GL_TEXTURE_2D,texID);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    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_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    if use_gltexsubimage=false then
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer)
                               else begin
    if use_glteximage=true then begin
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer);
    use_glteximage:=false;
    end
                           else begin
    glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, buffer);
            ans:=glGetError;
                        end;
                                    end;

    glcolor3f(1,1,1);

    glBindTexture (GL_TEXTURE_2D,texID);
    glEnable(GL_TEXTURE_2D);
    glBegin (GL_QUADS);
    glTexCoord2f (0, 0);
    glVertex3f (vertexes[4,1], vertexes[4,2], vertexes[4,3]);
    glTexCoord2f (1, 0);
    glVertex3f (vertexes[2,1], vertexes[2,2], vertexes[2,3]);
    glTexCoord2f (1, 1);
    glVertex3f (vertexes[1,1], vertexes[1,2], vertexes[1,3]);
    glTexCoord2f (0, 1);
    glVertex3f (vertexes[3,1], vertexes[3,2], vertexes[3,3]);
    glEnd;
    glBindTexture (GL_TEXTURE_2D,0);

                     end;

我用 glTexSubImage2d 解决了我的问题。我将纹理生成转移到表单创建过程,将纹理删除转移到表单关闭过程。