Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Point Point3DToScreen2D(Point3D point3D,Viewport3D viewPort )
- {
- double screenX = 0d, screenY = 0d;
- // Camera is defined in XAML as:
- // <Viewport3D.Camera>
- // <PerspectiveCamera Position="0,0,800" LookDirection="0,0,-1" />
- // </Viewport3D.Camera>
- PerspectiveCamera cam = viewPort.Camera as PerspectiveCamera;
- // Translate input point using camera position
- double inputX = point3D.X - cam.Position.X;
- double inputY = point3D.Y - cam.Position.Y;
- double inputZ = point3D.Z - cam.Position.Z;
- double aspectRatio = viewPort.ActualWidth / viewPort.ActualHeight;
- // Apply projection to X and Y
- screenX = inputX / (-inputZ * Math.Tan(cam.FieldOfView / 2));
- screenY = (inputY * aspectRatio) / (-inputZ * Math.Tan(cam.FieldOfView / 2));
- // Convert to screen coordinates
- screenX = screenX * viewPort.ActualWidth;
- screenY = screenY * viewPort.ActualHeight;
- // Additional, currently unused, projection scaling factors
- /*
- double xScale = 1 / Math.Tan(Math.PI * cam.FieldOfView / 360);
- double yScale = aspectRatio * xScale;
- double zFar = cam.FarPlaneDistance;
- double zNear = cam.NearPlaneDistance;
- double zScale = zFar == Double.PositiveInfinity ? -1 : zFar / (zNear - zFar);
- double zOffset = zNear * zScale;
- */
- return new Point(screenX, screenY);
- }
- screenY = viewPort.ActualHeight * (1 - screenY);
- screenY = screenY * viewPort.ActualHeight;
- screenX = viewPort.ActualWidth * (screenX + 1.0) / 2.0
- screenY = viewPort.ActualHeight * (1.0 - ((screenY + 1.0) / 2.0))
- /// <summary>
- /// Takes a 3D point and returns the corresponding 2D point (X,Y) within the viewport.
- /// Requires the 3DUtils project available at http://www.codeplex.com/Wiki/View.aspx?ProjectName=3DTools
- /// </summary>
- /// <param name="point3D">A point in 3D space</param>
- /// <param name="viewPort">An instance of Viewport3D</param>
- /// <returns>The corresponding 2D point or null if it could not be calculated</returns>
- public Point? Point3DToScreen2D(Point3D point3D, Viewport3D viewPort)
- {
- bool bOK = false;
- // We need a Viewport3DVisual but we only have a Viewport3D.
- Viewport3DVisual vpv =VisualTreeHelper.GetParent(viewPort.Children[0]) as Viewport3DVisual;
- // Get the world to viewport transform matrix
- Matrix3D m = MathUtils.TryWorldToViewportTransform(vpv, out bOK);
- if (bOK)
- {
- // Transform the 3D point to 2D
- Point3D transformedPoint = m.Transform(point3D);
- Point screen2DPoint = new Point(transformedPoint.X, transformedPoint.Y);
- return new Nullable<Point>(screen2DPoint);
- }
- else
- {
- return null;
- }
- }
- y
- |
- |
- +------x
- +-------x
- |
- |
- |
- y
- void CompositionTarget_Rendering(object sender, EventArgs e)
- {
- UpdateWireframe();
- }
- void UpdateWireframe()
- {
- GeometryModel3D model = cube.Content as GeometryModel3D;
- canvas.Children.Clear();
- if (model != null)
- {
- GeneralTransform3DTo2D transform = cube.TransformToAncestor(viewport);
- MeshGeometry3D geometry = model.Geometry as MeshGeometry3D;
- for (int i = 0; i < geometry.TriangleIndices.Count;)
- {
- Polygon p = new Polygon();
- p.Stroke = Brushes.Blue;
- p.StrokeThickness = 0.25;
- p.Points.Add(transform.Transform(geometry.Positions[geometry.TriangleIndices[i++]]));
- p.Points.Add(transform.Transform(geometry.Positions[geometry.TriangleIndices[i++]]));
- p.Points.Add(transform.Transform(geometry.Positions[geometry.TriangleIndices[i++]]));
- canvas.Children.Add(p);
- }
- }
- }
Add Comment
Please, Sign In to add comment