1

我正在开发 WPF 中的 3D 应用程序并且遇到了相机问题。应该可以使用鼠标围绕自己的轴旋转相机(换句话说,环顾四周),但我无法让它正常工作。我使用以下代码创建相机:

PerspectiveCamera perspectiveCamera = new PerspectiveCamera(new Point3D(0, 30, 0), new Vector3D(0, -1, 0), new Vector3D(0, 0, 1), 90);
perspectiveCamera.NearPlaneDistance = 0.001;
perspectiveCamera.FarPlaneDistance = 1000;

center = new TranslateTransform3D(0, 30, 0);
rot_x = new AxisAngleRotation3D(new Vector3D(1, 0, 0), 0);
rot_y = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0);
rot_z = new AxisAngleRotation3D(new Vector3D(0, 0, 1), 0);
zoom = new ScaleTransform3D(1, 1, 1);

Transform3DGroup t = new Transform3DGroup();
t.Children.Add(zoom);
t.Children.Add(new RotateTransform3D(rot_x));            
t.Children.Add(new RotateTransform3D(rot_y));
t.Children.Add(new RotateTransform3D(rot_z));
t.Children.Add(center);
perspectiveCamera.Transform = t;
myViewport3D.Camera = perspectiveCamera;

然后我尝试使用以下代码旋转它:

private void OnViewportMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    if (mouseLeftIsDown)
    {
        Point position = e.GetPosition(this);
        double rotZAngle = (rot_z.Angle % 360) + oldLeftPosition.X - position.X;
        double rotXAngle = (rot_x.Angle % 360) + position.Y - oldLeftPosition.Y;
        if (rotZAngle < 0) //rotZAngle is negative, make it positive
        {
            rotZAngle = 360 + rotZAngle;
        }
        if (rotXAngle < 0) //rotXAngle is negative, make it positive
        {
            rotXAngle = 360 + rotXAngle;
        }
        rot_z.Angle = rotZAngle;
        rot_x.Angle = rotXAngle;
        oldLeftPosition = position;
    }
}

然而,看起来旋转并没有在相机周围发生,而是在其他地方发生。我在位置 (0,0,0) 加载的模型在我绕 z 轴旋转 180 度后变得可见,这不应该是这种情况。

我错过了什么?

4

0 回答 0