private static Ray3D RayFromPerspectiveCameraPoint(PerspectiveCamera camera, Point point, Size viewSize) { Point3D position = camera.Position; Vector3D lookDirection = camera.LookDirection; Vector3D upDirection = camera.UpDirection; Transform3D transform = camera.Transform; double nearPlaneDistance = camera.NearPlaneDistance; double farPlaneDistance = camera.FarPlaneDistance; double num1 = camera.FieldOfView * (Math.PI / 180.0); Point normalizedPoint = CameraRayHelpers.GetNormalizedPoint(point, viewSize); double aspectRatio = CameraRayHelpers.GetAspectRatio(viewSize); double num2 = Math.Tan(num1 / 2.0); double num3 = aspectRatio / num2; double num4 = 1.0 / num2; Vector3D vector = new Vector3D(normalizedPoint.X / num4, normalizedPoint.Y / num3, -1.0); Matrix3D viewMatrix = CameraRayHelpers.CreateViewMatrix((Transform3D)null, ref position, ref lookDirection, ref upDirection); viewMatrix.Invert(); Vector3D vector3D = viewMatrix.Transform(vector); Point3D point3D = position + nearPlaneDistance * vector3D; vector3D.Normalize(); if (transform != null && transform != Transform3D.Identity) { point3D = transform.Transform(point3D); vector3D = transform.Transform(vector3D); } return(new Ray3D(point3D, vector3D)); }
private static Ray3D RayFromOrthographicCameraPoint(OrthographicCamera camera, Point point, Size viewSize) { Point3D position = camera.Position; Vector3D lookDirection = camera.LookDirection; Vector3D upDirection = camera.UpDirection; Transform3D transform = camera.Transform; double nearPlaneDistance = camera.NearPlaneDistance; double farPlaneDistance = camera.FarPlaneDistance; double width = camera.Width; Point normalizedPoint = CameraRayHelpers.GetNormalizedPoint(point, viewSize); double aspectRatio = CameraRayHelpers.GetAspectRatio(viewSize); double num1 = width; double num2 = num1 / aspectRatio; Point3D point1 = new Point3D(normalizedPoint.X * (num1 / 2.0), normalizedPoint.Y * (num2 / 2.0), -nearPlaneDistance); Vector3D vector = new Vector3D(0.0, 0.0, -1.0); Matrix3D viewMatrix = CameraRayHelpers.CreateViewMatrix((Transform3D)null, ref position, ref lookDirection, ref upDirection); viewMatrix.Invert(); Point3D point3D = viewMatrix.Transform(point1); Vector3D vector3D = viewMatrix.Transform(vector); if (transform != null && transform != Transform3D.Identity) { point3D = transform.Transform(point3D); vector3D = transform.Transform(vector3D); } return(new Ray3D(point3D, vector3D)); }
private static Ray3D RayFromMatrixCameraPoint(MatrixCamera camera, Point point, Size viewSize) { Point normalizedPoint = CameraRayHelpers.GetNormalizedPoint(point, viewSize); Matrix3D matrix3D = camera.ViewMatrix * camera.ProjectionMatrix; if (!matrix3D.HasInverse) { throw new NotSupportedException(ExceptionStringTable.NeedToHandleSingularMatrixCameras); } matrix3D.Invert(); Point4D point4D = new Point4D(normalizedPoint.X, normalizedPoint.Y, 0.0, 1.0) * matrix3D; Point3D origin = new Point3D(point4D.X / point4D.W, point4D.Y / point4D.W, point4D.Z / point4D.W); Vector3D direction = new Vector3D(matrix3D.M31 - matrix3D.M34 * origin.X, matrix3D.M32 - matrix3D.M34 * origin.Y, matrix3D.M33 - matrix3D.M34 * origin.Z); direction.Normalize(); if (point4D.W < 0.0) { direction = -direction; } return(new Ray3D(origin, direction)); }