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