fMushroomD.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. unit fMushroomD;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Classes,
  6. Vcl.Graphics,
  7. Vcl.Imaging.Jpeg,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. Vcl.StdCtrls,
  12. Vcl.ExtCtrls,
  13. Stage.VectorTypes,
  14. Stage.VectorGeometry,
  15. GLS.Texture,
  16. GLS.Scene,
  17. GLS.VectorFileObjects,
  18. GLS.Objects,
  19. GLS.Behaviours,
  20. GLS.Cadencer,
  21. GLS.SceneViewer,
  22. GLS.GeomObjects,
  23. GLS.Coordinates,
  24. GLS.BaseClasses,
  25. GLS.File3DS,
  26. Stage.Utils;
  27. type
  28. TFormMushroom = class(TForm)
  29. GLScene1: TGLScene;
  30. GLSceneViewer1: TGLSceneViewer;
  31. GLCamera1: TGLCamera;
  32. GLLightSource1: TGLLightSource;
  33. FreeForm1: TGLFreeForm;
  34. DummyCube1: TGLDummyCube;
  35. Disk1: TGLDisk;
  36. Button1: TButton;
  37. Timer1: TTimer;
  38. GLCadencer1: TGLCadencer;
  39. procedure FormCreate(Sender: TObject);
  40. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
  41. X, Y: Integer);
  42. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  43. procedure Button1Click(Sender: TObject);
  44. procedure Timer1Timer(Sender: TObject);
  45. procedure FormResize(Sender: TObject);
  46. procedure GLCadencer1Progress(Sender: TObject; const deltaTime, newTime: Double);
  47. private
  48. procedure AddMushrooms;
  49. public
  50. mx, my: Integer;
  51. MushRoomCounter: Integer;
  52. end;
  53. var
  54. FormMushroom: TFormMushroom;
  55. implementation
  56. {$R *.DFM}
  57. const
  58. cSpread = 90;
  59. cNbMushrooms = 10;
  60. procedure TFormMushroom.FormCreate(Sender: TObject);
  61. begin
  62. var Path: TFileName := GetCurrentAssetPath();
  63. // Randomize;
  64. // Load mushroom mesh
  65. // Load skeletal Actor model
  66. SetCurrentDir(Path + '\model');
  67. FreeForm1.LoadFromFile('mushroom.3ds');
  68. // Load Texture for ground disk
  69. SetCurrentDir(Path + '\texture');
  70. Disk1.Material.Texture.Image.LoadFromFile('clover.jpg');
  71. // Duplicate our reference mushroom (but not its mesh data !)
  72. AddMushrooms;
  73. end;
  74. procedure TFormMushroom.AddMushrooms;
  75. var
  76. i: Integer;
  77. proxy: TGLProxyObject;
  78. s: TGLVector;
  79. f: Single;
  80. begin
  81. // spawn some more mushrooms using proxy objects
  82. for i := 0 to cNbMushrooms - 1 do
  83. begin
  84. // create a new proxy and set its MasterObject property
  85. proxy := TGLProxyObject(DummyCube1.AddNewChild(TGLProxyObject));
  86. with proxy do
  87. begin
  88. MasterObject := FreeForm1;
  89. ProxyOptions := [pooObjects];
  90. // retrieve reference attitude
  91. Direction := FreeForm1.Direction;
  92. Up := FreeForm1.Up;
  93. // randomize scale
  94. s := FreeForm1.Scale.AsVector;
  95. f := (Random + 0.2);
  96. ScaleVector(s, f);
  97. Scale.AsVector := s;
  98. // randomize position
  99. Position.SetPoint(Random(cSpread) - (cSpread / 2), f * FreeForm1.Position.Y,
  100. Random(cSpread) - (cSpread / 2));
  101. // randomize orientation
  102. RollAngle := Random(360);
  103. end;
  104. end;
  105. Inc(MushRoomCounter, cNbMushrooms);
  106. end;
  107. procedure TFormMushroom.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  108. Shift: TShiftState; X, Y: Integer);
  109. begin
  110. mx := X;
  111. my := Y;
  112. end;
  113. procedure TFormMushroom.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  114. begin
  115. if ssLeft in Shift then
  116. begin
  117. GLCamera1.MoveAroundTarget(my - Y, mx - X);
  118. mx := X;
  119. my := Y;
  120. end;
  121. end;
  122. procedure TFormMushroom.Button1Click(Sender: TObject);
  123. begin
  124. AddMushrooms;
  125. end;
  126. procedure TFormMushroom.Timer1Timer(Sender: TObject);
  127. begin
  128. Caption := Format('Mushroom Counter : %d (%.1f FPS)',
  129. [MushRoomCounter, GLSceneViewer1.FramesPerSecond]);
  130. GLSceneViewer1.ResetPerformanceMonitor;
  131. end;
  132. procedure TFormMushroom.FormResize(Sender: TObject);
  133. begin
  134. // adjust focal Length
  135. GLCamera1.FocalLength := GLSceneViewer1.Width / 8;
  136. // keep "add mushrooms" centered
  137. Button1.Left := (Width - Button1.Width) div 2;
  138. end;
  139. procedure TFormMushroom.GLCadencer1Progress(Sender: TObject; const deltaTime, newTime: Double);
  140. begin
  141. // keep it rendering, we want FPS stats !
  142. GLSceneViewer1.Invalidate;
  143. end;
  144. end.