delphi 更改相机面方向

mrfwxfqh  于 2023-04-29  发布在  其他
关注(0)|答案(1)|浏览(140)

我刚开始在 Delphi 中使用3D,并试图了解相机RotationAngle的用途。我原以为它是指定摄像机的方向和Angular 。我为此设置了一个小型试验台。它有一个摄像头,周围有6个立方体。我假设相机最初指向场景的后面。
更改RotationAngle X应使其按顺序转向底部、前部、顶部和后部。然而,在我的测试床上,它去底部,顶部和背部,错过了前面的步骤。
更改RotationAngle Y应使其依次向右、前、左和后转动。然而,在我的测试床上,它向右,向左和向后走,再次错过了前面的步骤。
我是不是把RotationAngle的用法搞错了?如果是这样,我应该使用什么。
这就是代码:

unit MainForm;
    
    interface
    
    uses
      System.SysUtils, FMX.Forms, FMX.Materials, System.Math.Vectors, FMX.Types3D, FMX.Objects3D, FMX.Controls3D, FMX.Viewport3D,
      System.Classes, FMX.Types, FMX.Controls, FMX.Layouts, FMX.MaterialSources, System.Types, System.UITypes,
      FMX.Controls.Presentation, FMX.StdCtrls, FMX.Edit;

type

  TForm1 = class(TForm)
    Viewport3D: TViewport3D;
    LayoutMain: TLayout;
    LightMaterialSource1: TLightMaterialSource;
    Panel1: TPanel;
    grpPosition: TGroupBox;
    Camera1: TCamera;
    CubeLeft: TCube;
    CubeRight: TCube;
    grpAngle: TGroupBox;
    ColorMaterialSource2: TColorMaterialSource;
    CubeBottom: TCube;
    CubeTop: TCube;
    CubeBack: TCube;
    CubeFront: TCube;
    ColorMaterialSource1: TColorMaterialSource;
    ColorMaterialSource3: TColorMaterialSource;
    ColorMaterialSource4: TColorMaterialSource;
    ColorMaterialSource5: TColorMaterialSource;
    ColorMaterialSource6: TColorMaterialSource;
    AngleX: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    AngleY: TEdit;
    Button4: TButton;
    Button5: TButton;
    AngleZ: TEdit;
    Button6: TButton;
    Button7: TButton;
    PositionZ: TEdit;
    Button8: TButton;
    Button9: TButton;
    PositionY: TEdit;
    Button10: TButton;
    Button11: TButton;
    PositionX: TEdit;
    Button12: TButton;
    txtStep: TEdit;
    procedure ChangePosition(Sender: TObject);
    procedure ChangeAngle(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure txtStepExit(Sender: TObject);
    procedure ChangeAngleText(Sender: TObject);
    procedure ChangePositionText(Sender: TObject);
  private
    Step: Single;
    procedure UpdateCameraPosition;
    procedure UpdateCameraAngle;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.ChangeAngle(Sender: TObject);
begin
  case TButton(Sender).Tag of
    0: Camera1.RotationAngle.X := Camera1.RotationAngle.X - Step;
    1: Camera1.RotationAngle.X := Camera1.RotationAngle.X + Step;
    2: Camera1.RotationAngle.Y := Camera1.RotationAngle.Y - Step;
    3: Camera1.RotationAngle.Y := Camera1.RotationAngle.Y + Step;
    4: Camera1.RotationAngle.Z := Camera1.RotationAngle.Z - Step;
    5: Camera1.RotationAngle.Z := Camera1.RotationAngle.Z + Step;
  end;
  UpdateCameraAngle;
end;

procedure TForm1.ChangeAngleText(Sender: TObject);
begin
  case TEdit(Sender).Tag of
    0: Camera1.RotationAngle.X := StrToFloat(AngleX.Text);
    1: Camera1.RotationAngle.Y := StrToFloat(AngleY.Text);
    2: Camera1.RotationAngle.Z := StrToFloat(AngleZ.Text);
  end;
end;

procedure TForm1.ChangePosition(Sender: TObject);
begin
  case TButton(Sender).Tag of
    0: Camera1.Position.X := Camera1.Position.X - Step;
    1: Camera1.Position.X := Camera1.Position.X + Step;
    2: Camera1.Position.Y := Camera1.Position.Y - Step;
    3: Camera1.Position.Y := Camera1.Position.Y + Step;
    4: Camera1.Position.Z := Camera1.Position.Z - Step;
    5: Camera1.Position.Z := Camera1.Position.Z + Step;
  end;
  UpdateCameraPosition;
end;

procedure TForm1.ChangePositionText(Sender: TObject);
begin
  case TEdit(Sender).Tag of
    0: Camera1.Position.X := StrToFloat(PositionX.Text);
    1: Camera1.Position.Y := StrToFloat(PositionY.Text);
    2: Camera1.Position.Z := StrToFloat(PositionZ.Text);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Step := StrToFloat(txtStep.Text);
end;

procedure TForm1.txtStepExit(Sender: TObject);
begin
  Step := StrToFloat(txtStep.Text);
end;

procedure TForm1.UpdateCameraAngle;
begin
  AngleX.Text := Format('%3.2f', [Camera1.RotationAngle.X]);
  AngleY.Text := Format('%3.2f', [Camera1.RotationAngle.Y]);
  AngleZ.Text := Format('%3.2f', [Camera1.RotationAngle.Z]);
end;

procedure TForm1.UpdateCameraPosition;
begin
  PositionX.Text := Format('%3.2f', [Camera1.Position.X]);
  PositionY.Text := Format('%3.2f', [Camera1.Position.Y]);
  PositionZ.Text := Format('%3.2f', [Camera1.Position.Z]);
end;

end.

这是一种形式:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 592
  ClientWidth = 713
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  DesignerMasterStyle = 0
  object LayoutMain: TLayout
    Align = Client
    Size.Width = 713.000000000000000000
    Size.Height = 592.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 2
    object Viewport3D: TViewport3D
      Align = Client
      Camera = Camera1
      Size.Width = 533.000000000000000000
      Size.Height = 592.000000000000000000
      Size.PlatformDefault = False
      UsingDesignCamera = False
      object Camera1: TCamera
        AngleOfView = 45.000000000000000000
        Width = 1.000000000000000000
        Height = 1.000000000000000000
        Depth = 1.000000000000000000
      end
      object CubeRight: TCube
        Position.X = 4.000000000000000000
        Width = 1.000000000000000000
        Height = 1.000000000000000000
        Depth = 1.000000000000000000
        TwoSide = True
        MaterialSource = ColorMaterialSource2
      end
      object CubeLeft: TCube
        Position.X = -4.000000000000000000
        Width = 1.000000000000000000
        Height = 1.000000000000000000
        Depth = 1.000000000000000000
        TwoSide = True
        MaterialSource = ColorMaterialSource1
      end
      object CubeBottom: TCube
        Position.Y = -4.000000000000000000
        Width = 1.000000000000000000
        Height = 1.000000000000000000
        Depth = 1.000000000000000000
        TwoSide = True
        MaterialSource = ColorMaterialSource5
      end
      object CubeTop: TCube
        Position.Y = 4.000000000000000000
        Width = 1.000000000000000000
        Height = 1.000000000000000000
        Depth = 1.000000000000000000
        TwoSide = True
        MaterialSource = ColorMaterialSource6
      end
      object CubeBack: TCube
        Position.Z = -4.000000000000000000
        Width = 1.000000000000000000
        Height = 1.000000000000000000
        Depth = 1.000000000000000000
        TwoSide = True
        MaterialSource = ColorMaterialSource3
      end
      object CubeFront: TCube
        Position.Z = 4.000000000000000000
        Width = 1.000000000000000000
        Height = 1.000000000000000000
        Depth = 1.000000000000000000
        TwoSide = True
        MaterialSource = ColorMaterialSource4
      end
      object ColorMaterialSource2: TColorMaterialSource
        Color = claSpringgreen
        Left = 344
        Top = 280
      end
      object ColorMaterialSource1: TColorMaterialSource
        Color = claRed
        Left = 272
        Top = 288
      end
      object ColorMaterialSource3: TColorMaterialSource
        Color = claYellow
        Left = 360
        Top = 296
      end
      object ColorMaterialSource5: TColorMaterialSource
        Color = claPink
        Left = 376
        Top = 312
      end
      object ColorMaterialSource6: TColorMaterialSource
        Color = claOrange
        Left = 384
        Top = 320
      end
      object ColorMaterialSource4: TColorMaterialSource
        Color = claViolet
        Left = 368
        Top = 304
      end
    end
    object Panel1: TPanel
      Align = Right
      Position.X = 533.000000000000000000
      Size.Width = 180.000000000000000000
      Size.Height = 592.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
      object grpPosition: TGroupBox
        Position.X = 5.000000000000000000
        Position.Y = 134.000000000000000000
        Size.Width = 169.000000000000000000
        Size.Height = 123.000000000000000000
        Size.PlatformDefault = False
        Text = 'Position'
        TabOrder = 1
        object Button7: TButton
          Tag = 5
          Position.X = 139.000000000000000000
          Position.Y = 87.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 41
          Text = '>'
          OnClick = ChangePosition
        end
        object PositionZ: TEdit
          Tag = 2
          Touch.InteractiveGestures = [LongTap, DoubleTap]
          TabOrder = 38
          Text = '0.00'
          Position.X = 35.000000000000000000
          Position.Y = 87.000000000000000000
        end
        object Button8: TButton
          Tag = 4
          Position.X = 9.000000000000000000
          Position.Y = 87.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 40
          Text = '<'
          OnClick = ChangePosition
        end
        object Button9: TButton
          Tag = 3
          Position.X = 139.000000000000000000
          Position.Y = 55.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 44
          Text = '>'
          OnClick = ChangePosition
        end
        object PositionY: TEdit
          Tag = 1
          Touch.InteractiveGestures = [LongTap, DoubleTap]
          TabOrder = 39
          Text = '0.00'
          Position.X = 35.000000000000000000
          Position.Y = 55.000000000000000000
        end
        object Button10: TButton
          Tag = 2
          Position.X = 9.000000000000000000
          Position.Y = 55.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 43
          Text = '<'
          OnClick = ChangePosition
        end
        object Button11: TButton
          Tag = 1
          Position.X = 139.000000000000000000
          Position.Y = 23.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 45
          Text = '>'
          OnClick = ChangePosition
        end
        object PositionX: TEdit
          Touch.InteractiveGestures = [LongTap, DoubleTap]
          TabOrder = 42
          Text = '0.00'
          Position.X = 35.000000000000000000
          Position.Y = 23.000000000000000000
          OnExit = ChangePositionText
        end
        object Button12: TButton
          Position.X = 9.000000000000000000
          Position.Y = 23.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 46
          Text = '<'
          OnClick = ChangePosition
        end
      end
      object grpAngle: TGroupBox
        Position.X = 5.000000000000000000
        Position.Y = 5.000000000000000000
        Size.Width = 169.000000000000000000
        Size.Height = 124.000000000000000000
        Size.PlatformDefault = False
        Text = 'Angle'
        TabOrder = 0
        object AngleX: TEdit
          Touch.InteractiveGestures = [LongTap, DoubleTap]
          TabOrder = 42
          Text = '0.00'
          Position.X = 34.000000000000000000
          Position.Y = 24.000000000000000000
          OnExit = ChangeAngleText
        end
        object Button1: TButton
          Position.X = 8.000000000000000000
          Position.Y = 24.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 46
          Text = '<'
          OnClick = ChangeAngle
        end
        object Button2: TButton
          Tag = 1
          Position.X = 138.000000000000000000
          Position.Y = 24.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 45
          Text = '>'
          OnClick = ChangeAngle
        end
        object Button3: TButton
          Tag = 3
          Position.X = 138.000000000000000000
          Position.Y = 56.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 44
          Text = '>'
          OnClick = ChangeAngle
        end
        object AngleY: TEdit
          Tag = 1
          Touch.InteractiveGestures = [LongTap, DoubleTap]
          TabOrder = 39
          Text = '0.00'
          Position.X = 34.000000000000000000
          Position.Y = 56.000000000000000000
          OnExit = ChangeAngleText
        end
        object Button4: TButton
          Tag = 2
          Position.X = 8.000000000000000000
          Position.Y = 56.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 43
          Text = '<'
          OnClick = ChangeAngle
        end
        object Button5: TButton
          Tag = 5
          Position.X = 138.000000000000000000
          Position.Y = 88.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 41
          Text = '>'
          OnClick = ChangeAngle
        end
        object AngleZ: TEdit
          Tag = 2
          Touch.InteractiveGestures = [LongTap, DoubleTap]
          TabOrder = 38
          Text = '0.00'
          Position.X = 34.000000000000000000
          Position.Y = 88.000000000000000000
          OnExit = ChangeAngleText
        end
        object Button6: TButton
          Tag = 4
          Position.X = 8.000000000000000000
          Position.Y = 88.000000000000000000
          Size.Width = 22.000000000000000000
          Size.Height = 22.000000000000000000
          Size.PlatformDefault = False
          TabOrder = 40
          Text = '<'
          OnClick = ChangeAngle
        end
      end
      object txtStep: TEdit
        Touch.InteractiveGestures = [LongTap, DoubleTap]
        TabOrder = 37
        Text = '2.00'
        Position.X = 43.000000000000000000
        Position.Y = 263.000000000000000000
        OnExit = txtStepExit
      end
    end
  end
  object LightMaterialSource1: TLightMaterialSource
    Diffuse = claWhite
    Ambient = xFF202020
    Emissive = claNull
    Specular = xFF606060
    Shininess = 30
    Left = 436
    Top = 56
  end
end
qacovj5a

qacovj5a1#

使用您的测试应用程序并保持位置在(0,0,0),旋转看起来像您预期的那样工作,与您声称的相反。
绕x轴旋转(按钮2):紫色-粉红色-黄色-棕色-紫色
绕y轴旋转(按钮4):紫-绿色-黄-红-紫
绕z轴旋转(按钮6):旋转紫色正方形。
缩小(第三位置设置)-15

  • 紫色被黄色立方体遮住,其他的都可见
  • 在旋转15 °时,立方体星座图像被旋转-15 °

因此,当围绕选定轴旋转相机时,视图会根据预期发生变化。

相关问题