123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- //
- // The graphics rendering engine GLScene http://glscene.org
- //
- unit DWS.VectorGeometry;
- (* DelphiWebScript symbol creation for GLS.VectorGeometry types and functions *)
- interface
- uses
- System.Classes,
- dwsExprs,
- dwsSymbols,
- dwsComp,
- dwsFunctions,
- GLS.VectorGeometry;
- type
- TdwsVectorGeometryUnit = class(TdwsUnitComponent)
- protected
- procedure AddUnitSymbols(SymbolTable: TSymbolTable); override;
- public
- constructor Create(AOwner: TComponent); override;
- end;
- procedure Register;
- // =========================================================
- implementation
- // =========================================================
- type
- TVectorMakeFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TSetVectorFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorAddFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorSubtractFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorScaleFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCombineVectorFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorCombineFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorCombine3Function = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorDotProductFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorCrossProductFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorNormalizeFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TVectorTransformFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TInvertMatrixFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TTransposeMatrixFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TMatrixMultiplyFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCreateScaleMatrixFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCreateTranslationMatrixFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCreateScaleAndTranslationMatrixFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCreateRotationMatrixXFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCreateRotationMatrixYFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCreateRotationMatrixZFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- TCreateRotationMatrixFunction = class(TInternalFunction)
- public
- procedure Execute; override;
- end;
- procedure Register;
- begin
- RegisterComponents('GLScene DWS', [TdwsVectorGeometryUnit]);
- end;
- function GetVectorFromInfo(Info: IInfo): TGLVector;
- begin
- Result := VectorMake(Info.Element([0]).Value, Info.Element([1]).Value,
- Info.Element([2]).Value, Info.Element([3]).Value);
- end;
- procedure SetInfoFromVector(Info: IInfo; vec: TGLVector);
- var
- i: Integer;
- begin
- for i := 0 to 3 do
- Info.Element([i]).Value := vec[i];
- end;
- function GetMatrixFromInfo(Info: IInfo): TGLMatrix;
- var
- i: Integer;
- begin
- for i := 0 to 3 do
- Result[i] := VectorMake(Info.Element([i]).Element([0]).Value,
- Info.Element([i]).Element([1]).Value, Info.Element([i]).Element([2])
- .Value, Info.Element([i]).Element([3]).Value);
- end;
- procedure SetInfoFromMatrix(Info: IInfo; mat: TGLMatrix);
- var
- i, j: Integer;
- begin
- for i := 0 to 3 do
- for j := 0 to 3 do
- Info.Element([i]).Element([j]).Value := mat[i][j];
- end;
- procedure TdwsVectorGeometryUnit.AddUnitSymbols(SymbolTable: TSymbolTable);
- var
- FloatSymbol: TSymbol;
- begin
- FloatSymbol := SymbolTable.FindSymbol('Float');
- // Array types
- SymbolTable.AddSymbol(TStaticArraySymbol.Create('TGLVector',
- FloatSymbol, 0, 3));
- SymbolTable.AddSymbol(TStaticArraySymbol.Create('TGLMatrix',
- SymbolTable.FindSymbol('TGLVector'), 0, 3));
- // Vector functions
- TVectorMakeFunction.Create(SymbolTable, 'VectorMake',
- ['x', 'Float', 'y', 'Float', 'z', 'Float', 'w', 'Float'], 'TGLVector');
- TSetVectorFunction.Create(SymbolTable, 'SetVector',
- ['@v', 'TGLVector', 'x', 'Float', 'y', 'Float', 'z', 'Float', 'w',
- 'Float'], '');
- TVectorAddFunction.Create(SymbolTable, 'VectorAdd',
- ['v1', 'TGLVector', 'v2', 'TGLVector'], 'TGLVector');
- TVectorSubtractFunction.Create(SymbolTable, 'VectorSubtract',
- ['v1', 'TGLVector', 'v2', 'TGLVector'], 'TGLVector');
- TVectorScaleFunction.Create(SymbolTable, 'VectorScale',
- ['v', 'TGLVector', 'f', 'Float'], 'TGLVector');
- TCombineVectorFunction.Create(SymbolTable, 'CombineVector',
- ['@vr', 'TGLVector', 'v', 'TGLVector', '@f', 'Float'], '');
- TVectorCombineFunction.Create(SymbolTable, 'VectorCombine',
- ['v1', 'TGLVector', 'v2', 'TGLVector', 'f1', 'Float', 'f2', 'Float'],
- 'TGLVector');
- TVectorCombine3Function.Create(SymbolTable, 'VectorCombine3',
- ['v1', 'TGLVector', 'v2', 'TGLVector', 'v3', 'TGLVector', 'f1', 'Float', 'f2',
- 'Float', 'f3', 'Float'], 'TGLVector');
- TVectorDotProductFunction.Create(SymbolTable, 'VectorDotProduct',
- ['v1', 'TGLVector', 'v2', 'TGLVector'], 'Float');
- TVectorCrossProductFunction.Create(SymbolTable, 'VectorCrossProduct',
- ['v1', 'TGLVector', 'v2', 'TGLVector'], 'TGLVector');
- TVectorNormalizeFunction.Create(SymbolTable, 'VectorNormalize',
- ['v', 'TGLVector'], 'TGLVector');
- TVectorTransformFunction.Create(SymbolTable, 'VectorTransform',
- ['v', 'TGLVector', 'm', 'TGLMatrix'], 'TGLVector');
- // Matrix function
- TInvertMatrixFunction.Create(SymbolTable, 'InvertMatrix',
- ['@mat', 'TGLMatrix'], '');
- TTransposeMatrixFunction.Create(SymbolTable, 'TransposeMatrix',
- ['@mat', 'TGLMatrix'], '');
- TMatrixMultiplyFunction.Create(SymbolTable, 'MatrixMultiply',
- ['m1', 'TGLMatrix', 'm2', 'TGLMatrix'], 'TGLMatrix');
- TCreateScaleMatrixFunction.Create(SymbolTable, 'CreateScaleMatrix',
- ['v', 'TGLVector'], 'TGLMatrix');
- TCreateTranslationMatrixFunction.Create(SymbolTable,
- 'CreateTranslationMatrix', ['v', 'TGLVector'], 'TGLMatrix');
- TCreateScaleAndTranslationMatrixFunction.Create(SymbolTable,
- 'CreateScaleAndTranslationMatrix', ['scale', 'TGLVector', 'offset',
- 'TGLVector'], 'TGLMatrix');
- TCreateRotationMatrixXFunction.Create(SymbolTable, 'CreateRotationMatrixX',
- ['angle', 'Float'], 'TGLMatrix');
- TCreateRotationMatrixYFunction.Create(SymbolTable, 'CreateRotationMatrixY',
- ['angle', 'Float'], 'TGLMatrix');
- TCreateRotationMatrixZFunction.Create(SymbolTable, 'CreateRotationMatrixZ',
- ['angle', 'Float'], 'TGLMatrix');
- TCreateRotationMatrixFunction.Create(SymbolTable, 'CreateRotationMatrix',
- ['anAxis', 'TGLVector', 'angle', 'Float'], 'TGLMatrix');
- end;
- constructor TdwsVectorGeometryUnit.Create(AOwner: TComponent);
- begin
- inherited;
- FUnitName := 'GLS.VectorGeometry';
- end;
- procedure TVectorMakeFunction.Execute;
- begin
- Info.Vars['Result'].Element([0]).Value := Info['x'];
- Info.Vars['Result'].Element([1]).Value := Info['y'];
- Info.Vars['Result'].Element([2]).Value := Info['z'];
- Info.Vars['Result'].Element([3]).Value := Info['w'];
- end;
- procedure TSetVectorFunction.Execute;
- begin
- Info.Vars['v'].Element([0]).Value := Info['x'];
- Info.Vars['v'].Element([1]).Value := Info['y'];
- Info.Vars['v'].Element([2]).Value := Info['z'];
- Info.Vars['v'].Element([3]).Value := Info['w'];
- end;
- procedure TVectorAddFunction.Execute;
- var
- v1, v2, vr: TGLVector;
- begin
- v1 := GetVectorFromInfo(Info.Vars['v1']);
- v2 := GetVectorFromInfo(Info.Vars['v2']);
- VectorAdd(v1, v2, vr);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TVectorSubtractFunction.Execute;
- var
- v1, v2, vr: TGLVector;
- begin
- v1 := GetVectorFromInfo(Info.Vars['v1']);
- v2 := GetVectorFromInfo(Info.Vars['v2']);
- VectorSubtract(v1, v2, vr);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TVectorScaleFunction.Execute;
- var
- v, vr: TGLVector;
- f: Single;
- begin
- v := GetVectorFromInfo(Info.Vars['v']);
- f := Info['f'];
- VectorScale(v, f, vr);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TCombineVectorFunction.Execute;
- var
- vr, v: TGLVector;
- f: Single;
- begin
- vr := GetVectorFromInfo(Info.Vars['vr']);
- v := GetVectorFromInfo(Info.Vars['v']);
- f := Info['f'];
- CombineVector(vr, v, f);
- SetInfoFromVector(Info.Vars['Result'], vr);
- Info.Vars['f'].Value := f;
- end;
- procedure TVectorCombineFunction.Execute;
- var
- v1, v2, vr: TGLVector;
- f1, f2: Single;
- begin
- v1 := GetVectorFromInfo(Info.Vars['v1']);
- v2 := GetVectorFromInfo(Info.Vars['v2']);
- f1 := Info['f1'];
- f2 := Info['f2'];
- VectorCombine(v1, v2, f1, f2, vr);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TVectorCombine3Function.Execute;
- var
- v1, v2, v3, vr: TGLVector;
- f1, f2, f3: Single;
- begin
- v1 := GetVectorFromInfo(Info.Vars['v1']);
- v2 := GetVectorFromInfo(Info.Vars['v2']);
- v3 := GetVectorFromInfo(Info.Vars['v3']);
- f1 := Info['f1'];
- f2 := Info['f2'];
- f3 := Info['f3'];
- VectorCombine3(v1, v2, v3, f1, f2, f3, vr);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TVectorDotProductFunction.Execute;
- var
- v1, v2: TGLVector;
- begin
- v1 := GetVectorFromInfo(Info.Vars['v1']);
- v2 := GetVectorFromInfo(Info.Vars['v2']);
- Info.Result := VectorDotProduct(v1, v2);
- end;
- procedure TVectorCrossProductFunction.Execute;
- var
- v1, v2, vr: TGLVector;
- begin
- v1 := GetVectorFromInfo(Info.Vars['v1']);
- v2 := GetVectorFromInfo(Info.Vars['v2']);
- VectorCrossProduct(v1, v2, vr);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TVectorNormalizeFunction.Execute;
- var
- v, vr: TGLVector;
- begin
- v := GetVectorFromInfo(Info.Vars['v']);
- vr := VectorNormalize(v);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TVectorTransformFunction.Execute;
- var
- v, vr: TGLVector;
- mat: TGLMatrix;
- begin
- v := GetVectorFromInfo(Info.Vars['v']);
- mat := GetMatrixFromInfo(Info.Vars['mat']);
- vr := VectorTransform(v, mat);
- SetInfoFromVector(Info.Vars['Result'], vr);
- end;
- procedure TInvertMatrixFunction.Execute;
- var
- mat: TGLMatrix;
- begin
- mat := GetMatrixFromInfo(Info.Vars['mat']);
- InvertMatrix(mat);
- SetInfoFromMatrix(Info.Vars['Result'], mat);
- end;
- procedure TTransposeMatrixFunction.Execute;
- var
- mat: TGLMatrix;
- begin
- mat := GetMatrixFromInfo(Info.Vars['mat']);
- TransposeMatrix(mat);
- SetInfoFromMatrix(Info.Vars['Result'], mat);
- end;
- procedure TMatrixMultiplyFunction.Execute;
- var
- m1, m2, mr: TGLMatrix;
- begin
- m1 := GetMatrixFromInfo(Info.Vars['m1']);
- m2 := GetMatrixFromInfo(Info.Vars['m2']);
- MatrixMultiply(m1, m2, mr);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- procedure TCreateScaleMatrixFunction.Execute;
- var
- v: TGLVector;
- mr: TGLMatrix;
- begin
- v := GetVectorFromInfo(Info.Vars['v']);
- mr := CreateScaleMatrix(v);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- procedure TCreateTranslationMatrixFunction.Execute;
- var
- v: TGLVector;
- mr: TGLMatrix;
- begin
- v := GetVectorFromInfo(Info.Vars['v']);
- mr := CreateTranslationMatrix(v);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- procedure TCreateScaleAndTranslationMatrixFunction.Execute;
- var
- scale, offset: TGLVector;
- mr: TGLMatrix;
- begin
- scale := GetVectorFromInfo(Info.Vars['scale']);
- offset := GetVectorFromInfo(Info.Vars['offset']);
- mr := CreateScaleAndTranslationMatrix(scale, offset);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- procedure TCreateRotationMatrixXFunction.Execute;
- var
- angle: Single;
- mr: TGLMatrix;
- begin
- angle := Info['angle'];
- mr := CreateRotationMatrixX(angle);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- procedure TCreateRotationMatrixYFunction.Execute;
- var
- angle: Single;
- mr: TGLMatrix;
- begin
- angle := Info['angle'];
- mr := CreateRotationMatrixY(angle);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- procedure TCreateRotationMatrixZFunction.Execute;
- var
- angle: Single;
- mr: TGLMatrix;
- begin
- angle := Info['angle'];
- mr := CreateRotationMatrixZ(angle);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- procedure TCreateRotationMatrixFunction.Execute;
- var
- angle: Single;
- anAxis: TGLVector;
- mr: TGLMatrix;
- begin
- anAxis := GetVectorFromInfo(Info.Vars['anAxis']);
- angle := Info['angle'];
- mr := CreateRotationMatrix(anAxis, angle);
- SetInfoFromMatrix(Info.Vars['Result'], mr);
- end;
- end.
|