Misa
Misa

Reputation: 133

Rotating camera Up and Left

How can I transform the camera to left and up? I have tried to do it by few examples, but nothing good. In result camera left rotates like on pic.2, that is incorrect. Also my camera up - flips + zooms out each time i press "up" button.

Example of camera transform My camera left transform

    mat3 Transform::rotate(const float degrees, const vec3& axis) {

    mat3 second_mat = glm::mat3(
        glm::vec3(axis.x*axis.x, axis.x*axis.y, axis.x*axis.z),
        glm::vec3(axis.x*axis.y, axis.y*axis.y, axis.y*axis.z),
        glm::vec3(axis.x*axis.z, axis.y*axis.z, axis.z*axis.z));
    mat3 third_mat = glm::mat3(
        glm::vec3(0.0f, -axis.z, axis.y),
        glm::vec3(axis.z, 0.0f, -axis.x),
        glm::vec3(-axis.y, axis.x, 0.0f));
    mat3 rot = mat3((glm::radians(degrees))*mat3(1.0) + (1 - cos(glm::radians(degrees))*second_mat)+ (sin(glm::radians(degrees))*third_mat));

    return rot;
}

// Transforms the camera left around the "crystal ball" interface
void Transform::left(float degrees, vec3& eye, vec3& up) {
        up = glm::normalize(glm::cross(up, eye));

}

// Transforms the camera up around the "crystal ball" interface
void Transform::up(float degrees, vec3& eye, vec3& up) {
        eye = eye * rotate(degrees, up);
        up = up * rotate(degrees, up);
}


mat4 Transform::lookAt(vec3 eye, vec3 up) {

    glm::mat4 view = glm::lookAt(
        glm::vec3(eye.x, eye.y, eye.z),
        glm::vec3(0.0f, 0.0f, 0.0f),
        glm::vec3(up.x, up.y, up.z)
        );
    return view;
}

Upvotes: 3

Views: 1035

Answers (1)

Little Fox
Little Fox

Reputation: 1252

You have missed "cos" in "rot".

mat3 Transform::rotate(const float degrees, const vec3& axis) {
    float d = degrees *(pi / 180);
...
    mat3 rot = mat3(cosf(d)*mat3(1.0) + (1 - cosf(d))*second_mat + sinf(d)*third_mat);
}

void Transform::left(float degrees, vec3& eye, vec3& up) {
       eye = eye * rotate(degrees, up);
       up =  up * rotate(degrees, up);
       }

void Transform::up(float degrees, vec3& eye, vec3& up) {
        vec3 upv = glm::normalize(glm::cross(up, eye)); 
        eye = eye * rotate(degrees, upv);
        up = up * rotate(degrees, upv);
}

Upvotes: 5

Related Questions