Unit1.cpp 8.2 KB

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