fdBunnyBump.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. unit fdBunnyBump;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. System.UITypes,
  8. Vcl.Graphics,
  9. Vcl.Controls,
  10. Vcl.Forms,
  11. Vcl.Dialogs,
  12. Vcl.ExtCtrls,
  13. Vcl.StdCtrls,
  14. Vcl.Imaging.Jpeg,
  15. GLS.Scene,
  16. Stage.VectorTypes,
  17. GLS.Objects,
  18. GLS.Texture,
  19. GLS.VectorFileObjects,
  20. GLS.Cadencer,
  21. GLS.SceneViewer,
  22. GLS.AsyncTimer,
  23. GLS.Material,
  24. GLS.Coordinates,
  25. GLS.BaseClasses,
  26. Stage.VectorGeometry,
  27. GLS.Context,
  28. GLS.FileOBJ,
  29. Stage.Utils,
  30. GLSL.BumpShaders;
  31. type
  32. TForm1 = class(TForm)
  33. GLSceneViewer1: TGLSceneViewer;
  34. GLScene1: TGLScene;
  35. GLCadencer1: TGLCadencer;
  36. MatLib: TGLMaterialLibrary;
  37. Camera: TGLCamera;
  38. WhiteLight: TGLLightSource;
  39. RedLight: TGLLightSource;
  40. BlueLight: TGLLightSource;
  41. GLBumpShader1: TGLBumpShader;
  42. Panel1: TPanel;
  43. ComboBox1: TComboBox;
  44. Label1: TLabel;
  45. GroupBox1: TGroupBox;
  46. cbWhite: TCheckBox;
  47. cbRed: TCheckBox;
  48. cbBlue: TCheckBox;
  49. ShapeWhite: TShape;
  50. ShapeRed: TShape;
  51. ShapeBlue: TShape;
  52. ColorDialog1: TColorDialog;
  53. DCLights: TGLDummyCube;
  54. AsyncTimer1: TGLAsyncTimer;
  55. CheckBox4: TCheckBox;
  56. ComboBox2: TComboBox;
  57. Label2: TLabel;
  58. LabelFPS: TLabel;
  59. ffBunny: TGLFreeForm;
  60. procedure FormCreate(Sender: TObject);
  61. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  62. Shift: TShiftState; X, Y: Integer);
  63. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  64. X, Y: Integer);
  65. procedure GLCadencer1Progress(Sender: TObject;
  66. const deltaTime, newTime: Double);
  67. procedure ShapeMouseDown(Sender: TObject; Button: TMouseButton;
  68. Shift: TShiftState; X, Y: Integer);
  69. procedure CheckBoxClick(Sender: TObject);
  70. procedure AsyncTimer1Timer(Sender: TObject);
  71. procedure ComboBox1Change(Sender: TObject);
  72. procedure FormResize(Sender: TObject);
  73. procedure GLSceneViewer1BeforeRender(Sender: TObject);
  74. procedure ComboBox2Change(Sender: TObject);
  75. private
  76. public
  77. mx, my, dx, dy: Integer;
  78. IsInitialized: Boolean;
  79. StartHeight: Integer;
  80. end;
  81. var
  82. Form1: TForm1;
  83. implementation //=============================================================
  84. {$R *.dfm}
  85. //----------------------------------------------------------------------------
  86. procedure TForm1.FormCreate(Sender: TObject);
  87. begin
  88. var Path: TFileName := GetCurrentAssetPath();
  89. SetCurrentDir(Path + '\modelext');
  90. // Load the bunny mesh and scale for viewing
  91. ffBunny.LoadFromFile('bunny.glsm');
  92. // ffBunny.LoadFromFile('bunny.obj');
  93. ffBunny.Scale.Scale(2 / ffBunny.BoundingSphereRadius);
  94. ffBunny.RollAngle := 90;
  95. ffBunny.TurnAngle := 90;
  96. // Load the normal map
  97. SetCurrentDir(Path + '\skin');
  98. MatLib.Materials[0].Material.Texture.Image.LoadFromFile('bunnynormals.jpg');
  99. // Link the lights to their toggles
  100. cbWhite.Tag := Integer(WhiteLight);
  101. cbRed.Tag := Integer(RedLight);
  102. cbBlue.Tag := Integer(BlueLight);
  103. ShapeWhite.Tag := Integer(WhiteLight);
  104. ShapeRed.Tag := Integer(RedLight);
  105. ShapeBlue.Tag := Integer(BlueLight);
  106. ComboBox1.ItemIndex := 0;
  107. ComboBox1Change(Self);
  108. StartHeight := Height;
  109. end;
  110. //----------------------------------------------------------------------------
  111. procedure TForm1.GLCadencer1Progress(Sender: TObject;
  112. const deltaTime, newTime: Double);
  113. begin
  114. // Orbit the camera
  115. if (dx <> 0) or (dy <> 0) then
  116. begin
  117. Camera.MoveAroundTarget(dy, dx);
  118. dx := 0;
  119. dy := 0;
  120. end;
  121. // Rotate the light sources
  122. if CheckBox4.Checked then
  123. DCLights.Turn(deltaTime * 20);
  124. GLSceneViewer1.Invalidate;
  125. end;
  126. //----------------------------------------------------------------------------
  127. procedure TForm1.CheckBoxClick(Sender: TObject);
  128. begin
  129. // Light Shining CheckBox
  130. TGLLightSource(TCheckBox(Sender).Tag).Shining := TCheckBox(Sender).Checked;
  131. end;
  132. //----------------------------------------------------------------------------
  133. procedure TForm1.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
  134. Shift: TShiftState; X, Y: Integer);
  135. begin
  136. // Light Color Dialog
  137. ColorDialog1.Color := TShape(Sender).Brush.Color;
  138. if ColorDialog1.Execute then
  139. begin
  140. TShape(Sender).Brush.Color := ColorDialog1.Color;
  141. with TGLLightSource(TShape(Sender).Tag) do
  142. Diffuse.AsWinColor := ColorDialog1.Color;
  143. end;
  144. end;
  145. //----------------------------------------------------------------------------
  146. procedure TForm1.ComboBox1Change(Sender: TObject);
  147. begin
  148. if ComboBox1.Text = 'Per-Vertex' then
  149. ffBunny.Material.LibMaterialName := ''
  150. else if ComboBox1.Text = 'Dot3 Texture Combiner' then
  151. begin
  152. ffBunny.Material.LibMaterialName := 'Bump';
  153. GLBumpShader1.BumpMethod := bmDot3TexCombiner;
  154. end
  155. else if ComboBox1.Text = 'Basic Fragment Program' then
  156. begin
  157. ffBunny.Material.LibMaterialName := 'Bump';
  158. GLBumpShader1.BumpMethod := bmBasicARBFP;
  159. end;
  160. end;
  161. //----------------------------------------------------------------------------
  162. procedure TForm1.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  163. Shift: TShiftState; X, Y: Integer);
  164. begin
  165. mx := X;
  166. my := Y;
  167. dx := 0;
  168. dy := 0;
  169. end;
  170. //----------------------------------------------------------------------------
  171. procedure TForm1.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  172. X, Y: Integer);
  173. begin
  174. if ssLeft in Shift then
  175. begin
  176. dx := dx + (mx - X);
  177. dy := dy + (my - Y);
  178. end
  179. else
  180. begin
  181. dx := 0;
  182. dy := 0;
  183. end;
  184. mx := X;
  185. my := Y;
  186. end;
  187. //----------------------------------------------------------------------------
  188. procedure TForm1.AsyncTimer1Timer(Sender: TObject);
  189. begin
  190. LabelFPS.Caption := GLSceneViewer1.FramesPerSecondText;
  191. GLSceneViewer1.ResetPerformanceMonitor;
  192. end;
  193. //----------------------------------------------------------------------------
  194. procedure TForm1.FormResize(Sender: TObject);
  195. begin
  196. Camera.SceneScale := Height / StartHeight;
  197. end;
  198. //----------------------------------------------------------------------------
  199. procedure TForm1.GLSceneViewer1BeforeRender(Sender: TObject);
  200. begin
  201. if IsInitialized then
  202. exit;
  203. if GL.ARB_multitexture and GL.ARB_vertex_program and GL.ARB_texture_env_dot3
  204. then
  205. ComboBox1.Items.Add('Dot3 Texture Combiner');
  206. if GL.ARB_multitexture and GL.ARB_vertex_program and GL.ARB_fragment_program
  207. then
  208. begin
  209. ComboBox1.Items.Add('Basic Fragment Program');
  210. if GLSceneViewer1.Buffer.LimitOf[limNbTextureUnits] < 3 then
  211. GLBumpShader1.SpecularMode := smOff;
  212. end;
  213. IsInitialized := True;
  214. end;
  215. //----------------------------------------------------------------------------
  216. procedure TForm1.ComboBox2Change(Sender: TObject);
  217. begin
  218. case ComboBox2.ItemIndex of
  219. 0:
  220. GLBumpShader1.SpecularMode := smOff;
  221. 1:
  222. GLBumpShader1.SpecularMode := smBlinn;
  223. 2:
  224. GLBumpShader1.SpecularMode := smPhong;
  225. end;
  226. end;
  227. end.