⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buildstaticmodel.pas

📁 这是我用Delphi和Matlab写的一个程序
💻 PAS
字号:
unit BuildStaticModel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jpeg, ExtCtrls;

type
  TfmStaticStereo = class(TForm)
    Label3: TLabel;
    editCX: TEdit;
    dlgSaveMaxScript: TSaveDialog;
    btnBuildStatic: TButton;
    btnExit: TButton;
    lblCX: TLabel;
    editCY: TEdit;
    lblCY: TLabel;
    editCZ: TEdit;
    lblTarget: TLabel;
    editTX: TEdit;
    editTY: TEdit;
    editTZ: TEdit;
    lblScale: TLabel;
    editScale: TEdit;
    lblFOV: TLabel;
    editFOV: TEdit;
    lblDeg: TLabel;
    lblSize: TLabel;
    editImageWidth: TEdit;
    lblStar: TLabel;
    editImageHeight: TEdit;
    lblPixel: TLabel;
    lblCZ: TLabel;
    lblTX: TLabel;
    lblTY: TLabel;
    lblTZ: TLabel;
    imgNote: TImage;
    BakLbl: TLabel;
    MarkLbl: TLabel;
    procedure btnBuildStaticClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmStaticStereo: TfmStaticStereo;

implementation

{$R *.dfm}

{ 点击生成按钮后,将生成的脚本程序保存在指定路径的指定文件中 }
procedure TfmStaticStereo.btnBuildStaticClick(Sender: TObject);
var
  MSFile: TextFile; //  定义TextFile类型的文件变量(文本文件)
  MSFileName: string; //  定义字符串类型的变量保存文件名
  FOV: string; //  定义字符串类型的变量保存摄像机的视野
  Ratio: string; //  定义字符串类型的变量保存左右摄像机距离、视点到目标点距离之比
  cX, cY, cZ, TX, TY, TZ: string; //  定义字符串类型的变量保存摄像机视点和目标点的坐标
  ImageWidth, ImageHeight: string; //  定义字符串类型的变量保存生成图像的大小
begin
  { 读入各字符串变量的值 }
  cX := editCX.Text;
  cY := editCY.Text;
  cZ := editCZ.Text;
  TX := editTX.Text;
  TY := editTY.Text;
  TZ := editTZ.Text;
  Ratio := editScale.Text;
  FOV := editFOV.Text;
  ImageWidth := editImageWidth.Text;
  ImageHeight := editImageHeight.Text;

  { 指定ms文件的存储路径和文件名并保存 }
  if dlgSaveMaxScript.Execute then
  begin
    { 保存指定的文件名 }
    MSFileName := dlgSaveMaxScript.FileName;
    { 将指定的文件名赋给文件变量 }
    AssignFile(MSFile, MSFileName);
    { 根据文件变量创建新的文本文件并打开 }
    Rewrite(MSFile);

    { 连续向文本文件中写入脚本程序的多行内容 }
    Writeln(MSFile, '--输入中央眼处的摄像机视点的坐标');
    Writeln(MSFile, 'select $*');
    if (cX = '0') and (cY = '0') and (cZ = '0') then
    begin
      Writeln(MSFile, 'camera_cx = 3 * $.max.x');
      Writeln(MSFile, 'camera_cy = 3 * $.max.y');
      Writeln(MSFile, 'camera_cz = 3 * $.max.z');
    end
    else
    begin
      Writeln(MSFile, 'camera_cx = ' + cX);
      Writeln(MSFile, 'camera_cy = ' + cY);
      Writeln(MSFile, 'camera_cz = ' + cZ);
    end;
    Writeln(MSFile, '');
    Writeln(MSFile, '--输入摄像机目标点的坐标');
    if (TX = '0') and (TY = '0') and (TZ = '0') then
    begin
      Writeln(MSFile, 'target_x = $.center.x');
      Writeln(MSFile, 'target_y = $.center.y');
      Writeln(MSFile, 'target_z = $.center.z');
    end
    else
    begin
      Writeln(MSFile, 'target_x = ' + TX);
      Writeln(MSFile, 'target_y = ' + TY);
      Writeln(MSFile, 'target_z = ' + TZ);
    end;
    Writeln(MSFile, 'clearSelection()');
    Writeln(MSFile, '');
    Writeln(MSFile,
      '--输入左右摄像机距离、视点到目标点距离的比例');
    Writeln(MSFile, '--镜头为50mm时,最佳比例为1:20');
    Writeln(MSFile, 'ratio = ' + Ratio);
    Writeln(MSFile, '');
    Writeln(MSFile,
      '--根据视点、目标点、比例ratio,计算左右摄像机视点的坐标');
    Writeln(MSFile,
      'distance_ct = sqrt( (camera_cx-target_x)^2 + (camera_cy-target_y)^2 + (camera_cz-target_z)^2 )');
    Writeln(MSFile,
      'coefficient = distance_ct * ratio / ( 2 * sqrt( (camera_cy-target_y)^2 + (target_x-camera_cx)^2 ) )');
    Writeln(MSFile,
      'camera_lx   = camera_cx + coefficient * (camera_cy-target_y)');
    Writeln(MSFile,
      'camera_ly   = camera_cy + coefficient * (target_x-camera_cx)');
    Writeln(MSFile, 'camera_lz   = camera_cz');
    Writeln(MSFile,
      'camera_rx   = camera_cx - coefficient * (camera_cy-target_y)');
    Writeln(MSFile,
      'camera_ry   = camera_cy - coefficient * (target_x-camera_cx)');
    Writeln(MSFile, 'camera_rz   = camera_cz');
    Writeln(MSFile, '');
    Writeln(MSFile, '--输入摄像机的视野');
    Writeln(MSFile,
      '--默认视野为39.598度,此时摄像机镜头为50mm');
    Writeln(MSFile, 'camera_FOV = ' + FOV);
    Writeln(MSFile, '');
    Writeln(MSFile, '--创建左右摄像机');
    Writeln(MSFile,
      'camera_l = TargetCamera name:"camera_l" pos:[camera_lx, camera_ly, camera_lz]');
    Writeln(MSFile,
      'camera_r = TargetCamera name:"camera_r" pos:[camera_rx, camera_ry, camera_rz]');
    Writeln(MSFile, 'camera_l.FOV = camera_r.FOV = camera_FOV');
    Writeln(MSFile, '');
    Writeln(MSFile, '--创建左右摄像机的共同目标点');
    Writeln(MSFile,
      'target_lr = TargetObject pos:[target_x, target_y, target_z]');
    Writeln(MSFile, '');
    Writeln(MSFile, '--将左右摄像机与共同目标点相关联');
    Writeln(MSFile, 'camera_l.Target = target_lr');
    Writeln(MSFile, 'camera_r.Target = target_lr');
    Writeln(MSFile, '');
    Writeln(MSFile, '--输入渲染图像的大小、输出文件的路径');
    Writeln(MSFile, 'image_width  = ' + ImageWidth);
    Writeln(MSFile, 'image_height = ' + ImageHeight);
    Writeln(MSFile, '');
    Writeln(MSFile, '--渲染左右摄像机的图像,生成左右图像对');

    {  去掉后缀ms  }
    Delete(MSFileName, Pos('.', MSFileName), 3);

    Writeln(MSFile,
      'render camera:$camera_l outputwidth:image_width outputheight:image_height outputFile:("' + MSFileName
      + '_l.bmp"' + ') vfb:on');
    Writeln(MSFile,
      'render camera:$camera_r outputwidth:image_width outputheight:image_height outputFile:("' + MSFileName
      + '_r.bmp"' + ') vfb:on');
    Writeln(MSFile, '');

    { 关闭打开的文本文件 }
    CloseFile(MSFile);
    { 显示完成信息 }
    MessageDlg('已成功生成了MAXScript程序文件' + MSFileName +
      '.ms,由3DS MAX生成的立体图像对也将保存在该目录下。',
      mtInformation, [mbOK], 0);
    btnBuildStatic.Enabled := False;
  end;

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -