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

时间:2018-10-16 18:21:30

标签: c++ opengl glsl glfw soil

当我运行该程序时,似乎只将纹理映射到直角三角形的3个边(顶点数组中的前3个点),而完全遗漏了第4个,就像我更改该位时一样,图像没有变化。 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);
}

2 个答案:

答案 0 :(得分:2)

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

您的顶点坐标具有3个分量x,y,z。颜色属性也具有3个成分(红色,绿色,蓝色)。纹理坐标具有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的顶点坐标的边界之外的顶点缓冲区。

答案 1 :(得分:0)

在我的glVertexAttribPointer中调整大小不正确。此后,我将顶点更改为vec2,vec3,vec2,并相应地更改了大小。