Unit1.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.BitmapFont"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLS.GeomObjects"
  11. #pragma link "GLS.HUDObjects"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SpaceText"
  15. #pragma link "GLS.SceneViewer"
  16. #pragma link "GLS.WindowsFont"
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19. const TColorVector
  20. SelectionColor[] = {0.243, 0.243, 0.243, 1.000};
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23. : TForm(Owner)
  24. {
  25. }
  26. //---------------------------------------------------------------------------
  27. void __fastcall TForm1::FormCreate(TObject *Sender)
  28. {
  29. UpdateHudText();
  30. }
  31. //---------------------------------------------------------------------------
  32. Gls::Vectorgeometry::TVector __fastcall TForm1::MouseWorldPos(int X, int Y)
  33. {
  34. Gls::Vectorgeometry::TVector v;
  35. Gls::Vectorgeometry::TVector Result;
  36. Y = Scn->Height - Y;
  37. if (CurrentPick)
  38. {
  39. SetVector(v, X, Y, 0);
  40. if (movingOnZ)
  41. Scn->Buffer->ScreenVectorIntersectWithPlaneXZ(
  42. v, CurrentPick->Position->Y, Result);
  43. else
  44. Scn->Buffer->ScreenVectorIntersectWithPlaneXY(
  45. v, CurrentPick->Position->Z, Result);
  46. }
  47. else
  48. SetVector(Result, NullVector);
  49. return Result;
  50. }
  51. //---------------------------------------------------------------------------
  52. void __fastcall TForm1::UpdateHudText()
  53. {
  54. TAffineVector objPos, winPos;
  55. if (CurrentPick)
  56. {
  57. SetVector(objPos, CurrentPick->AbsolutePosition);
  58. TopText->Text = Format(
  59. "New Object Position: Xn: %4.4f, Yn: %4.4f, Zn: %4.4f",
  60. ARRAYOFCONST ((objPos.X, objPos.Y, objPos.Z)));
  61. winPos = Scn->Buffer->WorldToScreen(objPos);
  62. ObjText->Visible = true;
  63. ObjText->Text = CurrentPick->Name;
  64. ObjText->Position->X = winPos.X + 10;
  65. ObjText->Position->Y = Scn->Height - winPos.Y + 10;
  66. }
  67. else
  68. {
  69. TopText->Text = "No selected object";
  70. ObjText->Visible = false;
  71. }
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TForm1::ProcessPick(TGLBaseSceneObject* pick)
  75. {
  76. if (pick)
  77. {
  78. // Only Cube1 and Cube2 can be selected
  79. if ((pick->Name != "Cube1") && (pick->Name != "Cube2"))
  80. pick = NULL;
  81. }
  82. if (pick != CurrentPick)
  83. {
  84. if (CurrentPick)
  85. {
  86. CurrentPick->ShowAxes = false;
  87. CurrentPick->Material->FrontProperties->Emission->Color = clrBlack;
  88. }
  89. CurrentPick = (TGLCustomSceneObject *)(pick);
  90. if (CurrentPick)
  91. {
  92. if (ShowAxes->Checked)
  93. CurrentPick->ShowAxes = true;
  94. //not translated CurrentPick->Material->FrontProperties->Emission->Color = SelectionColor;
  95. }
  96. }
  97. UpdateHudText();
  98. }
  99. //---------------------------------------------------------------------------
  100. void __fastcall TForm1::ScnMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  101. int X, int Y)
  102. {
  103. TGLBaseSceneObject* pick;
  104. movingOnZ = Shift.Contains(ssShift);
  105. // If an object is picked...
  106. pick = (Scn->Buffer->GetPickedObject(X, Y)); // as TGLCustomSceneObject);
  107. ProcessPick(pick);
  108. // store mouse pos
  109. if (CurrentPick)
  110. lastMouseWorldPos = MouseWorldPos(X, Y);
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TForm1::ScnMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
  114. {
  115. Gls::Vectorgeometry::TVector newPos;
  116. ScnMouseMoveCnt++;
  117. ////not translated Assert(ScnMouseMoveCnt < 2);
  118. if (Shift.Contains(ssLeft))
  119. {
  120. // handle hold/unhold of shift
  121. if (Shift.Contains(ssShift) != movingOnZ)
  122. {
  123. movingOnZ = (Shift.Contains(ssShift));
  124. lastMouseWorldPos = MouseWorldPos(X, Y);
  125. }
  126. newPos = MouseWorldPos(X, Y);
  127. if (CurrentPick && (VectorNorm(lastMouseWorldPos) != 0))
  128. CurrentPick->Position->Translate(VectorSubtract(newPos, lastMouseWorldPos));
  129. lastMouseWorldPos = newPos;
  130. UpdateHudText();
  131. }
  132. ScnMouseMoveCnt--;
  133. }
  134. //---------------------------------------------------------------------------
  135. void __fastcall TForm1::ShowAxesClick(TObject *Sender)
  136. {
  137. // Unselect all
  138. ProcessPick(NULL);
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  142. TPoint &MousePos, bool &Handled)
  143. {
  144. // Note that 1 wheel-step induces a WheelDelta of 120,
  145. // this code adjusts the distance to target with a 10% per wheel-step ratio
  146. if (WheelDelta != 0)
  147. GLCamera1->AdjustDistanceToTarget(Power(1.1, -WheelDelta / 120));
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TForm1::FormKeyPress(TObject *Sender, System::WideChar &Key)
  151. {
  152. switch (Key)
  153. {
  154. case '2':
  155. GLCamera1->MoveAroundTarget(3, 0); break;
  156. case '4':
  157. GLCamera1->MoveAroundTarget(0, -3);break;
  158. case '6':
  159. GLCamera1->MoveAroundTarget(0, 3);break;
  160. case '8':
  161. GLCamera1->MoveAroundTarget(-3, 0);break;
  162. case '-':
  163. GLCamera1->AdjustDistanceToTarget(1.1);break;
  164. case '+':
  165. GLCamera1->AdjustDistanceToTarget(1 / 1.1);break;
  166. default:
  167. ;
  168. }
  169. }
  170. //---------------------------------------------------------------------------
  171. void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
  172. {
  173. if (CurrentPick)
  174. {
  175. switch (Key)
  176. {
  177. case VK_UP:
  178. if (Shift.Contains(ssShift))
  179. CurrentPick->Translate(0, 0, 0.3);
  180. else
  181. CurrentPick->Translate(-0.3, 0, 0);
  182. break;
  183. case VK_DOWN:
  184. if (Shift.Contains(ssShift))
  185. CurrentPick->Translate(0, 0, -0.3);
  186. else
  187. CurrentPick->Translate(0.3, 0, 0);
  188. break;
  189. case VK_LEFT:
  190. CurrentPick->Translate(0, -0.3, 0); break;
  191. case VK_RIGHT:
  192. CurrentPick->Translate(0, 0.3, 0); break;
  193. default:
  194. ;
  195. }
  196. }
  197. }
  198. //---------------------------------------------------------------------------