cpp-xnoecraft2/src/camera.cpp
2022-08-06 09:47:08 +01:00

81 lines
2.1 KiB
C++

#include "camera.h"
Camera::Camera() {
this->width = Window::width;
this->height = Window::height;
this->field_of_view = glm::radians(80.0f);
this->projection_matrix = glm::perspective(
this->field_of_view,
(float)this->width / (float)this->height,
0.1f,
1000.0f
);
Window::current_window->register_resize_callback((void(*)(void*, int, int))&Camera::update_projection_matrix, this);
}
void Camera::update_projection_matrix(int width, int height) {
this->width = width;
this->height = height;
this->projection_matrix = glm::perspective(
this->field_of_view,
(float)this->width / (float)this->height,
0.1f,
1000.0f
);
}
glm::mat4 Camera::get_projection_matrix() {
return this->projection_matrix;
}
glm::mat4 Camera::get_view_matrix() {
glm::mat4 r1Matrix = glm::rotate(-this->rotation.x, glm::vec3(1, 0, 0));
glm::mat4 r2Matrix = glm::rotate(-this->rotation.y, glm::vec3(0, 1, 0));
glm::mat4 r3Matrix = glm::rotate(-this->rotation.z, glm::vec3(0, 0, 1));
glm::mat4 tMatrix = glm::translate(glm::mat4(1.0f), -this->position);
return r1Matrix * r2Matrix * r3Matrix * tMatrix;
}
glm::vec3 Camera::get_position() {
return this->position;
}
void Camera::set_position(glm::vec3 position) {
this->position = position;
}
void Camera::change_position_by(glm::vec3 position) {
this->position += position;
}
glm::vec3 Camera::get_rotation() {
return this->rotation;
}
void Camera::set_rotation(glm::vec3 rotation) {
this->rotation = rotation;
}
void Camera::change_rotation_by(glm::vec3 rotation) {
this->rotation += rotation;
}
void Camera::move_by(glm::vec3 position) {
glm::vec3 forward(
glm::cos(-this->rotation.x) * glm::sin(this->rotation.y),
glm::sin(-this->rotation.x),
glm::cos(-this->rotation.x) * glm::cos(this->rotation.y)
);
glm::vec3 right(
glm::sin(this->rotation.y + 1.57f),
0,
glm::cos(this->rotation.y + 1.57f)
);
glm::vec3 up = -glm::cross(right, forward);
this->position += right * position.x;
this->position += up * position.y;
this->position += forward * position.z;
}