fTorqueD.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. unit fTorqueD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.Classes,
  6. System.SysUtils,
  7. Vcl.Forms,
  8. Vcl.StdCtrls,
  9. Vcl.ExtCtrls,
  10. Vcl.Controls,
  11. Vcl.Dialogs,
  12. Vcl.Imaging.jpeg,
  13. GLS.Objects,
  14. GLS.Scene,
  15. GLS.PersistentClasses,
  16. GLS.Cadencer,
  17. GLS.SceneViewer,
  18. GLS.Coordinates,
  19. GLS.BaseClasses,
  20. GLS.Behaviours,
  21. GLS.HUDObjects,
  22. GLS.Color,
  23. GLS.BitmapFont,
  24. GLS.GeomObjects,
  25. GLS.RenderContextInfo,
  26. GLS.Mesh,
  27. GLS.VectorFileObjects;
  28. type
  29. TFormTorque = class(TForm)
  30. GLSceneViewer1: TGLSceneViewer;
  31. GLScene1: TGLScene;
  32. GLCamera1: TGLCamera;
  33. GLLightSource1: TGLLightSource;
  34. DummyCube1: TGLDummyCube;
  35. GLCadencer1: TGLCadencer;
  36. GLBitmapFont1: TGLBitmapFont;
  37. Panel1: TPanel;
  38. PanelBottom: TPanel;
  39. CheckBox1: TCheckBox;
  40. lHexahedron: TLabel;
  41. lDodecahedron: TLabel;
  42. lOctagedron: TLabel;
  43. lTetrahedron: TLabel;
  44. lIcosahedron: TLabel;
  45. Tetrahedron: TGLTetrahedron;
  46. Octahedron: TGLOctahedron;
  47. Dodecahedron: TGLDodecahedron;
  48. Cube: TGLCube;
  49. Icosahedron: TGLIcosahedron;
  50. Hexahedron: TGLHexahedron;
  51. Torus: TGLTorus;
  52. Teapot: TGLTeapot;
  53. Superellipsoid: TGLSuperellipsoid;
  54. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  55. X, Y: Integer);
  56. procedure FormCreate(Sender: TObject);
  57. procedure CheckBox1Click(Sender: TObject);
  58. procedure GLCadencer1Progress(Sender: TObject;
  59. const deltaTime, newTime: Double);
  60. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  61. Shift: TShiftState; X, Y: Integer);
  62. private
  63. lastTime: Double;
  64. pickedObject: TGLBaseSceneObject;
  65. public
  66. //
  67. end;
  68. var
  69. FormTorque: TFormTorque;
  70. implementation
  71. {$R *.DFM}
  72. procedure TFormTorque.FormCreate(Sender: TObject);
  73. begin
  74. // Initialize last time
  75. lastTime := Now * 3600 * 24;
  76. // Initialize rotation dampings...
  77. // ...using properties...
  78. with GetOrCreateInertia(Cube.Behaviours).RotationDamping do
  79. begin
  80. Constant := 1;
  81. Linear := 1;
  82. Quadratic := 0;
  83. end;
  84. // ...using helper function on the TGLBehaviours...
  85. GetOrCreateInertia(Tetrahedron.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  86. // ...or using helper function directly on the TGLBaseSceneObject
  87. GetOrCreateInertia(Octahedron.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  88. GetOrCreateInertia(Hexahedron.Behaviours).RotationDamping.SetDamping(5, 0, 0.01);
  89. GetOrCreateInertia(Dodecahedron.Behaviours).RotationDamping.SetDamping(10, 0, 0.01);
  90. GetOrCreateInertia(Icosahedron.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  91. GetOrCreateInertia(Torus.Behaviours).RotationDamping.SetDamping(10, 0, 0.01);
  92. GetOrCreateInertia(Superellipsoid.Behaviours).RotationDamping.SetDamping(10, 0, 0.01);
  93. GetOrCreateInertia(Teapot.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  94. end;
  95. procedure TFormTorque.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  96. Shift: TShiftState; X, Y: Integer);
  97. var
  98. pickedObject: TGLCustomSceneObject;
  99. oldColor: TGLColorVector;
  100. rci: TGLRenderContextInfo;
  101. begin
  102. // if an object is picked...
  103. pickedObject := (GLSceneViewer1.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject);
  104. if Assigned(pickedObject) then
  105. begin
  106. oldColor := pickedObject.Material.FrontProperties.Emission.Color;
  107. //...turn it to yellow and show its name
  108. // pickedObject.Material.FrontProperties.Emission.Color := clrYellow;
  109. ShowMessage('You clicked the ' + pickedObject.Name);
  110. pickedObject.BuildList(rci);
  111. pickedObject.Material.FrontProperties.Emission.Color := oldColor;
  112. end;
  113. end;
  114. procedure TFormTorque.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  115. X, Y: Integer);
  116. begin
  117. // Mouse moved, get what's underneath
  118. pickedObject := GLSceneViewer1.Buffer.GetPickedObject(X, Y);
  119. end;
  120. procedure TFormTorque.GLCadencer1Progress(Sender: TObject;
  121. const deltaTime, newTime: Double);
  122. begin
  123. // apply some "torque" to the pickedObject if any
  124. if Assigned(pickedObject) then
  125. GetOrCreateInertia(pickedObject).ApplyTorque(deltaTime, 200, 0, 0);
  126. GLSceneViewer1.Invalidate;
  127. end;
  128. procedure TFormTorque.CheckBox1Click(Sender: TObject);
  129. var
  130. i: Integer;
  131. mass: Single;
  132. begin
  133. if CheckBox1.Checked then
  134. mass := 2
  135. else
  136. mass := 1;
  137. // all our objects are child of the DummyCube1
  138. for i := 0 to DummyCube1.Count - 1 do
  139. GetOrCreateInertia(DummyCube1.Children[i]).mass := mass;
  140. end;
  141. end.