c++openglgraphicsglm-matharcball

How glm::perspective() and glm::lookAt() work together?


I am trying to figure out exactly how lookAt and perspective functions work.

I would like to change the value of glm::lookAt (in particular the z value of the camera position) and I have noticed that If that value is different from the value of near plane in glm::perspective it breaks my object (it looks anormal). Then, I do not know if my problem is related to the arcball (but I don't think so since the problem appears also before moving the object) or to the fact that for some reason I should use the same value for the camera position (z value) in the lookAt function and in the near plane in the perspective function.

Any suggestions?

I took the implementation of the arcball from https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Arcball

Main.cpp:

// ... Something ...

// lookAt function
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0., 
0., 0.), glm::vec3(0., 1., 0.));

// ... Something ...

while(!glfwWindowShouldClose(window)) {

    // ... Something ...

    // perspective
    glm::mat4 projection = glm::perspective(glm::radians(45.0f), 
    (float)WIDTH / (float)HEIGHT, 0.1f, 10.f);

    // arcball
    glm::mat4 rotated_view = view * arcball.rotation_matrix_view();
    glm::mat4 rotated_model = model * 
    arcball.rotation_matrix_model(rotated_view);

    ourShader.setMat4("projection", projection);
    ourShader.setMat4("view", rotated_view);
    ourShader.setMat4("model", rotated_model);

    // ... Something ...

}
// ... Something ...

Solution

  • Update of the question. SOLVED!

    I have just found that was a problem of z-fighting.

    For other users that have my same problem, I suggest that page: https://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm