04/src/Camera.cpp

142 lines
3.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Camera.h"
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/ext/matrix_transform.hpp>
// Защищенный (protected) конструктор камеры без перспективы
Camera::Camera(const glm::vec3 &pos, const glm::vec2 &xyOffset)
: position(pos), currentRotation(xyOffset)
{
sensitivity = 0.05;
recalcTarget();
}
// Конструктор камеры с проекцией перспективы
Camera::Camera(float aspect, const glm::vec3 &position, const glm::vec2 &xyOffset, float fovy)
: Camera(position, xyOffset)
{
setPerspective(fovy, aspect);
}
// Конструктор ортографической камеры
Camera::Camera(float width, float height, const glm::vec3 &position, const glm::vec2 &xyOffset)
: Camera(position, xyOffset)
{
setOrtho(width, height);
}
// Деструктор
Camera::~Camera() { }
// Пересчет цели, на которую смотрит камера
void Camera::recalcTarget()
{
if(currentRotation.y > 89.0f)
currentRotation.y = 89.0f;
if(currentRotation.y < -89.0f)
currentRotation.y = -89.0f;
target.x = cos(glm::radians(currentRotation.x)) * cos(glm::radians(currentRotation.y));
target.y = sin(glm::radians(currentRotation.y));
target.z = sin(glm::radians(currentRotation.x)) * cos(glm::radians(currentRotation.y));
requiredRecalcView = true;
requiredRecalcVP = true;
}
// Пересчет матрицы вида
void Camera::recalcView()
{
view = glm::lookAt(position, position + target, CAMERA_UP_VECTOR);
requiredRecalcView = false;
}
// Пересчет произведения матриц
void Camera::recalcVP()
{
vp = projection * view;
requiredRecalcVP = false;
}
// Возвращает ссылку на константную матрицу проекции
const glm::mat4& Camera::getProjection()
{
return projection;
}
// Возвращает ссылку на константную матрицу вида
const glm::mat4& Camera::getView()
{
if (requiredRecalcView)
recalcView();
return view;
}
// Возвращает ссылку на константную матрицу вида
const glm::mat4& Camera::getVP()
{
if (requiredRecalcVP)
{
if (requiredRecalcView)
recalcView();
recalcVP();
}
return vp;
}
// Поворачивает камеру на dx и dy пикселей
void Camera::rotate(const glm::vec2 &xyOffset)
{
currentRotation += xyOffset * sensitivity;
recalcTarget();
requiredRecalcView = true;
requiredRecalcVP = true;
}
// Сдвигает камеру на указанный вектор (dx,dy,dz)
void Camera::move(const glm::vec3 &posOffset)
{
position += posOffset;
requiredRecalcView = true;
requiredRecalcVP = true;
}
// Устанавливает местоположение
void Camera::setPosition(const glm::vec3 &pos)
{
position = pos;
requiredRecalcView = true;
requiredRecalcVP = true;
}
// Устанавливает угол поворота камеры
void Camera::setRotation(const glm::vec2 &xyOffset)
{
currentRotation = xyOffset;
recalcTarget();
}
// Устанавливает заданную матрицу перспективы
void Camera::setPerspective(float fovy, float aspect)
{
projection = glm::perspective(glm::radians(fovy), aspect, CAMERA_NEAR, CAMERA_FAR);
requiredRecalcVP = true;
}
// Устанавливает заданную ортографическую матрицу
void Camera::setOrtho(float width, float height)
{
const float aspect = width / height;
projection = glm::ortho(-1.0f, 1.0f, -1.0f/aspect, 1.0f/aspect, CAMERA_NEAR, CAMERA_FAR);
requiredRecalcVP = true;
}
// Изменяет чувствительность мыши
void Camera::setSensitivity(float sens)
{
sensitivity = sens;
}