Search code examples
c++openglglslshaderglm-math

OpenGL disappearing element after changing lookatmatrix


The vertices of my cube are defined to be at

float vertices[180] = {
    -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
     0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
    -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
     0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
     0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
     0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
    -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

    -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

     0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
     0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
     0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
     0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
     0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
     0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
     0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
     0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
     0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
     0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
};

where the last two coordinates of each line are texture coordinates.

In the vertex shader, I am simply multiplying the vertex position by the viewMatrix, and am not currently considering applying any modelMatrix or projectionMatrix yet.

Here is my vertex shader:

layout (location = 0) in vec3 aPos;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
void main()
{
   gl_Position =viewMatrix * vec4(aPos, 1.0f);
}

Now when I change the viewMatrix like so:

viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));

my object completely disappears.

Here is my render loop:

while (!glfwWindowShouldClose(window_)) {

    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // also clear the depth buffer now!

    auto elapsedTime = GetElapsedTime();
    shaderProgram_.Use();

    // change the color
    HSV::RGB timeColor = HSV::HSVtoRGB(static_cast<int>(elapsedTime* 100) % 360, 100, 100);
    shaderProgram_.SetUniformVec3("ResColor", { timeColor.r / 255, timeColor.g / 255, timeColor.b / 255 });


    viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));

    shaderProgram_.SetUniformMat4("viewMatrix", viewMatrix_);
    glBindVertexArray(VAO_);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    //glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_INT, 0);

    glfwSwapBuffers(window_);
    glfwPollEvents();
}

Solution

  • The projection matrix is the identity matrix. Therefore the near plane is -1 and the far plane is 1. The distance between the camera and the center of the cube is 2. Therefore the cube is clipped by the far plane.

    Change the position of the camera

    viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));

    viewMatrix_ = glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, 0, -2.0f), glm::vec3(0, 0, 0));
    

    Alternatively, you can use an orthographic projection matrix in which you increase the distance to the far plane (see glm::ortho)

    glm::mat4 projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 10.0f);
    

    Or a perspective projection (glm::perspective)

    glm::mat4 projection = glm::perspective(glm::radians(60.0f), 1.0f, 0.1f, 10.0f);