-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Geometry.Mesh2D.pas
343 lines (283 loc) · 8.7 KB
/
GS.Geometry.Mesh2D.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
unit GS.Geometry.Mesh2D;
interface
uses SysUtils, Classes,
GS.Geometry;
type
TGSRawMesh2D = class;
IGSRawMesh = interface
procedure reset;
procedure SetUpQuad(side : single);
procedure SetUpRect(width,height : single);
procedure Rotate(angle : single); //Todo : Specify a center ??
procedure Scale(x,y : single);
procedure Pan(x,y : single);
procedure Modify(xpos,ypos,angle : single); overload;
procedure Modify(xpos,ypos,angle,xscale,yscale : single); overload;
procedure UpdateVertices(const matrix : Mat3);
procedure reCenter; //Back all coordinate surround (0,0)
procedure Merge(from : TGSRawMesh2D);
end;
TGSRawMesh2D = class(TInterfacedObject, IGSRawMesh)
private
protected
public
BoundingBox : Vec4;
vertices : array of vec2;
uvs : array of vec2; //...
// cols : array of vec4; //... Synchro with vertices.
indexes : array of Uint32;
Constructor Create; virtual;
procedure reset;
procedure SetUpQuad(side : single);
procedure SetUpRect(width,height : single);
procedure Merge(from : TGSRawMesh2D);
//Setup....
//This modify the vertices ! No way to return exactly due to processing approx..
//to reverse a rotate exactly, we have to reset (setupxxx) the vertices before.
procedure Rotate(angle : single); //Todo : Specify a center ??
procedure Scale(x,y : single);
procedure Pan(x,y : single);
procedure Modify(xpos,ypos,angle : single); overload;
procedure Modify(xpos,ypos,angle,xscale,yscale : single); overload;
procedure UpdateVertices(const matrix : Mat3);
function fullRefreshBounding : vec4;
procedure progressiveRefreshBounding(a : vec2); //to Call when you add mesh.
function BoudingCenter : vec2;
procedure resetBoundingBox;
procedure reCenter; //Back all coordinate surround (0,0)
function getVerticeCount : Uint32;
function getIndicesCount : Uint32;
function getTriangleCount : Uint32;
procedure Triangle(const index : Uint32; out a,b,c, uva,uvb,uvc : vec2; out ca,cb,cc : vec4);
procedure copy(var destination : TGSRawMesh2D);
end;
implementation
{ TGSRawMesh2D }
procedure TGSRawMesh2D.Modify(xpos, ypos, angle: single);
var lLocalMatrix : Mat3;
begin
lLocalMatrix := Mat3Identity;
lLocalMatrix := lLocalMatrix * Mat3CreateRotation(angle);
lLocalMatrix := lLocalMatrix * Mat3CreateTranslation(xpos,ypos);
UpdateVertices(lLocalMatrix);
end;
function TGSRawMesh2D.BoudingCenter: vec2;
begin
result.create(BoundingBox.left + abs(BoundingBox.right - BoundingBox.Left)/2,
BoundingBox.top + abs(BoundingBox.top - BoundingBox.bottom)/2);
end;
procedure TGSRawMesh2D.copy(var destination: TGSRawMesh2D);
begin
SetLength(destination.vertices,length(vertices));
SetLength(destination.indexes,length(indexes));
SetLength(destination.uvs,length(uvs));
// SetLength(destination.cols,length(cols));
Move(vertices[0],destination.vertices[0],length(vertices)*sizeOf(vec2));
Move(uvs[0],destination.uvs[0],length(uvs)*sizeOf(vec2));
// Move(cols[0],destination.cols[0],length(cols)*sizeOf(vec2));
Move(indexes[0],destination.indexes[0],length(indexes)*SizeOf(Uint32));
destination.BoundingBox := BoundingBox;
end;
constructor TGSRawMesh2D.create;
begin
Inherited;
// SetUpQuad(1);
end;
function TGSRawMesh2D.FullRefreshBounding: vec4;
var i : integer;
p : vec2;
begin
result.create(999999,99999,-999999,-999999);
for i := 0 to length(vertices)-1 do
begin
p.create(vertices[i].x,vertices[i].y);
if result.left>p.X then
result.left := p.X;
if result.top>p.Y then
result.top := p.Y;
if result.right<p.X then
result.right := p.X;
if result.bottom<p.Y then
result.bottom := p.Y;
end;
BoundingBox := result;
end;
function TGSRawMesh2D.getIndicesCount: Uint32;
begin
result := System.Length(indexes);
end;
function TGSRawMesh2D.getTriangleCount: Uint32;
begin
result := Length(indexes) div 3;
end;
function TGSRawMesh2D.getVerticeCount: Uint32;
begin
result := System.Length(vertices);
end;
procedure TGSRawMesh2D.ProgressiveRefreshBounding(a: vec2);
begin
if a.x < BoundingBox.Left then
BoundingBox.Left := a.x;
if a.x > BoundingBox.right then
BoundingBox.right := a.x;
if a.y < BoundingBox.top then
BoundingBox.top := a.y;
if a.y > BoundingBox.bottom then
BoundingBox.bottom := a.y;
end;
procedure TGSRawMesh2D.Merge(from: TGSRawMesh2D);
var cp : integer;
I: Integer;
vold, iold : integer;
iv : integer;
begin
{ if (from.getVerticeCount=0) or (from.getIndicesCount=0) then
exit;
//vetices copy.
cp := System.Length(vertices);
SetLength(vertices,Length(vertices)+length(from.vertices));
move(from.vertices[0],vertices[cp],length(from.vertices)*Sizeof(from.vertices[0]));
//uvs
cp := System.Length(uvs);
SetLength(uvs,Length(uvs)+length(from.uvs));
move(from.uvs[0],uvs[cp],length(from.uvs)*Sizeof(from.uvs[0]));
//cols
// cp := System.Length(cols);
// SetLength(cols,Length(cols)+length(from.cols));
// move(from.cols[0],cols[cp],length(from.cols)*Sizeof(from.cols[0]));
cp := System.Length(indexes);
SetLength(indexes,System.Length(indexes)+System.length(from.indexes));
move(from.indexes[0],indexes[cp],System.length(from.indexes)*Sizeof(from.indexes[0]));
if cp>0 then //index corrector.
for i := cp-1 to System.length(indexes)-1 do
indexes[i] := indexes[i] + cp;
FullRefreshBounding;
}
vold := getVerticeCount;
iv := 0;
SetLength(vertices,vold+from.getVerticeCount);
for I := vold to getVerticeCount- 1 do
begin
Vertices[I].x := from.vertices[iv].X;
Vertices[I].y := from.vertices[iv].Y;
iv := iv + 1;
end;
iold := getIndicesCount;
iv := 0;
setLength(indexes,iold + length(from.indexes));
for I := iold to getIndicesCount - 1 do
begin
indexes[I] := vold + from.indexes[iv];
iv := iv+1;
end;
end;
procedure TGSRawMesh2D.Modify(xpos, ypos, angle, xscale, yscale: single);
var fLocalMatrix : Mat3;
begin
FLocalMatrix := Mat3Identity;
FLocalMatrix := FLocalMatrix * Mat3CreateRotation(angle);
FLocalMatrix := FLocalMatrix * Mat3CreateScaling(xscale,yscale);
FLocalMatrix := FLocalMatrix * Mat3CreateTranslation(xpos,ypos);
UpdateVertices(fLocalMatrix);
end;
procedure TGSRawMesh2D.Pan(x, y: single);
var fLocalMatrix : Mat3;
begin
FLocalMatrix := Mat3Identity;
FLocalMatrix := FLocalMatrix * Mat3CreateTranslation(x,y);
UpdateVertices(fLocalMatrix);
end;
procedure TGSRawMesh2D.reCenter;
begin
fullRefreshBounding;
Pan(-BoudingCenter.x,-BoudingCenter.y);
end;
procedure TGSRawMesh2D.reset;
begin
vertices := nil;
indexes := nil;
uvs := nil;
// cols := nil;
resetBoundingBox;
end;
procedure TGSRawMesh2D.resetBoundingBox;
begin
BoundingBox.create(M_MXVT,M_MXVT,-M_MXVT,-M_MXVT);
end;
procedure TGSRawMesh2D.Rotate(angle: single);
var fLocalMatrix : Mat3;
begin
FLocalMatrix := Mat3Identity;
FLocalMatrix := FLocalMatrix * Mat3CreateRotation(angle*Pi/180);
UpdateVertices(fLocalMatrix);
end;
procedure TGSRawMesh2D.Scale(x, y: single);
var fLocalMatrix : Mat3;
begin
FLocalMatrix := Mat3Identity;
FLocalMatrix := FLocalMatrix * Mat3CreateScaling(x,y);
UpdateVertices(fLocalMatrix);
end;
procedure TGSRawMesh2D.UpdateVertices(const matrix: Mat3);
var i : integer;
p : vec2;
begin
resetBoundingBox;
for i := 0 to length(vertices)-1 do
begin
p := vec2.create(vertices[i].x,vertices[i].y) * matrix;
vertices[i].x := p.x;
vertices[i].y := p.y;
ProgressiveRefreshBounding(p);
end;
end;
procedure TGSRawMesh2D.SetUpQuad(side: single);
begin
SetUpRect(side,side);
end;
procedure TGSRawMesh2D.SetUpRect(width, height: single);
begin
setLength(vertices,4);
vertices[0].create(-width/2,-height/2);
vertices[1].create(width/2,-height/2);
vertices[2].create(width/2,height/2);
vertices[3].create(-width/2,height/2);
setLength(indexes,6);
setLength(uvs,4);
// setLength(cols,4);
indexes[0] := 0;
indexes[1] := 1;
indexes[2] := 2;
indexes[3] := 2;
indexes[4] := 3;
indexes[5] := 0;
uvs[0] := vec2.create(0,0);
uvs[1] := vec2.create(1,0);
uvs[2] := vec2.create(1,1);
uvs[3] := vec2.create(0,1);
// cols[0] := vec4.create(1,0,0,1);
// cols[1] := vec4.create(0,1,0,1);
// cols[2] := vec4.create(0,0,1,1);
// cols[3] := vec4.create(1,1,1,1);
BoundingBox.create(-width/2,-height/2,width/2,height/2);
end;
procedure TGSRawMesh2D.Triangle(const index: Uint32; out a, b, c, uva,uvb,uvc : vec2; out ca,cb,cc : vec4);
var baseIndex : Uint32;
ai,bi,ci : Uint32;
begin
assert(index<=getTriangleCount);
baseIndex := index*3;
ai := indexes[baseIndex];
bi := indexes[baseIndex+1];
ci := indexes[baseIndex+2];
a := vertices[ai];
b := vertices[bi];
c := vertices[ci];
uva := uvs[ai];
uvb := uvs[bi];
uvc := uvs[ci];
// ca := cols[ai];
// cb := cols[bi];
// cc := cols[ci];
end;
end.