fProcCloudsD.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. unit fProcCloudsD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. Vcl.Graphics,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. Vcl.StdCtrls,
  12. Vcl.ExtCtrls,
  13. Vcl.Samples.Spin,
  14. Vcl.ComCtrls,
  15. Vcl.Buttons,
  16. Vcl.Imaging.Jpeg,
  17. GLS.Scene,
  18. GLS.Objects,
  19. GLS.Texture,
  20. GLS.HUDObjects,
  21. GLS.Cadencer,
  22. GLS.SceneViewer,
  23. GLS.ProcTextures,
  24. GLS.TextureFormat,
  25. GLS.Coordinates,
  26. GLS.BaseClasses,
  27. GLS.Utils
  28. ;
  29. type
  30. TFormClouds = class(TForm)
  31. GLSceneViewer1: TGLSceneViewer;
  32. GLScene1: TGLScene;
  33. Camera: TGLCamera;
  34. Panel1: TPanel;
  35. CBFormat: TComboBox;
  36. Label2: TLabel;
  37. Label3: TLabel;
  38. CBCompression: TComboBox;
  39. Label5: TLabel;
  40. RBDefault: TRadioButton;
  41. RBDouble: TRadioButton;
  42. LAUsedMemory: TLabel;
  43. RBQuad: TRadioButton;
  44. LARGB32: TLabel;
  45. LACompression: TLabel;
  46. GLCadencer1: TGLCadencer;
  47. CheckBox1: TCheckBox;
  48. Label4: TLabel;
  49. SpinEdit1: TSpinEdit;
  50. SpinEdit2: TSpinEdit;
  51. Label6: TLabel;
  52. CheckBox2: TCheckBox;
  53. Plane: TGLPlane;
  54. TrackBar1: TTrackBar;
  55. Timer1: TTimer;
  56. CloudRandomSeedUsedEdit: TEdit;
  57. CloudImageSizeUsedEdit: TEdit;
  58. UseCloudFileCB: TCheckBox;
  59. CloudFileOpenBtn: TSpeedButton;
  60. CloudFileUsedEdit: TEdit;
  61. MakeAndSaveCloudNoiseFile: TSpeedButton;
  62. Label61: TLabel;
  63. OpenDialog1: TOpenDialog;
  64. SaveDialog1: TSaveDialog;
  65. LabelFPS: TLabel;
  66. procedure GLSceneViewer1AfterRender(Sender: TObject);
  67. procedure CBFormatChange(Sender: TObject);
  68. procedure GLCadencer1Progress(Sender: TObject;
  69. const deltaTime, newTime: Double);
  70. procedure TrackBar1Change(Sender: TObject);
  71. procedure Timer1Timer(Sender: TObject);
  72. procedure FormCreate(Sender: TObject);
  73. procedure CloudFileOpenBtnClick(Sender: TObject);
  74. procedure MakeAndSaveCloudNoiseFileClick(Sender: TObject);
  75. private
  76. public
  77. PathToCloud: TFileName;
  78. newSelection: Boolean;
  79. end;
  80. var
  81. FormClouds: TFormClouds;
  82. aPERM: array [0 .. 255] of Byte;
  83. outfile: TextFile;
  84. implementation
  85. {$R *.DFM}
  86. //-------------------------------------------------------------------
  87. procedure TFormClouds.FormCreate(Sender: TObject);
  88. begin
  89. // Our cloud files are here
  90. PathToCloud := GetCurrentAssetPath() + '\cloud';
  91. SetCurrentDir(PathToCloud);
  92. CBFormat.ItemIndex := 3;
  93. CBCompression.ItemIndex := 0;
  94. CBFormatChange(Sender);
  95. end;
  96. //-------------------------------------------------------------------
  97. procedure TFormClouds.GLCadencer1Progress(Sender: TObject;
  98. const deltaTime, newTime: Double);
  99. begin
  100. if CheckBox1.Checked then
  101. TGLProcTextureNoise(Plane.Material.Texture.Image)
  102. .NoiseAnimate(deltaTime);
  103. end;
  104. //-------------------------------------------------------------------
  105. procedure TFormClouds.TrackBar1Change(Sender: TObject);
  106. begin
  107. Plane.XTiles := TrackBar1.Position;
  108. Plane.YTiles := TrackBar1.Position;
  109. // EnvColor clrLightBlue TextureMode Blend
  110. end;
  111. //-------------------------------------------------------------------
  112. procedure TFormClouds.Timer1Timer(Sender: TObject);
  113. begin
  114. LabelFPS.Caption := GLSceneViewer1.FramesPerSecondText();
  115. GLSceneViewer1.ResetPerformanceMonitor();
  116. end;
  117. //-------------------------------------------------------------------
  118. procedure TFormClouds.GLSceneViewer1AfterRender(Sender: TObject);
  119. var
  120. rgb: Integer;
  121. begin
  122. // update compression stats, only the 1st time after a new selection
  123. if newSelection then
  124. rgb := Plane.Material.Texture.Image.Width *
  125. Plane.Material.Texture.Image.Height * 4;
  126. LARGB32.Caption := Format('RGBA 32bits would require %d kB',
  127. [rgb div 1024]);
  128. LAUsedMemory.Caption := Format('Required memory : %d kB',
  129. [Plane.Material.Texture.TextureImageRequiredMemory() div 1024]);
  130. LACompression.Caption := Format('Compression ratio : %d %%',
  131. [100 - 100 * Plane.Material.Texture.TextureImageRequiredMemory() div rgb]);
  132. newSelection := False;
  133. end;
  134. //-------------------------------------------------------------------
  135. procedure TFormClouds.CBFormatChange(Sender: TObject);
  136. var
  137. s: String;
  138. i: Integer;
  139. begin
  140. // adjust settings from selection and reload the texture map
  141. if (UseCloudFileCB.Checked and (FileExists(CloudFileUsedEdit.Text))) then
  142. begin
  143. try
  144. AssignFile(outfile, CloudFileUsedEdit.Text);
  145. // File selected in dialog box
  146. Reset(outfile);
  147. Readln(outfile, s);
  148. for i := 0 to 255 do
  149. begin
  150. Readln(outfile, s);
  151. aPERM[i] := strtoint(s);
  152. end;
  153. finally
  154. CloseFile(outfile);
  155. end;
  156. TGLProcTextureNoise(Plane.Material.Texture.Image).SetPermFromData(aPERM);
  157. end
  158. else
  159. TGLProcTextureNoise(Plane.Material.Texture.Image).SetPermToDefault;
  160. Plane.Material.Texture.TextureFormat := TGLTextureFormat(Integer(tfRGB) + CBFormat.ItemIndex);
  161. Plane.Material.Texture.Compression := TGLTextureCompression(Integer(tcNone) +
  162. CBCompression.ItemIndex);
  163. TGLProcTextureNoise(Plane.Material.Texture.Image).MinCut := SpinEdit1.Value;
  164. TGLProcTextureNoise(Plane.Material.Texture.Image).NoiseSharpness := SpinEdit2.Value / 100;
  165. TGLProcTextureNoise(Plane.Material.Texture.Image).Height := strtoint(CloudImageSizeUsedEdit.Text);
  166. TGLProcTextureNoise(Plane.Material.Texture.Image).Width := strtoint(CloudImageSizeUsedEdit.Text);
  167. TGLProcTextureNoise(Plane.Material.Texture.Image).NoiseRandSeed :=
  168. strtoint(CloudRandomSeedUsedEdit.Text);;
  169. TGLProcTextureNoise(Plane.Material.Texture.Image).Seamless := CheckBox2.Checked;
  170. if RBDefault.Checked then
  171. begin
  172. Plane.Width := 50;
  173. Plane.Height := 50;
  174. end
  175. else if RBDouble.Checked then
  176. begin
  177. Plane.Width := 100;
  178. Plane.Height := 100;
  179. end
  180. else
  181. begin
  182. Plane.Width := 400;
  183. Plane.Height := 400;
  184. end;
  185. newSelection := True;
  186. end;
  187. //-------------------------------------------------------------------
  188. procedure TFormClouds.CloudFileOpenBtnClick(Sender: TObject);
  189. begin
  190. OpenDialog1.InitialDir := PathToCloud;
  191. OpenDialog1.Filter := 'Cloud base (*.clb)|*.clb';
  192. OpenDialog1.FileName := '*.clb';
  193. if OpenDialog1.Execute() then
  194. CloudFileUsedEdit.Text := OpenDialog1.FileName;
  195. end;
  196. //-------------------------------------------------------------------
  197. procedure TFormClouds.MakeAndSaveCloudNoiseFileClick(Sender: TObject);
  198. var
  199. i: Integer;
  200. (*sub*)procedure RandomPerm;
  201. var
  202. Id, Count, More, Less, Again: Integer;
  203. begin
  204. MakeAndSaveCloudNoiseFile.Caption := IntToStr(0);
  205. Application.ProcessMessages;
  206. for Id := 0 to 255 do
  207. begin
  208. aPERM[Id] := Random(256);
  209. // Label61.Caption:= IntToStr(Id);
  210. // Application.ProcessMessages;
  211. end;
  212. Count := 0;
  213. repeat
  214. again := 0;
  215. Less := Random(256);
  216. for Id := 0 to Count do
  217. begin
  218. More := aPERM[Id];
  219. If (Less = More) then
  220. Inc(Again);
  221. end;
  222. Label61.Caption := IntToStr(again);
  223. // these can be removed.. just for debugging
  224. Application.ProcessMessages;
  225. if (again = 0) then
  226. begin
  227. aPERM[Count + 1] := Less;
  228. Inc(Count);
  229. MakeAndSaveCloudNoiseFile.Caption := IntToStr(Less) + ',' +
  230. IntToStr(Count);
  231. Application.ProcessMessages;
  232. end;
  233. until Count = 255
  234. end;
  235. begin
  236. SaveDialog1.InitialDir := PathToCloud;
  237. SaveDialog1.Filter := 'Cloud base (*.clb)|*.clb';
  238. SaveDialog1.DefaultExt := 'rnd';
  239. SaveDialog1.Filename := '*.clb';
  240. if (SaveDialog1.Execute()) then
  241. begin
  242. if UpperCase(ExtractFileExt(SaveDialog1.Filename)) = '.clb' then
  243. begin
  244. Application.ProcessMessages;
  245. Randomize();
  246. RandomPerm();
  247. try
  248. AssignFile(outfile, SaveDialog1.Filename);
  249. // File selected in dialog box
  250. Rewrite(outfile);
  251. Writeln(outfile, 'Cloud Base V1.0');
  252. for i := 0 to 255 do
  253. Writeln(outfile, IntToStr(aPERM[i]));
  254. finally
  255. CloseFile(outfile);
  256. end;
  257. Label61.Caption := 'Done';
  258. MakeAndSaveCloudNoiseFile.Caption := '';
  259. end;
  260. end;
  261. end;
  262. end.