Move camera using glm::lookAt()?

179 views Asked by At

this is my CAMERA class which makes viewMat uniform var in vertexshader.

struct CAMERA {
    glm::vec3 EYE{ 0.0f,0.0f,150.0f };
    glm::vec3 AT{ 0.0f,0.0f,0.0f };
    glm::vec3 UP{ 0.0f,1.0f,0.0f };
    glm::vec3 Dir() { return glm::normalize(this->EYE - this->AT); }
    glm::vec3 Right() { return glm::normalize(glm::cross(this->UP, this->Dir())); }
    glm::vec3 Up() { return glm::normalize(glm::cross(this->Dir(), this->Right())); }
    glm::mat4 view_M() {
        glm::vec3 cameraDirection = glm::normalize(this->EYE - this->AT);
        glm::vec3 cameraRight = glm::normalize(glm::cross(this->UP, cameraDirection));
        glm::vec3 cameraUp = glm::normalize(glm::cross(cameraDirection, cameraRight));
        return glm::lookAt(this->EYE, cameraDirection, cameraUp);
    }
}camera;

and I want to make camera-movement go up down right left. I think this will be work.. but this code works not like I expect..

// go up
glm::vec3 t{ camera.Up() };
camera.EYE = glm::translate(glm::mat4(1.0f), t) * glm::vec4(camera.EYE,1.0f);
camera.AT = glm::translate(glm::mat4(1.0f), t) * glm::vec4(camera.AT, 1.0f);

enter image description here enter image description here

this is what I expected.. camera go up and objects are seem go down.. red dot is not real obj.

How can I make camera movement with (CAMERA)camera struct?

        if (up) {
            glm::mat4 R = glm::rotate(glm::mat4(1.0f), glm::radians(-degree), camera.Right());
            camera.EYE = glm::vec3(R * glm::vec4(camera.EYE, 1.0f));
            camera.UP = glm::vec3(R * glm::vec4(camera.UP, 1.0f));
        }
        if (down) {
            glm::mat4 R = glm::rotate(glm::mat4(1.0f), glm::radians(degree), camera.Right());
            camera.EYE = glm::vec3(R * glm::vec4(camera.EYE, 1.0f));
            camera.UP = glm::vec3(R * glm::vec4(camera.UP, 1.0f));
        }
        if (right) {
            glm::mat4 R = glm::rotate(glm::mat4(1.0f), glm::radians(degree), camera.Up());
            camera.EYE = glm::vec3(R * glm::vec4(camera.EYE, 1.0f));
            camera.UP = glm::vec3(R * glm::vec4(camera.UP, 1.0f));
        }
        if (left) {
            glm::mat4 R = glm::rotate(glm::mat4(1.0f), glm::radians(-degree), camera.Up());
            camera.EYE = glm::vec3(R * glm::vec4(camera.EYE, 1.0f));
            camera.UP = glm::vec3(R * glm::vec4(camera.UP, 1.0f));
        }

This is my satelite movement moving around 0,0 this code works right.

0

There are 0 answers