OpenGL纹理不显示,但给予损坏的双向链接列表错误

nwnhqdif  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(105)

我已经玩了一段时间的纹理,我只是不能让它工作
下面是代码


# include "App.h"

std::array <float, 4> background_col = {0.25, 0.25, 0.25, 1.0}  ;
std::string name                     = "OpenGL"                 ;
std::string icon                     = "data/images/dot.png"    ;
const int width                      = 800                      ;
const int height                     = 800                      ;
bool anti_aliasing                   = true                     ;
bool FPS_60_CAP                      = true                     ;

MVP mvp;

void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{

}

void App::run()
{
    Window window(true, anti_aliasing);
    window.make_Window(glfwCreateWindow(width, height, name.c_str(), NULL, NULL), key_callback, icon);

    srand(time(NULL));
    mvp.view = glm::ortho(0.0f, (float)width, 0.0f, (float)height);

    Renderer renderer;
    Player player;

    // redesighn with errors in mind

    // 1. TODO: Implement Text
    // 2. TODO: Implement UI

    std::vector<float> vertexes = {
        200, 200, 1, 1, 1, 1, 0, 0,
        200, 600, 1, 1, 1, 1, 0, 1,
        600, 600, 1, 1, 1, 1, 1, 1,
        600, 200, 1, 1, 1, 1, 1, 0
    };

    std::vector<uint32_t> indexes = {0, 1, 2, 3};

    while (!glfwWindowShouldClose(window.window))
    {
        glClearColor(background_col[0], background_col[1], background_col[2], background_col[3]);
        glClear(GL_COLOR_BUFFER_BIT);
        warn(0);
        Shader shader("shaders/text.shader");
        shader.useShader();
        warn(1);
        Texture tex("data/images/dot.png");
        tex.Bind();
        warn(2);
        shader.uniformMat4Float("u_projection_matrix", mvp.projection * mvp.view * mvp.model);
        shader.uniformInt({0}, "tex");
        warn(3);
        VBO vbo;
        VAO vao;
        IBO ibo;
        warn(4);
        vbo.write(vertexes.data(), vertexes.size() * sizeof(float));
        ibo.write(indexes.data(), indexes.size() * sizeof(uint32_t));
        warn(5);
        ibo.bind();
        vbo.bind();
        vao.bind();
        warn(6);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (const void *)(sizeof(float) * 0));

        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (const void *)(sizeof(float) * 2));

        glEnableVertexAttribArray(2);
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (const void *)(sizeof(float) * 6));
        warn(7);
        glDrawElements(GL_QUADS, indexes.size(), GL_UNSIGNED_INT, nullptr);
        warn(8);
        ibo.unbind();
        vbo.unbind();
        vao.unbind();
        warn(9);
        tex.unBind();
        tex.~Texture();
        warn(10);
        vbo.~VBO();
        vao.~VAO();
        ibo.~IBO();
        warn(11);
        shader.deleteShader();
        warn(12);
        glfwSwapBuffers(window.window);
        glfwSwapInterval(FPS_60_CAP);
        glfwPollEvents();
    }

    alcCloseDevice(player.Device);
    window.~Window();
}

纹理类:


# include "Core.h"

Texture::Texture(std::string path)
{
    this->path = path;
    this->bpp = 0;
    this->height = 0;
    this->width = 0;
    this->localbuffer = nullptr;
    this->TexID = 0;

    stbi_set_flip_vertically_on_load(1);
    localbuffer = stbi_load(path.c_str(), &width, &height, &bpp, 4);

    glGenTextures(1, &TexID);
    glBindTexture(GL_TEXTURE_2D, TexID);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, localbuffer);
    glBindTexture(GL_TEXTURE_2D, 0); 

    if (localbuffer) { stbi_image_free(localbuffer); }
    else { info("h"); }
}

void Texture::Bind(uint32_t slot)
{
    glActiveTexture(GL_TEXTURE0 + slot);
}

void Texture::unBind()
{
    glBindTexture(GL_TEXTURE_2D, 0); 
}

Texture::~Texture()
{
    glDeleteTextures(1, &TexID);
}

和着色器:


# shader vertex

# version 400 core

layout (location = 0) in vec4 Pos;
layout (location = 1) in vec4 vColor;
layout (location = 2) in vec2 vTexCoord;

out vec4 color;
out vec2 TexCoord;

uniform mat4 u_projection_matrix;

void main()
{
    gl_Position = u_projection_matrix * Pos;
    color = vColor;
    TexCoord = vTexCoord;
}

# shader fragment

# version 400 core

in vec4 color;
in vec2 TexCoord;

uniform sampler2D tex;

void main()
{
    vec4 texcol = texture(tex, TexCoord);
    gl_FragColor = texcol;
}

它将保持空白并显示此错误

corrupted double-linked list

需要指出的是,该错误发生在第二帧上,
我知道的代码是工作的,因为我已经得到了正确显示之前的纹理是:
Shader类别
VAO 、VBO和IBO
编辑:从rafix 07修复后,它不再崩溃,只是不显示
编辑2:
我已经实现了glDebugMessageCallback,但它返回了以下错误:
资料来源:33352
型号:33361
标识号:1
严重级别:33387
长度:130
GL错误!:着色器统计信息:SGPRS:24个VGPRS:24码大小:72 LDS:0刮擦:0最大波数:10个溢出的SGPR:0溢出的VGPR:0个专用内存VGPR:0

qlckcl4x

qlckcl4x1#

删除析构函数的所有显式调用,如:

tex.~Texture();

由于您两次取消分配资源,因此会得到 * 损坏的双向链表 *:第一次是显式调用析构函数,第二次是在定义了对象的作用域末尾调用析构函数时。

{
  Texture tex;
  // ...
  tex.~Texture();  // texture is deallocated and id texture is dangling reference
  // <-- dtor of tex is called implicitly, and the same id texture is deleted again leading to crash
}

相关问题