OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置

OpenGL 4.3 incorrectly mapping 4th texture coordinate to the same poisition as the 3rd texture coordinate

当我 运行 这个程序时,它似乎只将我的纹理映射到直角三角形的 3 个边(我的顶点数组中的前 3 个点),而完全错过了第四个,因为当我改变那一点时,图像没有任何变化。 https://imgur.com/a/MvhsYYv 这里是当前输出作为顶部图像,预期输出作为第二张图像。当我在我的着色器中只用一种颜色对其进行纹理处理时,它映射得很好,带有纹理,它似乎仍然在绘制一个正方形,但是当它应该映射到右上角时映射到左上角。

 Vertex vertices[] = {
     //Position                 //Colour
     vec3(-0.5f, 0.5f, 0.f),        vec3(1.f, 0.f, 0.f),    vec2(0.f, 1.f), //Top left
     vec3(-0.5f, -0.5f, 0.f),       vec3(0.f, 1.f, 0.f),    vec2(0.f, 0.f), //Bottom Left
     vec3(0.5f, -0.5f, 0.f),        vec3(0.f, 0.f, 1.f),    vec2(1.f, 0.f), //Bottom Right
     vec3(0.5f, 0.5f, 0.f),         vec3(1.f, 1.f, 1.f),    vec2(1.f, 1.f)  //Top Right
};

 unsigned int numOfVertices = sizeof(vertices) / sizeof(Vertex);

 GLuint indices[] = {
     0,1,2,
     3,0,2

 };

 unsigned numOfIndices = sizeof(indices) / sizeof(GLint);

void updateInput(GLFWwindow* window) {
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

int main() {
    //INIT GLFW
    glfwInit();

    Wrapper mainWrapper = Wrapper();
    Shader *mainShader = new Shader(vs_source.c_str(), fs_source.c_str());

    GLuint VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    GLuint VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    GLuint EBO;
    glGenBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glVertexAttribPointer(0,4, GL_FLOAT, GL_FALSE, sizeof(Vertex),(GLvoid*)offsetof(Vertex, position));
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
    glEnableVertexAttribArray(1);

    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texcoord));
    glEnableVertexAttribArray(2);

    glBindVertexArray(0);

    //TEXTURE INIT
    int image_width = 0;
    int image_height = 0;
    unsigned char* image = SOIL_load_image("images/super-meat-boy.png", &image_width, &image_height, NULL, SOIL_LOAD_RGBA);
    GLuint texture0;
    glGenTextures(1, &texture0);
    glBindTexture(GL_TEXTURE_2D, texture0);
    glGenerateMipmap(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    if (image) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
        glGenerateMipmap(GL_TEXTURE_2D);
        std::cout << "IMAGE LOADED";
    }
    else {
        std::cout << "Error loading image";
    }
    //Clean up, needed when stuff done with texture
    glActiveTexture(0);
    glBindTexture(GL_TEXTURE_2D, 0);
    SOIL_free_image_data(image);
    //MAIN LOOP
    while (!glfwWindowShouldClose(mainWrapper.getWindow())) {
        //UPDATE INPUT
        updateInput(mainWrapper.getWindow());
        glfwPollEvents();
        //UPDATE

        //DRAW------

        //clear
        glClearColor(0.f,0.f,0.f,1.f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

        //draw
        glUseProgram(mainShader->myProgram);

        //activate texture
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture0);

        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, numOfIndices, GL_UNSIGNED_INT, 0);

        //END DRAW
        glfwSwapBuffers(mainWrapper.getWindow());
        glFlush();
    }

    //END OF PROGRAM
    glfwDestroyWindow(mainWrapper.getWindow());
    delete mainShader;
    glfwTerminate();

    return 0;
}

    #version 440

layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec3 vertex_color;
layout (location = 2) in vec3 vertex_texcoord;

out vec3 vs_position;
out vec3 vs_color;
out vec2 vs_texcoord;

void main() {
    vs_position = vertex_position;
    vs_color = vertex_color;
    vs_texcoord = vec2(vertex_texcoord.x, vertex_texcoord.y * -1.f);

    gl_Position = vec4(vertex_position, 1.f);
}

    #version 440

in vec3 vs_position;
in vec3 vs_color;
in vec2 vs_texcoord;

out vec4 fs_color;

uniform sampler2D texture0;

void main() {
    fs_color = vec4(vs_color, 1.f);
    fs_color = texture(texture0, vs_texcoord);
}

glVertexAttribPointer 的第二个参数必须是顶点缓冲区中属性的元组大小(组件数)。

您的顶点坐标有 3 个分量 x、y、z。颜色属性也有 3 个分量(红色、绿色、蓝色)。纹理坐标有 2 个分量 u 和 v:

glVertexAttribPointer(0, 
    3, // 3 instead of 4
    GL_FLOAT, GL_FALSE, sizeof(Vertex),(GLvoid*)offsetof(Vertex, position));
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 
    3, // 3 instead of 4
    GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
glEnableVertexAttribArray(1);

glVertexAttribPointer(2,
    3, // 2 instead of 3
    GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(2);

纹理坐标元组大小的参数 3 导致您越界访问索引为 3 的顶点坐标的顶点缓冲区。

我的 glVertexAttribPointer 中的大小不正确。从那以后,我将我的顶点更改为 vec2、vec3、vec2,并相应地更改了大小。