openGL-无法显示图像

时间:2019-04-16 07:00:10

标签: qt opengl opengl-4

学习使用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);
}

2 个答案:

答案 0 :(得分: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 :(得分: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;
    }   
);