fdBumpShader.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. unit fdBumpShader;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Classes,
  6. System.Math,
  7. System.Types,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.ExtCtrls,
  11. Vcl.StdCtrls,
  12. Vcl.Imaging.Jpeg,
  13. GLS.Texture,
  14. GLS.Cadencer,
  15. GLS.SceneViewer,
  16. GLS.Scene,
  17. GLS.Objects,
  18. GLS.VectorFileObjects,
  19. GLS.Graph,
  20. GLS.GeomObjects,
  21. Stage.VectorGeometry,
  22. GLS.Material,
  23. GLS.Coordinates,
  24. GLS.BaseClasses,
  25. Stage.Utils,
  26. GLS.FileMS3D,
  27. GLS.File3DS,
  28. GLS.FileMD2,
  29. GLS.FileSMD,
  30. GLSL.Shader,
  31. GLSL.CustomShader,
  32. GLSL.BumpShaders,
  33. Formats.DDSImage;
  34. type
  35. TFormBumpShader = class(TForm)
  36. Scene: TGLScene;
  37. Viewer: TGLSceneViewer;
  38. Cadencer: TGLCadencer;
  39. Camera: TGLCamera;
  40. Timer1: TTimer;
  41. Light: TGLLightSource;
  42. LightCube: TGLDummyCube;
  43. GLSphere1: TGLSphere;
  44. GLXYZGrid1: TGLXYZGrid;
  45. GLArrowLine1: TGLArrowLine;
  46. Panel1: TPanel;
  47. LightMovingCheckBox: TCheckBox;
  48. GUICube: TGLDummyCube;
  49. WorldCube: TGLDummyCube;
  50. actFighter: TGLActor;
  51. actTeapot: TGLActor;
  52. actSphere_big: TGLActor;
  53. actSphere_lit: TGLActor;
  54. MaterialLibrary: TGLMaterialLibrary;
  55. RollPitchTurnCheckBox: TCheckBox;
  56. ShaderEnabledCheckBox: TCheckBox;
  57. GLSphere2: TGLSphere;
  58. Light2: TGLLightSource;
  59. LightCube2: TGLDummyCube;
  60. MultiLightShaderCheckBox: TCheckBox;
  61. UseSpecularTextureCheckBox: TCheckBox;
  62. UseNormalTextureCheckBox: TCheckBox;
  63. MyBumpShader: TGLSLBumpShader;
  64. TrinityMatlib: TGLMaterialLibrary;
  65. Cube: TGLCube;
  66. Dodecahedron: TGLDodecahedron;
  67. ShowNotGLSceneObjectsCheckBox: TCheckBox;
  68. procedure FormCreate(Sender: TObject);
  69. procedure CadencerProgress(Sender: TObject; const DeltaTime, newTime: Double);
  70. procedure ViewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  71. procedure ViewerMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  72. procedure Timer1Timer(Sender: TObject);
  73. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  74. procedure FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  75. procedure LightCubeProgress(Sender: TObject; const DeltaTime, newTime: Double);
  76. procedure ShaderEnabledCheckBoxClick(Sender: TObject);
  77. procedure MultiLightShaderCheckBoxClick(Sender: TObject);
  78. procedure UseSpecularTextureCheckBoxClick(Sender: TObject);
  79. procedure UseNormalTextureCheckBoxClick(Sender: TObject);
  80. procedure ShowNotGLSceneObjectsCheckBoxClick(Sender: TObject);
  81. end;
  82. var
  83. FormBumpShader: TFormBumpShader;
  84. mx, my: Integer;
  85. MultiLightShader: TGLSLMLBumpShader;
  86. implementation
  87. {$R *.dfm}
  88. procedure TFormBumpShader.FormCreate(Sender: TObject);
  89. var
  90. I : Integer;
  91. begin
  92. //First loading modelexts
  93. var Path: TFileName := GetCurrentAssetPath();
  94. SetCurrentDir(Path + '\modelext');
  95. actFighter.LoadFromFile('TRINITYrage.smd'); //Fighter
  96. actFighter.AddDataFromFile('walk.smd');
  97. actFighter.Animations[1].MakeSkeletalTranslationStatic;
  98. actFighter.AddDataFromFile('run.smd');
  99. actFighter.Animations[2].MakeSkeletalTranslationStatic;
  100. actFighter.AddDataFromFile('long_jump.smd');
  101. actFighter.AddDataFromFile('jump.smd');
  102. actFighter.AddDataFromFile('look_left_right.smd');
  103. actFighter.Animations[5].MakeSkeletalRotationDelta;
  104. actFighter.SwitchToAnimation(1);
  105. (*
  106. // or use a quake md2 model
  107. actFighter.LoadFromFile('waste.md2'); //Fighter
  108. actFighter.SwitchToAnimation(0, True);
  109. actFighter.AnimationMode := aamLoop;
  110. actFighter.Scale.Scale(3);
  111. *)
  112. actFighter.AnimationMode := aamLoop;
  113. actFighter.Scale.Scale(3);
  114. // actFighter.MeshObjects.BuildTangentSpace;
  115. // Loading static models
  116. SetCurrentDir(Path + '\model');
  117. actTeapot.LoadFromFile('Teapot.3ds'); //Teapot
  118. actTeapot.Scale.Scale(0.8);
  119. // actTeapotTeapot.MeshObjects.BuildTangentSpace; does not have texture coordinates...
  120. actSphere_big.LoadFromFile('Sphere.3DS'); //Sphere_big
  121. actSphere_big.Scale.Scale(70);
  122. actSphere_big.MeshObjects.BuildTangentSpace;
  123. actSphere_lit.LoadFromFile('Sphere.3DS'); //Sphere_little
  124. actSphere_lit.Scale.Scale(4);
  125. actSphere_lit.MeshObjects.BuildTangentSpace;
  126. // Then load textures
  127. SetCurrentDir(Path + '\map');
  128. MaterialLibrary.LibMaterialByName('Earth').Material.Texture.Image.LoadFromFile('earth.jpg');
  129. SetCurrentDir(Path + '\texture');
  130. MaterialLibrary.LibMaterialByName('EarthNormals').Material.Texture.Image.LoadFromFile('EarthNormals.jpg');
  131. SetCurrentDir(Path + '\cubemap');
  132. MaterialLibrary.LibMaterialByName('EarthGross').Material.Texture.Image.LoadFromFile('EarthSpec.dds');
  133. // Create Shader
  134. MultiLightShader := TGLSLMLBumpShader.Create(Self);
  135. MultiLightShader.LightSources := [1, 2];
  136. MultiLightShader.LightCompensation := 0.7;
  137. MultiLightShader.NormalTexture := MaterialLibrary.LibMaterialByName('EarthNormals').Material.Texture;
  138. MultiLightShader.SpecularTexture := MaterialLibrary.LibMaterialByName('EarthGross').Material.Texture;
  139. // Attach shader to the material
  140. MaterialLibrary.LibMaterialByName('Earth').Shader := MyBumpShader;
  141. for I := 0 to TrinityMatlib.Materials.Count - 1 do
  142. TrinityMatlib.Materials[I].Shader := MyBumpShader;
  143. ShowNotGLSceneObjectsCheckBoxClick(Sender);
  144. MultiLightShaderCheckBoxClick(Sender);
  145. end;
  146. procedure TFormBumpShader.ShaderEnabledCheckBoxClick(Sender: TObject);
  147. begin
  148. MyBumpShader.Enabled := ShaderEnabledCheckBox.Checked;
  149. MultiLightShader.Enabled := ShaderEnabledCheckBox.Checked;
  150. end;
  151. procedure TFormBumpShader.MultiLightShaderCheckBoxClick(Sender: TObject);
  152. var
  153. I: Integer;
  154. begin
  155. if MultiLightShaderCheckBox.Checked then
  156. begin
  157. MaterialLibrary.LibMaterialByName('Earth').Shader := MultiLightShader;
  158. for I := 0 to TrinityMatlib.Materials.Count - 1 do
  159. TrinityMatlib.Materials[I].Shader := MultiLightShader;
  160. end
  161. else
  162. begin
  163. MaterialLibrary.LibMaterialByName('Earth').Shader := MyBumpShader;
  164. for I := 0 to TrinityMatlib.Materials.Count - 1 do
  165. TrinityMatlib.Materials[I].Shader := MyBumpShader;
  166. end;
  167. Light2.Shining := MultiLightShaderCheckBox.Checked;
  168. LightCube2.Visible := MultiLightShaderCheckBox.Checked;
  169. end;
  170. procedure TFormBumpShader.UseSpecularTextureCheckBoxClick(Sender: TObject);
  171. begin
  172. if UseSpecularTextureCheckBox.Checked then
  173. begin
  174. MyBumpShader.SpecularTexture := MaterialLibrary.LibMaterialByName('EarthGross').Material.Texture;
  175. MultiLightShader.SpecularTexture := MaterialLibrary.LibMaterialByName('EarthGross').Material.Texture;
  176. end
  177. else
  178. begin
  179. MyBumpShader.SpecularTexture := nil;
  180. MultiLightShader.SpecularTexture := nil;
  181. end;
  182. end;
  183. procedure TFormBumpShader.UseNormalTextureCheckBoxClick(Sender: TObject);
  184. begin
  185. if UseNormalTextureCheckBox.Checked then
  186. begin
  187. MyBumpShader.NormalTexture := MaterialLibrary.LibMaterialByName('EarthNormals').Material.Texture;
  188. MultiLightShader.NormalTexture := MaterialLibrary.LibMaterialByName('EarthNormals').Material.Texture;
  189. end
  190. else
  191. begin
  192. MyBumpShader.NormalTexture := nil;
  193. MultiLightShader.NormalTexture := nil;
  194. end;
  195. end;
  196. procedure TFormBumpShader.ShowNotGLSceneObjectsCheckBoxClick(
  197. Sender: TObject);
  198. begin
  199. actTeapot.Visible := ShowNotGLSceneObjectsCheckBox.Checked;
  200. actFighter.Visible := ShowNotGLSceneObjectsCheckBox.Checked;
  201. Cube.Visible := ShowNotGLSceneObjectsCheckBox.Checked;
  202. Dodecahedron.Visible := ShowNotGLSceneObjectsCheckBox.Checked;
  203. end;
  204. procedure TFormBumpShader.CadencerProgress(Sender: TObject; const DeltaTime, newTime: Double);
  205. begin
  206. Viewer.Invalidate;
  207. if RollPitchTurnCheckBox.Checked then
  208. begin
  209. actSphere_big.Turn(DeltaTime * 40);
  210. actSphere_big.Roll(DeltaTime * 40);
  211. actSphere_lit.Pitch(DeltaTime * 20);
  212. actFighter.Roll(DeltaTime * 20);
  213. actTeapot.Roll(-DeltaTime * 10);
  214. Cube.Pitch(-DeltaTime * 10);
  215. Dodecahedron.Pitch(DeltaTime * 10);
  216. end;
  217. end;
  218. procedure TFormBumpShader.ViewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  219. begin
  220. mx := X;
  221. my := Y;
  222. end;
  223. procedure TFormBumpShader.ViewerMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  224. begin
  225. if (ssRight in Shift) and (ssLeft in Shift) then
  226. Camera.AdjustDistanceToTarget(Power(1.01, Y - my))
  227. else
  228. if (ssRight in Shift) or (ssLeft in Shift) then
  229. Camera.MoveAroundTarget(my - Y, mx - X);
  230. mx := X;
  231. my := Y;
  232. end;
  233. procedure TFormBumpShader.Timer1Timer(Sender: TObject);
  234. begin
  235. Caption := 'GLSL Bump Shader - ' + Viewer.FramesPerSecondText;
  236. Viewer.ResetPerformanceMonitor;
  237. end;
  238. procedure TFormBumpShader.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  239. begin
  240. Camera.AdjustDistanceToTarget(Power(1.1, WheelDelta / 120));
  241. end;
  242. procedure TFormBumpShader.LightCubeProgress(Sender: TObject; const DeltaTime, newTime: Double);
  243. begin
  244. if LightMovingCheckBox.Checked then
  245. LightCube.MoveObjectAround(Camera.TargetObject, sin(NewTime) * DeltaTime * 10, DeltaTime * 20);
  246. end;
  247. procedure TFormBumpShader.FormClose(Sender: TObject; var Action: TCloseAction);
  248. begin
  249. Cadencer.Enabled := False;
  250. end;
  251. end.