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