fMushroomC.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fMushroomC.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.BaseClasses"
  8. #pragma link "GLS.Cadencer"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLS.GeomObjects"
  11. #pragma link "GLS.Objects"
  12. #pragma link "GLS.Scene"
  13. #pragma link "GLS.VectorFileObjects"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma link "GLS.File3DS"
  16. #pragma resource "*.dfm"
  17. TForm1 *Form1;
  18. const float
  19. cSpread = 90.00;
  20. const int
  21. cNbMushrooms = 10;
  22. //---------------------------------------------------------------------------
  23. __fastcall TForm1::TForm1(TComponent* Owner)
  24. : TForm(Owner)
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TForm1::FormCreate(TObject *Sender)
  29. {
  30. SetGLSceneMediaDir();
  31. // Randomize;
  32. // Load mushroom mesh
  33. FreeForm1->LoadFromFile("mushroom.3ds");
  34. // Load ground texture
  35. Disk1->Material->Texture->Image->LoadFromFile("clover.jpg");
  36. // Duplicate our reference mushroom (but not its mesh data !)
  37. AddMushRooms();
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TForm1::AddMushRooms()
  41. {
  42. int i;
  43. TGLProxyObject *proxy;
  44. TGLVector s;
  45. float f;
  46. // spawn some more mushrooms using proxy objects
  47. for (i=0; i < cNbMushrooms-1; i++)
  48. {
  49. // create a new proxy and set its MasterObject property
  50. proxy = (TGLProxyObject *)(DummyCube1->AddNewChild(__classid(TGLProxyObject)));
  51. proxy->MasterObject = FreeForm1;
  52. proxy->ProxyOptions = proxy->ProxyOptions << pooObjects;
  53. // retrieve reference attitude
  54. proxy->Direction = FreeForm1->Direction;
  55. proxy->Up = FreeForm1->Up;
  56. // randomize scale
  57. s = FreeForm1->Scale->AsVector;
  58. f = (Random()+0.2);
  59. ScaleVector(s, 5*f);
  60. proxy->Scale->AsVector = s;
  61. // randomize position
  62. proxy->Position->SetPoint(Random(cSpread)-(cSpread/2),
  63. f*FreeForm1->Position->Y,
  64. Random(cSpread)-(cSpread/2));
  65. // randomize orientation
  66. proxy->RollAngle = Random(360);
  67. proxy->PitchAngle = -90;
  68. }
  69. MushRoomCounter = MushRoomCounter + cNbMushrooms; // Inc(mushroomCounter, cNbMushrooms);
  70. }
  71. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  72. TShiftState Shift, int X, int Y)
  73. {
  74. mx = X; my = Y;
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  78. int X, int Y)
  79. {
  80. if (Shift.Contains(ssLeft))
  81. {
  82. GLCamera1->MoveAroundTarget(my-Y, mx-X);
  83. mx = X; my = Y;
  84. }
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TForm1::Button1Click(TObject *Sender)
  88. {
  89. AddMushRooms();
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  93. {
  94. Caption = Format("Mushroom Counter : %d (%.1f FPS)",
  95. ARRAYOFCONST (( MushRoomCounter, GLSceneViewer1->FramesPerSecond())));
  96. GLSceneViewer1->ResetPerformanceMonitor();
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::FormResize(TObject *Sender)
  100. {
  101. // adjust focal Length
  102. GLCamera1->FocalLength = GLSceneViewer1->Width/8;
  103. // keep "add mushrooms" centered
  104. Button1->Left = (Width-Button1->Width)/2;
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  108. const double newTime)
  109. {
  110. // keep it rendering, we want FPS stats !
  111. GLSceneViewer1->Invalidate();
  112. }
  113. //---------------------------------------------------------------------------