fObjmoveC.cpp 5.8 KB

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