Friday, May 3, 2024
HomeGame Developmentopengl - Cube gets squashed after applying perspective matrix

opengl – Cube gets squashed after applying perspective matrix


I’m trying to apply perspective to a simple, rotating cube. I created a perspective matrix using glm library, multiplied it with lookAt, rotation and translation matrix and passed the result to the vertex shader as a uniform. In vertex shader I’m simply multiplying that matrix times vertex coordinates.

Without perspective I’m observing a spinning cube, but when I apply perspective, it’s getting squashed and distorted. I’m trying to understand what is causing that effect and possibly what am I doing wrong.

Here is a video of that effect: https://imgur.com/hMdZPza and a 2 screenshots before and after projection https://imgur.com/a/oisEEm1. For me it looks like the front face has moved to the back of the cube?

My render function:

    void render() {
        createVertexBuffer();
        createIndexBuffer();

        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);

        // Position
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0);

        //Color
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);

        while (!engine->windowShouldClose()) {
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            static float scale{};
            scale += 0.01;

            glm::mat4 m_projection = glm::perspective(glm::radians(30.0f),
                (float)engine->getWidth() / (float)engine->getHeight(),
                0.1f, 100.0f);
            glm::mat4 m_view = glm::lookAt(
                glm::vec3(0, 0, 5),
                glm::vec3(0, 0, 0),
                glm::vec3(0, 1, 0)
            );
            glm::mat4 m_rotation = glm::rotate(glm::mat4(1), scale, glm::vec3(0, 1, 0));
            glm::mat4 m_model = glm::translate(m_rotation, glm::vec3(0.0f, 0.0f, 0.0f));

            glm::mat4 mvp_matrix{};
            if (projection) {
                mvp_matrix = m_projection * m_view * m_model;
            }
            else {
                mvp_matrix = m_model;
            }
            glUniformMatrix4fv(uniforms[0].location, 1, GL_TRUE, &mvp_matrix[0][0]);

            glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);

            engine->swapBuffers();
            glfwPollEvents();
        }
    }

And a vertex shader

#version 460 core

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 inColor;

out vec4 color;

uniform mat4 MVP;

void main() {
    gl_Position = MVP * vec4(position, 1);
    color = vec4(inColor, 1);
}
```

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments