-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathC2Land.pas
More file actions
2341 lines (1931 loc) · 87.2 KB
/
C2Land.pas
File metadata and controls
2341 lines (1931 loc) · 87.2 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
@Abstract(CAST II Engine landscapes unit)
(C) 2006-2007 George "Mirage" Bakhtadze. <a href="http://www.casteng.com">www.casteng.com</a> <br>
The source code may be used under either MPL 1.1 or LGPL 2.1 license. See included license.txt file <br>
Created: 29.01.2007 <br>
Unit contains basic landscape classes
*)
{$Include GDefines.inc}
{$Include C2Defines.inc}
unit C2Land;
interface
uses
Logger,
SysUtils,
BaseTypes, BaseMsg, Basics, BaseStr, Base2D, Base3D, Props, BaseGraph,
BaseClasses,
Geometry,
C2Types, C2Res, CAST2, Resources, C2Visual, C2Maps, C2MapEditMsg,
C2Render, C2Core;
const
// Enumeration strings for light map type
LightmapTypesEnum = 'Light map' + StringDelimiter + 'Normal map';
MipColors: array[0..15] of TColor = ((C: $00000000), (C: $00000080), (C: $00008000), (C: $00008080),
(C: $00800000), (C: $00800080), (C: $00808000), (C: $00808080),
(C: $00404040), (C: $000000FF), (C: $0000FF00), (C: $0000FFFF),
(C: $00FF0000), (C: $00FF00FF), (C: $00FFFF00), (C: $00FFFFFF));
type
// Type of texture used for landscape lighting
TLightmapType = (// Simple lightmap for FFP lighting
lmtLightMap,
// Texture contains normals to calculate lighting in shader
lmtNormalMap);
THeightMap = class(C2Maps.TMap)
protected
FImage: Resources.TImageResource;
function GetData: Pointer; override;
procedure ResolveLinks; override;
function GetRawHeight(XI, ZI: Integer): Integer; override;
procedure SetRawHeight(XI, ZI: Integer; const Value: Integer); override;
public
constructor Create(AManager: TItemsManager); override;
procedure SwapRectHeights(const ARect: TRect; ABuf: Pointer); override;
procedure SetDimensions(AWidth, AHeight: Integer); override;
procedure SetImage(Image: Resources.TImageResource); virtual;
function IsReady: Boolean; override;
procedure AddProperties(const Result: Props.TProperties); override;
procedure SetProperties(Properties: Props.TProperties); override;
property Image: Resources.TImageResource read FImage;
end;
THeightMapTesselator = class(TMappedTesselator)
protected
FTextureScale: Single;
public
constructor Create; override;
procedure Init; override;
procedure AddProperties(const Result: Props.TProperties; const PropNamePrefix: TNameString); override;
procedure SetProperties(Properties: Props.TProperties; const PropNamePrefix: TNameString); override;
function Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer; override;
function SetIndices(IBPTR: Pointer): Integer; override;
end;
THeightMapLandscape = class(C2Visual.TMappedItem)
public
function GetTesselatorClass: CTesselator; override;
procedure AddProperties(const Result: Props.TProperties); override;
procedure SetProperties(Properties: Props.TProperties); override;
end;
TIslandTesselator = class(TMappedTesselator)
private
// Params
FIslandThickness, FTextureScale: Single;
public
IslandThickness, TextureScale: Single;
constructor Create; override;
function RetrieveParameters(out Parameters: Pointer; Internal: Boolean): Integer; override;
procedure Init; override;
function Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer; override;
function SetIndices(IBPTR: Pointer): Integer; override;
end;
TIsland = class(C2Visual.TMappedItem)
public
function GetTesselatorClass: CTesselator; override;
procedure AddProperties(const Result: Props.TProperties); override;
procedure SetProperties(Properties: Props.TProperties); override;
end;
TVertexWaterTesselator = class(THeightMapTesselator)
private
// Parameters
FWaterColor: TColor;
FWavesSpeed, FWavesFalloff, FViscosity: Single;
FFullRefAngle: Integer;
// Other
Vel, Arr: array of single;
public
WaterColor: TColor;
WavesSpeed, WavesFalloff, Viscosity: Single;
FullRefAngle: Integer;
procedure Init; override;
function RetrieveParameters(out Parameters: Pointer; Internal: Boolean): Integer; override;
procedure Iterate;
function Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer; override;
end;
TVertexWater = class(THeightMapLandscape)
private
Counter: Cardinal;
WaterColor: TColor;
WavesSpeed, WavesFalloff, Viscosity: Single;
FullRefAngle: Integer;
public
function GetTesselatorClass: CTesselator; override;
procedure AddProperties(const Result: Props.TProperties); override;
procedure SetProperties(Properties: Props.TProperties); override;
procedure Process(const DeltaTime: Single); override;
end;
TQuadPoints = array[0..3] of TVector3s;
TProjectedLandTesselator = class(THeightMapTesselator)
private
protected
// Params
FGridWidth, FGridHeight, SmoothX, SmoothZ: Integer;
MipBias, MipScale, DetailBalance, ViewDepth: Single;
ExcessDist, TrilinearRange: Single;
// Other
CameraDir, CameraRight: TVector3s;
FlipSign: Single;
NearMip, FarMip: Integer;
MipDetail: array[0..31] of Integer;
MipStart: array[0..31] of Single;
MipTexture: array[0..31] of Integer;
FMipZ: array of Single;
CamOfsX, CamOfsZ: Single;
LastTexUpdX, LastTexUpdZ: Single;
FMegaTextureScale: Single;
FLastClipmapSize, FClipmapSize: Integer;
Renderer: TRenderer;
FGrid: array of TVector2s;
protected
OldCameraMatrix: TMatrix4s;
// function GetCameraInModel: TVector3s;
procedure ProjectGrid(const Params: TTesselationParameters; out PrjPnt: TQuadPoints);
public
constructor Create; override;
destructor Destroy; override;
function GetMaxVertices: Integer; override;
procedure Init; override;
procedure DoManualRender(Item: TItem); override;
procedure AddProperties(const Result: Props.TProperties; const PropNamePrefix: TNameString); override;
procedure SetProperties(Properties: Props.TProperties; const PropNamePrefix: TNameString); override;
end;
TProjGridTesselator = class(TProjectedLandTesselator)
private
Pnt, PrjPnt: TQuadPoints;
public
function GetUpdatedElements(Buffer: TTesselationBuffer; const Params: TTesselationParameters): Integer; override;
function Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer; override;
function SetIndices(IBPTR: Pointer): Integer; override;
end;
TRadGridTesselator = class(TProjectedLandTesselator)
private
procedure InitGrid;
public
procedure Init; override;
function GetUpdatedElements(Buffer: TTesselationBuffer; const Params: TTesselationParameters): Integer; override;
function Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer; override;
function SetIndices(IBPTR: Pointer): Integer; override;
end;
TProjectedLandscape = class(C2Visual.TMappedItem)
private
ShaderConsts: TShaderConstants;
procedure InitShaderConstants;
function GetMegaTexture: TMegaImageResource;
protected
FLightmapType: TLightmapType;
FTextureScale: Single;
procedure OnModify(const ARect: BaseTypes.TRect); override;
public
constructor Create(AManager: TItemsManager); override;
function VisibilityCheck(const Camera: TCamera): Boolean; override;
procedure OnSceneLoaded; override;
procedure RetrieveShaderConstants(var ConstList: TShaderConstants); override;
procedure AddProperties(const Result: Props.TProperties); override;
procedure SetProperties(Properties: Props.TProperties); override;
procedure HandleMessage(const Msg: TMessage); override;
procedure Process(const DeltaTime: Single); override;
// Returns projected on the landscape four projected camera frustum points
procedure ProjectGrid(const Camera: TCamera; out PrjPnt: TQuadPoints);
procedure RecalcLightMap(ARect: BaseTypes.TRect);
property MegaTexture: TMegaImageResource read GetMegaTexture;
end;
TProjGridLandscape = class(TProjectedLandscape)
public
function GetTesselatorClass: CTesselator; override;
end;
TRadGridLandscape = class(TProjectedLandscape)
public
function GetTesselatorClass: CTesselator; override;
end;
TLandscapeShadowMapCamera = class(TShadowMapCamera)
protected
// Active camera before the shadow map render
FOldCamera: TCamera;
// If a landscape is specified it will be used to optimize shadow camera frustum
FLandscape: TProjectedLandscape;
procedure ResolveLinks; override;
public
procedure HandleMessage(const Msg: TMessage); override;
procedure AddProperties(const Result: Props.TProperties); override;
procedure SetProperties(Properties: Props.TProperties); override;
// Calculates camera view matrix according to FLight
procedure ComputeViewMatrix; override;
// OnApply event overridden to assign previous camera variable and setup clipping plane
procedure OnApply(const OldCamera: TCamera); override;
end;
// Returns list of classes introduced by the unit
function GetUnitClassList: TClassArray;
implementation
function GetUnitClassList: TClassArray;
begin
Result := GetClassList([TMap, THeightMap, THeightMapLandscape, TIsland, TVertexWater, TProjGridLandscape, TRadGridLandscape]);
end;
{ TIslandTesselator }
constructor TIslandTesselator.Create;
begin
inherited;
IslandThickness := 1;
TextureScale := 0.1;
end;
function TIslandTesselator.RetrieveParameters(out Parameters: Pointer; Internal: Boolean): Integer;
begin
Result := 2;
if Internal then Parameters := @FIslandThickness else Parameters := @IslandThickness;
end;
procedure TIslandTesselator.Init;
begin
inherited;
if Assigned(FMap) then begin
TotalVertices := FMap.Width*FMap.Height;
TotalIndices := MaxI(0, (FMap.Width-1)) * MaxI(0, (FMap.Height-1)) * 6;
TotalPrimitives := MaxI(0, (FMap.Width-1)) * MaxI(0, (FMap.Height-1)) * 2;
end else begin
TotalVertices := 0;
TotalIndices := 0;
TotalPrimitives := 0;
end;
IndexingVertices := TotalVertices;
PrimitiveType := ptTRIANGLELIST;
InitVertexFormat(GetVertexFormat(False, True, False, False, False, 0, [2]));
end;
function TIslandTesselator.Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer;
var i, j: Integer; HalfLengthX, HalfLengthZ, y: Single;
begin
Result := 0;
if not Assigned(FMap) or not FMap.IsReady then Exit;
HalfLengthX := (FMap.Width-1) * FMap.CellWidthScale * 0.5;
HalfLengthZ := (FMap.Height-1) * FMap.CellHeightScale * 0.5;
for j := 0 to FMap.Height-1 do for i := 0 to FMap.Width-1 do begin
SetVertexDataUV(i * FTextureScale, j * FTextureScale, j * FMap.Width + i, VBPTR);
if (i = 0) or (j = 0) or (i = FMap.Width-1) or (j = FMap.Height-1) or
// (HMap.GetCellHeight(i-1, j-1) = 0) or (HMap.GetCellHeight(i+1, j-1) = 0) or
// (HMap.GetCellHeight(i-1, j+1) = 0) or (HMap.GetCellHeight(i+1, j+1) = 0) or
(FMap[i-1, j] = 0) or (FMap[i+1, j] = 0) or
(FMap[i, j-1] = 0) or (FMap[i, j+1] = 0) then begin
Y := -FIslandThickness;
// SetVertexDataD(FMap.GetCellColor(i, j) and $FFFFFF, j * FMap.Width + i, VBPTR);
end else begin
Y := FMap[i+Ord(i=0)-Ord(i=FMap.Width-1), j+Ord(j=0)-Ord(j=FMap.Height-1)] * FMap.DepthScale;
// SetVertexDataD(FMap.GetCellColor(i, j) or $FF000000, j * FMap.Width + i, VBPTR);
end;
SetVertexDataC(i * FMap.CellWidthScale - HalfLengthX, Y, j * FMap.CellHeightScale - HalfLengthZ, j * FMap.Width + i, VBPTR);
SetVertexDataN(FMap.GetCellNormal(i, j), j * FMap.Width + i, VBPTR);
// SetVertexDataD(GetColor($FFFFFFFF), j * FMap.Width + i, VBPTR);
end;
TesselationStatus[tbVertex].Status := tsTesselated;
Result := TotalVertices;
IndexingVertices := TotalVertices;
LastTotalVertices := TotalVertices;
end;
function TIslandTesselator.SetIndices(IBPTR: Pointer): Integer;
var i, j: Integer; y11, y12, y21, y22, Points: Integer;
begin
{ * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * }
TotalPrimitives := 0;
if Assigned(FMap) and FMap.IsReady then for j := 0 to FMap.Height-2 do for i := 0 to FMap.Width-2 do begin
y11 := FMap[i, j];
y12 := FMap[i, j+1];
y21 := FMap[i+1, j];
y22 := FMap[i+1, j+1];
// 8 4 2 1
Points := Ord(y11 > 0) shl 3 + Ord(y12 > 0) shl 2 + Ord(y21 > 0) shl 1 + Ord(y22 > 0);
case Points of
7: begin // y11 zero
TWordBuffer(IBPTR^)[TotalPrimitives*3+0] := (j+1) * FMap.Width + i; // *
TWordBuffer(IBPTR^)[TotalPrimitives*3+1] := (j+1) * FMap.Width + i+1; // **
TWordBuffer(IBPTR^)[TotalPrimitives*3+2] := j * FMap.Width + i+1;
Inc(TotalPrimitives);
end;
11: begin // y12 zero
TWordBuffer(IBPTR^)[TotalPrimitives*3+0] := j * FMap.Width + i; // **
TWordBuffer(IBPTR^)[TotalPrimitives*3+1] := (j+1) * FMap.Width + i+1; // *
TWordBuffer(IBPTR^)[TotalPrimitives*3+2] := j * FMap.Width + i + 1;
Inc(TotalPrimitives);
end;
13: begin // y21 zero
TWordBuffer(IBPTR^)[TotalPrimitives*3+0] := j * FMap.Width + i; // *
TWordBuffer(IBPTR^)[TotalPrimitives*3+1] := (j+1) * FMap.Width + i; // **
TWordBuffer(IBPTR^)[TotalPrimitives*3+2] := (j+1) * FMap.Width + i+1;
Inc(TotalPrimitives);
end;
14: begin // y22 zero
TWordBuffer(IBPTR^)[TotalPrimitives*3+0] := j * FMap.Width + i; // **
TWordBuffer(IBPTR^)[TotalPrimitives*3+1] := (j+1) * FMap.Width + i; // *
TWordBuffer(IBPTR^)[TotalPrimitives*3+2] := j * FMap.Width + i+1;
Inc(TotalPrimitives);
end;
15: begin // All points
TWordBuffer(IBPTR^)[TotalPrimitives*3+0] := j * FMap.Width + i;
TWordBuffer(IBPTR^)[TotalPrimitives*3+1] := (j+1) * FMap.Width + i;
TWordBuffer(IBPTR^)[TotalPrimitives*3+2] := (j+1) * FMap.Width + i+1;
TWordBuffer(IBPTR^)[TotalPrimitives*3+3] := j * FMap.Width + i;
TWordBuffer(IBPTR^)[TotalPrimitives*3+4] := (j+1) * FMap.Width + i+1;
TWordBuffer(IBPTR^)[TotalPrimitives*3+5] := j * FMap.Width + i+1;
Inc(TotalPrimitives, 2);
end;
end;
end;
TotalIndices := TotalPrimitives*3;
TesselationStatus[tbIndex].Status := tsTesselated;
Result := TotalIndices;
LastTotalIndices := TotalIndices;
end;
{ TIsland }
function TIsland.GetTesselatorClass: CTesselator; begin Result := TIslandTesselator; end;
procedure TIsland.AddProperties(const Result: Props.TProperties);
begin
inherited;
if Assigned(Result) and Assigned(CurrentTesselator) then begin
Result.Add('Thickness', vtSingle, [], FloatToStr(TIslandTesselator(CurrentTesselator).IslandThickness), '0,1-10');
Result.Add('TextureScale', vtSingle, [], FloatToStr(TIslandTesselator(CurrentTesselator).TextureScale), '0,01-10');
end;
end;
procedure TIsland.SetProperties(Properties: Props.TProperties);
begin
inherited;
if Assigned(CurrentTesselator) then begin
if Properties.Valid('Thickness') then TIslandTesselator(CurrentTesselator).IslandThickness := StrToFloatDef(Properties['Thickness'], 0);
if Properties.Valid('TextureScale') then TIslandTesselator(CurrentTesselator).TextureScale := StrToFloatDef(Properties['TextureScale'], 0);
// CurrentTesselator.Init;
SetMesh;
end;
end;
{ THeightMapTesselator }
constructor THeightMapTesselator.Create;
begin
inherited;
FTextureScale := 0.1;
end;
procedure THeightMapTesselator.Init;
begin
inherited;
if Assigned(FMap) then begin
TotalVertices := FMap.Width*FMap.Height;
TotalPrimitives := MaxI(0, (FMap.Width-1)) * 2;
TotalStrips := MaxI(0, (FMap.Height-1));
TotalIndices := FMap.Width * (FMap.Height-1) * 2;
StripOffset := FMap.Width;
end else begin
TotalVertices := 0;
TotalStrips := 0;
TotalIndices := 0;
TotalPrimitives := 0;
StripOffset := 0;
end;
PrimitiveType := ptTRIANGLESTRIP;
IndexingVertices := TotalPrimitives+2;
InitVertexFormat(GetVertexFormat(False, True, False, False, False, 0, [2]));
end;
procedure THeightMapTesselator.AddProperties(const Result: Props.TProperties; const PropNamePrefix: TNameString);
begin
inherited;
if Assigned(Result) then begin
Result.Add(PropNamePrefix + 'TextureScale', vtSingle, [], FloatToStr(FTextureScale), '0,01-4');
end;
end;
procedure THeightMapTesselator.SetProperties(Properties: Props.TProperties; const PropNamePrefix: TNameString);
begin
inherited;
if Properties.Valid(PropNamePrefix + 'TextureScale') then FTextureScale := StrToFloatDef(Properties[PropNamePrefix + 'TextureScale'], 0.1);
end;
function THeightMapTesselator.Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer;
var i, j: Integer; HalfLengthX, HalfLengthZ, y: Single;
begin
Result := 0;
if not Assigned(FMap) or (FMap.Width = 0) or (FMap.Height = 0) then Exit;
HalfLengthX := (FMap.Width-1) * FMap.CellWidthScale * 0.5;
HalfLengthZ := (FMap.Height-1) * FMap.CellHeightScale * 0.5;
for j := 0 to FMap.Height-1 do for i := 0 to FMap.Width-1 do begin
SetVertexDataUV(i * FTextureScale, j * FTextureScale, j * FMap.Width + i, VBPTR);
Y := FMap[i, j] * FMap.DepthScale;
SetVertexDataC(i * FMap.CellWidthScale - HalfLengthX,
Y,
j * FMap.CellHeightScale - HalfLengthZ,
j * FMap.Width + i, VBPTR);
SetVertexDataN(FMap.GetCellNormal(i, j), j * FMap.Width + i, VBPTR);
end;
TesselationStatus[tbVertex].Status := tsTesselated;
Result := TotalVertices;
LastTotalVertices := TotalVertices;
end;
function THeightMapTesselator.SetIndices(IBPTR: Pointer): Integer;
var i, j: Integer;
begin
for j := 0 to TotalStrips-1 do for i := 0 to FMap.Width-1 do begin
TWordBuffer(IBPTR^)[(j * FMap.Width + i)*2] := (0*j+0) * FMap.Width + i;
TWordBuffer(IBPTR^)[(j * FMap.Width + i)*2+1] := (0*j+1) * FMap.Width + i;
end;
TesselationStatus[tbIndex].Status := tsTesselated;
Result := TotalIndices;
LastTotalIndices := TotalIndices;
end;
{ THeightMapLandscape }
function THeightMapLandscape.GetTesselatorClass: CTesselator;
begin
Result := THeightMapTesselator;
end;
procedure THeightMapLandscape.AddProperties(const Result: Props.TProperties);
begin
inherited;
end;
procedure THeightMapLandscape.SetProperties(Properties: Props.TProperties);
begin
inherited;
end;
{ THeightMap }
const ImagePropName = 'Image';
function THeightMap.GetData: Pointer;
begin
if Assigned(FImage) then Result := FImage.Data else Result := nil;
end;
procedure THeightMap.ResolveLinks;
var Item: TItem;
begin
inherited;
ResolveLink(ImagePropName, Item);
if Assigned(Item) then SetImage(Item as Resources.TImageResource);
end;
function THeightMap.GetRawHeight(XI, ZI: Integer): Integer;
begin
Assert((XI >= 0) and (ZI >= 0) and (XI < Width) and (ZI < Height), ClassName + '.GetCellHeight: Invalid cell index');
Result := BaseTypes.PByteBuffer(FImage.Data)^[ZI * Width + XI];
end;
procedure THeightMap.SetRawHeight(XI, ZI: Integer; const Value: Integer);
begin
Assert((XI >= 0) and (ZI >= 0) and (XI < Width) and (ZI < Height), ClassName + '.SetRawHeight: Invalid cell index');
BaseTypes.PByteBuffer(FImage.Data)^[ZI * Width + XI] := Value;
end;
procedure THeightMap.SwapRectHeights(const ARect: TRect; ABuf: Pointer);
begin
inherited;
FImage.GenerateMipLevels(ARect);
end;
procedure THeightMap.SetDimensions(AWidth, AHeight: Integer);
begin
inherited;
end;
procedure THeightMap.SetImage(Image: Resources.TImageResource);
//var i, j: Integer;
begin
if Assigned(Image) then begin
if (GetBytesPerPixel(Image.Format) <> 1) then begin
Log(Format('%S("%S").%S: a 8 bits per pixel image required', [ClassName, GetFullName, 'SetImage']), lkError);
Exit;
end;
if Image.MipPolicy = mpNoMips then begin
Log(Format('%S("%S").%S: an image with mipmaps enabled required', [ClassName, GetFullName, 'SetImage']), lkError);
// Exit;
end;
end;
FImage := Image;
end;
procedure THeightMap.AddProperties(const Result: Props.TProperties);
begin
inherited;
if Assigned(Result) then begin
end;
AddItemLink(Result, ImagePropName, [], 'TImageResource');
end;
procedure THeightMap.SetProperties(Properties: Props.TProperties);
begin
inherited;
if Properties.Valid(ImagePropName) then SetLinkProperty(ImagePropName, Properties[ImagePropName]);
ResolveLinks;
end;
constructor THeightMap.Create(AManager: TItemsManager);
begin
inherited;
FMaxHeight := 255; // 8-bit heights
FElementSize := 1;
end;
function THeightMap.IsReady: Boolean;
begin
Result := inherited IsReady and Assigned(FImage) and Assigned(FImage.Data);
end;
{ TVertexWaterTesselator }
procedure TVertexWaterTesselator.Init;
begin
inherited;
if Assigned(FMap) then begin
SetLength(arr, TotalVertices);
SetLength(Vel, TotalVertices);
FillChar(arr[0], TotalVertices*4, 0);
FillChar(vel[0], TotalVertices*4, 0);
end;
InitVertexFormat(GetVertexFormat(False, True, True, False, False, 0, [2]));
end;
function TVertexWaterTesselator.RetrieveParameters(out Parameters: Pointer; Internal: Boolean): Integer;
begin
Result := 5;
if Internal then Parameters := @FWaterColor else Parameters := @WaterColor;
end;
const MaxArr = 1000000000.0;
procedure TVertexWaterTesselator.Iterate;
//const k = 0.25; VC = 0.25*1+0*1; FalOff = 1*1+0*0.98; RestoreForce = 1*0+1*0.98;
var i, j: Integer;
begin
for j := 1 to FMap.Height-2 do
for i := 1 to FMap.Width-2 do
vel[j * FMap.Width + i] := (vel[j * FMap.Width + i] +
(-arr[j * FMap.Width + i]*4 +
arr[(j+1) * FMap.Width + i] + arr[(j-1) * FMap.Width + i] +
arr[j * FMap.Width + i + 1] + arr[j * FMap.Width + i - 1]) * FWavesSpeed ) * FWavesFalloff;
for j := 0 to FMap.Height-1 do begin
for i := 0 to FMap.Width-1 do begin
// arr[j * FMap.Width + i] := arr[j * FMap.Width + i] + Vel[j * FMap.Width + i] * 0.5;
arr[j * FMap.Width + i] := (arr[j * FMap.Width + i] + Vel[j * FMap.Width + i]) * FViscosity;
if arr[j * FMap.Width + i]>MaxArr then arr[j * FMap.Width + i] := MaxArr;
if arr[j * FMap.Width + i]<0 then arr[j * FMap.Width + i] := 0;
FMap[i, j] := Round((arr[(j div 1) * FMap.Width + i div 1]*1) / MaxArr*255);
{ FMap[i, j] := Round((
arr[(j div 1) * FMap.Width + i div 1]*(1-k*4) +
arr[MaxI(0, j-1) * FMap.Width + i]*k +
arr[(j) * FMap.Width + MaxI(0, i-1)]*k +
arr[MinI(FMap.Height-1, j+1) * FMap.Width + i]*k +
arr[(j) * FMap.Width + MinI(FMap.Width-1, i+1)]*k
) / MaxArr*255);}
end;
end;
end;
function TVertexWaterTesselator.Tesselate(const Params: TTesselationParameters; VBPTR: Pointer): Integer;
var i, j: Integer; HalfLengthX, HalfLengthZ, y, a, MinA, FRACos: Single; c, n: TVector3s;
begin
Result := 0;
if not Assigned(FMap) or not FMap.IsReady then Exit;
if TesselationStatus[tbVertex].Status <> tsTesselated then Iterate;
MinA := FWaterColor.A / 255;
FRACos := Sin((FFullRefAngle)/180*pi);
HalfLengthX := (FMap.Width-1) * FMap.CellWidthScale * 0.5;
HalfLengthZ := (FMap.Height-1) * FMap.CellHeightScale * 0.5;
for j := 0 to FMap.Height-1 do for i := 0 to FMap.Width-1 do begin
SetVertexDataUV(i * FTextureScale, j * FTextureScale, j * FMap.Width + i, VBPTR);
Y := FMap[i, j] * FMap.DepthScale;
c := GetVector3s(i * FMap.CellWidthScale - HalfLengthX, Y, j * FMap.CellHeightScale - HalfLengthZ);
SetVertexDataC(c, j * FMap.Width + i, VBPTR);
n := FMap.GetCellNormal(i, j);
SetVertexDataN(n, j * FMap.Width + i, VBPTR);
n := Transform3Vector3s(CutMatrix3s(Params.ModelMatrix), n);
c := Transform4Vector33s(Params.ModelMatrix, c);
c := NormalizeVector3s(SubVector3s(Params.Camera.Position, c));
// a := MaxS(0, MinA + MinS(1-MinA, (1-Abs(DotProductVector3s(c, n)))/FRACos) );
a := MaxS(0, MinA + MinS(1-MinA, (1-MinA)*Sqrt(1-Sqr(DotProductVector3s(c, n)))/FRACos ));
SetVertexDataD(GetColor(FWaterColor.R, FWaterColor.G, FWaterColor.B, Round(a*255)), j * FMap.Width + i, VBPTR);
end;
TesselationStatus[tbVertex].Status := tsTesselated;
Result := TotalVertices;
LastTotalVertices := TotalVertices;
end;
{ TVertexWater}
function TVertexWater.GetTesselatorClass: CTesselator; begin Result := TVertexWaterTesselator; end;
procedure TVertexWater.AddProperties(const Result: TProperties);
begin
inherited;
if Assigned(Result) then begin
AddColorProperty(Result, 'Water color', WaterColor);
Result.Add('100% reflection angle', vtInt, [], IntToStr(FullRefAngle), '0-90');
Result.Add('Waves speed', vtSingle, [], FloatToStr(WavesSpeed), '0.1-1');
Result.Add('Waves falloff', vtSingle, [], FloatToStr(WavesFalloff), '0.9-1');
Result.Add('Water viscosity', vtSingle, [], FloatToStr(Viscosity), '0.8-1');
end;
end;
procedure TVertexWater.SetProperties(Properties: TProperties);
var Mesh: TVertexWaterTesselator;
begin
inherited;
SetColorProperty(Properties, 'Water color', WaterColor);
if Properties.Valid('100% reflection angle') then FullRefAngle := StrToIntDef(Properties['100% reflection angle'], 30);
if Properties.Valid('Waves speed') then WavesSpeed := StrToFloatDef(Properties['Waves speed'], 0);
if Properties.Valid('Waves falloff') then WavesFalloff := StrToFloatDef(Properties['Waves falloff'], 0);
if Properties.Valid('Water viscosity') then Viscosity := StrToFloatDef(Properties['Water viscosity'], 0);
if not (CurrentTesselator is TVertexWaterTesselator) then Exit;
Mesh := CurrentTesselator as TVertexWaterTesselator;
Mesh.WaterColor := WaterColor;
Mesh.FullRefAngle := FullRefAngle;
Mesh.WavesSpeed := WavesSpeed;
Mesh.WavesFalloff := WavesFalloff;
Mesh.Viscosity := Viscosity;
SetMesh;
end;
procedure TVertexWater.Process(const DeltaTime: Single);
procedure MakeWave(z, len: Integer; Height, Freq, Radii: Single);
var i, j: Integer;
begin
with (CurrentTesselator as TVertexWaterTesselator) do begin
for i := 2 to FMap.Width-4 do
for j := 0 to len-1 do
arr[MinI(FMap.Height-2, j+z-Round(Sin(pi*((i-2)/(FMap.Width-4))*FReq)*Radii)) * FMap.Width + MinI(FMap.Width-2, i)] := MaxArr*Height * Sin(j/(len-1)*pi);
// arr[MinI(FMap.Height-2, z+j) * FMap.Width + MinI(FMap.Width-2, i)] := MaxArr*Height * Sin(j/(len-1)*pi);
end;
end;
var w, h: Integer;
begin
inherited;
if not Assigned((CurrentTesselator as TVertexWaterTesselator).FMap) or not (CurrentTesselator as TVertexWaterTesselator).FMap.IsReady then Exit;
CurrentTesselator.Invalidate([tbVertex], False);
with (CurrentTesselator as TVertexWaterTesselator) do begin
Inc(Counter);
w := FMap.Width;
w := w div 2 + Random(w div 2-2) - Random(w div 2-2);
h := FMap.Height;
h := h div 2 + Random(h div 2-2) - Random(h div 2-2);
if Counter mod 2 = 0 then arr[h * FMap.Width + w] := (MaxArr + Random * MaxArr)/2/2;
// if Counter mod 2 = 0 then MakeWave(6, 0.03, 4, 4);
// if Counter mod 6 = 0 then MakeWave(67, 0.08, 4, 4);
if Counter mod 10 = 0 then MakeWave(126-6, 6, 0.2, 1, 18);
end;
end;
{ TProjectedLandTesselator }
procedure TProjectedLandTesselator.ProjectGrid(const Params: TTesselationParameters; out PrjPnt: TQuadPoints);
var
ModelInv: TMatrix4s; ModelInv33: TMatrix3s;
CameraInModel: TVector3s;
CameraElevation: Single;
OPnt2, OPnt3: TVector3s;
procedure SwapVec(var V1, V2: TVector3s);
var Vec: TVector3s;
begin
Vec := V1;
V1 := V2;
V2 := Vec;
end;
function ProjectOnGrid2(X, Y: Single; out Point: TVector3s): Boolean;
var PickRay: TVector3s;
begin
Result := False;
PickRay := Transform3Vector3s(CutMatrix3s(InvertAffineMatrix4s(Params.Camera.ViewMatrix)), Params.Camera.GetPickRay(X, Y));
PickRay := NormalizeVector3s(Transform3Vector3s(ModelInv33, PickRay) );
if (Abs(PickRay.Y) > epsilon) and (Sign(PickRay.Y) <> Sign(CameraInModel.Y)) and
(Abs(CameraElevation/PickRay.Y) < ViewDepth) then begin // Ray intersects the surface
SubVector3s(Point, CameraInModel, ScaleVector3s(PickRay, CameraElevation/PickRay.Y));
Point.Y := 0;
Result := True;
end;
end;
function ProjectNearEdge(FlipSign: Single): Boolean;
var TempK, y: Single; Pnt0, Pnt1: TVector3s;
begin
Result := False;
y := Params.Camera.RenderHeight * Ord(FlipSign >= 0);
if not ProjectOnGrid2(Params.Camera.RenderWidth, y, PrjPnt[2]) then Exit;
if not ProjectOnGrid2(0, y, PrjPnt[3]) then Exit;
OPnt2 := PrjPnt[2];
OPnt3 := PrjPnt[3];
// Near edge with elevation taken in account
TempK := SqrMagnitude(CameraDir);
if TempK > epsilon then begin
ScaleVector3s(CameraDir, CameraDir, InvSqrt(TempK));
CameraElevation := MaxS(0, CameraInModel.Y-FMap.MaxHeight * FMap.DepthScale);
if not ProjectOnGrid2(Params.Camera.RenderWidth, y, Pnt1) then Exit;
if not ProjectOnGrid2(0, y, Pnt0) then Exit;
TempK := MinS(0, MinS(DotProductVector3s(CameraDir, SubVector3s(Pnt1, PrjPnt[2])),
DotProductVector3s(CameraDir, SubVector3s(Pnt0, PrjPnt[3]))));
AddVector3s(PrjPnt[2], PrjPnt[2], ScaleVector3s(CameraDir, TempK));
AddVector3s(PrjPnt[3], PrjPnt[3], ScaleVector3s(CameraDir, TempK));
CameraElevation := CameraInModel.Y;
end;
Result := True;
end;
function ProjectFarEdge(FlipSign: Single): Boolean;
var y: Single; LeftRail, RightRail: TVector3s;
begin
Result := False;
y := Params.Camera.RenderHeight * Ord(FlipSign >= 0);
if not ProjectOnGrid2(Params.Camera.RenderWidth, y + 1 - 2*Ord(FlipSign >= 0), PrjPnt[1]) then Exit;
if not ProjectOnGrid2(0, y + 1 - 2*Ord(FlipSign >= 0), PrjPnt[0]) then Exit;
LeftRail := NormalizeVector3s(SubVector3s(PrjPnt[0], OPnt3));
RightRail := NormalizeVector3s(SubVector3s(PrjPnt[1], OPnt2));
if not ProjectOnGrid2(Params.Camera.RenderWidth, Params.Camera.RenderHeight-y, PrjPnt[1]) then
PrjPnt[1] := AddVector3s(OPnt2, ScaleVector3s(RightRail, ViewDepth));
if not ProjectOnGrid2(0, Params.Camera.RenderHeight-y, PrjPnt[0]) then
PrjPnt[0] := AddVector3s(OPnt3, ScaleVector3s(LeftRail, ViewDepth));
Result := True;
end;
var TempK: Single;
begin
ModelInv := InvertAffineMatrix4s(Params.ModelMatrix);
ModelInv33 := CutMatrix3s(ModelInv);
Transform4Vector33s(CameraInModel, ModelInv, Params.Camera.GetAbsLocation);
CameraElevation := CameraInModel.Y;
FlipSign := Sign(DotProductVector3s(Params.Camera.UpDir, Params.ModelMatrix.ViewUp));// *
// -DotProductVector3s(Params.Camera.LookDir, Params.ModelMatrix.ViewUp);
CameraRight := ScaleVector3s(Transform3Vector3s(ModelInv33, Params.Camera.RightVector), FlipSign);
CameraRight.Y := 0;
TempK := SqrMagnitude(CameraRight);
// Assert(TempK > epsilon);
ScaleVector3s(CameraRight, CameraRight, InvSqrt(TempK));
CameraDir := Transform3Vector3s(ModelInv33, Params.Camera.ForwardVector);
CameraDir.Y := 0;
if not ProjectNearEdge(FlipSign) then Exit;
if not ProjectFarEdge(FlipSign) then Exit;
TempK := SqrMagnitude(CameraDir);
if TempK <= epsilon then
CameraDir := CrossProductVector3s(Transform3Vector3s(ModelInv33, Params.Camera.ForwardVector), CameraRight);
if FlipSign < 0 then begin
SwapVec(PrjPnt[0], PrjPnt[1]);
SwapVec(PrjPnt[2], PrjPnt[3]);
SwapVec(OPnt2, OPnt3);
end;
end;
constructor TProjectedLandTesselator.Create;
var i: Integer;
begin
inherited;
FGridWidth := 100;
FGridHeight := 200;
MipBias := 100;
MipScale := 1;
SmoothX := 1;
SmoothZ := 1;
DetailBalance := 0.5;
TrilinearRange := 0.3;
FMegaTextureScale := 1;
FLastClipmapSize := 0;
FClipmapSize := 256;
for i := 0 to High(MipTexture) do MipTexture[i] := -1;
end;
destructor TProjectedLandTesselator.Destroy;
begin
SetLength(FGrid, 0);
SetLength(FMipZ, 0);
inherited;
end;
function TProjectedLandTesselator.GetMaxVertices: Integer;
begin
Result := TotalVertices;
end;
procedure TProjectedLandTesselator.Init;
begin
inherited;
if Assigned(FMap) then begin
TotalVertices := (FGridWidth+1)*(FGridHeight+1);
TotalIndices := (FGridWidth+1)*2; // - - 89, 1 - 309-315, 2 - 85
TotalStrips := FGridHeight;
TotalPrimitives := FGridWidth*2;
StripOffset := FGridWidth+1;
// StripOffset := FGridWidth+1;
SetLength(FMipZ, FGridHeight+1);
// 0 2 4 0 1 2 3 4 5 5 1 P: ??? (w*2)*(h-1)-2 = (3*2)*3-2 = 16
// 1 3 5 1 6 3 7 5 8 8 6 V: w*h = 3*4 = 12
// 6 7 8 6 9 7 A 8 B I: (2+(w-1)*2+2)*(h-1)-2 = 2*(w+1)*(h-1) = (2+(3-1)*2+2)*3-2 = 8*3-2 = 22
// 9 A B
{ TotalIndices := 2*(FGridWidth+2)*(FGridHeight);//-2
TotalStrips := 1;
TotalPrimitives := 2*(FGridWidth+1)*(FGridHeight)-2;//(TotalIndices-2-2);
StripOffset := 0;}
end else begin
TotalVertices := 0;
TotalStrips := 0;
TotalIndices := 0;
TotalPrimitives := 0;
StripOffset := 0;
end;
if not ManualRender then Log('TProjectedLandTesselator.Init: Manual render should be turned on for megatextured landscapes', lkWarning);
PrimitiveType := ptTRIANGLESTRIP;
TesselationStatus[tbVertex].TesselatorType := ttStatic;
TesselationStatus[tbIndex].TesselatorType := ttStatic;
// IndexingVertices := TotalVertices;
IndexingVertices := (FGridWidth+1)*2;
// IndexingVertices := (FGridHeight+1)*2;
// InitVertexFormat(GetVertexFormat(False, False, True, False, False, 0, [2]));
InitVertexFormat(GetVertexFormat(False, False, False, False, False, 0, []));
LastTexUpdX := 0;
LastTexUpdZ := 0;
end;
procedure TProjectedLandTesselator.DoManualRender(Item: TItem);
var TexI: Integer; MipWorldSize, LandSizeX, LandSizeZ: Single; TexUpd: Boolean;
procedure ApplyNextMip;
var LockedData: TLockedRectData; CenX, CenZ: Single;
begin
// texofs := texofs - 0.5/TexDim;
TexI := TexI + 1;
MipWorldSize := FClipmapSize * (1 shl TexI)/FMegaTextureScale;
CenX := TProjectedLandscape(Item).MegaTexture.LevelInfo[TexI].Width * (0.5 + FMegaTextureScale * CamOfsX / TProjectedLandscape(Item).MegaTexture.LevelInfo[0].Width);
CenZ := TProjectedLandscape(Item).MegaTexture.LevelInfo[TexI].Height * (0.5 + FMegaTextureScale * CamOfsZ / TProjectedLandscape(Item).MegaTexture.LevelInfo[0].Height);
CenZ := TProjectedLandscape(Item).MegaTexture.LevelInfo[TexI].Height - CenZ;
if TexUpd or (MipTexture[TexI] = -1) or (FLastClipmapSize <> FClipmapSize) then begin
if (MipTexture[TexI] = -1) or (FLastClipmapSize <> FClipmapSize) then begin
if MipTexture[TexI] <> -1 then Renderer.Textures.Delete(MipTexture[TexI]);
MipTexture[TexI] := Renderer.Textures.NewProceduralTexture(TProjectedLandscape(Item).MegaTexture.Format, FClipmapSize, FClipmapSize, 0, 1, [toProcedural]);
end;
if Renderer.Textures.Lock(MipTexture[TexI], 0, nil, LockedData, []) then begin
TProjectedLandscape(Item).MegaTexture.LoadRect(GetRect(Trunc(CenX - FClipmapSize*0.5), Trunc(CenZ - FClipmapSize*0.5),
Trunc(CenX - FClipmapSize*0.5)+FClipmapSize, Trunc(CenZ - FClipmapSize*0.5)+FClipmapSize), TexI, LockedData.Data, FClipmapSize);
// FillDWord(LockedData.Data^, 512*512, MipColors[i]);
Renderer.Textures.UnLock(MipTexture[TexI], 0);
LastTexUpdX := CamOfsX;
LastTexUpdZ := CamOfsZ;
end;
end;
CenX := Frac(CenX - FClipmapSize*0.5)/FClipmapSize;
CenZ := Frac(CenZ - FClipmapSize*0.5)/FClipmapSize;
Renderer.Textures.Apply(0, MipTexture[TexI]);
Renderer.APIState.SetShaderConstant(skVertex, 9, GetVector4s(1/MipWorldSize, -1/MipWorldSize, CenX, CenZ));
end;
var i, j, k: Integer;
begin
if not Assigned(TProjectedLandscape(Item).MegaTexture) then Exit;