Unit1.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "GLAsyncTimer"
  9. #pragma link "GLBaseClasses"
  10. #pragma link "GLBitmapFont"
  11. #pragma link "GLCadencer"
  12. #pragma link "GLCoordinates"
  13. #pragma link "GLCrossPlatform"
  14. #pragma link "GLEParticleMasksManager"
  15. #pragma link "GLGeomObjects"
  16. #pragma link "GLMaterial"
  17. #pragma link "GLObjects"
  18. #pragma link "GLParticleFX"
  19. #pragma link "GLScene"
  20. #pragma link "GLWin32Viewer"
  21. #pragma link "GLWindowsFont"
  22. #pragma link "GLAsyncTimer"
  23. #pragma resource "*.dfm"
  24. TForm1 *Form1;
  25. //---------------------------------------------------------------------------
  26. __fastcall TForm1::TForm1(TComponent* Owner)
  27. : TForm(Owner)
  28. {
  29. PlaneHeights = 5;
  30. PlaneWidths = 5;
  31. PlaneDepths = 5;
  32. PlaneOffSets = 3;
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TForm1::RefreshMask()
  36. {
  37. TRect Rect;
  38. TGLLibMaterial *Mat;
  39. char Letter;
  40. int Depth;
  41. Letter = Edit2->Text[1];
  42. Depth = StrToIntDef(Edit1->Text, 50);
  43. XPlane->Position->X = -PlaneOffSets;
  44. XPlane->Height = PlaneHeights;
  45. XPlane->Width = PlaneWidths;
  46. YPlane->Position->Y = -PlaneOffSets;
  47. YPlane->Height = PlaneHeights;
  48. YPlane->Width = PlaneWidths;
  49. ZPlane->Position->Z = -PlaneOffSets;
  50. ZPlane->Height = PlaneHeights;
  51. ZPlane->Width = PlaneWidths;
  52. Rect.Left = 0;
  53. Rect.Top = 0;
  54. Rect.Bottom = XImage->Height;
  55. Rect.Right = XImage->Width;
  56. XImage->Canvas->Font->Name = "Arial";
  57. XImage->Canvas->Font->Size = 180;
  58. XImage->Canvas->Font->Color = clWhite;
  59. XImage->Canvas->Pen->Color = clBlack;
  60. XImage->Canvas->Pen->Style = psSolid;
  61. XImage->Canvas->Brush->Color = clBlack;
  62. XImage->Canvas->Brush->Style = bsSolid;
  63. XImage->Canvas->FillRect(Rect);
  64. XImage->Canvas->TextOut(Round((XImage->Width - XImage->Canvas->TextWidth(Letter)) / 2),
  65. Round((XImage->Height - XImage->Canvas->TextHeight(Letter)) / 2), Letter);
  66. Mat = MatLib->LibMaterialByName("XMask");
  67. ((TGLPersistentImage *)Mat->Material->Texture->Image)->Picture->Bitmap->Height = XImage->Height;
  68. ((TGLPersistentImage *)Mat->Material->Texture->Image)->Picture->Bitmap->Width = XImage->Width;
  69. ((TGLPersistentImage *)Mat->Material->Texture->Image)->Picture->Bitmap->Canvas->Draw(0, 0, XImage->Picture->Graphic);
  70. // this is a very recent implementation, the ability to generate other masks from 1 mask, so it satisfies
  71. // the requirements for the particle mask manager. useful for text and making basic shapes (cylinders etc)
  72. GLEParticleMasksManager1->ParticleMaskByName("mask")->GenerateMaskFromProjection(pptXMask, pptYMask, Depth);
  73. GLEParticleMasksManager1->ParticleMaskByName("mask")->GenerateMaskFromProjection(pptXMask, pptZMask, Depth);
  74. Mat = MatLib->LibMaterialByName("YMask");
  75. YImage->Canvas->Draw(0, 0, ((TGLPersistentImage *)Mat->Material->Texture->Image)->Picture->Graphic);
  76. Mat = MatLib->LibMaterialByName("ZMask");
  77. ZImage->Canvas->Draw(0, 0, ((TGLPersistentImage *)Mat->Material->Texture->Image)->Picture->Graphic);
  78. }
  79. void __fastcall TForm1::FormCreate(TObject *Sender)
  80. {
  81. RefreshMask();
  82. Sphere->Visible = CheckBox1->Checked;
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TForm1::GLCadencerProgress(TObject *Sender, const double deltaTime,
  86. const double newTime)
  87. {
  88. Sphere->TurnAngle = -newTime * 20;
  89. Sphere->Position->X = Cos(DegToRad(newTime * 20)) * 1.5;
  90. Sphere->Position->Z = Sin(DegToRad(newTime * 20)) * 1.5;
  91. SceneViewer->Invalidate();
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TForm1::SceneViewerMouseMove(TObject *Sender, TShiftState Shift, int X,
  95. int Y)
  96. {
  97. if (Shift.Contains(ssLeft))
  98. Camera->MoveAroundTarget(my - Y, mx - X);
  99. mx = X;
  100. my = Y;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  104. TPoint &MousePos, bool &Handled)
  105. {
  106. Camera->FocalLength = Camera->FocalLength + (WheelDelta / 150);
  107. Handled = true;
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TForm1::PLManagerCreateParticle(TObject *Sender, TGLParticle *aParticle)
  111. {
  112. int i;
  113. TGLParticle *Particle;
  114. // using point lights just coz it is easier to see the effect
  115. if (PLManager->Particles->ItemCount() > 0)
  116. for (i = 0; i < PLManager->Particles->ItemCount(); i++)
  117. {
  118. Particle = PLManager->Particles->Items[i];
  119. // first we find the particles that are tagged (since before particle creation, the position is overridden)
  120. if (Particle->Tag == 1)
  121. {
  122. if (CheckBox1->Checked)
  123. GLEParticleMasksManager1->SetParticlePositionFromMaskTarget(Particle, "mask", Sphere);
  124. else
  125. GLEParticleMasksManager1->SetParticlePositionFromMask(Particle, "mask");
  126. Particle->Tag = 0;
  127. }
  128. }
  129. // we tag the new particle for when another particle is made so we know this one needs updating aswell
  130. aParticle->Tag = 1;
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall TForm1::CheckBox1Click(TObject *Sender)
  134. {
  135. Sphere->Visible = CheckBox1->Checked;
  136. }
  137. //---------------------------------------------------------------------------
  138. void __fastcall TForm1::AsyncTimer1Timer(TObject *Sender)
  139. {
  140. Caption = FormatFloat("Particle Masking - FPS: 0.0", SceneViewer->FramesPerSecond()) + " Particle Count: " + IntToStr(PLManager->ParticleCount());
  141. SceneViewer->ResetPerformanceMonitor();
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TForm1::Button1Click(TObject *Sender)
  145. {
  146. Camera->Position->Z = 0;
  147. Camera->Position->Y = 0;
  148. Camera->Position->X = 4;
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TForm1::Button2Click(TObject *Sender)
  152. {
  153. Camera->Position->X = 0;
  154. Camera->Position->Z = 0.01;
  155. Camera->Position->Y = 4;
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TForm1::Button3Click(TObject *Sender)
  159. {
  160. Camera->Position->X = 0;
  161. Camera->Position->Y = 0;
  162. Camera->Position->Z = 4;
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TForm1::Button4Click(TObject *Sender)
  166. {
  167. FormCreate(Sender);
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TForm1::Edit2Change(TObject *Sender)
  171. {
  172. RefreshMask();
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TForm1::Edit3Change(TObject *Sender)
  176. {
  177. GLEParticleMasksManager1->ParticleMaskByName("mask")->PitchAngle = System::Sysutils::StrToFloatDef(Edit3->Text, 0);
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall TForm1::Edit4Change(TObject *Sender)
  181. {
  182. GLEParticleMasksManager1->ParticleMaskByName("mask")->RollAngle = System::Sysutils::StrToFloatDef(Edit4->Text, 0);
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TForm1::Edit5Change(TObject *Sender)
  186. {
  187. GLEParticleMasksManager1->ParticleMaskByName("mask")->TurnAngle = System::Sysutils::StrToFloatDef(Edit5->Text, 0);
  188. }
  189. //---------------------------------------------------------------------------