scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
camera.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <scimesh/types.h>
12#include <scimesh/scene.h>
13
14namespace scimesh {
15
16// ---------------------------------------------------------------------------
17// ProjectionType
18// ---------------------------------------------------------------------------
19
23enum class ProjectionType {
30
36};
37
38// ---------------------------------------------------------------------------
39// Camera
40// ---------------------------------------------------------------------------
41
77struct Camera {
82 Vec3 eye = Vec3(0.0f, 0.0f, 10.0f);
83
88 Vec3 center = Vec3(0.0f, 0.0f, 0.0f);
89
94 Vec3 up = Vec3(0.0f, 1.0f, 0.0f);
95
100
109 float fov_degrees = 45.0f;
110
117 Mat4 get_view_matrix() const;
118
125 Mat4 get_projection_matrix(float aspect_ratio, float near_plane, float far_plane) const;
126};
127
128// ---------------------------------------------------------------------------
129// Camera helper functions
130// ---------------------------------------------------------------------------
131
149inline float perp_extent_radius(const Vec3 &bmin, const Vec3 &bmax,
150 const Vec3 &center, const Vec3 &dir,
151 float fov_radians, float *out_dist = nullptr) {
152 Vec3 corners[8] = {
153 Vec3(bmin.x, bmin.y, bmin.z), Vec3(bmax.x, bmin.y, bmin.z),
154 Vec3(bmin.x, bmax.y, bmin.z), Vec3(bmax.x, bmax.y, bmin.z),
155 Vec3(bmin.x, bmin.y, bmax.z), Vec3(bmax.x, bmin.y, bmax.z),
156 Vec3(bmin.x, bmax.y, bmax.z), Vec3(bmax.x, bmax.y, bmax.z),
157 };
158 float max_dist = 0.0f;
159 float half_tan = std::tan(fov_radians * 0.5f);
160 if (half_tan < 1e-6f) half_tan = 1e-6f;
161
162 for (int i = 0; i < 8; i++) {
163 Vec3 delta = corners[i] - center;
164 float along = glm::dot(delta, dir);
165 float perp_sq = glm::dot(delta, delta) - along * along;
166 float perp = std::sqrt(perp_sq);
167 // At the corner's depth (centre_z + along), required eye distance
168 // so that this corner stays inside the frustum:
169 // perp ≤ (dist - along) * tan(half_fov)
170 // ⇒ dist ≥ perp / tan(half_fov) + along
171 float d = perp / half_tan + along;
172 if (d > max_dist) max_dist = d;
173 }
174 if (max_dist < 1e-6f) max_dist = 1.0f;
175
176 if (out_dist) *out_dist = max_dist;
177 // Convert back to the "sphere radius" that camera_look_at expects.
178 float sin_half = std::sin(fov_radians * 0.5f);
179 if (sin_half < 1e-6f) sin_half = 1e-6f;
180 return max_dist * sin_half;
181}
182
196inline float max_ortho_extent(const Vec3 &bmin, const Vec3 &bmax,
197 const Vec3 &center, const Vec3 &dir) {
198 Vec3 corners[8] = {
199 Vec3(bmin.x, bmin.y, bmin.z), Vec3(bmax.x, bmin.y, bmin.z),
200 Vec3(bmin.x, bmax.y, bmin.z), Vec3(bmax.x, bmax.y, bmin.z),
201 Vec3(bmin.x, bmin.y, bmax.z), Vec3(bmax.x, bmin.y, bmax.z),
202 Vec3(bmin.x, bmax.y, bmax.z), Vec3(bmax.x, bmax.y, bmax.z),
203 };
204 float max_perp = 0.0f;
205 for (int i = 0; i < 8; i++) {
206 Vec3 delta = corners[i] - center;
207 float along = glm::dot(delta, dir);
208 float perp = std::sqrt(std::max(0.0f, glm::dot(delta, delta) - along * along));
209 if (perp > max_perp) max_perp = perp;
210 }
211 if (max_perp < 1e-6f) max_perp = 1.0f;
212 return max_perp;
213}
214
238Camera camera_look_at(const Vec3 &center, float radius,
239 const Vec3 &direction, const Vec3 &up,
240 float fov_degrees, float margin = 1.1f,
242
266Camera camera_fit_scene(const Scene &scene, const Vec3 &direction,
267 const Vec3 &up, float fov_degrees, float margin = 1.1f,
269
290Camera camera_fit_mesh(const Mesh &mesh, const Vec3 &direction,
291 const Vec3 &up, float fov_degrees, float margin = 1.1f,
293
312Camera camera_orbit(const Camera &camera, const Vec3 &axis, float angle_degrees);
313
314} // namespace scimesh
Camera camera_look_at(const Vec3 &center, 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.
Definition camera.cpp:38
glm::mat4 Mat4
4×4 floating-point matrix.
Definition types.h:65
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
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.
Definition camera.cpp:58
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.
Definition camera.cpp:75
ProjectionType
The type of 3D→2D projection used by the camera.
Definition camera.h:23
@ 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 &center, 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...
Definition camera.h:149
float max_ortho_extent(const Vec3 &bmin, const Vec3 &bmax, const Vec3 &center, const Vec3 &dir)
Compute the maximum perpendicular extent of an AABB from a view ray (for orthographic projection fram...
Definition camera.h:196
Camera camera_orbit(const Camera &camera, const Vec3 &axis, float angle_degrees)
Orbit the camera around its look-at point.
Definition camera.cpp:92
The Scene — a collection of meshes rendered together.
A virtual camera that defines the viewpoint for rendering.
Definition camera.h:77
Mat4 get_view_matrix() const
Compute the view matrix (world → camera space).
Definition camera.cpp:9
ProjectionType projection
Which projection type to use.
Definition camera.h:99
Vec3 center
The point the camera looks at.
Definition camera.h:88
Vec3 up
The camera's "up" direction.
Definition camera.h:94
Vec3 eye
Camera position in world space.
Definition camera.h:82
float fov_degrees
Vertical field of view in degrees (perspective only).
Definition camera.h:109
Mat4 get_projection_matrix(float aspect_ratio, float near_plane, float far_plane) const
Compute the projection matrix (camera → clip space).
Definition camera.cpp:13
Fundamental types used throughout the scimesh rendering engine.