98 lines
2.5 KiB
C++
98 lines
2.5 KiB
C++
//
|
|
// Created by Lenn on 2026/1/30.
|
|
//
|
|
|
|
#ifndef INC_3DVIEWER_CAMERA_H
|
|
#define INC_3DVIEWER_CAMERA_H
|
|
#include <glad/glad.h>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
class Camera {
|
|
public:
|
|
typedef enum CameraMoveBit {
|
|
FORWARD = 0x0001,
|
|
BACKWARD = 0x0002,
|
|
LEFT = 0x0004,
|
|
RIGHT = 0x0008,
|
|
} CameraMove;
|
|
|
|
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
|
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
|
|
float yaw = -90.0f, float pitch = 0.0f)
|
|
: front_(glm::vec3(0.0f, 0.0f, -1.0f)), yaw_(yaw), pitch_(pitch), position_(position)
|
|
, worldUp_(up){
|
|
zoom_ = 45.0f;
|
|
mouseSensitivity_ = 0.1f;
|
|
moveSpeed_ = 2.5f;
|
|
updateCameraVectors();
|
|
}
|
|
|
|
void keyboardCallback(CameraMove direction, float deltaTime) {
|
|
float velocity = moveSpeed_ * deltaTime;
|
|
if (direction == CameraMove::FORWARD) {
|
|
position_ += front_ * velocity;
|
|
}
|
|
else if (direction == CameraMove::BACKWARD) {
|
|
position_ -= front_ * velocity;
|
|
}
|
|
else if (direction == CameraMove::LEFT) {
|
|
position_ -= right_ * velocity;
|
|
}
|
|
else {
|
|
position_ += right_ * velocity;
|
|
}
|
|
updateCameraVectors();
|
|
}
|
|
|
|
void mouseCallback(float xoffset, float yoffset, GLboolean constrainPitch = true) {
|
|
xoffset *= mouseSensitivity_;
|
|
yoffset *= mouseSensitivity_;
|
|
|
|
yaw_ += xoffset;
|
|
pitch_ += yoffset;
|
|
|
|
if (constrainPitch) {
|
|
if (pitch_ > 89.0f) {
|
|
pitch_ = 89.0f;
|
|
}
|
|
if (pitch_ < -89.0f) {
|
|
pitch_ = -89.0f;
|
|
}
|
|
}
|
|
updateCameraVectors();
|
|
}
|
|
|
|
glm::mat4 getViewMatrix() {
|
|
return glm::lookAt(position_, position_ + front_, up_);
|
|
}
|
|
|
|
private:
|
|
void updateCameraVectors() {
|
|
glm::vec3 front;
|
|
front.x = std::cos(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
|
front.y = std::sin(glm::radians(pitch_));
|
|
front.z = std::sin(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
|
front_ = glm::normalize(front);
|
|
|
|
right_ = glm::normalize(glm::cross(front_, worldUp_));
|
|
up_ = glm::normalize(glm::cross(right_, front_));
|
|
}
|
|
private:
|
|
glm::vec3 position_;
|
|
glm::vec3 up_;
|
|
glm::vec3 right_;
|
|
glm::vec3 front_;
|
|
glm::vec3 worldUp_;
|
|
|
|
float yaw_;
|
|
float pitch_;
|
|
|
|
float moveSpeed_;
|
|
float mouseSensitivity_;
|
|
float zoom_;
|
|
|
|
bool firstMouse_ = true;
|
|
};
|
|
|
|
#endif //INC_3DVIEWER_CAMERA_H
|