Unit1.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "GLCadencer"
  9. #pragma link "GLCoordinates"
  10. #pragma link "GLCrossPlatform"
  11. #pragma link "GLGraph"
  12. #pragma link "GLMaterial"
  13. #pragma link "GLObjects"
  14. #pragma link "GLScene"
  15. #pragma link "GLVectorFileObjects"
  16. #pragma link "GLWin32Viewer"
  17. #pragma link "GLFileSMD"
  18. #pragma resource "*.dfm"
  19. TForm1 *Form1;
  20. //---------------------------------------------------------------------------
  21. __fastcall TForm1::TForm1(TComponent* Owner)
  22. : TForm(Owner)
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26. void __fastcall TForm1::FormCreate(TObject *Sender)
  27. {
  28. SetGLSceneMediaDir();
  29. // We load the SMD model here
  30. // Note the actor was linked to a material library, and textures are loaded
  31. // automatically (4 textures are used by this model)
  32. //
  33. // Kind thanks to ~A.u.s.t.i.n. & Neal 'Guplik' Corbett for the model
  34. // and allowing its use ;)
  35. Actor1->LoadFromFile("trinityRage.smd");
  36. // Now we load the walk & run animations and "fix" their translation
  37. // (HL walk/run animations have a built-in "slide" that we don't want here)
  38. Actor1->AddDataFromFile("walk.smd");
  39. Actor1->Animations->Items[1]->MakeSkeletalTranslationStatic();
  40. Actor1->AddDataFromFile("run.smd");
  41. Actor1->Animations->Items[2]->MakeSkeletalTranslationStatic();
  42. // Then load the two jumps
  43. Actor1->AddDataFromFile("long_jump.smd");
  44. Actor1->AddDataFromFile("jump.smd");
  45. // And the 'look_left_right' blending animations, that we immediately
  46. // assign to the controler. The MakeSkeletalRotationDelta removes absolute
  47. // information from the SMD (which HL may use, but GLScene doesn't)
  48. Actor1->AddDataFromFile("look_left_right.smd");
  49. Actor1->Animations->Items[5]->MakeSkeletalRotationDelta();
  50. AnimationControler1->AnimationName = "look_left_right";
  51. // Skeleton visible, and start with walk animation
  52. // (pseudo-animation 0 is for the static model in its default attitude)
  53. Actor1->OverlaySkeleton = true;
  54. baseAnimation = "walk";
  55. Actor1->SwitchToAnimation(baseAnimation);
  56. }
  57. //---------------------------------------------------------------------------
  58. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  59. TPoint &MousePos, bool &Handled)
  60. {
  61. GLCamera1 = GLSceneViewer1->Camera;
  62. GLCamera1->AdjustDistanceToTarget(Power(1.1, WheelDelta / 120));
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TForm1::RBWalkClick(TObject *Sender)
  66. {
  67. // user requested 'walk'
  68. baseAnimation = "walk";
  69. Actor1->SwitchToAnimation(baseAnimation, true);
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TForm1::RBRunClick(TObject *Sender)
  73. {
  74. // user requested 'run'
  75. baseAnimation = "run";
  76. Actor1->SwitchToAnimation(baseAnimation, true);
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TForm1::BULongJumpClick(TObject *Sender)
  80. {
  81. // Smoothly switch to Long Jump
  82. Actor1->SwitchToAnimation(3, true);
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TForm1::BUHighJumpClick(TObject *Sender)
  86. {
  87. // Smoothly switch to High Jump
  88. Actor1->SwitchToAnimation(4, true);
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TForm1::Actor1EndFrameReached(TObject *Sender)
  92. {
  93. // If we weren't walking, switch back to walk
  94. if (Actor1->CurrentAnimation() != baseAnimation)
  95. Actor1->SwitchToAnimation(baseAnimation, true);
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TForm1::CBBlendClick(TObject *Sender)
  99. {
  100. // Enable/disable blending by binding or unbinding the animation controler
  101. // to the actor
  102. if (CBBlend->Checked)
  103. {
  104. AnimationControler1->Actor = Actor1;
  105. TrackBar1Change(this);
  106. }
  107. else
  108. AnimationControler1->Actor = NULL;
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall TForm1::TrackBar1Change(TObject *Sender)
  112. {
  113. // Blending along the controler's animation is just a matter of adjusting
  114. // the ratio, with 0 = first frame and 1 = last frame.
  115. AnimationControler1->Ratio = TrackBar1->Position*0.01;
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall TForm1::CheckBox1Click(TObject *Sender)
  119. {
  120. Actor1->OverlaySkeleton = CheckBox1->Checked;
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  124. TShiftState Shift, int X, int Y)
  125. {
  126. mx = X; my = Y;
  127. }
  128. //---------------------------------------------------------------------------
  129. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  130. int X, int Y)
  131. {
  132. if (Shift.Contains(ssLeft) || Shift.Contains(ssRight))
  133. {
  134. GLCamera1->MoveAroundTarget(my-Y, mx-X);
  135. mx = X; my = Y;
  136. }
  137. }
  138. //---------------------------------------------------------------------------
  139. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  140. {
  141. LabelFPS->Caption = Format("%.1f FPS",
  142. ARRAYOFCONST ((GLSceneViewer1->FramesPerSecond())));
  143. GLSceneViewer1->ResetPerformanceMonitor();
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  147. const double newTime)
  148. {
  149. GLScene1->NotifyChange(NULL);
  150. }
  151. //---------------------------------------------------------------------------