1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| unit uMainForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
BZColors, BZGraphic, BZBitmap;
type
{ TBZBitmapRenderFiltersHelper }
TBZBitmapRenderFiltersHelper = class helper for TBZBitmapRenderFilters
public
procedure DiamondSquare(Const RangeOffset : Integer = 64; Const FallOff : Single = 0.8);
end;
{ TForm1 }
TForm1 = class(TForm)
pnlView: TPanel;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure pnlViewPaint(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
protected
FTexture : TBZBitmap;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses BZMath, BZTypesHelpers;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
FTexture := TBZBitmap.Create(512,512);
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
FreeAndNil(FTexture);
end;
procedure TForm1.pnlViewPaint(Sender: TObject);
begin
if Assigned(FTexture) then FTexture.DrawToCanvas(pnlView.Canvas, pnlView.ClientRect);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FTexture.RenderFilter.DiamondSquare();
pnlView.Invalidate;
end;
{ TBZBitmapRenderFiltersHelper }
{ https://fr.wikipedia.org/wiki/Algorithme_Diamant-Carré
https://yahiko.developpez.com/tutoriels/heightmap/
https://hiko-seijuro.developpez.com/articles/diamond-square/
https://stackoverflow.com/questions/2755750/diamond-square-algorithm
https://learn.64bitdragon.com/articles/computer-science/procedural-generation/the-diamond-square-algorithm }
procedure TBZBitmapRenderFiltersHelper.DiamondSquare(Const RangeOffset: Integer; Const FallOff: Single);
Var
TileSize, RandValue, rand, HalfSize : Integer;
xx, yy, xmax, ymax : integer;
AColor, AColorSum : TBZColorVector;
NeedResize : Boolean;
DstColor : TBZColor;
begin
// Ajustement des dimensions du bitmap si elles ne sont pas de l'ordre de 2n + 1
NeedResize := False;
xMax := OwnerBitmap.Width;
yMax := OwnerBitmap.Height;
if xMax.IsPowerOfTwo then
begin
xMax := xMax + 1;
NeedResize := True;
end;
if yMax.IsPowerOfTwo then
begin
yMax := yMax + 1;
NeedResize := True;
end;
if NeedResize then OwnerBitmap.SetSize(xMax, yMax);
OwnerBitmap.Clear(clrBlack);
Randomize;
xmax := OwnerBitmap.MaxWidth;//((x2-x1) div 2)-1;
ymax := OwnerBitmap.MaxHeight;//((y2-y1) div 2)-1;
randValue := random(256);
DstColor.Create(RandValue, RandValue, RandValue);
OwnerBitmap.setPixel(0,0, DstColor);
//randValue := random(MaxValue);
//RndColor.Create(RandValue, RandValue, RandValue);
OwnerBitmap.setPixel(xmax,0, DstColor);
//randValue := random(MaxValue);
//RndColor.Create(RandValue, RandValue, RandValue);
OwnerBitmap.setPixel(0,ymax, DstColor);
//randValue := random(MaxValue);
//RndColor.Create(RandValue, RandValue, RandValue);
OwnerBitmap.setPixel(xmax,ymax, DstColor);
TileSize := xmax;
RandValue := RangeOffset;
While TileSize > 1 do
begin
HalfSize := TileSize div 2; //((TileSize + 1) div 2) - 1;
// Diamond
xx := 0;
While (xx < OwnerBitmap.Width) do
begin
yy := 0;
While (yy < OwnerBitmap.Height) do
begin
AColorSum := ClrTransparent.AsColorVector;
AColor := OwnerBitmap.getPixel(xx, yy).AsColorVector;
AColorSum := AColorSum + AColor;
AColor := OwnerBitmap.getPixel(xx + TileSize, yy).AsColorVector;
AColorSum := AColorSum + AColor;
AColor := OwnerBitmap.getPixel(xx, yy + TileSize).AsColorVector;
AColorSum := AColorSum + AColor;
AColor := OwnerBitmap.getPixel(xx + TileSize, yy + TileSize).AsColorVector;
AColorSum := AColorSum + AColor;
AColorSum := AColorSum * 0.25;
Rand := Integer.RandomRange(-RandValue, RandValue);
AColor.Create(Rand * _FloatColorRatio);
AColorSum := AColorSum + AColor;
DstColor.Create(AColorSum);
OwnerBitmap.setPixel(xx + HalfSize, yy + HalfSize, DstColor);
yy := yy + TileSize;
end;
xx := xx + TileSize;
end;
// Square
xx := 0;
While (xx < OwnerBitmap.Width) do
begin
yy := (xx + HalfSize) mod TileSize;
While (yy < OwnerBitmap.Height) do
begin
AColorSum := ClrTransparent.AsColorVector;
AColor := OwnerBitmap.getPixel((xx - halfSize + xmax) mod xmax, yy).AsColorVector;
AColorSum := AColorSum + AColor;
AColor := OwnerBitmap.getPixel((xx + halfSize) mod xmax, yy).AsColorVector;
AColorSum := AColorSum + AColor;
AColor := OwnerBitmap.getPixel(xx, (yy + halfSize) mod ymax).AsColorVector;
AColorSum := AColorSum + AColor;
AColor := OwnerBitmap.getPixel(xx, (yy - halfSize + ymax) mod ymax).AsColorVector;
AColorSum := AColorSum + AColor;
AColorSum := AColorSum * 0.25;
Rand := Integer.RandomRange(-RandValue, RandValue);
AColor.Create(Rand * _FloatColorRatio);
AColorSum := AColorSum + AColor;
DstColor.Create(AColorSum);
OwnerBitmap.setPixel(xx, yy, DstColor);
if (xx = 0) then OwnerBitmap.setPixel(xmax, yy, DstColor);
if (yy = 0) then OwnerBitmap.setPixel(xx, ymax, DstColor);
yy := yy + TileSize;
end;
xx := xx + HalfSize;
end;
//RandValue := (RandValue div 2) + 1;
Randvalue := RandValue - Round((RandValue * 0.5) * FallOff);
TileSize := TileSize div 2;
end;
end;
end. |
Partager