mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-30 20:13:22 +08:00
Readjust camera's FOV zoom code.
This commit is contained in:
@@ -27,21 +27,21 @@ const float ZOOM = 45.0f;
|
|||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Camera Attributes
|
// camera Attributes
|
||||||
glm::vec3 Position;
|
glm::vec3 Position;
|
||||||
glm::vec3 Front;
|
glm::vec3 Front;
|
||||||
glm::vec3 Up;
|
glm::vec3 Up;
|
||||||
glm::vec3 Right;
|
glm::vec3 Right;
|
||||||
glm::vec3 WorldUp;
|
glm::vec3 WorldUp;
|
||||||
// Euler Angles
|
// euler Angles
|
||||||
float Yaw;
|
float Yaw;
|
||||||
float Pitch;
|
float Pitch;
|
||||||
// Camera options
|
// camera options
|
||||||
float MovementSpeed;
|
float MovementSpeed;
|
||||||
float MouseSensitivity;
|
float MouseSensitivity;
|
||||||
float Zoom;
|
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)
|
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;
|
Position = position;
|
||||||
@@ -50,7 +50,7 @@ public:
|
|||||||
Pitch = pitch;
|
Pitch = pitch;
|
||||||
updateCameraVectors();
|
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)
|
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);
|
Position = glm::vec3(posX, posY, posZ);
|
||||||
@@ -60,13 +60,13 @@ public:
|
|||||||
updateCameraVectors();
|
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()
|
glm::mat4 GetViewMatrix()
|
||||||
{
|
{
|
||||||
return glm::lookAt(Position, Position + Front, Up);
|
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)
|
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
|
||||||
{
|
{
|
||||||
float velocity = MovementSpeed * deltaTime;
|
float velocity = MovementSpeed * deltaTime;
|
||||||
@@ -80,7 +80,7 @@ public:
|
|||||||
Position += Right * velocity;
|
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)
|
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
|
||||||
{
|
{
|
||||||
xoffset *= MouseSensitivity;
|
xoffset *= MouseSensitivity;
|
||||||
@@ -89,7 +89,7 @@ public:
|
|||||||
Yaw += xoffset;
|
Yaw += xoffset;
|
||||||
Pitch += yoffset;
|
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 (constrainPitch)
|
||||||
{
|
{
|
||||||
if (Pitch > 89.0f)
|
if (Pitch > 89.0f)
|
||||||
@@ -98,33 +98,32 @@ public:
|
|||||||
Pitch = -89.0f;
|
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();
|
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)
|
void ProcessMouseScroll(float yoffset)
|
||||||
{
|
{
|
||||||
if (Zoom >= 1.0f && Zoom <= 45.0f)
|
Zoom -= (float)yoffset;
|
||||||
Zoom -= yoffset;
|
if (Zoom < 1.0f)
|
||||||
if (Zoom <= 1.0f)
|
|
||||||
Zoom = 1.0f;
|
Zoom = 1.0f;
|
||||||
if (Zoom >= 45.0f)
|
if (Zoom > 45.0f)
|
||||||
Zoom = 45.0f;
|
Zoom = 45.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
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()
|
void updateCameraVectors()
|
||||||
{
|
{
|
||||||
// Calculate the new Front vector
|
// calculate the new Front vector
|
||||||
glm::vec3 front;
|
glm::vec3 front;
|
||||||
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||||
front.y = sin(glm::radians(Pitch));
|
front.y = sin(glm::radians(Pitch));
|
||||||
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||||
Front = glm::normalize(front);
|
Front = glm::normalize(front);
|
||||||
// Also re-calculate the Right and Up vector
|
// 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.
|
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));
|
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)
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||||
{
|
{
|
||||||
if (fov >= 1.0f && fov <= 45.0f)
|
fov -= (float)yoffset;
|
||||||
fov -= yoffset;
|
if (fov < 1.0f)
|
||||||
if (fov <= 1.0f)
|
|
||||||
fov = 1.0f;
|
fov = 1.0f;
|
||||||
if (fov >= 45.0f)
|
if (fov > 45.0f)
|
||||||
fov = 45.0f;
|
fov = 45.0f;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user