mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-02 04:37:54 +08:00
Readjust camera's FOV zoom code.
This commit is contained in:
@@ -27,21 +27,21 @@ const float ZOOM = 45.0f;
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
// Camera Attributes
|
||||
// camera Attributes
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 WorldUp;
|
||||
// Euler Angles
|
||||
// euler Angles
|
||||
float Yaw;
|
||||
float Pitch;
|
||||
// Camera options
|
||||
// camera options
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Zoom;
|
||||
|
||||
// Constructor with vectors
|
||||
// constructor with vectors
|
||||
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 = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
|
||||
{
|
||||
Position = position;
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
Pitch = pitch;
|
||||
updateCameraVectors();
|
||||
}
|
||||
// Constructor with scalar values
|
||||
// constructor with scalar values
|
||||
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
|
||||
{
|
||||
Position = glm::vec3(posX, posY, posZ);
|
||||
@@ -60,13 +60,13 @@ public:
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
// Returns the view matrix calculated using Euler Angles and the LookAt Matrix
|
||||
// returns the view matrix calculated using Euler Angles and the LookAt Matrix
|
||||
glm::mat4 GetViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, Up);
|
||||
}
|
||||
|
||||
// Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
|
||||
// processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
|
||||
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
|
||||
{
|
||||
float velocity = MovementSpeed * deltaTime;
|
||||
@@ -80,7 +80,7 @@ public:
|
||||
Position += Right * velocity;
|
||||
}
|
||||
|
||||
// Processes input received from a mouse input system. Expects the offset value in both the x and y direction.
|
||||
// processes input received from a mouse input system. Expects the offset value in both the x and y direction.
|
||||
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
@@ -89,7 +89,7 @@ public:
|
||||
Yaw += xoffset;
|
||||
Pitch += yoffset;
|
||||
|
||||
// Make sure that when pitch is out of bounds, screen doesn't get flipped
|
||||
// make sure that when pitch is out of bounds, screen doesn't get flipped
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
@@ -98,33 +98,32 @@ public:
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
// Update Front, Right and Up Vectors using the updated Euler angles
|
||||
// update Front, Right and Up Vectors using the updated Euler angles
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
|
||||
// processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
|
||||
void ProcessMouseScroll(float yoffset)
|
||||
{
|
||||
if (Zoom >= 1.0f && Zoom <= 45.0f)
|
||||
Zoom -= yoffset;
|
||||
if (Zoom <= 1.0f)
|
||||
Zoom -= (float)yoffset;
|
||||
if (Zoom < 1.0f)
|
||||
Zoom = 1.0f;
|
||||
if (Zoom >= 45.0f)
|
||||
Zoom = 45.0f;
|
||||
if (Zoom > 45.0f)
|
||||
Zoom = 45.0f;
|
||||
}
|
||||
|
||||
private:
|
||||
// Calculates the front vector from the Camera's (updated) Euler Angles
|
||||
// calculates the front vector from the Camera's (updated) Euler Angles
|
||||
void updateCameraVectors()
|
||||
{
|
||||
// Calculate the new Front vector
|
||||
// calculate the new Front vector
|
||||
glm::vec3 front;
|
||||
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
front.y = sin(glm::radians(Pitch));
|
||||
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
Front = glm::normalize(front);
|
||||
// Also re-calculate the Right and Up vector
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
|
||||
// also re-calculate the Right and Up vector
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp)); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -349,10 +349,9 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
||||
// ----------------------------------------------------------------------
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
if (fov >= 1.0f && fov <= 45.0f)
|
||||
fov -= yoffset;
|
||||
if (fov <= 1.0f)
|
||||
fov -= (float)yoffset;
|
||||
if (fov < 1.0f)
|
||||
fov = 1.0f;
|
||||
if (fov >= 45.0f)
|
||||
if (fov > 45.0f)
|
||||
fov = 45.0f;
|
||||
}
|
||||
Reference in New Issue
Block a user