2009-11-17 132 views
4

我需要在C#中的轴上旋转3D网格对象。
你能告诉我这是怎么完成的吗?如何在C#3D中旋转对象?

+1

也许你能告诉我们一些代码,以便我们可以回答使用正确类等 – GraemeF 2009-11-17 21:59:42

回答

10

乘以旋转矩阵所有顶点

alt text

+2

我想翻译“上轴”是指围绕一定的PREDEF。轴。所以不要忘了翻译,因为这些矩阵没有内置的平移向量,所以会围绕原点旋转。 – bastijn 2009-11-17 22:04:02

2

这取决于你想使用的API:

在WPF中,你可以做这样的:

<Viewport3D> 
     <Viewport3D.Camera> 
      <PerspectiveCamera Position="-40,40,40" LookDirection="40,-40,-40 " 
          UpDirection="0,0,1" /> 
     </Viewport3D.Camera> 
     <ModelVisual3D> 
      <ModelVisual3D.Content> 
      <Model3DGroup> 
       <DirectionalLight Color="White" Direction="-1,-1,-3" /> 
        <GeometryModel3D> 
        <Model3DGroup.Transform> 
         <RotateTransform3D> 
          <RotateTransform3D.Rotation> 
           <!-- here you do the rotation --> 
           <AxisAngleRotation3D x:Name="rotation" Axis="0 0 1" Angle="45" /> 
          </RotateTransform3D.Rotation> 
         </RotateTransform3D> 
         </Model3DGroup.Transform> 
         <GeometryModel3D.Geometry> 
         <MeshGeometry3D Positions="0,0,0 10,0,0 10,10,0 0,10,0 0,0,10 
          10,0,10 10,10,10 0,10,10" 
          TriangleIndices="0 1 3 1 2 3 0 4 3 4 7 3 4 6 7 4 5 6 
              0 4 1 1 4 5 1 2 6 6 5 1 2 3 7 7 6 2"/> 
         </GeometryModel3D.Geometry> 
         <GeometryModel3D.Material> 
         <DiffuseMaterial Brush="Red"/> 
         </GeometryModel3D.Material> 
        </GeometryModel3D> 
       </Model3DGroup> 
      </ModelVisual3D.Content> 
      </ModelVisual3D> 
    </Viewport3D> 

还是在代码隐藏c#:

this.rotation.Angle = 90; 

如果你使用XNA,你会使用像Matrix.CreateRotationY这样的东西,并将其应用于你的ModelMesh实例。

当然,您可以利用大量的第三方引擎和apis。一个有趣的选择可能是SlimDX这是一个像Managed DirectX一样的Direct3D类型的超薄包装。

+0

也许他的意思是XNA!:) – 2009-11-17 22:17:41

1

让我们把更多的细节放在这里。

鉴于旋转角度和旋转轴的规格,我们可以通过几个步骤完成旋转。

  1. 翻译的对象,以使旋转轴穿过坐标系原点
  2. 旋转对象,使得旋转轴与坐标轴中的一个相一致
  3. 关于所选择的坐标执行指定旋转轴。
  4. 应用逆向旋转使旋转轴回到其原始方向。
  5. 应用反向平移使旋转轴回到其原始空间位置。

该代码是一个阅读练习,因为这是我的大脑训练(计算机图形很久以前:d)。也许当我想起来的时候,我会多发一点。

我相信它是这样的: R(θ)= T^-1。 Rx^-1(alpha)。 Ry^-1(Beta)。 Rz(theta)。 Ry(测试版)。 Rx(alpha)。牛逼

其中:

  • T =平移矩阵
  • 的Rx =约X等旋转

脑教练编辑

O上,我们可以简化(即使不使用quarternations )

alt text http://www.gamedev.net/reference/articles/1199/image008.gif

alt text http://www.gamedev.net/reference/articles/1199/image010.gif

和(X,Y,Z)是在旋转轴的单位矢量,并且旋转的角度。

如果我可以相信谷歌。该证明作为练习留给读者,但我相信这是正确的,据我所知(来源:Graphics Gems(Glassner,Academic Press,1990)。)

1

我有类似的问题。我不确定你想要旋转什么,但让我们假设你想要变换一个MeshGeometry3D。这是我的解决方案。

public void RotateMesh(MeshGeometry3D mesh, Vector3D axis, double angle) 
    { 
     var transform = new RotateTransform3D(); 
     transform.Rotation = new AxisAngleRotation3D(axis, angle); 

     for (int i = 0; i < mesh.Positions.Count; ++i) 
      mesh.Positions[i] = transform.Transform(mesh.Positions[i]); 
    }