1use crate::{
10 Error, Result,
11 core::transform::Transform,
12 vlm::{Mat4, Vec3},
13};
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum CameraProjection {
18 Perspective,
19 Orthographic,
20}
21
22#[derive(Clone, Debug, PartialEq)]
31pub struct Camera {
32 transform: Transform,
33 projection: CameraProjection,
34 fov_y: f32,
35 aspect: f32,
36 near: f32,
37 far: f32,
38 ortho_width: f32,
39 ortho_height: f32,
40 zoom: f32,
41 offset_x: f32,
42 offset_y: f32,
43 orbit_target: Vec3,
45}
46
47impl Default for Camera {
48 fn default() -> Self {
50 Self::new_perspective(
51 Vec3 {
52 x: 0.0,
53 y: 0.0,
54 z: 5.0,
55 },
56 Vec3 {
57 x: 0.0,
58 y: 0.0,
59 z: 0.0,
60 },
61 60.0,
62 1280.0 / 720.0,
63 0.1,
64 10000.0,
65 )
66 }
67}
68
69impl Camera {
70 pub fn new_perspective(
75 position: Vec3,
76 target: Vec3,
77 fov_y: f32,
78 aspect: f32,
79 near: f32,
80 far: f32,
81 ) -> Self {
82 let mut transform = Transform::default();
83 transform.set_position(position);
84 transform.look_at(
85 target,
86 Vec3 {
87 x: 0.0,
88 y: 1.0,
89 z: 0.0,
90 },
91 );
92 Self {
93 transform,
94 projection: CameraProjection::Perspective,
95 fov_y,
96 aspect,
97 near,
98 far,
99 ortho_width: 1.0,
100 ortho_height: 1.0,
101 zoom: 1.0,
102 offset_x: 0.0,
103 offset_y: 0.0,
104 orbit_target: Vec3 {
105 x: 0.0,
106 y: 0.0,
107 z: 0.0,
108 },
109 }
110 }
111
112 pub fn new_orthographic(width: f32, height: f32, near: f32, far: f32) -> Self {
114 let position = Vec3 {
115 x: 0.0,
116 y: 0.0,
117 z: 1.0,
118 };
119 let target = Vec3 {
120 x: 0.0,
121 y: 0.0,
122 z: 0.0,
123 };
124 let mut transform = Transform::default();
125 transform.set_position(position);
126 transform.look_at(
127 target,
128 Vec3 {
129 x: 0.0,
130 y: 1.0,
131 z: 0.0,
132 },
133 );
134 Self {
135 transform,
136 projection: CameraProjection::Orthographic,
137 fov_y: 60.0,
138 aspect: if height != 0.0 { width / height } else { 1.0 },
139 near,
140 far,
141 ortho_width: width,
142 ortho_height: height,
143 zoom: 1.0,
144 offset_x: 0.0,
145 offset_y: 0.0,
146 orbit_target: Vec3 {
147 x: 0.0,
148 y: 0.0,
149 z: 0.0,
150 },
151 }
152 }
153
154 pub const fn transform(&self) -> &Transform {
158 &self.transform
159 }
160
161 pub fn transform_mut(&mut self) -> &mut Transform {
163 &mut self.transform
164 }
165
166 pub fn position(&self) -> Vec3 {
168 self.transform.position()
169 }
170
171 pub fn set_position(&mut self, position: Vec3) {
173 self.transform.set_position(position);
174 }
175
176 pub fn look_at(&mut self, target: Vec3) {
178 self.transform.look_at(
179 target,
180 Vec3 {
181 x: 0.0,
182 y: 1.0,
183 z: 0.0,
184 },
185 );
186 }
187
188 pub const fn projection(&self) -> CameraProjection {
192 self.projection
193 }
194
195 pub const fn fov_y(&self) -> f32 {
197 self.fov_y
198 }
199
200 pub fn effective_fov_y(&self) -> f32 {
204 (self.fov_y / self.zoom.max(f32::EPSILON)).clamp(f32::EPSILON, 179.999)
205 }
206
207 pub const fn aspect(&self) -> f32 {
209 self.aspect
210 }
211
212 pub const fn near(&self) -> f32 {
214 self.near
215 }
216
217 pub const fn far(&self) -> f32 {
219 self.far
220 }
221
222 pub fn set_perspective(&mut self, fov_y: f32, aspect: f32, near: f32, far: f32) {
224 self.projection = CameraProjection::Perspective;
225 self.fov_y = fov_y;
226 self.aspect = aspect;
227 self.near = near;
228 self.far = far;
229 }
230
231 pub fn set_orthographic(&mut self, width: f32, height: f32, near: f32, far: f32) {
233 self.projection = CameraProjection::Orthographic;
234 self.ortho_width = width;
235 self.ortho_height = height;
236 self.near = near;
237 self.far = far;
238 }
239
240 pub fn set_aspect(&mut self, aspect: f32) {
242 self.aspect = aspect;
243 }
244
245 pub fn fit_to_window(&mut self, window_width: f32, window_height: f32) {
247 if window_height > 0.0 {
248 self.aspect = window_width / window_height;
249 }
250 if matches!(self.projection, CameraProjection::Orthographic) {
251 self.ortho_width = window_width;
252 self.ortho_height = window_height;
253 }
254 }
255
256 pub fn set_zoom(&mut self, zoom: f32) {
258 self.zoom = zoom.max(f32::EPSILON);
259 }
260
261 pub const fn zoom(&self) -> f32 {
263 self.zoom
264 }
265
266 pub fn get_view_matrix(&self) -> Mat4 {
273 let eye = self.transform.position();
274 let fwd = self.transform.forward();
275 let up = Vec3 {
276 x: 0.0,
277 y: 1.0,
278 z: 0.0,
279 };
280 let center = Vec3 {
281 x: eye.x + fwd.x,
282 y: eye.y + fwd.y,
283 z: eye.z + fwd.z,
284 };
285 look_at(eye, center, up)
286 }
287
288 pub fn get_projection_matrix(&self) -> Mat4 {
293 match self.projection {
294 CameraProjection::Perspective => {
295 perspective_vlm(self.effective_fov_y(), self.aspect, self.near, self.far)
296 }
297 CameraProjection::Orthographic => {
298 let hw = (self.ortho_width * 0.5) / self.zoom.max(f32::EPSILON);
299 let hh = (self.ortho_height * 0.5) / self.zoom.max(f32::EPSILON);
300 ortho_vlm(
301 -hw + self.offset_x,
302 hw + self.offset_x,
303 -hh + self.offset_y,
304 hh + self.offset_y,
305 self.near,
306 self.far,
307 )
308 }
309 }
310 }
311
312 pub fn get_view_projection_matrix(&self) -> Mat4 {
314 self.get_view_matrix() * self.get_projection_matrix()
316 }
317
318 pub fn set_orbit_target(&mut self, target: Vec3) {
322 self.orbit_target = target;
323 }
324
325 pub fn set_orbit_distance(&mut self, distance: f32) {
327 let pos = self.transform.position();
328 let dir = pos - self.orbit_target;
329 let len = dir.length();
330 if len > f32::EPSILON {
331 let normalized = Vec3 {
332 x: dir.x / len,
333 y: dir.y / len,
334 z: dir.z / len,
335 };
336 let new_pos = self.orbit_target + normalized * distance;
337 self.transform.set_position(new_pos);
338 self.transform.look_at(
339 self.orbit_target,
340 Vec3 {
341 x: 0.0,
342 y: 1.0,
343 z: 0.0,
344 },
345 );
346 }
347 }
348
349 pub fn orbit_yaw_pitch(&mut self, yaw_delta: f32, pitch_delta: f32) {
351 let offset = self.transform.position() - self.orbit_target;
352 let offset = rotate_y(offset, yaw_delta.to_radians());
353 let right = Vec3 {
354 x: 0.0,
355 y: 1.0,
356 z: 0.0,
357 }
358 .cross(offset)
359 .try_normalized()
360 .unwrap_or(Vec3 {
361 x: 1.0,
362 y: 0.0,
363 z: 0.0,
364 });
365 let offset = rotate_axis(offset, right, pitch_delta.to_radians());
366 let new_pos = self.orbit_target + offset;
367 self.transform.set_position(new_pos);
368 self.transform.look_at(
369 self.orbit_target,
370 Vec3 {
371 x: 0.0,
372 y: 1.0,
373 z: 0.0,
374 },
375 );
376 }
377
378 pub fn orbit_set_yaw_pitch(&mut self, yaw: f32, pitch: f32) {
380 let radius = (self.transform.position() - self.orbit_target).length();
381 let (sy, cy) = yaw.to_radians().sin_cos();
382 let (sp, cp) = pitch.to_radians().sin_cos();
383 let new_pos = Vec3 {
384 x: self.orbit_target.x + radius * cy * cp,
385 y: self.orbit_target.y + radius * sp,
386 z: self.orbit_target.z + radius * sy * cp,
387 };
388 self.transform.set_position(new_pos);
389 self.transform.look_at(
390 self.orbit_target,
391 Vec3 {
392 x: 0.0,
393 y: 1.0,
394 z: 0.0,
395 },
396 );
397 }
398
399 pub fn validate(&self) -> Result<()> {
406 if !self.fov_y.is_finite()
407 || !self.aspect.is_finite()
408 || !self.near.is_finite()
409 || !self.far.is_finite()
410 || self.near <= 0.0
411 || self.far <= 0.0
412 || self.near >= self.far
413 || self.aspect <= 0.0
414 {
415 return Err(Error::invalid_argument(
416 "render::Camera requires finite positive near/far with near < far and a positive aspect ratio",
417 ));
418 }
419 let vp = self.get_view_projection_matrix();
420 if !vp.is_finite() {
421 return Err(Error::invalid_argument(
422 "render::Camera view-projection matrix must be finite",
423 ));
424 }
425 Ok(())
426 }
427}
428
429fn look_at(eye: Vec3, center: Vec3, up: Vec3) -> Mat4 {
433 let f = (center - eye).try_normalized().unwrap_or(Vec3 {
434 x: 0.0,
435 y: 0.0,
436 z: -1.0,
437 });
438 let r = f.cross(up).try_normalized().unwrap_or(Vec3 {
439 x: 1.0,
440 y: 0.0,
441 z: 0.0,
442 });
443 let u = r.cross(f);
444 Mat4 {
446 m: [
447 [r.x, u.x, -f.x, 0.0],
448 [r.y, u.y, -f.y, 0.0],
449 [r.z, u.z, -f.z, 0.0],
450 [-eye.dot(r), -eye.dot(u), eye.dot(f), 1.0],
451 ],
452 }
453}
454
455fn perspective_vlm(fov_y_deg: f32, aspect: f32, near: f32, far: f32) -> Mat4 {
457 let half = (fov_y_deg * 0.5).to_radians().tan();
458 let sy = 1.0 / half;
459 let sx = sy / aspect;
460 let range = far / (near - far);
461 Mat4 {
462 m: [
463 [sx, 0.0, 0.0, 0.0],
464 [0.0, -sy, 0.0, 0.0],
465 [0.0, 0.0, range, -1.0],
466 [0.0, 0.0, near * range, 0.0],
467 ],
468 }
469}
470
471#[allow(clippy::too_many_arguments)]
473fn ortho_vlm(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4 {
474 let rml = right - left;
475 let tmb = top - bottom;
476 let fmn = far - near;
477 Mat4 {
478 m: [
479 [2.0 / rml, 0.0, 0.0, 0.0],
480 [0.0, -2.0 / tmb, 0.0, 0.0],
481 [0.0, 0.0, -1.0 / fmn, 0.0],
482 [
483 -(right + left) / rml,
484 (top + bottom) / tmb,
485 -near / fmn,
486 1.0,
487 ],
488 ],
489 }
490}
491
492fn rotate_y(v: Vec3, radians: f32) -> Vec3 {
494 let (s, c) = radians.sin_cos();
495 Vec3 {
496 x: v.x * c + v.z * s,
497 y: v.y,
498 z: -v.x * s + v.z * c,
499 }
500}
501
502fn rotate_axis(v: Vec3, axis: Vec3, radians: f32) -> Vec3 {
504 let (s, c) = radians.sin_cos();
505 let k = axis;
506 v * c + k.cross(v) * s + k * k.dot(v) * (1.0 - c)
507}