fTilesD.pas 6.6 KB

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