openGL - 无法显示图像

问题描述 投票:1回答:2

学习使用QOpenGLWidget显示图像。但是,我遇到了一些问题。

  1. 如何将GLuint texture变量(从图像加载的实际纹理)传递到着色器脚本?就像如何将GLuint texture绑定到uniform sampler2D texture?也许我只是没有意识到我已经这样做了。
  2. attribute vec4 vertexColorInuniform sampler2D texture有什么区别?我认为颜色来自纹理。
  3. 我可以使用glTexCoord2f()glVertex2f()而不是glVertexAttribPointer()glVertexAttribPointer()吗?这是因为他们对我来说似乎更好。

尽管我做了很多研究,但我仍然不清楚openGL如何显示图像的概念。我不确定我做错了什么。图像未显示。

MyGLWiget.cpp

着色器脚本:

#define STR(x) #x
#define VS_LOCATION 0
#define FS_LOCATION 1

const char* vertextShader = STR(
    attribute vec4 position;
    attribute vec4 vertexColorIn;
    varying vec4 vertexColorOut;
    void main(void)
    {
        gl_Position = position;
        vertexColorOut = vertexColorIn;
    }
);

const char* fragmentShader = STR(
    varying vec4 vertexColorOut;
    uniform sampler2D texture;
    void main(void)
    {
        ??? = texture2D(???, textureOut).r // no clue how to use it
        gl_FragColor = vertexColorOut;
    }   
);

加载图像纹理:

void MyGLWiget::loadTexture(const char* file_path)
{
    img_data = SOIL_load_image(file_path, &width, &height, &channels, SOIL_LOAD_RGB);

    glEnable(GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data);

    SOIL_free_image_data(img_data);
}

初始化:

void MyGLWiget::initializeGL()
{
    initializeOpenGLFunctions();

    program.addShaderFromSourceCode(QGLShader::Vertex, vertextShader);
    program.bindAttributeLocation("position", VS_LOCATION);

    program.addShaderFromSourceCode(QGLShader::Fragment, fragmentShader);
    program.bindAttributeLocation("vertexColorIn", FS_LOCATION);

    program.link();
    program.bind();


    static const GLfloat ver[] = {
        -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f
    };

    static const GLfloat  tex[] = {
        0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
    };

    glVertexAttribPointer(VS_LOCATION, 2, GL_FLOAT, 0, 0, ver);
    glEnableVertexAttribArray(VS_LOCATION);

    glVertexAttribPointer(FS_LOCATION, 2, GL_FLOAT, 0, 0, tex);
    glEnableVertexAttribArray(FS_LOCATION);

    program.setUniformValue("texture", texture); 
    //texture = program.uniformLocation("texture");
}

paintGL:

我真的很困惑这个部分。我不知道应该用什么来绘制图像。

void MyGLWiget::paintGL()
{
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, img_data);
    glUniform1i(texture, 0);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 1);
}
qt opengl opengl-4
2个回答
2
投票

如何将GLuint纹理变量(从图像加载的实际纹理)传递到着色器脚本?就像如何将GLuint纹理绑定到均匀的sampler2D纹理?也许我只是没有意识到我已经这样做了。

这将纹理绑定到纹理单元0:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

这是无效的,因为texture不是统一的位置,所以删除这一行:

glUniform1i(texture, 0); // <-- invalid

这也是无效的,因为统一的texture应该设置为纹理单元的数量:

program.setUniformValue("texture", texture); // <-- invalid

所以用以下代替:

program.setUniformValue("texture", 0); // <-- sampler2D texture uses GL_TEXTURE0

注意:我在这里假设setUniformValue正常工作。


属性vec4 vertexColorIn和统一sampler2D纹理之间有什么区别?我认为颜色来自纹理。

vertexColorIn来自VAO,每个顶点都不同。 texture是从纹理中采样的采样器,该纹理绑定到您在上面设置的纹理单元。

在您的代码中,您不需要顶点颜色,但您需要纹理坐标。所以你的着色器看起来像:

const char* vertextShader = STR(
    attribute vec4 position;
    attribute vec4 texcoordIn;
    varying vec4 texcoordOut;
    void main(void)
    {
        gl_Position = position;
        texcoordOut = texcoordIn;
    }
);

const char* fragmentShader = STR(
    varying vec4 texcoordOut;
    uniform sampler2D texture;
    void main(void)
    {
        gl_FragColor = texture2D(texture, texcoordOut);
    }   
);

我可以使用glTexCoord2f()和glVertex2f()代替glVertexAttribPointer()和glVertexAttribPointer()吗?这是因为他们对我来说似乎更好。

glTexCoord2fglVertex2f是在OpenGL 3中删除的遗留函数,仅在兼容性配置文件中可用。你不应该使用它们。


这行是在错误的地方:

 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

他们应该在你绑定纹理之后:

 glGenTextures(1, &texture);
 glBindTexture(GL_TEXTURE_2D, texture);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data);
 // sets the filtering for the bound texture:
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

由于这个问题被标记为:在这种情况下你不需要设置任何制服。您可以直接在着色器中指定位置和绑定:

const char* vertextShader =
    "#version 450 core\n" STR(
    layout(location = 0) in vec4 position;
    layout(location = 1) in vec4 texcoordIn;
    layout(location = 0) out vec4 texcoordOut;
    void main(void)
    {
        gl_Position = position;
        texcoordOut = texcoordIn;
    }
);

const char* fragmentShader =
    "#version 450 core\n" STR(
    layout(location = 0) in vec4 texcoord;
    layout(binding = 0) uniform sampler2D TEX;
    layout(location = 0) out vec4 OUT;
    void main(void)
    {
        OUT = texture(TEX, texcoord);
    }   
);

1
投票

一些编辑

   const char* vertextShader = STR(
    attribute vec4 position;
    attribute vec4 vertexColorIn;
    varying vec4 vertexColorOut;
    out vec2 TexCoord;//--->add
    void main(void)
    {
        gl_Position = position;
        vertexColorOut = vertexColorIn;
        TexCoord = vec2(aPos.x/2.0+0.5, 0.5-aPos.y/2.0);//a hack,ideally you need to pass the UV coordinates for proper texture mapping.UVs need to be passed in as a uniform or an attribute depending on preference.
    }
);

const char* fragmentShader = STR(
    varying vec4 vertexColorOut;
    uniform sampler2D texture;
    in vec2 TexCoord; //---->add
    void main(void)
    {
        gl_FragColor  = texture2D(texture,TexCoord) //( no clue how to use it) -->here is the change
        //gl_FragColor = vertexColorOut;
    }   
);
© www.soinside.com 2019 - 2024. All rights reserved.