c++openglglslglfwsoil

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


When I run this program, it seems to only be mapping my textures to 3 sides of a right angle triangle (the first 3 points in my vertices array), and completely missing out the fourth, as when I change that bit, nothing changes in the image. https://i.sstatic.net/d8loL.jpg here is the current output as the top image, and the expected output as the 2nd image. When I texture it with just a colour in my shader, it maps fine, with a texture, it still seems that it is drawing a square, but then mapping to the top left when it should be mapping to the top right.

 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);
}

Solution

  • The 2nd parameter of glVertexAttribPointer has to be the tuple size (number) of components) of the attribute in the vertex buffer.

    Your vertex coordinates have 3 components x, y, z. The color attributes have 3 components too (red, green, blue). The texture coordinate have 2 components u and 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);
    

    The parameter of 3 for the tuple size of the texture coordinates causes that you access the vertex buffer out of bounds for the vertex coordinate with index 3.