'Can't get perspective to work with my image

So I'm following LearnOpenGL tutorials for the camera. I am trying to render a cube (definied with the vector of verts) and just circle around the cube to make sure it has rendered properly. I can render the front face of the cube just fine, but as soon as I switch the shader to use MVP nothing renders

void GraphicsProgram::run_program() 
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    state = RUNNING;

    // TODO: Move these into their respective classes
    // Hardcoded for some testing
    // ----------------------------------------------
    const char *vertexShaderPath = "vertex_shader.vs";
    const char *fragmentShaderPath = "fragment_shader.fs";
    Shader firstShader = Shader(vertexShaderPath, fragmentShaderPath);

    std::vector<Vertex> verts;
    Vertex v1, v2, v3, v4, v5, v6, v7, v8;
    v1.position = { -0.5, -0.5, +0.5 };
    v2.position = { +0.5, -0.5, +0.5 };
    v3.position = { +0.5, +0.5, +0.5 };
    v4.position = { -0.5, +0.5, +0.5 };
    v5.position = { +0.5, -0.5, -0.5 };
    v6.position = { +0.5, +0.5, -0.5 };
    v7.position = { -0.5, +0.5, -0.5 };
    v8.position = { -0.5, -0.5, -0.5 };

    verts.push_back(v1);
    verts.push_back(v2);
    verts.push_back(v3);
    verts.push_back(v4);
    verts.push_back(v5);
    verts.push_back(v6);
    verts.push_back(v7);
    verts.push_back(v8);

    std::vector<unsigned int> indicies = { 
        0, 1, 2, 0, 2, 3, // front
        1, 4, 5, 1, 5, 2, // right
        4, 7, 6, 4, 6, 7, // back
        7, 0, 3, 7, 3, 6, // left
        3, 2, 5, 3, 5, 6, // top
        7, 4, 1, 7, 1, 0  // bottom
    };


    unsigned int VBO, VAO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(Vertex), &verts[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicies.size() * sizeof(unsigned int), &indicies[0], GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);


    glUseProgram(firstShader.shaderID);

    glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
    firstShader.set_mat4("projection", projection);

    while (state == RUNNING) {
        currentFrame = (float)SDL_GetTicks() / 1000;
        deltaTime = lastFrame - currentFrame;
        lastFrame = currentFrame;

        process_input();

        glUseProgram(firstShader.shaderID);

        glm::mat4 view;
        float radius = 10.0f;
        float camX = sin((float)SDL_GetTicks() / 1000) * radius;
        float camZ = cos((float)SDL_GetTicks() / 1000) * radius;
        view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        firstShader.set_mat4("view", view);

        glBindVertexArray(VAO);

        glm::mat4 model;
        model = glm::translate(model, glm::vec3(0.0, 0.0, 0.0));
        float angle = 0.0;
        model = glm::rotate(model, glm::radians(angle), glm::vec3(0,0,0));
        firstShader.set_mat4("model", model);

        glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_INT, 0);
        SDL_GL_SwapWindow(window);
    }

    shutdown_program();
}

Vertex struct:

struct Vertex {
    glm::vec3 position;
    glm::vec3 normal;
    glm::vec2 texCoord;
};

Vertex Shader code:

#version 330 core
layout (location = 0) in vec3 aPos;

out vec4 vertexColor;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main() {
    gl_Position = projection * view * model * vec4(aPos, 1.0f);
    vertexColor = vec4(abs(aPos.x), abs(aPos.y), abs(aPos.z), 1.0);
}

Fragment Shader:

#version 330 core

in vec4 vertexColor;
out vec4 fragColor;

void main() {
    fragColor = vertexColor;
}

The vertex shader I used originally just took in the vertex positions.



Solution 1:[1]

You have to initialize the matrix variables glm::mat4 model and glm::mat4 view.

The glm API documentation refers to The OpenGL Shading Language specification 4.20.

5.4.2 Vector and Matrix Constructors

If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.

This means, that an identity matrix can be initialized by the single parameter 1.0:

glm::mat4 model(1.0f);

....

glm::mat4 view(1.0f);

You missed to set a proper rotation axis in the glm::rotate:

model = glm::rotate(model, glm::radians(angle), 
    glm::vec3(1.0,0,0)); // e.g. (1, 0, 0) instead of (0, 0, 0)

You have to clear the default farmebuffer before every frame (glClear):

glClearColor(0.0, 0.0, 0.0, 1.0);

while (state == RUNNING)
{
    glClear(GL_COLOR_BUFFER_BIT);

    .....

You should enable the Depth Test and clear the depth buffer too:

glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);

while (state == RUNNING)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    .....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community