fCameraC.cpp 4.3 KB

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