#include "camera.h" #include #include #include #include Camera::Camera(GlobalPosition3D pos, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar) : position {pos}, forward {::north}, up {::up}, near {zNear}, far {zFar}, projection { glm::perspective(fov, aspect, static_cast(zNear), static_cast(zFar))}, viewProjection {}, inverseViewProjection {} { updateView(); } Ray Camera::unProject(const ScreenRelCoord & mouse) const { static constexpr const glm::vec4 screen {0, 0, 1, 1}; const auto mouseProjection = glm::lookAt(::origin, forward, up); return {position, glm::normalize(glm::unProject(mouse ^ 1, mouseProjection, projection, screen))}; } void Camera::updateView() { viewProjection = projection * glm::lookAt(origin, forward, up); inverseViewProjection = glm::inverse(viewProjection); } Direction3D Camera::upFromForward(const Direction3D & forward) { const auto right = glm::cross(forward, ::down); return glm::cross(forward, right); } std::array Camera::extentsAtDist(const GlobalDistance dist) const { const auto clampToSeaFloor = [this, dist](GlobalPosition3D target) -> GlobalPosition4D { target += position; if (target.z < -1500) { const auto diff = (target - position); const auto something = (-1500 - position.z) / diff.z; return {position + diff * something, dist}; } return {target, dist}; }; const auto depth = -(2.F * (static_cast(dist - near)) * static_cast(far)) / (static_cast(dist) * (static_cast(near - far))) - 1.F; static constexpr const std::array extents {-1.F, 1.F}; static constexpr const auto cartesianExtents = extents * extents; return cartesianExtents * [&depth, this, &clampToSeaFloor](const auto & extent) { const glm::vec4 in {extent.first, extent.second, depth, 1.F}; return clampToSeaFloor(perspective_divide(inverseViewProjection * in)); }; }