fTorque.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. unit fTorque;
  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.Utils,
  27. GLS.Mesh,
  28. GLS.VectorFileObjects;
  29. type
  30. TFormTorque = class(TForm)
  31. GLSceneViewer1: TGLSceneViewer;
  32. GLScene1: TGLScene;
  33. GLCamera1: TGLCamera;
  34. GLLightSource1: TGLLightSource;
  35. DummyCube1: TGLDummyCube;
  36. GLCadencer1: TGLCadencer;
  37. GLBitmapFont1: TGLBitmapFont;
  38. Panel1: TPanel;
  39. PanelBottom: TPanel;
  40. CheckBox1: TCheckBox;
  41. lHexahedron: TLabel;
  42. lDodecahedron: TLabel;
  43. lOctagedron: TLabel;
  44. lTetrahedron: TLabel;
  45. lIcosahedron: TLabel;
  46. Tetrahedron: TGLTetrahedron;
  47. Octahedron: TGLOctahedron;
  48. Dodecahedron: TGLDodecahedron;
  49. Cube: TGLCube;
  50. Icosahedron: TGLIcosahedron;
  51. Hexahedron: TGLHexahedron;
  52. Torus: TGLTorus;
  53. Teapot: TGLTeapot;
  54. Superellipsoid: TGLSuperellipsoid;
  55. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  56. X, Y: Integer);
  57. procedure FormCreate(Sender: TObject);
  58. procedure CheckBox1Click(Sender: TObject);
  59. procedure GLCadencer1Progress(Sender: TObject;
  60. const deltaTime, newTime: Double);
  61. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  62. Shift: TShiftState; X, Y: Integer);
  63. private
  64. lastTime: Double;
  65. pickedObject: TGLBaseSceneObject;
  66. public
  67. //
  68. end;
  69. var
  70. FormTorque: TFormTorque;
  71. implementation
  72. {$R *.DFM}
  73. procedure TFormTorque.FormCreate(Sender: TObject);
  74. begin
  75. // Load the font bitmap from media dir
  76. SetGLSceneMediaDir();
  77. GLBitmapFont1.Glyphs.LoadFromFile('darkgold_font.bmp');
  78. // Initialize last time
  79. lastTime := Now * 3600 * 24;
  80. // Initialize rotation dampings...
  81. // ...using properties...
  82. with GetOrCreateInertia(Cube.Behaviours).RotationDamping do
  83. begin
  84. Constant := 1;
  85. Linear := 1;
  86. Quadratic := 0;
  87. end;
  88. // ...using helper function on the TGLBehaviours...
  89. GetOrCreateInertia(Tetrahedron.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  90. // ...or using helper function directly on the TGLBaseSceneObject
  91. GetOrCreateInertia(Octahedron.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  92. GetOrCreateInertia(Hexahedron.Behaviours).RotationDamping.SetDamping(5, 0, 0.01);
  93. GetOrCreateInertia(Dodecahedron.Behaviours).RotationDamping.SetDamping(10, 0, 0.01);
  94. GetOrCreateInertia(Icosahedron.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  95. GetOrCreateInertia(Torus.Behaviours).RotationDamping.SetDamping(10, 0, 0.01);
  96. GetOrCreateInertia(Superellipsoid.Behaviours).RotationDamping.SetDamping(10, 0, 0.01);
  97. GetOrCreateInertia(Teapot.Behaviours).RotationDamping.SetDamping(0, 0, 0.01);
  98. end;
  99. procedure TFormTorque.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  100. Shift: TShiftState; X, Y: Integer);
  101. var
  102. pickedObject: TGLCustomSceneObject;
  103. oldColor: TColorVector;
  104. rci: TGLRenderContextInfo;
  105. begin
  106. // if an object is picked...
  107. pickedObject := (GLSceneViewer1.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject);
  108. if Assigned(pickedObject) then
  109. begin
  110. oldColor := pickedObject.Material.FrontProperties.Emission.Color;
  111. //...turn it to yellow and show its name
  112. pickedObject.Material.FrontProperties.Emission.Color := clrYellow;
  113. ShowMessage('You clicked the ' + pickedObject.Name);
  114. pickedObject.BuildList(rci);
  115. /// HUDText.Text := 'Calculated Volume:+ '#13#10 + 'Vertices:'#13#10#13#10 + 'Faces:'#13#10#13#10 + 'Edges:';
  116. pickedObject.Material.FrontProperties.Emission.Color := oldColor;
  117. end;
  118. end;
  119. procedure TFormTorque.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  120. X, Y: Integer);
  121. begin
  122. // Mouse moved, get what's underneath
  123. pickedObject := GLSceneViewer1.Buffer.GetPickedObject(X, Y);
  124. end;
  125. procedure TFormTorque.GLCadencer1Progress(Sender: TObject;
  126. const deltaTime, newTime: Double);
  127. begin
  128. // apply some "torque" to the pickedObject if any
  129. if Assigned(pickedObject) then
  130. GetOrCreateInertia(pickedObject).ApplyTorque(deltaTime, 200, 0, 0);
  131. GLSceneViewer1.Invalidate;
  132. end;
  133. procedure TFormTorque.CheckBox1Click(Sender: TObject);
  134. var
  135. i: Integer;
  136. mass: Single;
  137. begin
  138. if CheckBox1.Checked then
  139. mass := 2
  140. else
  141. mass := 1;
  142. // all our objects are child of the DummyCube1
  143. for i := 0 to DummyCube1.Count - 1 do
  144. GetOrCreateInertia(DummyCube1.Children[i]).mass := mass;
  145. end;
  146. end.