"""Shared camera defaults used by Qt, GLUT, and backend projection helpers."""
from __future__ import annotations
from dataclasses import dataclass, field, replace
from pyglm import glm
@dataclass(frozen=True)
[docs]
class ProjectionConfig:
"""Perspective projection parameters used by PicoGL camera helpers."""
[docs]
def with_aspect(self, aspect: float) -> ProjectionConfig:
"""Return a copy with ``aspect`` set."""
return replace(self, aspect=float(aspect))
[docs]
def with_size(self, width: float, height: float) -> ProjectionConfig:
"""Return a copy with aspect from ``width / max(height, 1)``."""
return self.with_aspect(float(width) / float(max(height, 1)))
[docs]
def matrix(self, aspect: float | None = None) -> glm.mat4:
"""Build a perspective projection matrix.
If *aspect* is given, use it without mutating this config.
"""
cfg = self if aspect is None else self.with_aspect(aspect)
return glm.perspective(
glm.radians(cfg.fovy),
float(cfg.aspect),
float(cfg.near),
float(cfg.far),
)
[docs]
FOVY = ProjectionConfig().fovy
@dataclass
[docs]
class CameraParameters:
"""Look-at parameters that build a view matrix.
Example::
camera = CameraParameters(eye=glm.vec3(4, 3, zoom_distance))
view = camera.view_matrix()
"""
[docs]
center: glm.vec3 = field(default_factory=lambda: glm.vec3(0, 0, 0))
[docs]
up: glm.vec3 = field(default_factory=lambda: glm.vec3(0, 1, 0))
[docs]
def view_matrix(self) -> glm.mat4:
"""Build the view matrix for this look-at triple."""
return glm.lookAt(self.eye, self.center, self.up)
@classmethod
[docs]
def tutorial(
cls, distance: float = 3.0, *, z_sign: float = 1.0
) -> CameraParameters:
"""OpenGL-tutorial style eye at ``(4, 3, z_sign * distance)``."""
return cls(eye=glm.vec3(4, 3, z_sign * distance))
@dataclass
[docs]
class CameraConfig:
"""Bundled camera pose plus projection settings."""
[docs]
projection: ProjectionConfig = field(default_factory=ProjectionConfig)
[docs]
def camera_parameters(self) -> CameraParameters:
"""Look-at triple for this camera (ignores projection)."""
return CameraParameters(eye=self.position, center=self.target, up=self.up)
[docs]
DEFAULT_CAMERA_CONFIG = CameraConfig(
position=glm.vec3(4, 3, 3),
target=glm.vec3(0, 0, 0),
up=glm.vec3(0, 1, 0),
projection=ProjectionConfig(),
)