2#include <glm/gtc/matrix_transform.hpp>
3#include <glm/gtc/constants.hpp>
15 if (aspect_ratio <= 0.0f) {
16 throw std::invalid_argument(
17 "aspect_ratio must be > 0 for perspective projection");
19 if (near_plane <= 0.0f) {
20 throw std::invalid_argument(
21 "near_plane must be > 0 for perspective projection");
23 if (far_plane <= near_plane) {
24 throw std::invalid_argument(
25 "far_plane must be > near_plane for perspective projection");
27 return glm::perspective(glm::radians(
fov_degrees), aspect_ratio, near_plane, far_plane);
33 float half_w = half_h * aspect_ratio;
34 return glm::ortho(-half_w, half_w, -half_h, half_h, near_plane, far_plane);
39 const Vec3 &direction,
const Vec3 &up,
40 float fov_degrees,
float margin,
44 cam.
up = glm::normalize(up);
47 float fov_rad = glm::radians(fov_degrees);
50 dist = radius * margin;
52 dist = radius / std::sin(fov_rad * 0.5f) * margin;
54 cam.
eye = center + glm::normalize(direction) * dist;
59 const Vec3 &up,
float fov_degrees,
float margin,
61 Vec3 bmin(0.0f), bmax(0.0f);
63 Vec3 center = (bmin + bmax) * 0.5f;
64 Vec3 dir = glm::normalize(direction);
67 return camera_look_at(center, extent, direction, up, fov_degrees, margin, projection);
70 glm::radians(fov_degrees));
71 return camera_look_at(center, radius, direction, up, fov_degrees, margin, projection);
76 const Vec3 &up,
float fov_degrees,
float margin,
78 Vec3 bmin(0.0f), bmax(0.0f);
80 Vec3 center = (bmin + bmax) * 0.5f;
81 Vec3 dir = glm::normalize(direction);
84 return camera_look_at(center, extent, direction, up, fov_degrees, margin, projection);
87 glm::radians(fov_degrees));
88 return camera_look_at(center, radius, direction, up, fov_degrees, margin, projection);
94 float angle_rad = glm::radians(angle_degrees);
95 Mat4 rotation = glm::rotate(
Mat4(1.0f), angle_rad, axis);
98 Vec4 rotated_up = rotation *
Vec4(camera.
up, 0.0f);
99 result.
up = glm::normalize(
Vec3(rotated_up));
Camera definition and helper functions for view setup.
Camera camera_look_at(const Vec3 ¢er, float radius, const Vec3 &direction, const Vec3 &up, float fov_degrees, float margin, ProjectionType projection)
Create a camera that looks at a point from a given distance and direction.
glm::mat4 Mat4
4Ć4 floating-point matrix.
glm::vec3 Vec3
3-component floating-point vector (xyz).
Camera camera_fit_scene(const Scene &scene, const Vec3 &direction, const Vec3 &up, float fov_degrees, float margin, ProjectionType projection)
Create a camera that automatically frames an entire Scene.
Camera camera_fit_mesh(const Mesh &mesh, const Vec3 &direction, const Vec3 &up, float fov_degrees, float margin, ProjectionType projection)
Create a camera that automatically frames a single Mesh.
ProjectionType
The type of 3Dā2D projection used by the camera.
@ ORTHOGRAPHIC
Orthographic projection: parallel lines stay parallel.
@ PERSPECTIVE
Perspective projection: objects farther away appear smaller.
float perp_extent_radius(const Vec3 &bmin, const Vec3 &bmax, const Vec3 ¢er, const Vec3 &dir, float fov_radians, float *out_dist=nullptr)
Compute the "perpendicular extent radius" of an axis-aligned bounding box relative to a view directio...
float max_ortho_extent(const Vec3 &bmin, const Vec3 &bmax, const Vec3 ¢er, const Vec3 &dir)
Compute the maximum perpendicular extent of an AABB from a view ray (for orthographic projection fram...
Camera camera_orbit(const Camera &camera, const Vec3 &axis, float angle_degrees)
Orbit the camera around its look-at point.
glm::vec4 Vec4
4-component floating-point vector (xyzw).
A virtual camera that defines the viewpoint for rendering.
Mat4 get_view_matrix() const
Compute the view matrix (world ā camera space).
ProjectionType projection
Which projection type to use.
Vec3 center
The point the camera looks at.
Vec3 up
The camera's "up" direction.
Vec3 eye
Camera position in world space.
float fov_degrees
Vertical field of view in degrees (perspective only).
Mat4 get_projection_matrix(float aspect_ratio, float near_plane, float far_plane) const
Compute the projection matrix (camera ā clip space).
A 3D triangle mesh using an indexed face set representation.
void compute_bounding_box(Vec3 &min_bound, Vec3 &max_bound) const
Compute the axis-aligned bounding box (AABB) of the mesh.
A collection of Mesh objects to be rendered together.
void compute_bounding_box(Vec3 &min_bound, Vec3 &max_bound) const
Compute the combined axis-aligned bounding box of all meshes, after applying each mesh's placement tr...