fBunnyBump.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. unit fBunnyBump;
  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. GLS.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. GLS.VectorGeometry,
  27. GLS.Context,
  28. GLS.FileOBJ,
  29. GLS.Utils,
  30. GLSL.BumpShaders;
  31. type
  32. TForm1 = class(TForm)
  33. GLSceneViewer1: TGLSceneViewer;
  34. GLScene1: TGLScene;
  35. GLCadencer1: TGLCadencer;
  36. GLMaterialLibrary1: 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. CheckBox1: TCheckBox;
  47. CheckBox2: TCheckBox;
  48. CheckBox3: TCheckBox;
  49. Shape1: TShape;
  50. Shape2: TShape;
  51. Shape3: 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. procedure TForm1.FormCreate(Sender: TObject);
  86. begin
  87. SetGLSceneMediaDir();
  88. // Load the bunny mesh and scale for viewing
  89. ffBunny.LoadFromFile('bunny.glsm');
  90. // ffBunny.LoadFromFile('bunny.obj');
  91. ffBunny.Scale.Scale(2 / ffBunny.BoundingSphereRadius);
  92. // Load the normal map
  93. with GLMaterialLibrary1.Materials[0].Material.Texture.Image do
  94. LoadFromFile('bunnynormals.jpg');
  95. // Link the lights to their toggles
  96. CheckBox1.Tag := Integer(WhiteLight);
  97. CheckBox2.Tag := Integer(RedLight);
  98. CheckBox3.Tag := Integer(BlueLight);
  99. Shape1.Tag := Integer(WhiteLight);
  100. Shape2.Tag := Integer(RedLight);
  101. Shape3.Tag := Integer(BlueLight);
  102. ComboBox1.ItemIndex := 0;
  103. ComboBox1Change(nil);
  104. StartHeight := Height;
  105. end;
  106. procedure TForm1.GLCadencer1Progress(Sender: TObject;
  107. const deltaTime, newTime: Double);
  108. begin
  109. // Orbit the camera
  110. if (dx <> 0) or (dy <> 0) then
  111. begin
  112. Camera.MoveAroundTarget(dy, dx);
  113. dx := 0;
  114. dy := 0;
  115. end;
  116. // Rotate the light sources
  117. if CheckBox4.Checked then
  118. DCLights.Turn(deltaTime * 20);
  119. GLSceneViewer1.Invalidate;
  120. end;
  121. procedure TForm1.CheckBoxClick(Sender: TObject);
  122. begin
  123. // Light Shining CheckBox
  124. TGLLightSource(TCheckBox(Sender).Tag).Shining := TCheckBox(Sender).Checked;
  125. end;
  126. procedure TForm1.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
  127. Shift: TShiftState; X, Y: Integer);
  128. begin
  129. // Light Color Dialog
  130. ColorDialog1.Color := TShape(Sender).Brush.Color;
  131. if ColorDialog1.Execute then
  132. begin
  133. TShape(Sender).Brush.Color := ColorDialog1.Color;
  134. with TGLLightSource(TShape(Sender).Tag) do
  135. Diffuse.AsWinColor := ColorDialog1.Color;
  136. end;
  137. end;
  138. procedure TForm1.ComboBox1Change(Sender: TObject);
  139. begin
  140. if ComboBox1.Text = 'Per-Vertex' then
  141. ffBunny.Material.LibMaterialName := ''
  142. else if ComboBox1.Text = 'Dot3 Texture Combiner' then
  143. begin
  144. ffBunny.Material.LibMaterialName := 'Bump';
  145. GLBumpShader1.BumpMethod := bmDot3TexCombiner;
  146. end
  147. else if ComboBox1.Text = 'Basic Fragment Program' then
  148. begin
  149. ffBunny.Material.LibMaterialName := 'Bump';
  150. GLBumpShader1.BumpMethod := bmBasicARBFP;
  151. end;
  152. end;
  153. procedure TForm1.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  154. Shift: TShiftState; X, Y: Integer);
  155. begin
  156. mx := X;
  157. my := Y;
  158. dx := 0;
  159. dy := 0;
  160. end;
  161. procedure TForm1.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  162. X, Y: Integer);
  163. begin
  164. if ssLeft in Shift then
  165. begin
  166. dx := dx + (mx - X);
  167. dy := dy + (my - Y);
  168. end
  169. else
  170. begin
  171. dx := 0;
  172. dy := 0;
  173. end;
  174. mx := X;
  175. my := Y;
  176. end;
  177. procedure TForm1.AsyncTimer1Timer(Sender: TObject);
  178. begin
  179. LabelFPS.Caption := GLSceneViewer1.FramesPerSecondText;
  180. GLSceneViewer1.ResetPerformanceMonitor;
  181. end;
  182. procedure TForm1.FormResize(Sender: TObject);
  183. begin
  184. Camera.SceneScale := Height / StartHeight;
  185. end;
  186. procedure TForm1.GLSceneViewer1BeforeRender(Sender: TObject);
  187. begin
  188. if IsInitialized then
  189. exit;
  190. if GL.ARB_multitexture and GL.ARB_vertex_program and GL.ARB_texture_env_dot3
  191. then
  192. ComboBox1.Items.Add('Dot3 Texture Combiner');
  193. if GL.ARB_multitexture and GL.ARB_vertex_program and GL.ARB_fragment_program
  194. then
  195. begin
  196. ComboBox1.Items.Add('Basic Fragment Program');
  197. if GLSceneViewer1.Buffer.LimitOf[limNbTextureUnits] < 3 then
  198. GLBumpShader1.SpecularMode := smOff;
  199. end;
  200. IsInitialized := True;
  201. end;
  202. procedure TForm1.ComboBox2Change(Sender: TObject);
  203. begin
  204. case ComboBox2.ItemIndex of
  205. 0:
  206. GLBumpShader1.SpecularMode := smOff;
  207. 1:
  208. GLBumpShader1.SpecularMode := smBlinn;
  209. 2:
  210. GLBumpShader1.SpecularMode := smPhong;
  211. end;
  212. end;
  213. end.