简体   繁体   中英

Limiting rotation around the x-axis in opengl

For fun, I've made a 3d camera in opengl. It works well, except for the fact that I cannot figure out how to limit rotation about the x-axis. If you scroll up too much, the up and down controls will invert. I've tried limiting the camFront.y variable to an arbitrary value, but the camera will still flip over the x-axis.

Here is my code:

#ifndef CAMERA_H
#define CAMERA_H

#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>

#define WORLD_UP glm::vec3(0.0f, 1.0f, 0.0f)

#include <iostream>


enum CamDirection {
    CAM_FORWARD,
    CAM_BACKWARD,
    CAM_LEFT,
    CAM_RIGHT
};


class Camera {
public:
    void cameraUpdate();

    glm::mat4 getViewMatrix();

    Camera();

    Camera(glm::vec3 startPosition);

    void move(CamDirection dir, GLfloat deltaTime);

    void look(double xOffset, double yOffset);

    void update();

private:

    glm::vec3 camPos;
    glm::vec3 camFront;
    glm::vec3 camUp;
    glm::vec3 camRight;

    const GLfloat camSpeed = 5.05f;

};

glm::mat4 Camera::getViewMatrix() {
    return glm::lookAt(camPos, camPos + camFront, camUp);
}

Camera::Camera():
    camPos  (glm::vec3(0.0f, 0.0f,  0.0f)),
    camFront(glm::vec3(0.0f, 0.0f, -1.0f)),
    camUp   (WORLD_UP)
{}

Camera::Camera(glm::vec3 startPos):
    camPos   (startPos),
    camFront (glm::vec3(0.0f, 0.0f, -1.0f)),
    camUp    (WORLD_UP)
{}

void Camera::move(CamDirection dir, GLfloat deltaTime) {
    const GLfloat v = camSpeed * deltaTime;
    if (dir == CAM_FORWARD)
        camPos += v * camFront;
    else if (dir == CAM_BACKWARD)
        camPos -= v * camFront;
    else if (dir == CAM_RIGHT)
        camPos += v * camRight;
    else
        camPos -= v * camRight;
}
void Camera::look(double xOffset, double yOffset) {
    glm::quat startQuat = {0, camFront.x, camFront.y, camFront.z};

    glm::quat rotation = glm::angleAxis((GLfloat)xOffset, glm::vec3(0.0f, 1.0f, 0.0f));
    glm::quat view = startQuat * rotation;

    rotation = glm::angleAxis((GLfloat)yOffset, glm::vec3(-1.0f, 0.0f, 0.0f));
    view = view * rotation;

    camFront = glm::vec3(view.x, view.y, view.z);
    std::cerr << camFront.x << ' ' << camFront.y << ' ' << camFront.z << std::endl;

}

void Camera::update() {
        // Also re-calculate the Right and Up vector
        camRight = glm::normalize(glm::cross(camFront, WORLD_UP));  // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
        camUp    = glm::normalize(glm::cross(camRight, camFront));
}
#endif // CAMERA_H

How can I fix this?

You need to limit view.y and view.z values before assigning them to camFront , to be max 89 degrees and minimum -89 degrees. At 90 and -90 degrees it starts to invert. So a very simple approach could be,

if(view.y > 89)
{
  view.y = 89;
}
if(view.y < -89)
{
  view.y = -89;
}
if(view.z > 89)
{
  view.z = 89;
}
if(view.z < -89)
{
  view.z = -89;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM