From 500ad2e711fcdae1a1f8cc504f119aded87024e6 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 6 May 2026 08:57:31 -0700 Subject: [PATCH] fix(camera): NaN/range guards on Camera setters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit setPosition/setRotation/setAspectRatio/setFov now reject: - NaN/inf inputs (would produce NaN view/proj matrix → frozen GPU on some drivers, garbage frustum culling everywhere) - aspectRatio <= 0 (degenerate perspective) - fov <= 0 or >= 180 (degenerate perspective) Camera is constructed and set from many code paths; pushing the guards into the setters means none of them need to remember. --- include/rendering/camera.hpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/include/rendering/camera.hpp b/include/rendering/camera.hpp index ed4732f2..47897ba2 100644 --- a/include/rendering/camera.hpp +++ b/include/rendering/camera.hpp @@ -2,6 +2,7 @@ #include #include +#include namespace wowee { namespace rendering { @@ -15,10 +16,26 @@ class Camera { public: Camera(); - void setPosition(const glm::vec3& pos) { position = pos; updateViewMatrix(); } - void setRotation(float yaw, float pitch) { this->yaw = yaw; this->pitch = pitch; updateViewMatrix(); } - void setAspectRatio(float aspect) { aspectRatio = aspect; updateProjectionMatrix(); } - void setFov(float fov) { this->fov = fov; updateProjectionMatrix(); } + void setPosition(const glm::vec3& pos) { + // Reject NaN/inf — would produce a NaN view matrix and freeze the + // GPU in some drivers, or produce garbage frustum culling. + if (!std::isfinite(pos.x) || !std::isfinite(pos.y) || !std::isfinite(pos.z)) return; + position = pos; updateViewMatrix(); + } + void setRotation(float yaw, float pitch) { + if (!std::isfinite(yaw) || !std::isfinite(pitch)) return; + this->yaw = yaw; this->pitch = pitch; updateViewMatrix(); + } + void setAspectRatio(float aspect) { + // glm::perspective with aspect <= 0 produces NaN in the projection. + if (!std::isfinite(aspect) || aspect <= 0.0f) return; + aspectRatio = aspect; updateProjectionMatrix(); + } + void setFov(float fov) { + // glm::perspective(0) is degenerate; >180 wraps the trig. + if (!std::isfinite(fov) || fov <= 0.0f || fov >= 180.0f) return; + this->fov = fov; updateProjectionMatrix(); + } const glm::vec3& getPosition() const { return position; } const glm::mat4& getViewMatrix() const { return viewMatrix; }