【问题标题】:WPF 3D. Strange behavoir with converting 3D coordinates to 2D coordinatesWPF 3D。将 3D 坐标转换为 2D 坐标的奇怪行为
【发布时间】:2014-07-24 09:19:24
【问题描述】:

由于某种原因,从 3D 到 2D 的对话因我的相机的某些旋转状态而失败。

我编写了一个小示例应用程序来演示我的问题。当您按下示例应用程序左下角的小按钮“旋转相机”时,会发生错误的计算。在这个旋转之后,看起来如果其中一个线框(红线)以某种方式被镜像。

这是我的例子。

Xaml:

<Window x:Class="Projection2D3D.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800">
<Grid>
    <Viewport3D x:Name="Viewport">
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="Cam" Position="-.9,-.9,-.5" LookDirection="0,0,1" UpDirection="0,-1,0"
                    FieldOfView="90" />
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight />
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D x:Name="ModelVisual3D">
            <ModelVisual3D.Content>
                <GeometryModel3D>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D
                                Positions="-1,-1,0 1,-1,0 -1,1,0 1,1,0"
                                TriangleIndices="0 1 2  1 3 2" />
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.BackMaterial>
                        <DiffuseMaterial Brush="Blue"/>
                    </GeometryModel3D.BackMaterial>
                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
    <Canvas x:Name="CnvOverlay"></Canvas>
    <Button VerticalAlignment="Bottom" HorizontalAlignment="Left" Content="Rotate Camera" Margin="10" Click="BtnRotate_OnClick"/>
    <TextBlock x:Name="TextBlock" Text="Wireframe correct!" FontWeight="Bold" FontSize="20" IsHitTestVisible="False"/>
</Grid>

C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        CompositionTarget.Rendering +=CompositionTarget_Rendering;
    }

    void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        UpdateWireframe();
    }

    void UpdateWireframe()
    {
        GeometryModel3D model = ModelVisual3D.Content as GeometryModel3D;

        CnvOverlay.Children.Clear();

        if (model != null)
        {
            GeneralTransform3DTo2D transform = ModelVisual3D.TransformToAncestor(Viewport);
            MeshGeometry3D geometry = model.Geometry as MeshGeometry3D;

            for (int i = 0; i < geometry.TriangleIndices.Count; )
            {
                Polygon p = new Polygon();
                p.Stroke = Brushes.Red;
                p.StrokeThickness = 1;
                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++]]));
                CnvOverlay.Children.Add(p);
            }
        }
    }

    private void BtnRotate_OnClick(object sender, RoutedEventArgs e)
    {
        (sender as UIElement).IsEnabled = false;
        this.TextBlock.Text = "Wireframe incorrect!";

        var rotation = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 40), new Point3D(-1, -1, 0));
        UpdateCamera(rotation);
    }

    private void UpdateCamera(Transform3D transform3D)
    {
        Cam.Position = transform3D.Transform(Cam.Position);
        Cam.UpDirection = transform3D.Transform(Cam.UpDirection);
        Cam.LookDirection = transform3D.Transform(Cam.LookDirection);
    }
}

希望有人可以帮助我。提前谢谢你。

【问题讨论】:

    标签: c# wpf 3d transformation projection


    【解决方案1】:

    http://www.neoaxis.com/forum/viewtopic.php?f=2&t=6692 。这里也讨论了这个问题。

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 2015-06-29
      • 2014-06-12
      • 2015-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2011-09-02
      相关资源
      最近更新 更多