fTilesD.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. unit fTilesD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. Winapi.Windows,
  6. System.SysUtils,
  7. System.Classes,
  8. System.Math,
  9. Vcl.Graphics,
  10. Vcl.Controls,
  11. Vcl.Forms,
  12. Vcl.Dialogs,
  13. Vcl.ExtCtrls,
  14. Vcl.Imaging.Jpeg,
  15. Vcl.StdCtrls,
  16. Stage.VectorTypes,
  17. GLS.Coordinates,
  18. GLS.BaseClasses,
  19. Stage.Keyboard,
  20. Stage.VectorGeometry,
  21. Stage.TextureFormat,
  22. GLS.XCollection,
  23. Stage.Utils,
  24. GLS.Objects,
  25. GLS.Graph,
  26. GLS.Scene,
  27. GLS.SceneViewer,
  28. GLS.TilePlane,
  29. GLS.Texture,
  30. GLS.Cadencer,
  31. GLS.Context,
  32. GLS.Material,
  33. GLS.RenderContextInfo,
  34. GLS.SimpleNavigation;
  35. type
  36. TFormTiles = class(TForm)
  37. GLScene: TGLScene;
  38. GLSceneViewer1: TGLSceneViewer;
  39. GLCamera: TGLCamera;
  40. dcTarget: TGLDummyCube;
  41. XYZGrid: TGLXYZGrid;
  42. Panel1: TPanel;
  43. GLMaterialLibrary: TGLMaterialLibrary;
  44. LightSource: TGLLightSource;
  45. Timer1: TTimer;
  46. GLCadencer1: TGLCadencer;
  47. Label1: TLabel;
  48. CBMaterial: TComboBox;
  49. TilePlane: TGLTilePlane;
  50. DirectOpenGL: TGLDirectOpenGL;
  51. dcSelection: TGLDummyCube;
  52. GLLines1: TGLLines;
  53. BUPack: TButton;
  54. Label2: TLabel;
  55. CBShowGrid: TCheckBox;
  56. CBSortByMaterials: TCheckBox;
  57. GLSimpleNavigation1: TGLSimpleNavigation;
  58. DummyCube: TGLDummyCube;
  59. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  60. Shift: TShiftState; X, Y: Integer);
  61. procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
  62. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  63. procedure FormCreate(Sender: TObject);
  64. procedure Timer1Timer(Sender: TObject);
  65. procedure GLCadencer1Progress(Sender: TObject;
  66. const deltaTime, newTime: Double);
  67. procedure DirectOpenGLRender(Sender: TObject;
  68. var rci: TGLRenderContextInfo);
  69. procedure BUPackClick(Sender: TObject);
  70. procedure CBShowGridClick(Sender: TObject);
  71. procedure CBSortByMaterialsClick(Sender: TObject);
  72. private
  73. public
  74. mx, my: Integer;
  75. tileX, tileY: Integer;
  76. mip, translateOffset: TGLVector;
  77. translating: Boolean;
  78. end;
  79. var
  80. FormTiles: TFormTiles;
  81. implementation
  82. {$R *.dfm}
  83. //------------------------------------------------------
  84. procedure TFormTiles.FormCreate(Sender: TObject);
  85. var
  86. i, j: Integer;
  87. begin
  88. var Path: TFileName := GetCurrentAssetPath();
  89. SetCurrentDir(Path + '\texture');
  90. GLMaterialLibrary.TexturePaths := GetCurrentDir();
  91. GLMaterialLibrary.LibMaterialByName('beigemarble').Material.Texture.Image.LoadFromFile('beigemarble.jpg');
  92. GLMaterialLibrary.LibMaterialByName('marbletiles').Material.Texture.Image.LoadFromFile('marbletiles.jpg');
  93. GLMaterialLibrary.LibMaterialByName('walkway').Material.Texture.Image.LoadFromFile('walkway.jpg');
  94. // fill the tiled area with random tiles
  95. RandSeed := 0;
  96. for i := -20 to 20 do
  97. for j := -20 to 20 do
  98. TilePlane.Tiles[i, j] :=
  99. Random(GLMaterialLibrary.Materials.Count - 1) + 1;
  100. // set all tile materials to anisotropic,
  101. // add them to the material selection combo
  102. for i := 0 to GLMaterialLibrary.Materials.Count - 1 do
  103. begin
  104. GLMaterialLibrary.Materials[i].Material.Texture.FilteringQuality :=
  105. tfAnisotropic;
  106. CBMaterial.Items.Add(GLMaterialLibrary.Materials[i].Name);
  107. end;
  108. CBMaterial.ItemIndex := 0;
  109. end;
  110. //------------------------------------------------------
  111. procedure TFormTiles.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  112. Shift: TShiftState; X, Y: Integer);
  113. begin
  114. mx := X;
  115. my := Y;
  116. if Shift = [ssLeft] then
  117. begin
  118. TilePlane.Tiles[tileX, tileY] := CBMaterial.ItemIndex;
  119. TilePlane.StructureChanged;
  120. end
  121. else if Shift = [ssRight] then
  122. begin
  123. TilePlane.Tiles[tileX, tileY] := 0;
  124. TilePlane.StructureChanged;
  125. end;
  126. end;
  127. //------------------------------------------------------
  128. procedure TFormTiles.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  129. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  130. begin
  131. GLCamera.AdjustDistanceToTarget(Power(1.1, WheelDelta / 120));
  132. end;
  133. //------------------------------------------------------
  134. procedure TFormTiles.GLCadencer1Progress(Sender: TObject;
  135. const deltaTime, newTime: Double);
  136. var
  137. ip: TGLVector;
  138. mp: TPoint;
  139. shiftDown: Boolean;
  140. begin
  141. shiftDown := (IsKeyDown(VK_LSHIFT) or IsKeyDown(VK_RSHIFT));
  142. DCSelection.Visible := not shiftDown;
  143. if DCSelection.Visible then
  144. GLSceneViewer1.Cursor := crDefault
  145. else
  146. GLSceneViewer1.Cursor := crHandPoint;
  147. GetCursorPos(mp);
  148. mp := GLSceneViewer1.ScreenToClient(mp);
  149. if PtInRect(GLSceneViewer1.ClientRect, mp) then
  150. begin
  151. GLSceneViewer1.Buffer.ScreenVectorIntersectWithPlaneXY
  152. (VectorMake(mp.X, GLSceneViewer1.Height - mp.Y, 0), 0, ip);
  153. tileX := Round(ip.X - 0.5);
  154. tileY := Round(ip.Y - 0.5);
  155. DCSelection.Position.SetPoint(tileX, tileY, 0);
  156. if shiftDown then
  157. begin
  158. if IsKeyDown(VK_LBUTTON) then
  159. begin
  160. if not translating then
  161. begin
  162. translateOffset := ip;
  163. translating := True;
  164. end;
  165. DCTarget.Position.Translate(VectorAdd(VectorSubtract(mip, ip),
  166. translateOffset))
  167. end
  168. else
  169. translating := False;
  170. if IsKeyDown(VK_RBUTTON) then
  171. begin
  172. GLCamera.MoveAroundTarget((my - mp.Y) * 0.5, (mx - mp.X) * 0.5);
  173. end;
  174. end
  175. else
  176. begin
  177. translating := False;
  178. if IsKeyDown(VK_LBUTTON) then
  179. begin
  180. TilePlane.Tiles[tileX, tileY] := CBMaterial.ItemIndex;
  181. TilePlane.StructureChanged;
  182. end;
  183. if IsKeyDown(VK_RBUTTON) then
  184. begin
  185. TilePlane.Tiles[tileX, tileY] := 0;
  186. TilePlane.StructureChanged;
  187. end;
  188. end;
  189. mx := mp.X;
  190. my := mp.Y;
  191. end;
  192. GLSceneViewer1.Invalidate;
  193. end;
  194. //------------------------------------------------------
  195. procedure TFormTiles.DirectOpenGLRender(Sender: TObject;
  196. var rci: TGLRenderContextInfo);
  197. begin
  198. // we clear the depth buffer, so that the grid is always in front of the
  199. // tile plane and won't Z-Fight with it
  200. glClear(GL_DEPTH_BUFFER_BIT);
  201. end;
  202. //------------------------------------------------------
  203. procedure TFormTiles.BUPackClick(Sender: TObject);
  204. begin
  205. // packing a tile area removes unused area from the in-memory structures
  206. TilePlane.Tiles.Pack;
  207. end;
  208. //------------------------------------------------------
  209. procedure TFormTiles.CBShowGridClick(Sender: TObject);
  210. begin
  211. XYZGrid.Visible := CBShowGrid.Checked;
  212. end;
  213. //------------------------------------------------------
  214. procedure TFormTiles.CBSortByMaterialsClick(Sender: TObject);
  215. begin
  216. TilePlane.SortByMaterials := CBSortByMaterials.Checked;
  217. end;
  218. //------------------------------------------------------
  219. procedure TFormTiles.Timer1Timer(Sender: TObject);
  220. begin
  221. Caption := GLSceneViewer1.FramesPerSecondText;
  222. GLSceneViewer1.ResetPerformanceMonitor;
  223. end;
  224. end.