DWS.VectorGeometry.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // The graphics rendering engine GLScene http://glscene.org
  3. //
  4. unit DWS.VectorGeometry;
  5. (* DelphiWebScript symbol creation for GLS.VectorGeometry types and functions *)
  6. interface
  7. uses
  8. System.Classes,
  9. dwsExprs,
  10. dwsSymbols,
  11. dwsComp,
  12. dwsFunctions,
  13. GLS.VectorGeometry;
  14. type
  15. TdwsVectorGeometryUnit = class(TdwsUnitComponent)
  16. protected
  17. procedure AddUnitSymbols(SymbolTable: TSymbolTable); override;
  18. public
  19. constructor Create(AOwner: TComponent); override;
  20. end;
  21. procedure Register;
  22. // =========================================================
  23. implementation
  24. // =========================================================
  25. type
  26. TVectorMakeFunction = class(TInternalFunction)
  27. public
  28. procedure Execute; override;
  29. end;
  30. TSetVectorFunction = class(TInternalFunction)
  31. public
  32. procedure Execute; override;
  33. end;
  34. TVectorAddFunction = class(TInternalFunction)
  35. public
  36. procedure Execute; override;
  37. end;
  38. TVectorSubtractFunction = class(TInternalFunction)
  39. public
  40. procedure Execute; override;
  41. end;
  42. TVectorScaleFunction = class(TInternalFunction)
  43. public
  44. procedure Execute; override;
  45. end;
  46. TCombineVectorFunction = class(TInternalFunction)
  47. public
  48. procedure Execute; override;
  49. end;
  50. TVectorCombineFunction = class(TInternalFunction)
  51. public
  52. procedure Execute; override;
  53. end;
  54. TVectorCombine3Function = class(TInternalFunction)
  55. public
  56. procedure Execute; override;
  57. end;
  58. TVectorDotProductFunction = class(TInternalFunction)
  59. public
  60. procedure Execute; override;
  61. end;
  62. TVectorCrossProductFunction = class(TInternalFunction)
  63. public
  64. procedure Execute; override;
  65. end;
  66. TVectorNormalizeFunction = class(TInternalFunction)
  67. public
  68. procedure Execute; override;
  69. end;
  70. TVectorTransformFunction = class(TInternalFunction)
  71. public
  72. procedure Execute; override;
  73. end;
  74. TInvertMatrixFunction = class(TInternalFunction)
  75. public
  76. procedure Execute; override;
  77. end;
  78. TTransposeMatrixFunction = class(TInternalFunction)
  79. public
  80. procedure Execute; override;
  81. end;
  82. TMatrixMultiplyFunction = class(TInternalFunction)
  83. public
  84. procedure Execute; override;
  85. end;
  86. TCreateScaleMatrixFunction = class(TInternalFunction)
  87. public
  88. procedure Execute; override;
  89. end;
  90. TCreateTranslationMatrixFunction = class(TInternalFunction)
  91. public
  92. procedure Execute; override;
  93. end;
  94. TCreateScaleAndTranslationMatrixFunction = class(TInternalFunction)
  95. public
  96. procedure Execute; override;
  97. end;
  98. TCreateRotationMatrixXFunction = class(TInternalFunction)
  99. public
  100. procedure Execute; override;
  101. end;
  102. TCreateRotationMatrixYFunction = class(TInternalFunction)
  103. public
  104. procedure Execute; override;
  105. end;
  106. TCreateRotationMatrixZFunction = class(TInternalFunction)
  107. public
  108. procedure Execute; override;
  109. end;
  110. TCreateRotationMatrixFunction = class(TInternalFunction)
  111. public
  112. procedure Execute; override;
  113. end;
  114. procedure Register;
  115. begin
  116. RegisterComponents('GLScene DWS', [TdwsVectorGeometryUnit]);
  117. end;
  118. function GetVectorFromInfo(Info: IInfo): TGLVector;
  119. begin
  120. Result := VectorMake(Info.Element([0]).Value, Info.Element([1]).Value,
  121. Info.Element([2]).Value, Info.Element([3]).Value);
  122. end;
  123. procedure SetInfoFromVector(Info: IInfo; vec: TGLVector);
  124. var
  125. i: Integer;
  126. begin
  127. for i := 0 to 3 do
  128. Info.Element([i]).Value := vec[i];
  129. end;
  130. function GetMatrixFromInfo(Info: IInfo): TGLMatrix;
  131. var
  132. i: Integer;
  133. begin
  134. for i := 0 to 3 do
  135. Result[i] := VectorMake(Info.Element([i]).Element([0]).Value,
  136. Info.Element([i]).Element([1]).Value, Info.Element([i]).Element([2])
  137. .Value, Info.Element([i]).Element([3]).Value);
  138. end;
  139. procedure SetInfoFromMatrix(Info: IInfo; mat: TGLMatrix);
  140. var
  141. i, j: Integer;
  142. begin
  143. for i := 0 to 3 do
  144. for j := 0 to 3 do
  145. Info.Element([i]).Element([j]).Value := mat[i][j];
  146. end;
  147. procedure TdwsVectorGeometryUnit.AddUnitSymbols(SymbolTable: TSymbolTable);
  148. var
  149. FloatSymbol: TSymbol;
  150. begin
  151. FloatSymbol := SymbolTable.FindSymbol('Float');
  152. // Array types
  153. SymbolTable.AddSymbol(TStaticArraySymbol.Create('TGLVector',
  154. FloatSymbol, 0, 3));
  155. SymbolTable.AddSymbol(TStaticArraySymbol.Create('TGLMatrix',
  156. SymbolTable.FindSymbol('TGLVector'), 0, 3));
  157. // Vector functions
  158. TVectorMakeFunction.Create(SymbolTable, 'VectorMake',
  159. ['x', 'Float', 'y', 'Float', 'z', 'Float', 'w', 'Float'], 'TGLVector');
  160. TSetVectorFunction.Create(SymbolTable, 'SetVector',
  161. ['@v', 'TGLVector', 'x', 'Float', 'y', 'Float', 'z', 'Float', 'w',
  162. 'Float'], '');
  163. TVectorAddFunction.Create(SymbolTable, 'VectorAdd',
  164. ['v1', 'TGLVector', 'v2', 'TGLVector'], 'TGLVector');
  165. TVectorSubtractFunction.Create(SymbolTable, 'VectorSubtract',
  166. ['v1', 'TGLVector', 'v2', 'TGLVector'], 'TGLVector');
  167. TVectorScaleFunction.Create(SymbolTable, 'VectorScale',
  168. ['v', 'TGLVector', 'f', 'Float'], 'TGLVector');
  169. TCombineVectorFunction.Create(SymbolTable, 'CombineVector',
  170. ['@vr', 'TGLVector', 'v', 'TGLVector', '@f', 'Float'], '');
  171. TVectorCombineFunction.Create(SymbolTable, 'VectorCombine',
  172. ['v1', 'TGLVector', 'v2', 'TGLVector', 'f1', 'Float', 'f2', 'Float'],
  173. 'TGLVector');
  174. TVectorCombine3Function.Create(SymbolTable, 'VectorCombine3',
  175. ['v1', 'TGLVector', 'v2', 'TGLVector', 'v3', 'TGLVector', 'f1', 'Float', 'f2',
  176. 'Float', 'f3', 'Float'], 'TGLVector');
  177. TVectorDotProductFunction.Create(SymbolTable, 'VectorDotProduct',
  178. ['v1', 'TGLVector', 'v2', 'TGLVector'], 'Float');
  179. TVectorCrossProductFunction.Create(SymbolTable, 'VectorCrossProduct',
  180. ['v1', 'TGLVector', 'v2', 'TGLVector'], 'TGLVector');
  181. TVectorNormalizeFunction.Create(SymbolTable, 'VectorNormalize',
  182. ['v', 'TGLVector'], 'TGLVector');
  183. TVectorTransformFunction.Create(SymbolTable, 'VectorTransform',
  184. ['v', 'TGLVector', 'm', 'TGLMatrix'], 'TGLVector');
  185. // Matrix function
  186. TInvertMatrixFunction.Create(SymbolTable, 'InvertMatrix',
  187. ['@mat', 'TGLMatrix'], '');
  188. TTransposeMatrixFunction.Create(SymbolTable, 'TransposeMatrix',
  189. ['@mat', 'TGLMatrix'], '');
  190. TMatrixMultiplyFunction.Create(SymbolTable, 'MatrixMultiply',
  191. ['m1', 'TGLMatrix', 'm2', 'TGLMatrix'], 'TGLMatrix');
  192. TCreateScaleMatrixFunction.Create(SymbolTable, 'CreateScaleMatrix',
  193. ['v', 'TGLVector'], 'TGLMatrix');
  194. TCreateTranslationMatrixFunction.Create(SymbolTable,
  195. 'CreateTranslationMatrix', ['v', 'TGLVector'], 'TGLMatrix');
  196. TCreateScaleAndTranslationMatrixFunction.Create(SymbolTable,
  197. 'CreateScaleAndTranslationMatrix', ['scale', 'TGLVector', 'offset',
  198. 'TGLVector'], 'TGLMatrix');
  199. TCreateRotationMatrixXFunction.Create(SymbolTable, 'CreateRotationMatrixX',
  200. ['angle', 'Float'], 'TGLMatrix');
  201. TCreateRotationMatrixYFunction.Create(SymbolTable, 'CreateRotationMatrixY',
  202. ['angle', 'Float'], 'TGLMatrix');
  203. TCreateRotationMatrixZFunction.Create(SymbolTable, 'CreateRotationMatrixZ',
  204. ['angle', 'Float'], 'TGLMatrix');
  205. TCreateRotationMatrixFunction.Create(SymbolTable, 'CreateRotationMatrix',
  206. ['anAxis', 'TGLVector', 'angle', 'Float'], 'TGLMatrix');
  207. end;
  208. constructor TdwsVectorGeometryUnit.Create(AOwner: TComponent);
  209. begin
  210. inherited;
  211. FUnitName := 'GLS.VectorGeometry';
  212. end;
  213. procedure TVectorMakeFunction.Execute;
  214. begin
  215. Info.Vars['Result'].Element([0]).Value := Info['x'];
  216. Info.Vars['Result'].Element([1]).Value := Info['y'];
  217. Info.Vars['Result'].Element([2]).Value := Info['z'];
  218. Info.Vars['Result'].Element([3]).Value := Info['w'];
  219. end;
  220. procedure TSetVectorFunction.Execute;
  221. begin
  222. Info.Vars['v'].Element([0]).Value := Info['x'];
  223. Info.Vars['v'].Element([1]).Value := Info['y'];
  224. Info.Vars['v'].Element([2]).Value := Info['z'];
  225. Info.Vars['v'].Element([3]).Value := Info['w'];
  226. end;
  227. procedure TVectorAddFunction.Execute;
  228. var
  229. v1, v2, vr: TGLVector;
  230. begin
  231. v1 := GetVectorFromInfo(Info.Vars['v1']);
  232. v2 := GetVectorFromInfo(Info.Vars['v2']);
  233. VectorAdd(v1, v2, vr);
  234. SetInfoFromVector(Info.Vars['Result'], vr);
  235. end;
  236. procedure TVectorSubtractFunction.Execute;
  237. var
  238. v1, v2, vr: TGLVector;
  239. begin
  240. v1 := GetVectorFromInfo(Info.Vars['v1']);
  241. v2 := GetVectorFromInfo(Info.Vars['v2']);
  242. VectorSubtract(v1, v2, vr);
  243. SetInfoFromVector(Info.Vars['Result'], vr);
  244. end;
  245. procedure TVectorScaleFunction.Execute;
  246. var
  247. v, vr: TGLVector;
  248. f: Single;
  249. begin
  250. v := GetVectorFromInfo(Info.Vars['v']);
  251. f := Info['f'];
  252. VectorScale(v, f, vr);
  253. SetInfoFromVector(Info.Vars['Result'], vr);
  254. end;
  255. procedure TCombineVectorFunction.Execute;
  256. var
  257. vr, v: TGLVector;
  258. f: Single;
  259. begin
  260. vr := GetVectorFromInfo(Info.Vars['vr']);
  261. v := GetVectorFromInfo(Info.Vars['v']);
  262. f := Info['f'];
  263. CombineVector(vr, v, f);
  264. SetInfoFromVector(Info.Vars['Result'], vr);
  265. Info.Vars['f'].Value := f;
  266. end;
  267. procedure TVectorCombineFunction.Execute;
  268. var
  269. v1, v2, vr: TGLVector;
  270. f1, f2: Single;
  271. begin
  272. v1 := GetVectorFromInfo(Info.Vars['v1']);
  273. v2 := GetVectorFromInfo(Info.Vars['v2']);
  274. f1 := Info['f1'];
  275. f2 := Info['f2'];
  276. VectorCombine(v1, v2, f1, f2, vr);
  277. SetInfoFromVector(Info.Vars['Result'], vr);
  278. end;
  279. procedure TVectorCombine3Function.Execute;
  280. var
  281. v1, v2, v3, vr: TGLVector;
  282. f1, f2, f3: Single;
  283. begin
  284. v1 := GetVectorFromInfo(Info.Vars['v1']);
  285. v2 := GetVectorFromInfo(Info.Vars['v2']);
  286. v3 := GetVectorFromInfo(Info.Vars['v3']);
  287. f1 := Info['f1'];
  288. f2 := Info['f2'];
  289. f3 := Info['f3'];
  290. VectorCombine3(v1, v2, v3, f1, f2, f3, vr);
  291. SetInfoFromVector(Info.Vars['Result'], vr);
  292. end;
  293. procedure TVectorDotProductFunction.Execute;
  294. var
  295. v1, v2: TGLVector;
  296. begin
  297. v1 := GetVectorFromInfo(Info.Vars['v1']);
  298. v2 := GetVectorFromInfo(Info.Vars['v2']);
  299. Info.Result := VectorDotProduct(v1, v2);
  300. end;
  301. procedure TVectorCrossProductFunction.Execute;
  302. var
  303. v1, v2, vr: TGLVector;
  304. begin
  305. v1 := GetVectorFromInfo(Info.Vars['v1']);
  306. v2 := GetVectorFromInfo(Info.Vars['v2']);
  307. VectorCrossProduct(v1, v2, vr);
  308. SetInfoFromVector(Info.Vars['Result'], vr);
  309. end;
  310. procedure TVectorNormalizeFunction.Execute;
  311. var
  312. v, vr: TGLVector;
  313. begin
  314. v := GetVectorFromInfo(Info.Vars['v']);
  315. vr := VectorNormalize(v);
  316. SetInfoFromVector(Info.Vars['Result'], vr);
  317. end;
  318. procedure TVectorTransformFunction.Execute;
  319. var
  320. v, vr: TGLVector;
  321. mat: TGLMatrix;
  322. begin
  323. v := GetVectorFromInfo(Info.Vars['v']);
  324. mat := GetMatrixFromInfo(Info.Vars['mat']);
  325. vr := VectorTransform(v, mat);
  326. SetInfoFromVector(Info.Vars['Result'], vr);
  327. end;
  328. procedure TInvertMatrixFunction.Execute;
  329. var
  330. mat: TGLMatrix;
  331. begin
  332. mat := GetMatrixFromInfo(Info.Vars['mat']);
  333. InvertMatrix(mat);
  334. SetInfoFromMatrix(Info.Vars['Result'], mat);
  335. end;
  336. procedure TTransposeMatrixFunction.Execute;
  337. var
  338. mat: TGLMatrix;
  339. begin
  340. mat := GetMatrixFromInfo(Info.Vars['mat']);
  341. TransposeMatrix(mat);
  342. SetInfoFromMatrix(Info.Vars['Result'], mat);
  343. end;
  344. procedure TMatrixMultiplyFunction.Execute;
  345. var
  346. m1, m2, mr: TGLMatrix;
  347. begin
  348. m1 := GetMatrixFromInfo(Info.Vars['m1']);
  349. m2 := GetMatrixFromInfo(Info.Vars['m2']);
  350. MatrixMultiply(m1, m2, mr);
  351. SetInfoFromMatrix(Info.Vars['Result'], mr);
  352. end;
  353. procedure TCreateScaleMatrixFunction.Execute;
  354. var
  355. v: TGLVector;
  356. mr: TGLMatrix;
  357. begin
  358. v := GetVectorFromInfo(Info.Vars['v']);
  359. mr := CreateScaleMatrix(v);
  360. SetInfoFromMatrix(Info.Vars['Result'], mr);
  361. end;
  362. procedure TCreateTranslationMatrixFunction.Execute;
  363. var
  364. v: TGLVector;
  365. mr: TGLMatrix;
  366. begin
  367. v := GetVectorFromInfo(Info.Vars['v']);
  368. mr := CreateTranslationMatrix(v);
  369. SetInfoFromMatrix(Info.Vars['Result'], mr);
  370. end;
  371. procedure TCreateScaleAndTranslationMatrixFunction.Execute;
  372. var
  373. scale, offset: TGLVector;
  374. mr: TGLMatrix;
  375. begin
  376. scale := GetVectorFromInfo(Info.Vars['scale']);
  377. offset := GetVectorFromInfo(Info.Vars['offset']);
  378. mr := CreateScaleAndTranslationMatrix(scale, offset);
  379. SetInfoFromMatrix(Info.Vars['Result'], mr);
  380. end;
  381. procedure TCreateRotationMatrixXFunction.Execute;
  382. var
  383. angle: Single;
  384. mr: TGLMatrix;
  385. begin
  386. angle := Info['angle'];
  387. mr := CreateRotationMatrixX(angle);
  388. SetInfoFromMatrix(Info.Vars['Result'], mr);
  389. end;
  390. procedure TCreateRotationMatrixYFunction.Execute;
  391. var
  392. angle: Single;
  393. mr: TGLMatrix;
  394. begin
  395. angle := Info['angle'];
  396. mr := CreateRotationMatrixY(angle);
  397. SetInfoFromMatrix(Info.Vars['Result'], mr);
  398. end;
  399. procedure TCreateRotationMatrixZFunction.Execute;
  400. var
  401. angle: Single;
  402. mr: TGLMatrix;
  403. begin
  404. angle := Info['angle'];
  405. mr := CreateRotationMatrixZ(angle);
  406. SetInfoFromMatrix(Info.Vars['Result'], mr);
  407. end;
  408. procedure TCreateRotationMatrixFunction.Execute;
  409. var
  410. angle: Single;
  411. anAxis: TGLVector;
  412. mr: TGLMatrix;
  413. begin
  414. anAxis := GetVectorFromInfo(Info.Vars['anAxis']);
  415. angle := Info['angle'];
  416. mr := CreateRotationMatrix(anAxis, angle);
  417. SetInfoFromMatrix(Info.Vars['Result'], mr);
  418. end;
  419. end.