Unit1.cpp 3.9 KB

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