3#include <glm/gtc/matrix_transform.hpp>
27static_assert(
sizeof(ViewTransform) == 128,
28 "ViewTransform must be exactly 128 bytes (Vulkan minimum push constant size)");
39 const glm::vec3& target,
40 const glm::vec3& up = glm::vec3(0.0F, 1.0F, 0.0F))
42 return { .
view = glm::lookAt(eye, target, up) };
59 return { .
projection = glm::perspective(fov_radians, aspect, near, far) };
73 float left,
float right,
74 float bottom,
float top,
75 float near = -1.0F,
float far = 1.0F)
77 return { .
projection = glm::ortho(left, right, bottom, top, near, far) };
93 const glm::vec3& target,
98 const glm::vec3& up = glm::vec3(0.0F, 1.0F, 0.0F))
101 .
view = glm::lookAt(eye, target, up),
102 .projection = glm::perspective(fov_radians, aspect, near, far)
115 double window_x,
double window_y,
116 uint32_t
width, uint32_t height)
119 (
static_cast<float>(window_x) /
static_cast<float>(
width)) * 2.0F - 1.0F,
120 1.0F - (
static_cast<float>(window_y) /
static_cast<float>(height)) * 2.0F,
132 double window_x,
double window_y,
133 uint32_t
width, uint32_t height)
135 const float aspect =
static_cast<float>(
width) /
static_cast<float>(height);
136 float x = (
static_cast<float>(window_x) /
static_cast<float>(
width)) * 2.0F - 1.0F;
137 float y = 1.0F - (
static_cast<float>(window_y) /
static_cast<float>(height)) * 2.0F;
138 if (aspect >= 1.0F) {
143 return { x, y, 0.0F };
154 const glm::vec3& ndc,
155 uint32_t
width, uint32_t height)
158 (ndc.x + 1.0F) * 0.5F *
static_cast<float>(
width),
159 (1.0F - ndc.y) * 0.5F *
static_cast<float>(height)
168 return static_cast<float>(
width) /
static_cast<float>(height);
175 double window_x,
double window_y,
176 uint32_t
width, uint32_t height)
178 return window_x >= 0.0
179 && window_x < static_cast<double>(
width)
181 && window_y < static_cast<double>(height);
ViewTransform look_at(const glm::vec3 &eye, const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.0F, 1.0F, 0.0F))
Construct view matrix from eye position, target, and up vector.
glm::vec2 to_window(const glm::vec3 &ndc, uint32_t width, uint32_t height)
Convert NDC to window pixel coordinates.
ViewTransform look_at_perspective(const glm::vec3 &eye, const glm::vec3 &target, float fov_radians, float aspect, float near, float far, const glm::vec3 &up=glm::vec3(0.0F, 1.0F, 0.0F))
Construct complete ViewTransform from look-at and perspective parameters.
float aspect_ratio(uint32_t width, uint32_t height)
Aspect ratio from pixel dimensions.
ViewTransform ortho(float left, float right, float bottom, float top, float near=-1.0F, float far=1.0F)
Construct orthographic projection matrix.
bool in_bounds(double window_x, double window_y, uint32_t width, uint32_t height)
Check if a window-space point is within bounds.
glm::vec3 to_ndc(double window_x, double window_y, uint32_t width, uint32_t height)
Convert window pixel coordinates to NDC.
glm::vec3 to_ndc_aspect(double window_x, double window_y, uint32_t width, uint32_t height)
Convert window pixel coordinates to NDC, corrected for aspect ratio.
ViewTransform perspective(float fov_radians, float aspect, float near, float far)
Construct perspective projection matrix.