Unit1.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLBaseClasses"
  9. #pragma link "GLCadencer"
  10. #pragma link "GLCoordinates"
  11. #pragma link "GLCrossPlatform"
  12. #pragma link "GLObjects"
  13. #pragma link "GLScene"
  14. #pragma link "GLTeapot"
  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::FormKeyPress(TObject *Sender, System::WideChar &Key)
  25. {
  26. switch (Key) {
  27. case '7' : Teapot1->RotateAbsolute(-15, 0, 0); break;
  28. case '9' : Teapot1->RotateAbsolute(+15, 0, 0); break;
  29. case '4' : Teapot1->RotateAbsolute( 0,-15, 0); break;
  30. case '6' : Teapot1->RotateAbsolute( 0,+15, 0); break;
  31. case '1' : Teapot1->RotateAbsolute( 0, 0,-15); break;
  32. case '3' : Teapot1->RotateAbsolute( 0, 0,+15); break;
  33. default: ;
  34. }
  35. }
  36. //---------------------------------------------------------------------------
  37. void __fastcall TForm1::RadioGroup1Click(TObject *Sender)
  38. {
  39. switch (RadioGroup1->ItemIndex) {
  40. case 0: GLCamera1->CameraStyle = csPerspective; break;
  41. case 1: GLCamera1->CameraStyle = csInfinitePerspective; break;
  42. case 2: GLCamera1->CameraStyle = csPerspectiveKeepFOV; break;
  43. case 3: GLCamera1->CameraStyle = csCustom; break;
  44. default: ;
  45. }
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TForm1::RadioGroup2Click(TObject *Sender)
  49. {
  50. GLCamera1->KeepFOVMode = TGLCameraKeepFOVMode(RadioGroup2->ItemIndex);
  51. }
  52. //---------------------------------------------------------------------------
  53. void __fastcall TForm1::GLCamera1CustomPerspective(const TRectangle &viewport, int width,
  54. int height, int DPI, float &viewPortRadius)
  55. {
  56. Glvectorgeometry::TMatrix Mat;
  57. Mat = CreatePerspectiveMatrix(GLCamera1->GetFieldOfView(Width)/4,
  58. Width / Height, GLCamera1->NearPlaneBias, GLCamera1->DepthOfView);
  59. Mat = MatrixMultiply(Mat, CreateRotationMatrixZ(a));
  60. *CurrentGLContext()->PipelineTransformation->ProjectionMatrix = Mat;
  61. }
  62. //---------------------------------------------------------------------------
  63. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  64. const double newTime)
  65. {
  66. a = M_PI * sin(newTime) / 18;
  67. GLSceneViewer1->Invalidate();
  68. }
  69. //---------------------------------------------------------------------------
  70. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  71. TShiftState Shift, int X, int Y)
  72. {
  73. // store mouse coordinates when a button went down
  74. mdx = X; mdy = Y;
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  78. int X, int Y)
  79. {
  80. int dx, dy;
  81. Glvectorgeometry::TVector v;
  82. // calculate delta since last move or last mousedown
  83. dx = mdx-X; dy = mdy-Y;
  84. mdx = X; mdy = Y;
  85. if (Shift.Contains(ssLeft))
  86. if (Shift.Contains(ssShift))
  87. // right button with shift rotates the teapot
  88. // (rotation happens around camera's axis)
  89. GLCamera1->RotateObject(Teapot1, dy, dx);
  90. else
  91. // right button without shift changes camera angle
  92. // (we're moving around the parent and target dummycube)
  93. GLCamera1->MoveAroundTarget(dy, dx);
  94. else
  95. if (Shift.Contains(ssRight))
  96. {
  97. // left button moves our target and parent dummycube
  98. v = GLCamera1->ScreenDeltaToVectorXY(dx, -dy,
  99. 0.12*GLCamera1->DistanceToTarget()/GLCamera1->FocalLength);
  100. DummyCube1->Position->Translate(v);
  101. // notify camera that its position/target has been changed
  102. GLCamera1->TransformationChanged();
  103. }
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  107. TPoint &MousePos, bool &Handled)
  108. {
  109. // Note that 1 wheel-step induces a WheelDelta of 120,
  110. // this code adjusts the distance to target with a 10% per wheel-step ratio
  111. GLCamera1->AdjustDistanceToTarget(Power((float)1.1, WheelDelta/120));
  112. }
  113. //---------------------------------------------------------------------------