fBumpShaderC.cpp 8.4 KB

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