wpf 创建 3d 立方体并沿 x、y 和 z 轴旋转它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15640619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf create 3d cube and rotate it along x, y and z axis
提问by Tono Nam
I have the following code:
我有以下代码:
<Window x:Class="Demo3D.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="1 0 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>
I am able to rotate the cube by sliding the slider. The problem is that the cube does not rotate around it's center. How can I make that cube rotate around it's center? In other words it will be nice if I could do something like http://www.youtube.com/watch?feature=player_embedded&v=a7mTytwRGqI
我可以通过滑动滑块来旋转立方体。问题是立方体不会围绕它的中心旋转。我怎样才能让那个立方体围绕它的中心旋转?换句话说,如果我能做一些类似http://www.youtube.com/watch?feature=player_embedded&v=a7mTytwRGqI 的事情就好了
回答by David
Change
改变
<RotateTransform3D>
to
到
<RotateTransform3D CenterX="0.5" CenterY="0.5" CenterZ="0.5" >
Results:


结果:


回答by Nico Schertler
Rotations are evaluated in respect to the origin. So the easiest way is to define the geometry that way that its origin matches the global origin:
相对于原点评估旋转。所以最简单的方法是定义几何,使其原点与全局原点匹配:
<MeshGeometry3D x:Name="meshMain"
Positions="-0.5 -0.5 -0.5 0.5 -0.5 -0.5 -0.5 0.5 -0.5 0.5 0.5 -0.5 -0.5 -0.5 0.5 0.5 -0.5 0.5 -0.5 0.5 0.5 0.5 0.5 0.5"
...
Another way is being aware of the offset. You can compensate for this offset in your transformation:
另一种方法是了解偏移量。您可以在转换中补偿此偏移:
<ModelVisual3D.Transform>
<Transform3DGroup>
<Transform3DGroup.Children>
<!-- move the model to the origin -->
<TranslateTransform3D OffsetX="-0.5" OffsetY="-0.5" OffsetZ="-0.5" />
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D x:Name="rotate" Axis="1 0 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<!-- undo translation -->
<TranslateTransform3D OffsetX="0.5" OffsetY="0.5" OffsetZ="0.5" />
</Transform3DGroup.Children>
</Transform3DGroup>
</ModelVisual3D.Transform>

