mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-02 04:37:54 +08:00
Code/content re-work: getting started.
This commit is contained in:
@@ -16,11 +16,11 @@ enum Camera_Movement {
|
||||
};
|
||||
|
||||
// Default camera values
|
||||
const GLfloat YAW = -90.0f;
|
||||
const GLfloat PITCH = 0.0f;
|
||||
const GLfloat SPEED = 2.5f;
|
||||
const GLfloat SENSITIVTY = 0.1f;
|
||||
const GLfloat ZOOM = 45.0f;
|
||||
const float YAW = -90.0f;
|
||||
const float PITCH = 0.0f;
|
||||
const float SPEED = 2.5f;
|
||||
const float SENSITIVTY = 0.1f;
|
||||
const float ZOOM = 45.0f;
|
||||
|
||||
|
||||
// An abstract camera class that processes input and calculates the corresponding Eular Angles, Vectors and Matrices for use in OpenGL
|
||||
@@ -34,83 +34,83 @@ public:
|
||||
glm::vec3 Right;
|
||||
glm::vec3 WorldUp;
|
||||
// Eular Angles
|
||||
GLfloat Yaw;
|
||||
GLfloat Pitch;
|
||||
float Yaw;
|
||||
float Pitch;
|
||||
// Camera options
|
||||
GLfloat MovementSpeed;
|
||||
GLfloat MouseSensitivity;
|
||||
GLfloat Zoom;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Zoom;
|
||||
|
||||
// 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), GLfloat yaw = YAW, GLfloat pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), 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(SENSITIVTY), Zoom(ZOOM)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = up;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->updateCameraVectors();
|
||||
Position = position;
|
||||
WorldUp = up;
|
||||
Yaw = yaw;
|
||||
Pitch = pitch;
|
||||
updateCameraVectors();
|
||||
}
|
||||
// Constructor with scalar values
|
||||
Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), 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(SENSITIVTY), Zoom(ZOOM)
|
||||
{
|
||||
this->Position = glm::vec3(posX, posY, posZ);
|
||||
this->WorldUp = glm::vec3(upX, upY, upZ);
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->updateCameraVectors();
|
||||
Position = glm::vec3(posX, posY, posZ);
|
||||
WorldUp = glm::vec3(upX, upY, upZ);
|
||||
Yaw = yaw;
|
||||
Pitch = pitch;
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
// Returns the view matrix calculated using Eular Angles and the LookAt Matrix
|
||||
glm::mat4 GetViewMatrix()
|
||||
{
|
||||
return glm::lookAt(this->Position, this->Position + this->Front, this->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)
|
||||
void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime)
|
||||
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
|
||||
{
|
||||
GLfloat velocity = this->MovementSpeed * deltaTime;
|
||||
float velocity = MovementSpeed * deltaTime;
|
||||
if (direction == FORWARD)
|
||||
this->Position += this->Front * velocity;
|
||||
Position += Front * velocity;
|
||||
if (direction == BACKWARD)
|
||||
this->Position -= this->Front * velocity;
|
||||
Position -= Front * velocity;
|
||||
if (direction == LEFT)
|
||||
this->Position -= this->Right * velocity;
|
||||
Position -= Right * velocity;
|
||||
if (direction == RIGHT)
|
||||
this->Position += this->Right * velocity;
|
||||
Position += Right * velocity;
|
||||
}
|
||||
|
||||
// Processes input received from a mouse input system. Expects the offset value in both the x and y direction.
|
||||
void ProcessMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch = true)
|
||||
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
|
||||
{
|
||||
xoffset *= this->MouseSensitivity;
|
||||
yoffset *= this->MouseSensitivity;
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
this->Yaw += xoffset;
|
||||
this->Pitch += yoffset;
|
||||
Yaw += xoffset;
|
||||
Pitch += yoffset;
|
||||
|
||||
// Make sure that when pitch is out of bounds, screen doesn't get flipped
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (this->Pitch > 89.0f)
|
||||
this->Pitch = 89.0f;
|
||||
if (this->Pitch < -89.0f)
|
||||
this->Pitch = -89.0f;
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
// Update Front, Right and Up Vectors using the updated Eular angles
|
||||
this->updateCameraVectors();
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
|
||||
void ProcessMouseScroll(GLfloat yoffset)
|
||||
void ProcessMouseScroll(float yoffset)
|
||||
{
|
||||
if (this->Zoom >= 1.0f && this->Zoom <= 45.0f)
|
||||
this->Zoom -= yoffset;
|
||||
if (this->Zoom <= 1.0f)
|
||||
this->Zoom = 1.0f;
|
||||
if (this->Zoom >= 45.0f)
|
||||
this->Zoom = 45.0f;
|
||||
if (Zoom >= 1.0f && Zoom <= 45.0f)
|
||||
Zoom -= yoffset;
|
||||
if (Zoom <= 1.0f)
|
||||
Zoom = 1.0f;
|
||||
if (Zoom >= 45.0f)
|
||||
Zoom = 45.0f;
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -119,13 +119,13 @@ private:
|
||||
{
|
||||
// Calculate the new Front vector
|
||||
glm::vec3 front;
|
||||
front.x = cos(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch));
|
||||
front.y = sin(glm::radians(this->Pitch));
|
||||
front.z = sin(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch));
|
||||
this->Front = glm::normalize(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
|
||||
this->Right = glm::normalize(glm::cross(this->Front, this->WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
|
||||
this->Up = glm::normalize(glm::cross(this->Right, this->Front));
|
||||
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));
|
||||
}
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user