fShadowVolumesC.cpp 5.6 KB

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