Unit1.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.FileSMD"
  8. #pragma resource "*.dfm"
  9. TForm1 *Form1;
  10. //---------------------------------------------------------------------------
  11. __fastcall TForm1::TForm1(TComponent * Owner):TForm(Owner)
  12. {
  13. const float cSpacing = 1, cRadius = 0.3;
  14. const int cNb = 1;
  15. int x, y, z;
  16. TGLSphere *sphere;
  17. String MediaPath = ExtractFilePath(ParamStr(0));
  18. String SubStr = "Samples";
  19. int I = MediaPath.Pos(SubStr);
  20. if (I != 0) {
  21. MediaPath.Delete(I+8,MediaPath.Length()-I);
  22. SetCurrentDir(MediaPath+"Media\\");
  23. }
  24. // Dynamically construct an array of spheres, and make them shadow casters
  25. // Note that as the spheres are children of the shadowvolume component,
  26. // they are thus also shadow receivers. If they were created as child of
  27. // another object (not under the shadow volume), they would not receive
  28. // shadows (which can sometimes be interesting).
  29. for(x = -cNb; x <= cNb; x++)
  30. for(y = -cNb; y <= cNb; y++)
  31. for(z = -cNb; z <= cNb; z++)
  32. if((x && y && z) != 0)
  33. {
  34. sphere = (TGLSphere *) DCSpheres->AddNewChild(__classid(TGLSphere));
  35. sphere->Position->SetPoint(x * cSpacing, y * cSpacing, z * cSpacing);
  36. sphere->Radius = cRadius;
  37. GLS.ShadowVolume->Occluders->AddCaster(sphere, 0, scmParentVisible);
  38. }
  39. DCSpheres->MoveTo(GLS.ShadowVolume);
  40. GLFreeForm->LoadFromFile("trinityrage.smd");
  41. GLFreeForm->BuildSilhouetteConnectivityData();
  42. GLS.ShadowVolume->Occluders->AddCaster(GLFreeForm,0,scmRecursivelyVisible);
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall TForm1::GLCadencer1Progress(TObject * Sender,
  46. const double deltaTime,
  47. const double newTime)
  48. {
  49. DCLight2->TurnAngle = newTime * 45;
  50. DCLight3->RollAngle = newTime * 50;
  51. GLSceneViewer->Invalidate();
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TForm1::CBShowVolumesClick(TObject * Sender)
  55. {
  56. if(CBShowVolumes->Checked)
  57. GLS.ShadowVolume->Options = GLS.ShadowVolume->Options << svoShowVolumes;
  58. else
  59. GLS.ShadowVolume->Options = GLS.ShadowVolume->Options >> svoShowVolumes;
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall TForm1::RBZFailClick(TObject * Sender)
  63. {
  64. // this event handles all the radio buttons
  65. if(RBDarkening->Checked)
  66. GLS.ShadowVolume->Mode = svmDarkening;
  67. else if(RBNoShadows->Checked)
  68. GLS.ShadowVolume->Mode = svmOff;
  69. else
  70. {
  71. GLS.ShadowVolume->Mode = svmAccurate;
  72. if(RBZFail->Checked)
  73. GLS.ShadowVolume->Capping = svcAlways;
  74. else
  75. GLS.ShadowVolume->Capping = svcNever;
  76. }
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TForm1::GLSceneViewerMouseDown(TObject * Sender,
  80. TMouseButton Button,
  81. TShiftState Shift, int X, int Y)
  82. {
  83. mx = X;
  84. my = Y;
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TForm1::GLSceneViewerMouseMove(TObject * Sender,
  88. TShiftState Shift, int X, int Y)
  89. {
  90. if(Shift.Contains(ssLeft))
  91. {
  92. GLCamera->MoveAroundTarget((float)(my - Y) * 0.5, (float)(mx - X) * 0.5);
  93. GLCadencer1->Progress();
  94. }
  95. else if(Shift.Contains(ssRight))
  96. {
  97. DCLight1Turn->Turn((float)(mx - X) * 0.5);
  98. DCLight1Pitch->Pitch((float)(my - Y) * 0.5);
  99. GLLightSource1->TransformationChanged();
  100. GLCadencer1->Progress();
  101. }
  102. mx = X;
  103. my = Y;
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TForm1::Timer1Timer(TObject * Sender)
  107. {
  108. LabelFPS->Caption = GLSceneViewer->FramesPerSecondText(1);
  109. GLSceneViewer->ResetPerformanceMonitor();
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TForm1::FormResize(TObject * Sender)
  113. {
  114. GLCamera->SceneScale = GLSceneViewer->Width / 450;
  115. }
  116. //---------------------------------------------------------------------------
  117. void __fastcall TForm1::CBMainLightClick(TObject * Sender)
  118. {
  119. GLLightSource1->Shining = CBMainLight->Checked;
  120. }
  121. //---------------------------------------------------------------------------
  122. void __fastcall TForm1::CBBlueLightClick(TObject * Sender)
  123. {
  124. GLLightSource2->Shining = CBBlueLight->Checked;
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TForm1::CBRedLightClick(TObject * Sender)
  128. {
  129. GLLightSource3->Shining = CBRedLight->Checked;
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TForm1::ScrollBar_ShadowResolutionChange(TObject * Sender)
  133. {
  134. GLSphere_Shadow->Stacks = ScrollBar_ShadowResolution->Position;
  135. GLSphere_Shadow->Slices = ScrollBar_ShadowResolution->Position;
  136. GLS.ShadowVolume->FlushSilhouetteCache();
  137. }
  138. //---------------------------------------------------------------------------
  139. void __fastcall TForm1::Button_GenerateSilhouetteClick(TObject * Sender)
  140. {
  141. TGLSilhouetteParameters silhouetteParameters;
  142. TGLSilhouette *Silhouette;
  143. int i;
  144. TGLSceneObject *Target = GLSphere4;
  145. silhouetteParameters.CappingRequired = false;
  146. SetVector(silhouetteParameters.SeenFrom,
  147. GLLines1->AbsoluteToLocal(GLCamera->AbsolutePosition));
  148. silhouetteParameters.Style = ssOmni;
  149. Silhouette = Target->GenerateSilhouette(silhouetteParameters);
  150. GLLines1->Nodes->Clear();
  151. for(i = 0; i <= Silhouette->Indices->Count - 1; i++)
  152. GLLines1->Nodes->AddNode(GLLines1->
  153. AbsoluteToLocal(Target->
  154. LocalToAbsolute(Silhouette->
  155. Vertices->
  156. Items[Silhouette->
  157. Indices->
  158. Items[i]])));
  159. delete Silhouette;
  160. }
  161. //---------------------------------------------------------------------------