glDrawElements()大小为4的无效写入

时间:2019-07-08 12:06:21

标签: c opengl clang

所以我正在使用C和OpenGL绘制一些文本,它确实很好用,但是在使用Valgrind查找内存泄漏时发现了一些问题。

这是我的代码:

void draw_text(
    const char* text,
    float x,
    float y,
    float z,
    float r,
    float g,
    float b,
    float size,
    float spacing,
    state_T* state
)
{
    float d_r = r / 255.0f;
    float d_g = g / 255.0f;
    float d_b = b / 255.0f;

    float vertices[] = 
    {
        // positions          // colors           // texture coords
        size,  size, 0.0f,   d_r, d_g, d_b,   1.0f, 1.0f,   // top right
        size, -size, 0.0f,   d_r, d_g, d_b,   1.0f, 0.0f,   // bottom right
        -size, -size, 0.0f,   d_r, d_g, d_b,   0.0f, 0.0f,   // bottom left
        -size,  size, 0.0f,   d_r, d_g, d_b,   0.0f, 1.0f    // top left
    };

    unsigned int indices [] =
    {
        0, 1, 3,   // first triangle
        1, 2, 3    // second triangle
    };

    glBindVertexArray(state->VAO);
    glUseProgram(SHADER_TEXTURED);
    send_projection_view_state(SHADER_TEXTURED, state->camera->projection_view);

    unsigned int VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    for (int i = 0; i < strlen(text); i++)
    {
        char c = text[i];

        unsigned int EBO;

        glGenBuffers(1, &EBO);

        mat4 model =
        {
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
        };

        glm_translate(model, (vec3){x, y, z});

        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

        // position attribute
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        // color attribute
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3* sizeof(float)));
        glEnableVertexAttribArray(1);

        // texcoords
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
        glEnableVertexAttribArray(2);

        glActiveTexture(GL_TEXTURE0);
        //unsigned int tex = get_char_texture_from_texture(c, 7, 7, 8, 12, TEXTURE_DEFAULT_FONT);
        unsigned int tex = get_char_texture_from_texture(c, 8, 8, 16, 16, TEXTURE_LN_EGA8x8_FONT);
        glBindTexture(GL_TEXTURE_2D, tex);
        glUniform1i(glGetUniformLocation(SHADER_TEXTURED, "ourTexture"), 0); 
        glBindVertexArray(state->VAO);

        unsigned uniform_mat4_model = glGetUniformLocation(SHADER_TEXTURED, "model");
        glm_translate(model, (vec3){(i * (size + spacing)), 0, 0});
        glUniformMatrix4fv(uniform_mat4_model, 1, GL_FALSE, (float *) model);

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glDeleteBuffers(1, &EBO);
        glDeleteTextures(1, &tex);
    }

    glDeleteBuffers(1, &VBO);
    glBindVertexArray(0); 
}

现在,valgrind告诉我这是我调用glDrawElements的那一行的“大小为4的无效写入”。

以下是确切的valgrind输出:

==29044== Invalid write of size 4
==29044==    at 0x7385901: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
==29044==    by 0x7390B8F: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
==29044==    by 0x737E8FB: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
==29044==    by 0x75BD48D: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
==29044==    by 0x75BE1A6: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
==29044==    by 0x10FE07: draw_text (draw_utils.c:365)

我该如何解决?

我尝试摆弄glDrawElements调用的参数,但没有解决任何问题。

注意: get_char_texture_from_texture只是从我正在使用的Spritesheet字体中“切出”一个字符并将其绑定到纹理上。

0 个答案:

没有答案