2013-03-26 109 views
2

我有以下代碼:WPF創建3D立方體,並沿X,Y和Z軸旋轉它

<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> 

我能夠通過滑動滑塊來旋轉立方體。問題是立方體不會圍繞它的中心旋轉。如何讓該多維數據集圍繞它的中心旋轉?換句話說,如果我可以做類似http://www.youtube.com/watch?feature=player_embedded&v=a7mTytwRGqI

回答

2

旋轉會根據原點進行評估。所以,最簡單的方法是定義方式,它的起源全局原點相匹配的幾何形狀:

<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" 
    ... 

另一種方式是意識到的偏移量。您可以彌補這一點在你的變身偏移:

<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> 
5

變化

<RotateTransform3D> 

<RotateTransform3D CenterX="0.5" CenterY="0.5" CenterZ="0.5" > 

結果: enter image description hereenter image description here

+0

+1,這將是比使用更方便額外的轉換。 – 2013-03-26 15:49:27