/// <summary> /// Computes the effective projection matrix for the given /// camera. /// </summary> public static Matrix3D GetProjectionMatrix(Camera camera, double aspectRatio) { if (camera == null) { throw new ArgumentNullException("camera"); } PerspectiveCamera perspectiveCamera = camera as PerspectiveCamera; if (perspectiveCamera != null) { return GetProjectionMatrix(perspectiveCamera, aspectRatio); } OrthographicCamera orthographicCamera = camera as OrthographicCamera; if (orthographicCamera != null) { return GetProjectionMatrix(orthographicCamera, aspectRatio); } MatrixCamera matrixCamera = camera as MatrixCamera; if (matrixCamera != null) { return matrixCamera.ProjectionMatrix; } throw new ArgumentException(String.Format("Unsupported camera type '{0}'.", camera.GetType().FullName), "camera"); }
/// <summary> /// Exports the camera. /// </summary> /// <param name="camera"> /// The camera. /// </param> protected override void ExportCamera(Camera camera) { base.ExportCamera(camera); // todo... // http://www.povray.org/documentation/view/3.6.1/17/ }
public CameraController(Camera c, IListLayout l, PointLight p) { camera = (ProjectionCamera)c; layout = l; light = p; try { Transform3DGroup group = camera.Transform as Transform3DGroup; RotateTransform3D rot = group.Children[0] as RotateTransform3D; cameraRotX = rot.Rotation as AxisAngleRotation3D; rot = group.Children[1] as RotateTransform3D; cameraRotY = rot.Rotation as AxisAngleRotation3D; rot = group.Children[2] as RotateTransform3D; cameraRotZ = rot.Rotation as AxisAngleRotation3D; } catch (Exception ) { System.Diagnostics.Debug.WriteLine("camera transformations are wrong!!!"); } try { Transform3DGroup group = light.Transform as Transform3DGroup; RotateTransform3D rot = group.Children[0] as RotateTransform3D; lightRotX = rot.Rotation as AxisAngleRotation3D; rot = group.Children[1] as RotateTransform3D; lightRotY = rot.Rotation as AxisAngleRotation3D; rot = group.Children[2] as RotateTransform3D; lightRotZ = rot.Rotation as AxisAngleRotation3D; } catch (Exception ) { System.Diagnostics.Debug.WriteLine("light transformations are wrong!!!"); } }
public override void OnMouse3DUp(object sender, RoutedEventArgs e) { base.OnMouse3DUp(sender, e); if (this.isCaptured) { this.isCaptured = false; this.camera = null; this.viewport = null; } }
/// <summary> /// Resets the specified camera. /// </summary> /// <param name="camera">The camera.</param> public static void Reset(Camera camera) { var pcamera = camera as PerspectiveCamera; if (pcamera != null) Reset(pcamera); var ocamera = camera as OrthographicCamera; if (ocamera != null) Reset(ocamera); }
public static void LookAt(Camera camera, Point3D worldPos) { ProjectionCamera c = (camera as ProjectionCamera); if (c == null) throw new ArgumentException("Only projection camera's can be set to look at."); Vector3D direction = (worldPos - c.Position); direction.Normalize(); c.LookDirection = direction; }
public override void OnMouse3DDown(object sender, RoutedEventArgs e) { base.OnMouse3DDown(sender, e); var args = e as Mouse3DEventArgs; if (args == null) return; if (args.Viewport == null) return; this.isCaptured = true; this.viewport = args.Viewport; this.camera = args.Viewport.Camera; this.lastHitPos = args.HitTestResult.PointHit; }
/// <summary> /// Computes the effective view matrix for the given /// camera. /// </summary> public static Matrix3D GetViewMatrix(Camera camera) { if (camera == null) throw new ArgumentNullException("camera"); ProjectionCamera projectionCamera = camera as ProjectionCamera; if (projectionCamera != null) { return GetViewMatrix(projectionCamera); } MatrixCamera matrixCamera = camera as MatrixCamera; if (matrixCamera != null) { return matrixCamera.ViewMatrix; } throw new ArgumentException(String.Format("Unsupported camera type '{0}'.", camera.GetType().FullName), "camera"); }
/// <summary> /// Gets an information string about the specified camera. /// </summary> /// <param name="camera"> /// The camera. /// </param> /// <returns> /// The get info. /// </returns> public static string GetInfo(Camera camera) { var matrixCamera = camera as MatrixCamera; var perspectiveCamera = camera as PerspectiveCamera; var projectionCamera = camera as ProjectionCamera; var orthographicCamera = camera as OrthographicCamera; var sb = new StringBuilder(); sb.AppendLine(camera.GetType().Name); if (projectionCamera != null) { sb.AppendLine( string.Format( CultureInfo.InvariantCulture, "LookDirection:\t{0:0.000},{1:0.000},{2:0.000}", projectionCamera.LookDirection.X, projectionCamera.LookDirection.Y, projectionCamera.LookDirection.Z)); sb.AppendLine( string.Format( CultureInfo.InvariantCulture, "UpDirection:\t{0:0.000},{1:0.000},{2:0.000}", projectionCamera.UpDirection.X, projectionCamera.UpDirection.Y, projectionCamera.UpDirection.Z)); sb.AppendLine( string.Format( CultureInfo.InvariantCulture, "Position:\t\t{0:0.000},{1:0.000},{2:0.000}", projectionCamera.Position.X, projectionCamera.Position.Y, projectionCamera.Position.Z)); var target = projectionCamera.Position + projectionCamera.LookDirection; sb.AppendLine( string.Format( CultureInfo.InvariantCulture, "Target:\t\t{0:0.000},{1:0.000},{2:0.000}", target.X, target.Y, target.Z)); sb.AppendLine( string.Format( CultureInfo.InvariantCulture, "NearPlaneDist:\t{0}", projectionCamera.NearPlaneDistance)); sb.AppendLine( string.Format(CultureInfo.InvariantCulture, "FarPlaneDist:\t{0}", projectionCamera.FarPlaneDistance)); } if (perspectiveCamera != null) { sb.AppendLine( string.Format(CultureInfo.InvariantCulture, "FieldOfView:\t{0:0.#}°", perspectiveCamera.FieldOfView)); } if (orthographicCamera != null) { sb.AppendLine( string.Format(CultureInfo.InvariantCulture, "Width:\t{0:0.###}", orthographicCamera.Width)); } if (matrixCamera != null) { sb.AppendLine("ProjectionMatrix:"); sb.AppendLine(matrixCamera.ProjectionMatrix.ToString(CultureInfo.InvariantCulture)); sb.AppendLine("ViewMatrix:"); sb.AppendLine(matrixCamera.ViewMatrix.ToString(CultureInfo.InvariantCulture)); } return sb.ToString().Trim(); }
protected BaseViewModel() { //// set lighting model //ShadingModelCollection = new List<string>() //{ // Effects.RenderPhong, // Effects.RenderBlinnPhong, // Effects.RenderColors, // Effects.RenderWires, // Effects.RenderDeferred, //}; // camera models CameraModelCollection = new List<string>() { ProjectionCamera.Orthographic, ProjectionCamera.Perspective, }; // on camera changed callback this.CameraModelChanged += (s, e) => { if (this.cameraModel == PerspectiveCamera.Orthographic) { //if (this.Camera != null) //{ // var newCamera = new OrthographicCamera(); // this.Camera.CopyTo(newCamera); // newCamera.NearPlaneDistance = znear; // newCamera.FarPlaneDistance = zfar; // this.Camera = newCamera; //} //else { this.Camera = this.defaultOrthographicCamera; } } else if (this.cameraModel == PerspectiveCamera.Perspective) { //if (this.Camera != null) //{ // var newCamera = new PerspectiveCamera(); // this.Camera.CopyTo(newCamera); // newCamera.NearPlaneDistance = znear; // newCamera.FarPlaneDistance = zfar; // this.Camera = newCamera; //} //else { this.Camera = this.defaultPerspectiveCamera; } } else { throw new HelixToolkitException("Camera Model Error."); } }; // default camera model this.CameraModel = ProjectionCamera.Perspective; this.Title = "Demo (HelixToolkitDX)"; this.SubTitle = "Default Base View Model"; this.RenderTechnique = Techniques.RenderPhong; }
/// <summary> /// Exports the camera. /// </summary> /// <param name="c"> /// The c. /// </param> protected override void ExportCamera(Camera c) { var pc = c as PerspectiveCamera; if (pc == null) { throw new InvalidOperationException("Only perspective cameras are supported."); } const string name = "Camera #1"; this.WriteStartObject("./Cameras/" + name, "Pinhole Camera", name, "Camera"); // FOV = 2 arctan (x / (2 f)), x is diagonal, f is focal length // f = x / 2 / Tan(FOV/2) // http://en.wikipedia.org/wiki/Angle_of_view // http://kmp.bdimitrov.de/technology/fov.html // PerspectiveCamera.FieldOfView: Horizontal field of view // Must multiply by ratio of Viewport Width/Height double ratio = this.Width / (double)this.Height; const double x = 40; double f = 0.5 * ratio * x / Math.Tan(0.5 * pc.FieldOfView / 180.0 * Math.PI); WriteParameter("Focal Length (mm)", f); WriteParameter("Film Height (mm)", x); this.WriteParameter( "Resolution", string.Format(CultureInfo.InvariantCulture, "{0}x{1}", this.Width, this.Height)); var t = CreateTransform(pc.Position, pc.LookDirection, pc.UpDirection); this.WriteTransform("Frame", t); this.WriteParameter("Focus Distance", this.FocusDistance); this.WriteParameter("f-number", this.Aperture); this.WriteParameter("Lens Samples", this.LensSamples); this.WriteParameter("Blades", 6); this.WriteParameter("Diaphragm", "Circular"); this.WriteParameter("Projection", "Planar"); this.WriteEndObject(); }
public static Matrix3D GetInverseTransform(Camera cam, double aspectRatio) { Matrix3D matx = GetTotalTransform(cam, aspectRatio); if (matx == ZeroMatrix) { ; } else if (!matx.HasInverse) { matx = ZeroMatrix; } else { matx.Invert(); } return matx; }
public static Matrix3D GetTotalTransform(Camera cam, double aspectRatio) { Matrix3D matx = Matrix3D.Identity; if (cam == null) { matx = ZeroMatrix; } else { if (cam.Transform != null) { Matrix3D matxCameraTransform = cam.Transform.Value; if (!matxCameraTransform.HasInverse) { matx = ZeroMatrix; } else { matxCameraTransform.Invert(); matx.Append(matxCameraTransform); } } matx.Append(CameraInfo.GetViewMatrix(cam)); matx.Append(CameraInfo.GetProjectionMatrix(cam, aspectRatio)); } return matx; }
public static Matrix3D GetProjectionMatrix(Camera cam, double aspectRatio) { Matrix3D matx = Matrix3D.Identity; if (cam == null) { matx = ZeroMatrix; } else if (cam is MatrixCamera) { matx = (cam as MatrixCamera).ProjectionMatrix; } else if (cam is OrthographicCamera) { OrthographicCamera orthocam = cam as OrthographicCamera; double xScale = 2 / orthocam.Width; double yScale = xScale * aspectRatio; double zNear = orthocam.NearPlaneDistance; double zFar = orthocam.FarPlaneDistance; // Hey, check this out! if (Double.IsPositiveInfinity(zFar)) zFar = 1E10; matx = new Matrix3D(xScale, 0, 0, 0, 0, yScale, 0, 0, 0, 0, 1 / (zNear - zFar), 0, 0, 0, zNear / (zNear - zFar), 1); } else if (cam is PerspectiveCamera) { PerspectiveCamera perscam = cam as PerspectiveCamera; // The angle-to-radian formula is a little off because only // half the angle enters the calculation. double xScale = 1 / Math.Tan(Math.PI * perscam.FieldOfView / 360); double yScale = xScale * aspectRatio; double zNear = perscam.NearPlaneDistance; double zFar = perscam.FarPlaneDistance; double zScale = (zFar == double.PositiveInfinity ? -1 : (zFar / (zNear - zFar))); double zOffset = zNear * zScale; matx = new Matrix3D(xScale, 0, 0, 0, 0, yScale, 0, 0, 0, 0, zScale, -1, 0, 0, zOffset, 0); } else if (cam != null) { throw new ApplicationException("ProjectionMatrix"); } return matx; }
public SelectedParts(Viewport3D viewport, Camera camera, EditorOptions options) { _viewport = viewport; _camera = camera; _options = options; }
public static Point? From3Dto2D(Visual3D space, Visual surface, Camera perspectiveCamera, Point3D target) { PerspectiveCamera cmr = perspectiveCamera as PerspectiveCamera; Point? rlt = new Point(); Vector3D look = cmr.Transform.Transform(cmr.LookDirection); Vector3D d = target - cmr.Transform.Transform(cmr.Position); double angle = AngleBetween(d, look); GeneralTransform3DTo2D gt = space.TransformToAncestor(surface); rlt = gt.Transform(target); if (Math.Abs(angle) < cmr.FieldOfView / 2) { return rlt; } return null; }
public static void LookAt(Camera camera, Visual3D visual) { Matrix3D visualToWorld = MathUtils.GetTransformToWorld(visual); LookAt(camera, visualToWorld.Transform(new Point3D())); }
/// <summary> /// Exports the camera. /// </summary> /// <param name="camera"> /// The camera. /// </param> protected virtual void ExportCamera(Camera camera) { }
/// <summary> /// Exports the camera. /// </summary> /// <param name="camera"> /// The camera. /// </param> protected override void ExportCamera(Camera camera) { base.ExportCamera(camera); // todo... }
/// <summary> /// Obtains the view transform matrix for a camera. /// </summary> /// <param name="camera"> /// Camera to obtain the /// </param> /// <returns> /// A Matrix3D objecvt with the camera view transform matrix, /// or a Matrix3D with all zeros if the "camera" is null. /// </returns> /// <exception cref="TK"> /// if the 'camera' is neither of type MatrixCamera nor /// ProjectionCamera. /// </exception> public static Matrix3D GetViewMatrix(Camera camera) { Matrix3D matx = Matrix3D.Identity; if (camera == null) { matx = ZeroMatrix; } else if (camera is MatrixCamera) { matx = (camera as MatrixCamera).ViewMatrix; } else if (camera is ProjectionCamera) { ProjectionCamera projcam = camera as ProjectionCamera; Vector3D zAxis = -projcam.LookDirection; zAxis.Normalize(); Vector3D xAxis = Vector3D.CrossProduct(projcam.UpDirection, zAxis); xAxis.Normalize(); Vector3D yAxis = Vector3D.CrossProduct(zAxis, xAxis); Vector3D pos = (Vector3D)projcam.Position; matx = new Matrix3D(xAxis.X, yAxis.X, zAxis.X, 0, xAxis.Y, yAxis.Y, zAxis.Y, 0, xAxis.Z, yAxis.Z, zAxis.Z, 0, -Vector3D.DotProduct(xAxis, pos), -Vector3D.DotProduct(yAxis, pos), -Vector3D.DotProduct(zAxis, pos), 1); } else if (camera != null) { throw new ApplicationException("ViewMatrix"); } return matx; }
public TrackballTransform(Camera camera) { _transform = new Transform3DGroup(); _transform.Children.Add(_scale); _transform.Children.Add(new RotateTransform3D(_rotation)); _transform.Children.Add(_translate); _camera = camera; }
internal static Matrix3D GetWorldToViewportTransform3D(Camera camera, Rect viewport) { Debug.Assert(camera != null, "Caller is responsible for ensuring camera is not null."); return camera.GetViewMatrix() * camera.GetProjectionMatrix(M3DUtil.GetAspectRatio(viewport.Size)) * M3DUtil.GetHomogeneousToViewportTransform3D(viewport); }
/// <summary> /// Converts the 2D point into 3D world point and ray /// </summary> /// <remarks> /// This method uses reflection to get at an internal method off of camera (I think it's rigged up for a perspective camera) /// http://grokys.blogspot.com/2010/08/wpf-3d-translating-2d-point-into-3d.html /// </remarks> public static RayHitTestParameters RayFromViewportPoint(Camera camera, Viewport3D viewport, Point point) { Size viewportSize = new Size(viewport.ActualWidth, viewport.ActualHeight); System.Reflection.MethodInfo method = typeof(Camera).GetMethod("RayFromViewportPoint", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); double distanceAdjustment = 0; object[] parameters = new object[] { point, viewportSize, null, distanceAdjustment }; return (RayHitTestParameters)method.Invoke(camera, parameters); }