link mira
link mira

Reputation: 19

Camera movement on opengl project: sideways movement rotates and skews image

im trying to make a simple triangle mesh and look at it with a camera. The mesh should be in the x/y plane with z = 0, while the camera should be at the same plane but z = 10. The project is in this repo

https://github.com/SeaMira/Computacion-En-GPU/tree/main/Tareas/Tarea3/T3

I define and draw the buffers info on main, while i use camera2.cpp and camera2.h for everything else.

The thing is that using left or right it should move along x axis, but it does a strange rotation and skewing, since center keeps its place, but everything get distorted more and more as you move away from it.

In first image i only press down arrow key to get away from plane and look at ir, then in the second image i press left arrow key, and looks rotated, but it shouldnt be that way.

Starting values for camera are m_pos=(5,5,10), m_front=(0,0,-1) and m_up=(0,1,0), with a view matrix this way: glm::lookAt(m_pos, m_pos + m_front, m_up);

For some reason it looks at (0,0,-1) and is not adding the camera position when doing the glm::lookAt. Image shows when i just run the project and move the camera a bit (since it also doesn't where it should)starting phase But then, by pressing right arrow key it looks like this: pressing right key Its still looking at 0,0,-1 and looks skewed. I dont know what is wrong with it.

This is camera movement

switch (key) {
    // up
    case 0:
        m_pos += (m_front * m_speed*dt);
        break;
    // down
    case 1:
        m_pos -= (m_front * m_speed*dt);
        break;
    // left
    case 2:
        {
            glm::vec3 Left = glm::cross(m_front,m_up);
            Left = glm::normalize(Left);
            Left *= m_speed*dt;
            // m_front -= Left;
            m_pos -= Left;
        }
        break;
    // right
    case 3:
        {
            glm::vec3 Right = glm::cross(m_up, m_front);
            Right = glm::normalize(Right);
            Right *= m_speed*dt;
            // m_front -= Right;
            m_pos -= Right;
        }
        break;

This are inital values for camera. Width and Height are the values of the viewport. I change m_pos to the one mentioned earlier.

Camera::Camera(int WindowWidth, int WindowHeight) {
    m_pos          = glm::vec3(1.0f, 1.0f, 5.0f);
    m_front       = glm::vec3(0.0f, 0.0f, -1.0f);
    m_up           = glm::vec3(0.0f, 1.0f, 0.0f);
    m_windowWidth = WindowWidth;
    m_windowHeight = WindowHeight;
    Init();
}

I get view matrix here:

glm::mat4 Camera::GetMatrix() {
    return glm::lookAt(m_pos, m_pos + m_front, m_up);
}

Projection is a perspective matrix

return glm::perspective(glm::radians(FOV), ar, zNear, zFar);

with values

float FOV = 45.0f;
float zNear = 0.1f;float zFar = 100.0f;
int width, height;

Width and Height are the values of the viewport too. Model matrix is the identity. I then give to my shader ProjectionViewModel.

I dont know whats wrong.

Upvotes: 1

Views: 33

Answers (0)

Related Questions