From 2452cb0ade94c9543a6b37b88608dfe1e9072562 Mon Sep 17 00:00:00 2001 From: fred-si Date: Sun, 23 Jun 2024 16:49:58 +0200 Subject: [PATCH] Fix TrackViewer crash on big zoom value The method Matrix.CreatePerspectiveFieldOfView throw an ArgumentException when value passed for the nearPlaneDistance parameter is less or equals to zero. With a really big zoom value, width become 0.0 and result of pass zero for value of nearPlaneDistance parameter, that cause unhandled exception. --- Source/Contrib/TrackViewer/Drawing/DrawTerrain.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Source/Contrib/TrackViewer/Drawing/DrawTerrain.cs b/Source/Contrib/TrackViewer/Drawing/DrawTerrain.cs index 78b828b2d..3ad003bfa 100644 --- a/Source/Contrib/TrackViewer/Drawing/DrawTerrain.cs +++ b/Source/Contrib/TrackViewer/Drawing/DrawTerrain.cs @@ -486,8 +486,14 @@ void UpdateCamera(DrawArea drawArea) float camHeight = width / device.Viewport.AspectRatio / 2; Vector3 cameraPosition = cameraTarget; cameraPosition.Y = -camHeight; + float nearPlaneDistance = camHeight / 2; + if (nearPlaneDistance <= 0) + { + // distance is too close for CreatePerspectiveFieldOfView + return; + } basicEffect.View = Matrix.CreateLookAt(cameraPosition, cameraTarget, new Vector3(0, 0, 1)); - basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, device.Viewport.AspectRatio, camHeight / 2, camHeight * 2); + basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, device.Viewport.AspectRatio, nearPlaneDistance, camHeight * 2); } #endregion