简体   繁体   中英

WPF 3D. Strange behavoir with converting 3D coordinates to 2D coordinates

For some reason the conversation from 3D to 2D fails with some rotation states of my camera.

I wrote a small example application to demonstrate my problem. The wrong calculation occurs when you press the small button "Rotate Camera" in the bottom left corner of the example app. After this rotation it looks like if one of the wireframes (red lines) is somehow mirrored.

Here my example.

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

Hope someone can help me with this. Thank you in advance.

http://www.neoaxis.com/forum/viewtopic.php?f=2&t=6692 . The problem is also disccussed here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM