-
Notifications
You must be signed in to change notification settings - Fork 8
/
Terrain.pas
156 lines (128 loc) · 3.97 KB
/
Terrain.pas
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
unit Terrain;
interface
uses
Noise, Matrix, MemoryBuffer,
GLTypes, GLUtils,
WebGL, JS,
Math;
type
TTerrain = class (TObject)
public
constructor Create (context: TJSWebGLRenderingContext; size, resolution: integer); overload;
constructor Create (context: TJSWebGLRenderingContext; inNoise: TNoise; size, resolution: integer; offset: TVec2); overload;
function GetHeightAtPoint (x, y: integer): single;
function GetWidth: integer;
function GetHeight: integer;
procedure Draw;
procedure Generate;
protected
noise: TNoise;
function GetHeightForVertex (localX, localY, x, y: integer): TNoiseFloat; virtual;
private
gl: TJSWebGLRenderingContext;
noiseOffset: TVec2;
terrainSize: integer;
terrainResolution: integer;
heights: TMatrix;
model: TModel;
function CalculateNormal (localX, localY, x, y: integer): TVec3;
end;
implementation
procedure TTerrain.Draw;
begin
model.Draw;
end;
function TTerrain.GetWidth: integer;
begin
result := heights.GetWidth;
end;
function TTerrain.GetHeight: integer;
begin
result := heights.GetHeight;
end;
function TTerrain.GetHeightAtPoint (x, y: integer): single;
begin
result := TNoiseFloat(heights.GetValue(x, y));
end;
function TTerrain.GetHeightForVertex (localX, localY, x, y: integer): TNoiseFloat;
begin
result := noise.GetNoise(x, y, heights.GetWidth, heights.GetHeight, 4, 3);
result := Power(result, 5);
result := (result * 20) - 6;
end;
function TTerrain.CalculateNormal (localX, localY, x, y: integer): TVec3;
var
heightL, heightR, heightD, heightU: TNoiseFloat;
begin
heightL := GetHeightForVertex(localX, localY, x-1, y);
heightR := GetHeightForVertex(localX, localY, x+1, y);
heightD := GetHeightForVertex(localX, localY, x, y-1);
heightU := GetHeightForVertex(localX, localY, x, y+1);
result := V3(heightL-heightR, 2.0, heightD - heightU);
result := Normalize(result);
end;
procedure TTerrain.Generate;
var
vertex: TModelVertex;
topLeft: integer;
topRight: integer;
bottomLeft: integer;
bottomRight: integer;
x, y, gz, gx: integer;
verticies: TJSArray;
indicies: TJSArray;
data: TModelData;
begin
if noise = nil then
noise := TNoise.Create(RandomNoiseSeed(1));
heights := TMatrix.Create(terrainResolution, terrainResolution);
verticies := TJSArray.new;
indicies := TJSArray.new;
for y := 0 to heights.GetWidth - 1 do
for x := 0 to heights.GetHeight - 1 do
begin
vertex.pos.x := x/(heights.GetWidth - 1) * terrainSize;
vertex.pos.y := GetHeightForVertex(x, y, Trunc(noiseOffset.x) + x, Trunc(noiseOffset.y) + y);
vertex.pos.z := y/(heights.GetHeight - 1) * terrainSize;
heights.SetValue(x, y, vertex.pos.y);
vertex.normal := CalculateNormal(x, y, Trunc(noiseOffset.x) + x, Trunc(noiseOffset.y) + y);
// distribute linearly between 0-1
vertex.texCoord.x := x/(heights.GetWidth - 1);
vertex.texCoord.y := y/(heights.GetHeight - 1);
ModelVertexAddToArray(vertex, verticies);
end;
for gz := 0 to heights.GetWidth - 2 do
for gx := 0 to heights.GetHeight - 2 do
begin
topLeft := (gz*heights.GetWidth)+gx;
topRight := topLeft + 1;
bottomLeft := ((gz+1)*heights.GetWidth)+gx;
bottomRight := bottomLeft + 1;
indicies.push(topLeft);
indicies.push(bottomLeft);
indicies.push(topRight);
indicies.push(topRight);
indicies.push(bottomLeft);
indicies.push(bottomRight);
end;
data.verticies := TJSFloat32Array.New(TJSObject(verticies));
data.indicies := TJSUint16Array.New(TJSObject(indicies));
data.floatsPerVertex := kModelVertexFloats;
model := TModel.Create(gl, data);
end;
constructor TTerrain.Create (context: TJSWebGLRenderingContext; inNoise: TNoise; size, resolution: integer; offset: TVec2);
begin
gl := context;
noise := inNoise;
noiseOffset := offset;
terrainSize := size;
terrainResolution := resolution;
end;
constructor TTerrain.Create (context: TJSWebGLRenderingContext; size, resolution: integer);
begin
gl := context;
noiseOffset := V2(0, 0);
terrainSize := size;
terrainResolution := resolution;
end;
end.