如何在C#3D中旋转对象?

时间:2009-11-17 21:53:09

标签: c# 3d rotation

我需要在C#中的轴上旋转3D网格物体 你能告诉我这是怎么做到的吗?

5 个答案:

答案 0 :(得分:10)

用旋转矩阵

乘以所有顶点

alt text

答案 1 :(得分: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,它是Direct3D的一个纤薄的包装,有点像Managed DirectX。

答案 2 :(得分:1)

让我们详细介绍一下。

考虑到旋转角度和旋转轴的规格,我们可以在几个步骤中完成旋转。

  1. 平移对象,使旋转轴穿过坐标系原点
  2. 旋转对象,使旋转轴与其中一个坐标轴重合
  3. 对选定的坐标轴执行指定的旋转。
  4. 应用反向旋转以使旋转轴恢复到原始方向。
  5. 应用反向平移以使旋转轴返回其原始空间位置。
  6. 代码是读者的练习,因为这是对我的大脑训练(计算机图形很久以前:d)。也许当我为它做准备时,我会发布更多信息。

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

    其中:

    • T =翻译矩阵
    • Rx =关于x等的旋转

    脑训练师编辑

    我们可以简化(即使不使用季度)

    alt text
    (来源:gamedev.net

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

    和(x,y,z)是旋转轴上的单位矢量,是旋转角度。

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

答案 3 :(得分: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]);
    }

答案 4 :(得分:0)

您可以使用Skiasharp(开源库)

检查此code的操作方法。

相关问题