Unit1.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "GLGeomObjects"
  12. #pragma link "GLGraph"
  13. #pragma link "GLObjects"
  14. #pragma link "GLScene"
  15. #pragma link "GLWin32Viewer"
  16. #pragma resource "*.dfm"
  17. TForm1 *Form1;
  18. //---------------------------------------------------------------------------
  19. __fastcall TForm1::TForm1(TComponent* Owner)
  20. : TForm(Owner)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::FormCreate(TObject *Sender)
  25. {
  26. Navigator = new TGLSmoothNavigator (this);
  27. Navigator->AngleLock = false;
  28. Navigator->AutoUpdateObject = false;
  29. Navigator->InvertHorizontalSteeringWhenUpsideDown = true;
  30. Navigator->MoveUpWhenMovingForward = true;
  31. Navigator->UseVirtualUp = true;
  32. Navigator->VirtualUp->AsAffineVector = YVector;
  33. Navigator->MovingObject = GLCamera1;
  34. Navigator->InertiaParams->MovementAcceleration = 7;
  35. Navigator->InertiaParams->MovementInertia = 200;
  36. Navigator->InertiaParams->MovementSpeed = 200;
  37. Navigator->InertiaParams->TurnInertia = 150;
  38. Navigator->InertiaParams->TurnSpeed = 40;
  39. Navigator->InertiaParams->TurnMaxAngle = 0.5;
  40. Navigator->MoveAroundParams->TargetObject = GLArrowLine1;
  41. UI = new TGLSmoothUserInterface(this);
  42. // UI.AutoUpdateMouse = false;
  43. UI->SmoothNavigator = Navigator;
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TForm1::CheckControls(double DeltaTime, double newTime)
  47. {
  48. bool NeedToAccelerate;
  49. NeedToAccelerate = IsKeyDown(VK_SHIFT);
  50. Navigator->StrafeVertical(IsKeyDown('F'), IsKeyDown('R'), DeltaTime, NeedToAccelerate);
  51. Navigator->MoveForward(IsKeyDown('W'), IsKeyDown('S'), DeltaTime, NeedToAccelerate);
  52. Navigator->StrafeHorizontal(IsKeyDown('D'), IsKeyDown('A'), DeltaTime, NeedToAccelerate);
  53. // GetCursorPos(RealPos);
  54. UI->MouseLook(/*RealPos, */DeltaTime);
  55. // if UI.MouseLookActive then
  56. // SetCursorPos(Round(UI.OriginalMousePos.X), Round(UI.OriginalMousePos.Y));
  57. }
  58. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  59. const double newTime)
  60. {
  61. GLSceneViewer1->Invalidate();
  62. if (UI->MouseLookActive)
  63. CheckControls(deltaTime, newTime);
  64. else
  65. {
  66. if (ShiftState.Contains(ssRight) && ShiftState.Contains(ssLeft))
  67. {
  68. Navigator->MoveAroundTarget(0, 0, deltaTime);
  69. Navigator->AdjustDistanceToTarget(yy - NewYY, deltaTime);
  70. }
  71. else
  72. if (ShiftState.Contains(ssRight) || ShiftState.Contains(ssLeft))
  73. {
  74. Navigator->MoveAroundTarget(yy - NewYY, xx - NewXX, deltaTime);
  75. Navigator->AdjustDistanceToTarget(0, deltaTime);
  76. }
  77. else
  78. {
  79. Navigator->MoveAroundTarget(0, 0, deltaTime);
  80. Navigator->AdjustDistanceToTarget(0, deltaTime);
  81. }
  82. xx = NewXX;
  83. yy = NewYY;
  84. }
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TForm1::FPSTimerTimer(TObject *Sender)
  88. {
  89. Caption = "Smooth Navigator - " + GLSceneViewer1->FramesPerSecondText();
  90. Navigator->AutoScaleParameters(GLSceneViewer1->FramesPerSecond());
  91. GLSceneViewer1->ResetPerformanceMonitor();
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TForm1::MouseLookCheckBoxClick(TObject *Sender)
  95. {
  96. if (MouseLookCheckBox->Checked)
  97. {
  98. GLCamera1->TargetObject = NULL;
  99. GLCamera1->PointTo(GLArrowLine1, YHmgVector);
  100. UI->MouseLookActive = true;
  101. // GetCursorPos(RealPos);
  102. // UI->OriginalMousePos->SetPoint2D(RealPos->X, RealPos->Y);
  103. // ShowCursor(false);
  104. }
  105. else
  106. {
  107. UI->MouseLookActive = false;
  108. // ShowCursor(true);
  109. GLCamera1->Up->SetVector(0, 1, 0);
  110. GLCamera1->TargetObject = GLArrowLine1;
  111. }
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall TForm1::FormKeyPress(TObject *Sender, System::WideChar &Key)
  115. {
  116. if (Key == Char(VK_SPACE))
  117. MouseLookCheckBoxClick(Sender);
  118. if (Key == Char(VK_ESCAPE))
  119. Close();
  120. }
  121. //---------------------------------------------------------------------------
  122. void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
  123. {
  124. GLSceneViewer1->Enabled = false;
  125. GLCadencer1->Enabled = false;
  126. FPSTimer->Enabled = false;
  127. delete UI;
  128. delete Navigator;
  129. GLShowCursor(true);
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TForm1::RadioButton6Click(TObject *Sender)
  133. {
  134. GLCadencer1->FixedDeltaTime = 0;
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TForm1::RadioButton7Click(TObject *Sender)
  138. {
  139. GLCadencer1->FixedDeltaTime = 0.01;
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TForm1::RadioButton8Click(TObject *Sender)
  143. {
  144. GLCadencer1->FixedDeltaTime = 0.1;
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  148. int X, int Y)
  149. {
  150. ShiftState = Shift;
  151. NewXX = X;
  152. NewYY = Y;
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  156. TShiftState Shift, int X, int Y)
  157. {
  158. xx = X;
  159. yy = Y;
  160. }
  161. //---------------------------------------------------------------------------
  162. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  163. TPoint &MousePos, bool &Handled)
  164. {
  165. // (WheelDelta / Abs(WheelDelta) is used to deternime the sign.
  166. Navigator->AdjustDistanceParams->AddImpulse((WheelDelta / abs(WheelDelta)));
  167. }
  168. //---------------------------------------------------------------------------