C OpenGL纹理,黑色正方形

C OpenGL纹理,黑色正方形,c,linux,opengl,glsl,C,Linux,Opengl,Glsl,我正试图找出在OpenGL中渲染单个纹理的正确方法,我已经取得了一些进展,但我只得到了一个黑色正方形。 我只是不确定是否有我没有做的事情,或者我没有按照正确的顺序做事情,或者我只是在某个地方犯了一个错误。互联网上的所有文章似乎都说了完全不同的东西,代码总是被分割成几个小样本,所以我永远无法弄清楚主循环中应该放什么,不应该放什么 首先,这是我到目前为止得到的。 我使用FreeImage加载图像: FIBITMAP *load_image(image_data *image) { image

我正试图找出在OpenGL中渲染单个纹理的正确方法,我已经取得了一些进展,但我只得到了一个黑色正方形。 我只是不确定是否有我没有做的事情,或者我没有按照正确的顺序做事情,或者我只是在某个地方犯了一个错误。互联网上的所有文章似乎都说了完全不同的东西,代码总是被分割成几个小样本,所以我永远无法弄清楚主循环中应该放什么,不应该放什么

首先,这是我到目前为止得到的。 我使用FreeImage加载图像:

FIBITMAP *load_image(image_data *image)
{
    image->texture_id = 0;
    FreeImage_Initialise(FALSE);
    FIBITMAP *bitmap = NULL;
    FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(image->path);

    if (!(bitmap = FreeImage_Load(
                    format,
                    image->path,
                    PNG_DEFAULT)))
        exit(EXIT_FAILURE);

    glGenTextures(1, &(image->texture_id));
    glBindTexture(GL_TEXTURE_2D, image->texture_id);
    glTexImage2D(
            GL_TEXTURE_2D,
            0,
            GL_BGR,
            FreeImage_GetWidth(bitmap),
            FreeImage_GetHeight(bitmap),
            0,
            GL_BGR,
            GL_UNSIGNED_INT,
            bitmap);
    FreeImage_Unload(bitmap);
    return bitmap;
}
然后这里是我的主程序循环:

void main_loop(
        image_data *image,
        shader_info *vertex_shader,
        shader_info *fragment_shader)
{
    /* Create variables to house vertex-array/vertex-buffer object names. */
    GLuint vArray, vBuffer, program, uniform_mytexture;

    /* NDC (x, y) pair for each vertex. */
    GLfloat vertices[4][2] = {
        {-0.90, -0.90},
        {0.90, -0.90},
        {-0.90, 0.90},
        {0.90, 0.90}};

    printf("%s\n", glGetString(GL_VERSION));

    /*
     * Allocates OpenGL memory, and binds vArray and vBuffer
     * to that memory for use as VAO and VBO names, repectively.
     */
    program = initialize_image(
            &vArray,
            &vBuffer,
            vertices,
            sizeof(vertices),
            vertex_shader,
            fragment_shader);

    /* Main display loop */
    while (!glfwWindowShouldClose(window.glfw_window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0, 0.5, 0.0, 1.0);
        glViewport(0, 0, window.width, window.height);
        glUseProgram(program);
        glBindVertexArray(vArray);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, image->texture_id);
        uniform_mytexture = glGetUniformLocation(program, "mytexture");
        glUniform1i(uniform_mytexture, GL_TEXTURE0);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        glfwSwapBuffers(window.glfw_window);
        glfwPollEvents();
        glFlush();
    }
    return;
}
如您所见,我在initialize_image()中执行了大部分glGen*和glBind*操作,如下所示:

int initialize_image(
        GLuint*vArray,
        GLuint *vBuffer,
        GLfloat vertices[][2],
        int size,
        shader_info *vertex_shader,
        shader_info *fragment_shader)
{
    GLuint vShader, fShader, program, link_ok;
    GLint attribute_vpos, attribute_texcoord;
    const char *attribute_name_vpos = "vertex_position", *attribute_name_texcoord = "texcoord";

    glGenVertexArrays(1, vArray);
    glBindVertexArray(*vArray);
    glGenBuffers(1, vBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, *vBuffer);
    glBufferData(
            GL_ARRAY_BUFFER,
            size,
            vertices,
            GL_STATIC_DRAW);

    vertex_shader->content = load_shader(vertex_shader->path);
    fragment_shader->content = load_shader(fragment_shader->path);
    vShader = compile_shader(GL_VERTEX_SHADER, vertex_shader->content);
    fShader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader->content);
    program = glCreateProgram();
    glAttachShader(program, vShader);
    glAttachShader(program, fShader);
    glLinkProgram(program);
    glGetProgramiv(program, GL_LINK_STATUS, &link_ok);

    if (!link_ok)
    {
        fprintf(stderr, "Shader linkage failed.\n");
        exit(EXIT_FAILURE);
    }

    attribute_vpos = glGetAttribLocation(program, attribute_name_vpos);

    if (attribute_vpos == -1)
    {
        fprintf(stderr, "Attribute binding failed.\n");
        exit(EXIT_FAILURE);
    }

    glVertexAttribPointer(
            attribute_vpos,
            2,
            GL_FLOAT,
            GL_FALSE,
            0,
            BUFFER_OFFSET(0));
    glEnableVertexAttribArray(attribute_vpos);

    attribute_texcoord = glGetAttribLocation(program, attribute_name_texcoord);

    if (attribute_texcoord == -1)
    {
        fprintf(stderr, "Attribute binding failed.\n");
        exit(EXIT_FAILURE);
    }

    glEnableVertexAttribArray(attribute_texcoord);

    GLuint texture_vbo;
    GLfloat texture_coords[4][2] = {
        {-1.0, -1.0},
        {1.0, -1.0},
        {-1.0, 1.0},
        {1.0, 1.0}};

    glGenBuffers(1, &texture_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, texture_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(texture_coords), texture_coords, GL_STATIC_DRAW);
    glEnableVertexAttribArray(attribute_texcoord);
    glBindBuffer(GL_ARRAY_BUFFER, texture_vbo);
    glVertexAttribPointer(
            attribute_texcoord,
            2,
            GL_FLOAT,
            GL_FALSE,
            0,
            0);

    return program;
}
这是我的顶点着色器:

#version 330 core

layout(location = 0) in vec4 vertex_position;
attribute vec2 texcoord;
varying vec2 f_texcoord;

void main(void)
{
    gl_Position = vertex_position;
    f_texcoord = texcoord;
    return;
}
和片段着色器:

#version 330 core

out vec4 fragment_color;
varying vec2 f_texcoord;
uniform sampler2D mytexture;

void main(void)
{
    fragment_color = texture2D(mytexture, f_texcoord);
    return;
}
out vec4 fragment_color;
in vec2 f_texcoord;
uniform sampler2D mytexture;
很抱歉,我扔掉了这么多代码,我真的无法指出哪里出了问题,所以我不想遗漏一些东西。 如果有人能给我指出正确的方向,我将非常感激


几天来我一直在想怎么做。我觉得花这么多时间在OpenGL的一个最基本的功能上太不称职了。

首先,你的图像加载程序是完全错误的

FIBITMAP *load_image(image_data *image)
{
    image->texture_id = 0;
    FreeImage_Initialise(FALSE);
    FIBITMAP *bitmap = NULL;
    FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(image->path);

    if (!(bitmap = FreeImage_Load(
                    format,
                    image->path,
                    PNG_DEFAULT)))
        exit(EXIT_FAILURE);

    glGenTextures(1, &(image->texture_id));
    glBindTexture(GL_TEXTURE_2D, image->texture_id);
    glTexImage2D(
            GL_TEXTURE_2D,
            0,
            GL_BGR,
            FreeImage_GetWidth(bitmap),
            FreeImage_GetHeight(bitmap),
            0,
            GL_BGR,
            GL_UNSIGNED_INT, //I think FreeImage only loads bytes...
            bitmap); //...Hello, undefined behaviour...
    FreeImage_Unload(bitmap);
    return bitmap; //WHAT?
}

另外,FreeImage_Initialise和FreeImage_Deinitialise意味着每个程序只能调用一次。

我很确定,如果加载PNG,您不希望像素数据类型使用
GL_UNSIGNED_INT
(这意味着96位RGB图像)。相反,您可能需要
GL\u UNSIGNED\u BYTE
(24位)

将对
glTexImage2D
的调用更改为: 通常情况下,在读取24位RGB图像时,您必须将GL的解包对齐方式从4字节更改为1字节,但FreeImage保证了一些我将在下面说明的所需行为(如果您遇到任何称为
glPixelStorei(GL\u解包\u对齐,1)
,它们在这里不适用)

-位图函数参考-p。14 在FreeImage中,出于性能原因,每条扫描线从32位边界开始


我建议您在阅读时浏览该API参考的其余部分。您可以使用这些函数中的许多函数来消除我上面提出的24位图像假设。

此代码中存在许多问题:

  • 据我所知,
    GL_BGR
    作为内部纹理格式无效。另外,将
    GL\u UNSIGNED\u INT
    指定为
    glTexImage2D()
    的类型意味着每个组件都是一个无符号INT,而纹理加载例程很可能为组件提供无符号字节。而
    bitmap
    是指向
    FIBITMAP
    对象的指针,而最后一个参数需要是指向实际图像数据的指针。因此,电话应该是:

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGB8,
        FreeImage_GetWidth(bitmap),
        FreeImage_GetHeight(bitmap),
        0,
        GL_BGR,
        GL_UNSIGNED_BYTE,
        FreeImage_GetBits(bitmap));
    
    layout(location = 0) in vec4 vertex_position;
    layout(location = 1) in vec2 texcoord;
    out vec2 f_texcoord;
    
  • 纹理采样器均匀变量的值错误:

    glUniform1i(uniform_mytexture, GL_TEXTURE0);
    
    这需要是纹理单元的索引,而不是相应的枚举值。将呼叫替换为:

    glUniform1i(uniform_mytexture, 0);
    
  • 着色器代码使用核心配置文件中不可用的某些存储限定符<代码>属性和
    变化
    在核心配置文件中无效<代码>属性在顶点着色器中替换为
    in
    ,在片段着色器中替换为
    out
    in
    。因此顶点着色器中的声明应为:

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGB8,
        FreeImage_GetWidth(bitmap),
        FreeImage_GetHeight(bitmap),
        0,
        GL_BGR,
        GL_UNSIGNED_BYTE,
        FreeImage_GetBits(bitmap));
    
    layout(location = 0) in vec4 vertex_position;
    layout(location = 1) in vec2 texcoord;
    out vec2 f_texcoord;
    
    在片段着色器中:

    #version 330 core
    
    out vec4 fragment_color;
    varying vec2 f_texcoord;
    uniform sampler2D mytexture;
    
    void main(void)
    {
        fragment_color = texture2D(mytexture, f_texcoord);
        return;
    }
    
    out vec4 fragment_color;
    in vec2 f_texcoord;
    uniform sampler2D mytexture;
    

  • 此代码中的
    bitmap
    变量是不透明的FreeImage句柄。您不希望将其直接传递到
    glTexImage2D(…)
    中的GL数据指针。有一个函数:
    FreeImage\u GetBits(…)
    ,您需要调用它。FreeImage支持48、64、96和128位图像,以及更传统的大小(1、4、8、16、24、32)。它绝对不能只支持字节。但在这种情况下,因为这是一个PNG,PNG最大的RGB图像是48位(每个通道16位),所以您是对的(
    GL\u UNSIGNED\u INT
    是错误的)。