fcBumpShader.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #include <System.Math.hpp>
  5. #pragma hdrstop
  6. #include "fcBumpShader.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "GLS.BaseClasses"
  10. #pragma link "GLS.Cadencer"
  11. #pragma link "GLS.Coordinates"
  12. #pragma link "GLSL.CustomShader"
  13. #pragma link "GLS.GeomObjects"
  14. #pragma link "GLS.Graph"
  15. #pragma link "GLS.Material"
  16. #pragma link "GLS.Objects"
  17. #pragma link "GLS.Scene"
  18. #pragma link "GLSL.Shader"
  19. #pragma link "GLS.VectorFileObjects"
  20. #pragma link "GLS.SceneViewer"
  21. #pragma link "GLS.FileSMD"
  22. #pragma link "GLS.FileMD2"
  23. #pragma link "GLS.File3DS"
  24. #pragma link "Formats.DDSImage"
  25. #pragma link "GLS.FileMS3D"
  26. #pragma link "GLS.BaseClasses"
  27. #pragma link "GLSL.Shader"
  28. #pragma link "GLSL.BumpShaders"
  29. #pragma resource "*.dfm"
  30. TForm1 *Form1;
  31. //---------------------------------------------------------------------------
  32. __fastcall TForm1::TForm1(TComponent* Owner)
  33. : TForm(Owner)
  34. {
  35. }
  36. //---------------------------------------------------------------------------
  37. void __fastcall TForm1::FormCreate(TObject *Sender)
  38. {
  39. int I;
  40. TFileName Path = GetCurrentAssetPath();
  41. // Loading animated models
  42. SetCurrentDir(Path + "\\modelext");
  43. Fighter->LoadFromFile("TRINITYrage.smd"); //Fighter
  44. Fighter->AddDataFromFile("walk.smd");
  45. Fighter->Animations->Items[1]->MakeSkeletalTranslationStatic();
  46. Fighter->AddDataFromFile("run.smd");
  47. Fighter->Animations->Items[2]->MakeSkeletalTranslationStatic();
  48. Fighter->AddDataFromFile("long_jump.smd");
  49. Fighter->Animations->Items[3]->MakeSkeletalTranslationStatic();
  50. Fighter->AddDataFromFile("jump.smd");
  51. Fighter->AddDataFromFile("look_left_right.smd");
  52. Fighter->Animations->Items[5]->MakeSkeletalRotationDelta();
  53. Fighter->SwitchToAnimation(1);
  54. /*
  55. // or use a quake md2 model
  56. Fighter->LoadFromFile("waste.md2"); //Fighter
  57. Fighter->SwitchToAnimation(0, True);
  58. Fighter->AnimationMode = aamLoop;
  59. Fighter->Scale->Scale(3);
  60. */
  61. Fighter->AnimationMode = aamLoop;
  62. Fighter->Scale->Scale(3);
  63. // Fighter->MeshObjects->BuildTangentSpace;
  64. // Loading static models
  65. SetCurrentDir(Path + "\\model");
  66. Teapot->LoadFromFile("Teapot.3ds"); //Teapot
  67. Teapot->Scale->Scale(0.8);
  68. // Teapot.MeshObjects->BuildTangentSpace; does not have texture coordinates...
  69. Sphere_big->LoadFromFile("Sphere.3DS"); //Sphere_big
  70. Sphere_big->Scale->Scale(70);
  71. Sphere_big->MeshObjects->BuildTangentSpace();
  72. Sphere_little->LoadFromFile("Sphere.3DS"); //Sphere_little
  73. Sphere_little->Scale->Scale(4);
  74. Sphere_little->MeshObjects->BuildTangentSpace();
  75. // Then load textures
  76. SetCurrentDir(Path + "\\map");
  77. MaterialLibrary->LibMaterialByName("Earth")->Material->Texture->Image->LoadFromFile("Earth.jpg");
  78. SetCurrentDir(Path + "\\texture");
  79. MaterialLibrary->LibMaterialByName("EarthNormals")->Material->Texture->Image->LoadFromFile("EarthNormals.jpg");
  80. // And cubemaps
  81. SetCurrentDir(Path + "\\cubemap");
  82. MaterialLibrary->LibMaterialByName("EarthGross")->Material->Texture->Image->LoadFromFile("EarthSpec.dds");
  83. // Create Shader
  84. MultiLightShader = new TGLSLMLBumpShader(this);
  85. MultiLightShader->LightSources = MultiLightShader->LightSources << 1, 2 ;
  86. MultiLightShader->LightCompensation = 0.7;
  87. MultiLightShader->NormalTexture = MaterialLibrary->LibMaterialByName("EarthNormals")->Material->Texture;
  88. MultiLightShader->SpecularTexture = MaterialLibrary->LibMaterialByName("EarthGross")->Material->Texture;
  89. // Attach shader to the material
  90. MaterialLibrary->LibMaterialByName("Earth")->Shader = MyBumpShader;
  91. for (I = 0; I < TrinityMatlib->Materials->Count - 1; I++)
  92. TrinityMatlib->Materials->Items[I]->Shader = MyBumpShader;
  93. ShowNotGLSceneObjectsCheckBoxClick(this);
  94. MyBumpShader->Enabled = ShaderEnabledCheckBox->Checked;
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TForm1::ShaderEnabledCheckBoxClick(TObject *Sender)
  98. {
  99. MyBumpShader->Enabled = ShaderEnabledCheckBox->Checked;
  100. MultiLightShader->Enabled = ShaderEnabledCheckBox->Checked;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TForm1::ShowNotGLSceneObjectsCheckBoxClick(TObject *Sender)
  104. {
  105. Teapot->Visible = ShowNotGLSceneObjectsCheckBox->Checked;
  106. Fighter->Visible = ShowNotGLSceneObjectsCheckBox->Checked;
  107. GLCube->Visible = ShowNotGLSceneObjectsCheckBox->Checked;
  108. GLDodecahedron->Visible = ShowNotGLSceneObjectsCheckBox->Checked;
  109. GLSphere->Visible = ShowNotGLSceneObjectsCheckBox->Checked;
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TForm1::MultiLightShaderCheckBoxClick(TObject *Sender)
  113. {
  114. int I;
  115. if (MultiLightShaderCheckBox->Checked)
  116. {
  117. MaterialLibrary->LibMaterialByName("Earth")->Shader = MultiLightShader;
  118. for (I = 0; I < TrinityMatlib->Materials->Count - 1; I++)
  119. TrinityMatlib->Materials->Items[I]->Shader = MultiLightShader;
  120. }
  121. else
  122. {
  123. MaterialLibrary->LibMaterialByName("Earth")->Shader = MyBumpShader;
  124. for (I = 0; I < TrinityMatlib->Materials->Count - 1; I++)
  125. TrinityMatlib->Materials->Items[I]->Shader = MyBumpShader;
  126. }
  127. Light2->Shining = MultiLightShaderCheckBox->Checked;
  128. LightCube2->Visible = MultiLightShaderCheckBox->Checked;
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TForm1::UseSpecularTextureCheckBoxClick(TObject *Sender)
  132. {
  133. if (UseSpecularTextureCheckBox->Checked) {
  134. MyBumpShader->SpecularTexture = MaterialLibrary->LibMaterialByName("EarthGross")->Material->Texture;
  135. MultiLightShader->SpecularTexture = MaterialLibrary->LibMaterialByName("EarthGross")->Material->Texture;
  136. }
  137. else {
  138. MyBumpShader->SpecularTexture = NULL;
  139. MultiLightShader->SpecularTexture = NULL;
  140. }
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TForm1::UseNormalTextureCheckBoxClick(TObject *Sender)
  144. {
  145. if (UseNormalTextureCheckBox->Checked) {
  146. MyBumpShader->NormalTexture = MaterialLibrary->LibMaterialByName("EarthNormals")->Material->Texture;
  147. MultiLightShader->NormalTexture = MaterialLibrary->LibMaterialByName("EarthNormals")->Material->Texture;
  148. }
  149. else {
  150. MyBumpShader->NormalTexture = NULL;
  151. MultiLightShader->NormalTexture = NULL;
  152. }
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TForm1::CadencerProgress(TObject *Sender, const double deltaTime,
  156. const double newTime)
  157. {
  158. Viewer->Invalidate();
  159. if (RollPitchTurnCheckBox->Checked)
  160. {
  161. Sphere_big->Turn(deltaTime * 40);
  162. Sphere_big->Roll(deltaTime * 40);
  163. Sphere_little->Pitch(deltaTime * 20);
  164. Fighter->Roll(deltaTime * 20);
  165. Teapot->Roll(-deltaTime * 10);
  166. GLCube->Pitch(-deltaTime * 10);
  167. GLDodecahedron->Pitch(deltaTime * 10);
  168. GLSphere->Roll(-deltaTime * 10);
  169. }
  170. }
  171. //---------------------------------------------------------------------------
  172. void __fastcall TForm1::ViewerMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  173. int X, int Y)
  174. {
  175. mx = X;
  176. my = Y;
  177. }
  178. //---------------------------------------------------------------------------
  179. void __fastcall TForm1::ViewerMouseMove(TObject *Sender, TShiftState Shift, int X,
  180. int Y)
  181. {
  182. if (Shift.Contains(ssRight) && Shift.Contains(ssLeft))
  183. Camera->AdjustDistanceToTarget(Power(1.01, Y - my));
  184. else
  185. if (Shift.Contains(ssRight) || Shift.Contains(ssLeft))
  186. Camera->MoveAroundTarget(my - Y, mx - X);
  187. mx = X;
  188. my = Y;
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  192. {
  193. Caption = "GLSL Bump Shader - " + Viewer->FramesPerSecondText();
  194. Viewer->ResetPerformanceMonitor();
  195. }
  196. //---------------------------------------------------------------------------
  197. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  198. TPoint &MousePos, bool &Handled)
  199. {
  200. Camera->AdjustDistanceToTarget(Power(1.1, WheelDelta / 120));
  201. }
  202. //---------------------------------------------------------------------------
  203. void __fastcall TForm1::LightCubeProgress(TObject *Sender, const double deltaTime,
  204. const double newTime)
  205. {
  206. if (LightMovingCheckBox->Checked)
  207. LightCube->MoveObjectAround(Camera->TargetObject, sin(newTime) * deltaTime * 10, deltaTime * 20);
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
  211. {
  212. Cadencer->Enabled = false;
  213. }
  214. //---------------------------------------------------------------------------