qt - openGL - 无法显示图像

标签 qt opengl opengl-4

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

  1. 如何将 GLuint 纹理 变量(从图像加载的实际纹理)传递到着色器脚本中?比如如何将GLuint纹理绑定(bind)到uniform Sampler2D纹理?也许我只是没有意识到我已经这样做了。
  2. 属性 vec4 vertexColorInuniform Sampler2D 纹理有什么区别?我认为颜色来自纹理。
  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);
}

最佳答案

How can I pass the GLuint texture variable (the actual texture loaded from the image) into the shader scripts? Like how to bind GLuint texture to uniform sampler2D texture? Maybe I am just not realising I already did that.

这会将纹理绑定(bind)到纹理单元 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 工作正常。


What's the difference between attribute vec4 vertexColorIn and uniform sampler2D texture? I think the color comes from the texture.

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

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

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

Can I use glTexCoord2f() and glVertex2f() instead of glVertexAttribPointer() and glVertexAttribPointer()? It's because they seem better to me.

glTexCoord2fglVertex2f 是 OpenGL 3 中删除的旧函数,仅在兼容性配置文件中可用。您不得使用它们。


此行位于错误的位置:

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

它们将在您绑定(bind)纹理后进行:

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

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

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

关于qt - openGL - 无法显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55702328/

相关文章:

java - OpenGL 纹理重复伪像

mesa - OpenGL 4.2 header 、Mesa 3D、扩展 - 它们如何组合在一起?

c++ - 谜团: In Qt, 为什么会调用editorEvent,但不会调用createEditor?

c++ - 获取QListWidgetItem x y的坐标,我要达到如图的效果

c++ - QObject 重入和线程安全

opengl - OpenGL 中的查询对象是什么?

c++ - Qt 显示超大富文本的最佳方式?

c++ - 在 Mac 终端编译 OpenGL 程序

c - 在 OpenGL 中调整窗口大小 : Garbage Output

c++ - 带交错缓冲区的 OpenGL glMultiDrawElementsIndirect