Unit1.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.BaseClasses"
  8. #pragma link "GLS.Cadencer"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLS.Graph"
  11. #pragma link "GLS.Objects"
  12. #pragma link "Physics.ODEManager"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma resource "*.dfm"
  16. TForm1 *Form1;
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19. : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::FormActivate(TObject *Sender)
  24. {
  25. ComboBox1->ItemIndex = 0;
  26. ComboBox2->ItemIndex = 0;
  27. CheckBox2->Enabled = false;
  28. Label2->Enabled = false;
  29. TrackBar1->Enabled = false;
  30. ComboBox2Change(Sender);
  31. }
  32. //---------------------------------------------------------------------------
  33. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  34. TShiftState Shift, int X, int Y)
  35. {
  36. mx = X;
  37. my = Y;
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  41. int X, int Y)
  42. {
  43. if (Shift.Contains(ssLeft))
  44. GLCamera1->MoveAroundTarget(my-Y,mx-X);
  45. mx = X;
  46. my = Y;
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TForm1::SpawnClick(TObject *Sender)
  50. {
  51. switch (ComboBox1->ItemIndex)
  52. {
  53. case 0 : DoSphere(); break;
  54. case 1 : DoBox(); break;
  55. case 2 : DoCapsule(); break;
  56. case 3 : DoCylinder(); break;
  57. // CONE IS CURRENTLY UNSUPPOETED FOR ODE 0.9
  58. // case 4 : DoCone(); break;
  59. }
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  63. const double newTime)
  64. {
  65. GLODEManager1->Step(deltaTime);
  66. }
  67. //---------------------------------------------------------------------------
  68. void TForm1::DoSphere(void)
  69. {
  70. TGLSphere *sphere;
  71. TGLODEDynamic *dyn;
  72. sphere = (TGLSphere *)(ODEObjects->AddNewChild(__classid(TGLSphere)));
  73. sphere->Position->SetPoint(5*Random()-2.5,2,5*Random()-2.5);
  74. sphere->Radius = 0.3*(Random()+1);
  75. sphere->Material->FrontProperties->Ambient->Color = clrRed;
  76. dyn = new TGLODEDynamic(sphere->Behaviours);
  77. dyn->Manager = GLODEManager1;
  78. ((TGLODEElementSphere *)(dyn->AddNewElement(__classid(TGLODEElementSphere))))->Radius = sphere->Radius;
  79. }
  80. void TForm1::DoBox(void)
  81. {
  82. TGLCube *cube;
  83. TGLODEDynamic *dyn;
  84. TGLODEElementBox *elem;
  85. cube = (TGLCube *)(ODEObjects->AddNewChild(__classid(TGLCube)));
  86. cube->Position->SetPoint(5*Random()-2.5,2,5*Random()-2.5);
  87. cube->CubeWidth = 0.5*(Random()+1);
  88. cube->CubeHeight = 0.5*(Random()+1);
  89. cube->CubeDepth = 0.5*(Random()+1);
  90. cube->Material->FrontProperties->Ambient->Color = clrGreen;
  91. dyn = new TGLODEDynamic(cube->Behaviours);
  92. dyn->Manager = GLODEManager1;
  93. elem = (TGLODEElementBox *) dyn->AddNewElement(__classid(TGLODEElementBox));
  94. elem->BoxWidth = cube->CubeWidth;
  95. elem->BoxHeight = cube->CubeHeight;
  96. elem->BoxDepth = cube->CubeDepth;
  97. }
  98. void TForm1::DoCapsule(void)
  99. {
  100. TGLCylinder *capsule;
  101. TGLODEDynamic *dyn;
  102. TGLSphere *sphere;
  103. TGLODEElementCapsule *elem;
  104. capsule = (TGLCylinder *)(ODEObjects->AddNewChild(__classid(TGLCylinder)));
  105. capsule->Position->SetPoint(5*Random()-2.5,2,5*Random()-2.5);
  106. capsule->BottomRadius = 0.25*(Random()+1);
  107. capsule->TopRadius = capsule->BottomRadius;
  108. capsule->Height = Random()+1;
  109. capsule->Parts << cySides;
  110. capsule->Material->FrontProperties->Ambient->Color = clrBlue;
  111. sphere = (TGLSphere *) capsule->AddNewChild(__classid(TGLSphere));
  112. sphere->Position->Y = 0.5*capsule->Height;
  113. sphere->Radius = capsule->BottomRadius;
  114. sphere->Bottom = 0;
  115. sphere = (TGLSphere *) capsule->AddNewChild(__classid(TGLSphere));
  116. sphere->Position->Y = -0.5*capsule->Height;
  117. sphere->Radius = capsule->BottomRadius;
  118. sphere->Top = 0;
  119. dyn = new TGLODEDynamic(capsule->Behaviours);
  120. dyn->Manager = GLODEManager1;
  121. elem = (TGLODEElementCapsule *) dyn->AddNewElement(__classid(TGLODEElementCapsule));
  122. elem->Radius = capsule->BottomRadius;
  123. elem->Length = capsule->Height;
  124. elem->Direction->SetVector(0,1,0);
  125. elem->Up->SetVector(0,0,1);
  126. }
  127. void TForm1::DoCylinder(void)
  128. {
  129. TGLCylinder *cylinder;
  130. TGLODEDynamic *dyn;
  131. TGLODEElementCylinder *elem;
  132. cylinder = (TGLCylinder *)(ODEObjects->AddNewChild(__classid(TGLCylinder)));
  133. cylinder->Position->SetPoint(5*Random()-2.5,2,5*Random()-2.5);
  134. cylinder->BottomRadius = 0.25*(Random()+1);
  135. cylinder->TopRadius = cylinder->BottomRadius;
  136. cylinder->Height = Random()+1;
  137. cylinder->Material->FrontProperties->Ambient->Color = clrYellow;
  138. dyn = new TGLODEDynamic(cylinder->Behaviours);
  139. dyn->Manager = GLODEManager1;
  140. elem = (TGLODEElementCylinder *) dyn->AddNewElement(__classid(TGLODEElementCylinder));
  141. elem->Radius = cylinder->BottomRadius;
  142. elem->Length = cylinder->Height;
  143. }
  144. // CONE IS CURRENTLY UNSUPPOETED FOR ODE 0.9
  145. //
  146. /*
  147. void TForm1::DoCone(void)
  148. {
  149. TGLCone *cone;
  150. TGLODEDynamic *dyn;
  151. TODEElementCone *elem;
  152. cone = (TGLCone *)(ODEObjects->AddNewChild(__classid(TGLCone)));
  153. cone->Position->SetPoint(5*Random()-2.5,2,5*Random()-2.5);
  154. cone->BottomRadius = 0.25*(Random()+1);
  155. cone->Height = Random()+1;
  156. dyn = new TGLODEDynamic(cone->Behaviours);
  157. dyn->Manager = GLODEManager1;
  158. elem = (TODEElementCone *) dyn->AddNewElement(__classid(TODEElementCone));
  159. elem->Radius = cone->BottomRadius;
  160. elem->Length = cone->Height;
  161. elem->Direction->SetVector(0,1,0);
  162. elem->Up->SetVector(0,0,1);
  163. elem->Position->SetPoint(0,-cone->Height/2,0);
  164. }
  165. */
  166. //---------------------------------------------------------------------------
  167. void __fastcall TForm1::GLHeightField1GetHeight(const float x, const float y, float &z,
  168. TVector4f &color, TTexPoint &texPoint)
  169. {
  170. z = 0.5*cos(x)*sin(y);
  171. }
  172. //---------------------------------------------------------------------------
  173. void __fastcall TForm1::CheckBox1Click(TObject *Sender)
  174. {
  175. GLODEManager1->Visible = CheckBox1->Checked;
  176. }
  177. //---------------------------------------------------------------------------
  178. void __fastcall TForm1::CheckBox2Click(TObject *Sender)
  179. {
  180. ((TGLODEHeightField *)(GLHeightField1->Behaviours->Behaviour[0]))->RenderContacts = CheckBox2->Checked;
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TForm1::ComboBox2Change(TObject *Sender)
  184. {
  185. if (ComboBox2->ItemIndex == 0)
  186. {
  187. GLPlane1->Visible = true;
  188. {
  189. GetOdeStatic(GLPlane1)->Manager = GLODEManager1;
  190. CheckBox2->Enabled = false;
  191. Label2->Enabled = false;
  192. TrackBar1->Enabled = false;
  193. }
  194. } else {
  195. GLPlane1->Visible = false;
  196. CheckBox2->Enabled = true;
  197. Label2->Enabled = true;
  198. TrackBar1->Enabled = true;
  199. GetOdeStatic(GLPlane1)->Manager = NULL;
  200. }
  201. if (ComboBox2->ItemIndex == 1)
  202. {
  203. GLHeightField1->Visible = true;
  204. CheckBox2->Enabled = true;
  205. Label2->Enabled = true;
  206. TrackBar1->Enabled = true;
  207. GetODEHeightField(GLHeightField1)->Manager = GLODEManager1;
  208. } else {
  209. GLHeightField1->Visible = false;
  210. CheckBox2->Enabled = false;
  211. Label2->Enabled = false;
  212. TrackBar1->Enabled = false;
  213. GetODEHeightField(GLHeightField1)->Manager = NULL;
  214. }
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TForm1::TrackBar1Change(TObject *Sender)
  218. {
  219. ((TGLODEHeightField *)(GLHeightField1->Behaviours->Behaviour[0]))->ContactResolution =
  220. 0.25+(float)(10-TrackBar1->Position)/20;
  221. }
  222. //---------------------------------------------------------------------------
  223. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  224. TPoint &MousePos, bool &Handled)
  225. {
  226. GLCamera1-> AdjustDistanceToTarget(Power(1.1, WheelDelta / 120.0));
  227. }
  228. //---------------------------------------------------------------------------