fProcCloudsD.pas 7.7 KB

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