C语言 OpenGL 纹理,黑色方块



我正在尝试找出在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("%sn", 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;
}

很抱歉转储了这么多代码,我无法真正确定我出错的地方,所以我不想遗漏东西。如果有人能指出我正确的方向,我将不胜感激。

几天来我一直在试图弄清楚如何做到这一点。我觉得自己很无能为力,不能花这么多时间在OpenGL最基本的功能之一上。

此代码中存在许多问题:

  1. 据我所知,GL_BGR作为内部纹理格式无效。此外,将GL_UNSIGNED_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));
    
  2. 纹理采样器均匀变量的值错误:

    glUniform1i(uniform_mytexture, GL_TEXTURE0);
    

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

    glUniform1i(uniform_mytexture, 0);
    
  3. 着色器代码使用一些在核心配置文件中不可用的存储限定符。 attributevarying在核心配置文件中无效。 attribute由顶点着色器中的in替换,varyingout替换,在片段着色器中由in替换。因此,顶点着色器中的声明应该是:

    layout(location = 0) in vec4 vertex_position;
    layout(location = 1) in vec2 texcoord;
    out vec2 f_texcoord;
    

    在片段着色器中:

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

好吧,对于初学者来说,您的图像加载例程是完全错误的。

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_UNSIGNED_BYTE(24 位(。

将您的呼叫更改为glTexImage2D以下内容:

glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGB,
        FreeImage_GetWidth  (bitmap),
        FreeImage_GetHeight (bitmap),
        0,
        GL_BGR,
        GL_UNSIGNED_BYTE, // Assumption: FreeImage_GetBPP (bitmap) == 24
        FreeImage_GetBits (bitmap));

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

FreeImage 3.13.1 文档 - 位图函数参考 - 第 14 页

在FreeImage中,出于性能原因,每个扫描线都从32位边界开始。

我建议您在阅读该 API 参考时浏览该 API 参考的其余部分。您可以使用其中的许多函数来消除诸如我上面所做的 24 位图像假设之类的东西。

最新更新