fShadowFBOC.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "fShadowFBOC.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLSL.CustomShader"
  12. #pragma link "GLS.FBORenderer"
  13. #pragma link "GLS.GeomObjects"
  14. #pragma link "GLS.HUDObjects"
  15. #pragma link "GLS.Material"
  16. #pragma link "GLS.Objects"
  17. #pragma link "GLS.Scene"
  18. #pragma link "Formats.DDSImage"
  19. #pragma link "GLS.SimpleNavigation"
  20. #pragma link "GLS.VectorFileObjects"
  21. #pragma link "GLS.SceneViewer"
  22. #pragma link "GLS.FileMD2"
  23. #pragma link "GLSL.Shader"
  24. #pragma resource "*.dfm"
  25. TForm1 *Form1;
  26. //---------------------------------------------------------------------------
  27. __fastcall TForm1::TForm1(TComponent* Owner)
  28. : TForm(Owner)
  29. {
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TForm1::PrepareShadowMappingRender(TObject *Sender, TGLRenderContextInfo &rci)
  33. {
  34. // prepare shadow mapping matrix
  35. FInvCameraMatrix = rci.PipelineTransformation->InvModelViewMatrix;
  36. // go from eye space to light's "eye" space
  37. *FEyeToLightMatrix = MatrixMultiply(*FInvCameraMatrix, *FLightModelViewMatrix);
  38. // then to clip space
  39. *FEyeToLightMatrix = MatrixMultiply(*FEyeToLightMatrix, *FLightProjMatrix);
  40. // and finally make the [-1..1] coordinates into [0..1]
  41. *FEyeToLightMatrix = MatrixMultiply(*FEyeToLightMatrix, *FBiasMatrix);
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall TForm1::FormCreate(TObject *Sender)
  45. {
  46. TFileName Path = GetCurrentAssetPath();
  47. // Loading textures
  48. SetCurrentDir(Path + "\\texture");
  49. GLMaterialLibrary1->TextureByName("Chekers")->Image->LoadFromFile("marbletiles.jpg");
  50. GLMaterialLibrary1->TextureByName("Chekers")->Disabled = false;
  51. GLMaterialLibrary1->TextureByName("Chekers2")->Image->LoadFromFile("concrete.jpg");
  52. GLMaterialLibrary1->TextureByName("Chekers2")->Disabled = false;
  53. GLMaterialLibrary1->TextureByName("Lightspot")->Image->LoadFromFile("flare1.bmp");
  54. GLMaterialLibrary1->TextureByName("Lightspot")->Disabled = false;
  55. // Loading skeletal models with skin texture
  56. SetCurrentDir(Path + "\\modelext");
  57. GLFreeForm1->LoadFromFile("waste.md2");
  58. GLFreeForm1->Scale->Scale(0.05);
  59. GLFreeForm1->Position->Y = GLPlane1->Position->Y + 0.6;
  60. FBiasMatrix =
  61. &CreateScaleAndTranslationMatrix(VectorMake(0.5, 0.5, 0.5), VectorMake(0.5, 0.5, 0.5));
  62. GLMaterialLibrary1->TextureByName("bark")->Image->LoadFromFile("waste.jpg");
  63. GLMaterialLibrary1->TextureByName("bark")->Disabled = false;
  64. // Loading cubemap
  65. SetCurrentDir(Path + "\\cubemap");
  66. GLMaterialLibrary1->TextureByName("mask")->Image->LoadFromFile("masks.dds");
  67. GLMaterialLibrary1->TextureByName("mask")->Disabled = false;
  68. // Loading shaders
  69. SetCurrentDir(Path + "\\shader");
  70. GLSLShader1->VertexProgram->LoadFromFile("shadowmap_vp.glsl");
  71. GLSLShader1->FragmentProgram->LoadFromFile("shadowmapvis_fp.glsl");
  72. GLSLShader1->Enabled = true;
  73. GLSLShader2->VertexProgram->LoadFromFile("shadowmap_vp.glsl");
  74. GLSLShader2->FragmentProgram->LoadFromFile("shadowmap_fp.glsl");
  75. GLSLShader2->Enabled = true;
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::FormResize(TObject *Sender)
  79. {
  80. GLSceneViewer1->Camera->SceneScale = GLSceneViewer1->ClientWidth / 400;
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  84. const double newTime)
  85. {
  86. GLTorus1->Turn(deltaTime * 25);
  87. GLCylinder1->Turn(deltaTime * 50);
  88. GLCamera2->Position->Rotate(VectorMake(0, 1, 0), deltaTime * 0.1);
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TForm1::GLSLShader1Apply(TGLCustomGLSLShader *Shader)
  92. {
  93. Shader->Param["ShadowMap"]->AsTexture2D[0] =
  94. GLMaterialLibrary1->TextureByName(LightFBORenderer->DepthTextureName);
  95. // set compare to none so we can read off the depth value directly
  96. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::GLSLShader1UnApply(TGLCustomGLSLShader *Shader, bool &ThereAreMorePasses)
  100. {
  101. // reset the compare mode to default
  102. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TForm1::GLSLShader2Apply(TGLCustomGLSLShader *Shader)
  106. {
  107. Shader->Param["ShadowMap"]->AsTexture2D[1] =
  108. GLMaterialLibrary1->TextureByName(LightFBORenderer->DepthTextureName);
  109. Shader->Param["LightspotMap"]->AsTexture2D[2] =
  110. GLMaterialLibrary1->TextureByName("Lightspot");
  111. Shader->Param["Scale"]->AsFloat = 16.0;
  112. Shader->Param["Softly"]->AsInteger = 1;
  113. Shader->Param["EyeToLightMatrix"]->AsMatrix4f = *FEyeToLightMatrix;
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TForm1::GLSLShader2Initialize(TGLCustomGLSLShader *Shader)
  117. {
  118. Shader->Param["TextureMap"]->AsTexture2D[0] =
  119. GLMaterialLibrary1->TextureByName("Chekers2");
  120. Shader->Param["ShadowMap"]->AsTexture2D[1] =
  121. GLMaterialLibrary1->TextureByName(LightFBORenderer->DepthTextureName);
  122. Shader->Param["LightspotMap"]->AsTexture2D[2] =
  123. GLMaterialLibrary1->TextureByName("Lightspot");
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TForm1::GLSceneViewer1BeforeRender(TObject *Sender)
  127. {
  128. if ((GLSceneViewer1->Buffer->RenderingContext->GL->EXT_framebuffer_object) = false)
  129. {
  130. ShowMessage("Sorry, this demo requires GL_EXT_framebuffer_object and either");
  131. Close();
  132. }
  133. }
  134. //---------------------------------------------------------------------------