Projecting a 3D point to a 2D screen coordinate

后端 未结 6 541
难免孤独
难免孤独 2020-12-08 18:06

Based on information in Chapter 7 of 3D Programming For Windows (Charles Petzold), I\'ve attempted to write as helper function that projects a Point3D to a standard 2D Point

6条回答
  •  自闭症患者
    2020-12-08 18:24

    This doesn't address the algoritm in question but it may be useful for peple coming across this question (as I did).

    In .NET 3.5 you can use Visual3D.TransformToAncestor(Visual ancestor). I use this to draw a wireframe on a canvas over my 3D viewport:

        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);
                }
            }
        }
    

    This also takes into account any transforms on the model etc.

    See also: http://blogs.msdn.com/wpf3d/archive/2009/05/13/transforming-bounds.aspx

提交回复
热议问题