在WPF中旋转3D立方体

时间:2012-09-29 17:50:03

标签: wpf xaml 3d

我使用XAML代码在WPF中创建一个3D立方体,如下所示:

<Viewport3D Name="viewport3D1">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4">
                </PerspectiveCamera>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1">
                    </DirectionalLight>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="meshMain"
                                Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1  1 1 1"
                                TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                            </MeshGeometry3D>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial x:Name="matDiffuseMain">
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>

然后是我窗口的构造函数,我想在轴OX,OY,OZ周围应用旋转,我认为应该这样做:

RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 2, 0), 1));
        meshMain.Transform=myRotateTransform;
        // etc...

似乎我没有将变换应用到XAML的正确节点,在我的情况下实现变换的正确方法是什么?

1 个答案:

答案 0 :(得分:7)

您需要为ModelVisual3D提供一个名称,MeshGeometry3D没有像模型那样的Transform属性。您还需要有权访问AxisAngleRotation3D对象才能设置Angle属性。

<ModelVisual3D x:Name="MyModel">
....

修改为CodeBehind方法添加了更多代码

public partial class MainWindow : Window
{
    AxisAngleRotation3D ax3d;
    public MainWindow()
    {
        InitializeComponent();

        ax3d = new AxisAngleRotation3D(new Vector3D(0, 2, 0), 1);
        RotateTransform3D myRotateTransform = new RotateTransform3D(ax3d);
        MyModel.Transform = myRotateTransform;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        ax3d.Angle += 1 ;
    }
}

虽然在这种情况下我认为你最好在Xaml中实现你的变换。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Viewport3D Name="viewport3D1">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4">
                </PerspectiveCamera>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1">
                    </DirectionalLight>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="MyModel">
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="meshMain" 
                                Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1  1 1 1" 
                                TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                            </MeshGeometry3D>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial x:Name="matDiffuseMain">
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
                <ModelVisual3D.Transform>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D x:Name="rotate" Axis="0 2 0"/>
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
        </Viewport3D>
        <Slider Height="23" HorizontalAlignment="Left" 
                Margin="12,12,0,0" Name="slider1"
                VerticalAlignment="Top" Width="187" 
                Maximum="360"
                Value="{Binding ElementName=rotate, Path=Angle}" />

    </Grid>
</Window>

同样通过此示例,您可以通过设置其Angle属性来更改CodeBehind中的AxisAngleRotation3D:

rotate.Angle +=1;
相关问题