fProcClouds.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. unit fProcClouds;
  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. implementation
  80. {$R *.DFM}
  81. procedure TFormClouds.FormCreate(Sender: TObject);
  82. begin
  83. CBFormat.ItemIndex := 3;
  84. CBCompression.ItemIndex := 0;
  85. CBFormatChange(Sender);
  86. end;
  87. procedure TFormClouds.GLCadencer1Progress(Sender: TObject;
  88. const deltaTime, newTime: Double);
  89. begin
  90. if CheckBox1.Checked then
  91. TGLProcTextureNoise(Plane.Material.Texture.Image)
  92. .NoiseAnimate(deltaTime);
  93. end;
  94. procedure TFormClouds.TrackBar1Change(Sender: TObject);
  95. begin
  96. Plane.XTiles := TrackBar1.Position;
  97. Plane.YTiles := TrackBar1.Position;
  98. { EnvColor clrLightBlue TextureMode Blend }
  99. end;
  100. procedure TFormClouds.Timer1Timer(Sender: TObject);
  101. begin
  102. LabelFPS.Caption := GLSceneViewer1.FramesPerSecondText;
  103. GLSceneViewer1.ResetPerformanceMonitor;
  104. end;
  105. procedure TFormClouds.GLSceneViewer1AfterRender(Sender: TObject);
  106. var
  107. rgb: Integer;
  108. begin
  109. // update compression stats, only the 1st time after a new selection
  110. if newSelection then
  111. rgb := Plane.Material.Texture.Image.Width *
  112. Plane.Material.Texture.Image.Height * 4;
  113. LARGB32.Caption := Format('RGBA 32bits would require %d kB',
  114. [rgb div 1024]);
  115. LAUsedMemory.Caption := Format('Required memory : %d kB',
  116. [Plane.Material.Texture.TextureImageRequiredMemory div 1024]);
  117. LACompression.Caption := Format('Compression ratio : %d %%',
  118. [100 - 100 * Plane.Material.Texture.TextureImageRequiredMemory div rgb]);
  119. newSelection := False;
  120. end;
  121. procedure TFormClouds.CBFormatChange(Sender: TObject);
  122. var
  123. aPERM: array [0 .. 255] of Byte;
  124. outfile: Textfile;
  125. s: string;
  126. i: Integer;
  127. begin
  128. // adjust settings from selection and reload the texture map
  129. with Plane.Material.Texture do
  130. begin
  131. If (UseCloudFileCB.Checked and (FileExists(CloudFileUsedEdit.Text))) then
  132. begin
  133. try
  134. AssignFile(outfile, CloudFileUsedEdit.Text);
  135. { File selected in dialog box }
  136. Reset(outfile);
  137. Readln(outfile, s { 'Cloud Base V1.0' } );
  138. For i := 0 to 255 do
  139. begin
  140. Readln(outfile, s);
  141. aPERM[i] := strtoint(s);
  142. end;
  143. finally
  144. CloseFile(outfile);
  145. end;
  146. TGLProcTextureNoise(Image).SetPermFromData(aPERM);
  147. end
  148. else
  149. TGLProcTextureNoise(Image).SetPermToDefault;
  150. TextureFormat := TGLTextureFormat(Integer(tfRGB) + CBFormat.ItemIndex);
  151. Compression := TGLTextureCompression(Integer(tcNone) +
  152. CBCompression.ItemIndex);
  153. TGLProcTextureNoise(Image).MinCut := SpinEdit1.Value;
  154. TGLProcTextureNoise(Image).NoiseSharpness := SpinEdit2.Value / 100;
  155. TGLProcTextureNoise(Image).Height := strtoint(CloudImageSizeUsedEdit.Text);
  156. TGLProcTextureNoise(Image).Width := strtoint(CloudImageSizeUsedEdit.Text);
  157. TGLProcTextureNoise(Image).NoiseRandSeed :=
  158. strtoint(CloudRandomSeedUsedEdit.Text);;
  159. TGLProcTextureNoise(Image).Seamless := CheckBox2.Checked;
  160. if RBDefault.Checked then
  161. begin
  162. Plane.Width := 50;
  163. Plane.Height := 50;
  164. end
  165. else if RBDouble.Checked then
  166. begin
  167. Plane.Width := 100;
  168. Plane.Height := 100;
  169. end
  170. else
  171. begin
  172. Plane.Width := 400;
  173. Plane.Height := 400;
  174. end;
  175. end;
  176. newSelection := True;
  177. end;
  178. procedure TFormClouds.CloudFileOpenBtnClick(Sender: TObject);
  179. begin
  180. OpenDialog1.Filter := 'Cloud base (*.clb)|*.clb';
  181. OpenDialog1.InitialDir := ExtractFilePath(ParamStr(0));
  182. OpenDialog1.Filename := '*.clb';
  183. if OpenDialog1.Execute then
  184. begin
  185. CloudFileUsedEdit.Text := OpenDialog1.Filename;
  186. end;
  187. end;
  188. procedure TFormClouds.MakeAndSaveCloudNoiseFileClick(Sender: TObject);
  189. var
  190. aPERM: array [0 .. 255] of Byte;
  191. outfile: Textfile;
  192. i: Integer;
  193. procedure RandomPerm;
  194. var
  195. Idiot, Count, More, Less, again: Integer;
  196. begin
  197. MakeAndSaveCloudNoiseFile.Caption := inttostr(0);
  198. Application.ProcessMessages;
  199. For Idiot := 0 to 255 do
  200. begin
  201. aPERM[Idiot] := Random(256);
  202. // Label61.Caption:= inttostr(Idiot);
  203. // Application.ProcessMessages;
  204. end;
  205. Count := 0;
  206. repeat
  207. again := 0;
  208. Less := Random(256);
  209. For Idiot := 0 to Count do
  210. begin
  211. More := aPERM[Idiot];
  212. If (Less = More) then
  213. inc(again);
  214. end;
  215. Label61.Caption := inttostr(again);
  216. // these can be removed.. just for debugging
  217. Application.ProcessMessages;
  218. If (again = 0) then
  219. begin
  220. aPERM[Count + 1] := Less;
  221. inc(Count);
  222. MakeAndSaveCloudNoiseFile.Caption := inttostr(Less) + ',' +
  223. inttostr(Count);
  224. Application.ProcessMessages;
  225. end;
  226. until Count = 255 end;
  227. begin
  228. SaveDialog1.Filter := 'Cloud base (*.clb)|*.clb';
  229. SaveDialog1.InitialDir := ExtractFilePath(ParamStr(0));
  230. SaveDialog1.DefaultExt := 'rnd';
  231. SaveDialog1.Filename := '*.clb';
  232. if (SaveDialog1.Execute) then
  233. begin
  234. if UpperCase(ExtractFileExt(SaveDialog1.Filename)) = '.CLB' then
  235. begin
  236. Application.ProcessMessages;
  237. Randomize;
  238. RandomPerm;
  239. try
  240. AssignFile(outfile, SaveDialog1.Filename);
  241. { File selected in dialog box }
  242. Rewrite(outfile);
  243. Writeln(outfile, 'Cloud Base V1.0');
  244. for i := 0 to 255 do
  245. Writeln(outfile, inttostr(aPERM[i]));
  246. finally
  247. CloseFile(outfile);
  248. end;
  249. Label61.Caption := 'Done';
  250. MakeAndSaveCloudNoiseFile.Caption := '';
  251. end;
  252. end;
  253. end;
  254. end.