Unit1.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.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 "GLS.SimpleNavigation"
  19. #pragma link "GLSLShader"
  20. #pragma link "GLS.VectorFileObjects"
  21. #pragma link "GLS.SceneViewer"
  22. #pragma link "GLFileDDS"
  23. #pragma link "GLS.FileMD2"
  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. // Loading textures
  47. SetGLSceneMediaDir();
  48. //with GLMaterialLibrary1 do
  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. GLMaterialLibrary1->TextureByName("bark")->Image->LoadFromFile("waste.jpg");
  56. GLMaterialLibrary1->TextureByName("bark")->Disabled = false;
  57. GLMaterialLibrary1->TextureByName("mask")->Image->LoadFromFile("masks.dds");
  58. GLMaterialLibrary1->TextureByName("mask")->Disabled = false;
  59. // Loading models
  60. GLFreeForm1->LoadFromFile("waste.md2");
  61. GLFreeForm1->Scale->Scale(0.05);
  62. GLFreeForm1->Position->Y = GLPlane1->Position->Y + 0.6;
  63. FBiasMatrix =
  64. CreateScaleAndTranslationMatrix(VectorMake(0.5, 0.5, 0.5), VectorMake(0.5, 0.5, 0.5));
  65. // Loading shader
  66. GLSLShader1->VertexProgram->LoadFromFile("shadowmap_vp.glsl");
  67. GLSLShader1->FragmentProgram->LoadFromFile("shadowmapvis_fp.glsl");
  68. GLSLShader1->Enabled = true;
  69. GLSLShader2->VertexProgram->LoadFromFile("shadowmap_vp.glsl");
  70. GLSLShader2->FragmentProgram->LoadFromFile("shadowmap_fp.glsl");
  71. GLSLShader2->Enabled = true;
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TForm1::FormResize(TObject *Sender)
  75. {
  76. GLSceneViewer1->Camera->SceneScale = GLSceneViewer1->ClientWidth / 400;
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  80. const double newTime)
  81. {
  82. GLTorus1->Turn(deltaTime * 25);
  83. GLCylinder1->Turn(deltaTime * 50);
  84. GLCamera2->Position->Rotate(VectorMake(0, 1, 0), deltaTime * 0.1);
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TForm1::GLSLShader1Apply(TGLCustomGLSLShader *Shader)
  88. {
  89. Shader->Param["ShadowMap"]->AsTexture2D[0] =
  90. GLMaterialLibrary1->TextureByName(LightFBORenderer->DepthTextureName);
  91. // set compare to none so we can read off the depth value directly
  92. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
  93. }
  94. //---------------------------------------------------------------------------
  95. void __fastcall TForm1::GLSLShader1UnApply(TGLCustomGLSLShader *Shader, bool &ThereAreMorePasses)
  96. {
  97. // reset the compare mode to default
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
  99. }
  100. //---------------------------------------------------------------------------
  101. void __fastcall TForm1::GLSLShader2Apply(TGLCustomGLSLShader *Shader)
  102. {
  103. Shader->Param["ShadowMap"]->AsTexture2D[1] =
  104. GLMaterialLibrary1->TextureByName(LightFBORenderer->DepthTextureName);
  105. Shader->Param["LightspotMap"]->AsTexture2D[2] =
  106. GLMaterialLibrary1->TextureByName("Lightspot");
  107. Shader->Param["Scale"]->AsFloat = 16.0;
  108. Shader->Param["Softly"]->AsInteger = 1;
  109. Shader->Param["EyeToLightMatrix"]->AsMatrix4f = FEyeToLightMatrix;
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TForm1::GLSLShader2Initialize(TGLCustomGLSLShader *Shader)
  113. {
  114. Shader->Param["TextureMap"]->AsTexture2D[0] =
  115. GLMaterialLibrary1->TextureByName("Chekers2");
  116. Shader->Param["ShadowMap"]->AsTexture2D[1] =
  117. GLMaterialLibrary1->TextureByName(LightFBORenderer->DepthTextureName);
  118. Shader->Param["LightspotMap"]->AsTexture2D[2] =
  119. GLMaterialLibrary1->TextureByName("Lightspot");
  120. }
  121. //---------------------------------------------------------------------------
  122. void __fastcall TForm1::GLSceneViewer1BeforeRender(TObject *Sender)
  123. {
  124. if ((GLSceneViewer1->Buffer->RenderingContext->GL->EXT_framebuffer_object) = false)
  125. {
  126. ShowMessage("Sorry, this demo requires GL_EXT_framebuffer_object and either");
  127. Close();
  128. }
  129. }
  130. //---------------------------------------------------------------------------