fBunnyBumpD.pas 5.8 KB

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