-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerrainEditing.cpp
1595 lines (1434 loc) · 45 KB
/
TerrainEditing.cpp
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
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "StdH.h"
#include <Engine/Templates/Stock_CTextureData.h>
// global variables used in terrain editing functions
UWORD *_puwBuffer=NULL;
Rect _rect;
PIX _srcExtraW=0;
PIX _srcExtraH=0;
CTextureData *_ptdBrush=NULL;
CTextureData *_ptdDistributionRandomNoise=NULL;
CTextureData *_ptdContinousRandomNoise=NULL;
UWORD *_puwNoiseTarget=NULL;
PIX _pixNoiseTargetW=0;
PIX _pixNoiseTargetH=0;
FLOAT _fStrength=0.0f;
// fbm noise buffer
FLOAT *_pafWhiteNoise=NULL;
#define WNOISE 64
// undo variables
UWORD *_puwUndoTerrain=NULL;
Rect _rectUndo;
BOOL _bUndoStart=FALSE;
BufferType _btUndoBufferType=BT_INVALID;
INDEX _iUndoBufferData=-1;
INDEX _iTerrainEntityID=-1;
// filter matrices
FLOAT _afFilterFineBlur[5][5]=
{
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0625f, 0.0625f, 0.0625f, 0.0f},
{0.0f, 0.0625f, 0.5f, 0.0625f, 0.0f},
{0.0f, 0.0625f, 0.0625f, 0.0625f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
};
FLOAT _afFilterBlurMore[5][5]=
{
{0.0277777f, 0.0277777f, 0.0277777f, 0.0277777f, 0.0277777f},
{0.0277777f, 0.0555555f, 0.0555555f, 0.0555555f, 0.0277777f},
{0.0277777f, 0.0555555f, 0.0111111f, 0.0555555f, 0.0277777f},
{0.0277777f, 0.0555555f, 0.0555555f, 0.0555555f, 0.0277777f},
{0.0277777f, 0.0277777f, 0.0277777f, 0.0277777f, 0.0277777f},
};
FLOAT _afFilterEdgeDetect[5][5]=
{
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 1.0f, 1.0f, 0.0f},
{0.0f, 1.0f, -7.0f, 1.0f, 0.0f},
{0.0f, 1.0f, 1.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
};
FLOAT _afFilterEmboss[5][5]=
{
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 1.0f, -1.0f, 0.0f},
{0.0f, 1.0f, 1.0f, -1.0f, 0.0f},
{0.0f, 1.0f, -1.0f, -1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
};
FLOAT _afFilterSharpen[5][5]=
{
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, -1.0f, -1.0f, -1.0f, 0.0f},
{0.0f, -1.0f, 16.0f, -1.0f, 0.0f},
{0.0f, -1.0f, -1.0f, -1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
};
FLOAT GetBrushMultiplier(INDEX x, INDEX y)
{
if(_ptdBrush==NULL) return 1.0f;
{
COLOR col=_ptdBrush->GetTexel(x,y);
FLOAT fResult=FLOAT(col>>24)/255.0f;
return fResult;
}
}
void ApplyAddPaint(UWORD uwMin, UWORD uwMax)
{
for(INDEX y=0; y<_rect.Height(); y++)
{
for(INDEX x=0; x<_rect.Width(); x++)
{
FLOAT fBrushMultiplier=GetBrushMultiplier(x,y);
if( fBrushMultiplier==0.0f) continue;
INDEX iOffset=y*_rect.Width()+x;
FLOAT fValue=_puwBuffer[iOffset];
fValue+=fBrushMultiplier*_fStrength*32.0f*theApp.m_fPaintPower;
fValue=Clamp(fValue,FLOAT(uwMin),FLOAT(uwMax));
_puwBuffer[iOffset]=fValue;
}
}
}
void ApplyRNDNoise(void)
{
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
FLOAT fMaxNoise=theApp.m_fNoiseAltitude/ptrTerrain->tr_vTerrainSize(2)*65535.0f;
for(INDEX y=0; y<_rect.Height(); y++)
{
for(INDEX x=0; x<_rect.Width(); x++)
{
FLOAT fBrushMultiplier=GetBrushMultiplier(x,y);
INDEX iPixSrc=y*_rect.Width()+x;
FLOAT fInfluence=_puwBuffer[iPixSrc];
FLOAT fRnd=FLOAT(rand())/RAND_MAX-0.5f;
FLOAT fValue=_puwBuffer[iPixSrc];
FLOAT fMaxRandomized=fValue+fRnd*fMaxNoise;
FLOAT fFilterPower=Clamp(fBrushMultiplier*_fStrength,0.0f,1.0f);
FLOAT fResult=Lerp( fValue, fMaxRandomized, fFilterPower);
UWORD uwResult=Clamp(UWORD(fResult),MIN_UWORD,MAX_UWORD);
_puwBuffer[iPixSrc]=uwResult;
}
}
}
FLOAT GetDistributionNoise( INDEX x, INDEX y, FLOAT fRandom)
{
INDEX iw=_ptdDistributionRandomNoise->GetPixWidth();
INDEX ih=_ptdDistributionRandomNoise->GetPixHeight();
INDEX ctSize=iw*ih;
INDEX iOffset=abs(INDEX(iw*y+x+fRandom*ctSize)%ctSize);
COLOR col=_ptdDistributionRandomNoise->td_pulFrames[iOffset];
FLOAT fResult=FLOAT(col&0xFF)/255.0f;
return fResult;
}
FLOAT GetContinousNoise( INDEX x, INDEX y, FLOAT fRandom)
{
INDEX iw=_ptdContinousRandomNoise->GetPixWidth();
INDEX ih=_ptdContinousRandomNoise->GetPixHeight();
INDEX ctSize=iw*ih;
INDEX iOffset=abs(INDEX(iw*y+x+fRandom*ctSize)%ctSize);
COLOR col=_ptdContinousRandomNoise->td_pulFrames[iOffset];
FLOAT fResult=FLOAT(col&0xFF)/255.0f;
return fResult;
}
void ApplyContinousNoise(void)
{
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
FLOAT fMaxNoise=theApp.m_fNoiseAltitude/ptrTerrain->tr_vTerrainSize(2)*65535.0f;
for(INDEX y=0; y<_rect.Height(); y++)
{
INDEX oy=y+_rect.rc_iTop;
for(INDEX x=0; x<_rect.Width(); x++)
{
INDEX ox=x+_rect.rc_iLeft;
FLOAT fBrushMultiplier=GetBrushMultiplier(x,y);
INDEX iPixSrc=y*_rect.Width()+x;
FLOAT fInfluence=_puwBuffer[iPixSrc];
FLOAT fRnd=GetContinousNoise( ox, oy, 0.0f)-0.5f;
FLOAT fValue=_puwBuffer[iPixSrc];
FLOAT fMaxRandomized=fValue+fRnd*fMaxNoise;
fMaxRandomized=Clamp(fMaxRandomized,(FLOAT)MIN_UWORD,(FLOAT)MAX_UWORD);
FLOAT fFilterPower=Clamp(fBrushMultiplier*_fStrength,0.0f,1.0f);
FLOAT fResult=Lerp( fValue, fMaxRandomized, fFilterPower);
UWORD uwResult=Clamp((UWORD)fResult,MIN_UWORD,MAX_UWORD);
_puwBuffer[iPixSrc]=uwResult;
}
}
}
void ApplyPosterize(void)
{
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
FLOAT fStepUW=theApp.m_fPosterizeStep/ptrTerrain->tr_vTerrainSize(2)*65535.0f;
for(INDEX y=0; y<_rect.Height(); y++)
{
for(INDEX x=0; x<_rect.Width(); x++)
{
FLOAT fBrushMultiplier=GetBrushMultiplier(x,y);
if(fBrushMultiplier==0.0f) continue;
INDEX iPixSrc=y*_rect.Width()+x;
FLOAT fValue=_puwBuffer[iPixSrc];
FLOAT fPosterized=(INDEX(fValue/fStepUW))*fStepUW+1.0f;
UWORD uwResult=Clamp(UWORD(fPosterized),MIN_UWORD,MAX_UWORD);
_puwBuffer[iPixSrc]=uwResult;
}
}
}
void ApplyFilterMatrix(FLOAT afFilterMatrix[5][5])
{
INDEX ctBuffBytes=_rect.Width()*_rect.Height()*sizeof(UWORD);
UWORD *puwDst=(UWORD*)AllocMemory(ctBuffBytes);
memcpy(puwDst,_puwBuffer,ctBuffBytes);
for(INDEX y=0; y<_rect.Height()-_srcExtraH*2; y++)
{
for(INDEX x=0; x<_rect.Width()-_srcExtraW*2; x++)
{
INDEX iPixDst=(y+_srcExtraH)*_rect.Width()+x+_srcExtraW;
FLOAT fBrushMultiplier=GetBrushMultiplier(x,y);
FLOAT fDivSum=0.0f;
FLOAT fSum=0.0f;
for(INDEX j=0; j<5; j++)
{
for(INDEX i=0; i<5; i++)
{
FLOAT fWeight=(afFilterMatrix)[i][j];
fDivSum+=fWeight;
INDEX iPixSrc=(y+j)*_rect.Width()+(x+i);
FLOAT fInfluence=_puwBuffer[iPixSrc];
fSum+=fInfluence*fWeight;
}
}
UWORD uwMax=Clamp(UWORD(fSum/fDivSum),MIN_UWORD,MAX_UWORD);
FLOAT fFilterPower=Clamp(fBrushMultiplier*_fStrength/64.0f,0.0f,1.0f);
UWORD uwResult=Lerp( puwDst[iPixDst], uwMax, fFilterPower);
puwDst[iPixDst]=uwResult;
}
}
memcpy(_puwBuffer,puwDst,ctBuffBytes);
FreeMemory( puwDst);
}
static INDEX _iTerrainWidth=0;
static UWORD *_puwHeightMap=NULL;
static INDEX _iRandomDX=0;
void SetHMPixel( UWORD pix, INDEX x, INDEX y)
{
UWORD *pdest=_puwHeightMap+y*_iTerrainWidth+x;
if (*pdest==65535) {
*pdest=pix;
}
}
UWORD GetHMPixel(INDEX x, INDEX y)
{
UWORD *pdest=_puwHeightMap+y*_iTerrainWidth+x;
return *pdest;
}
UWORD RandomizePixel(FLOAT fmid, FLOAT fdmax)
{
FLOAT fRand=((FLOAT)rand())/RAND_MAX-0.5f;
FLOAT fd=fdmax*fRand;
FLOAT fres=Clamp(fmid+fd,0.0f,65536.0f);
return fres;
}
void SubdivideAndDisplace(INDEX x, INDEX y, INDEX idx, FLOAT fdMax)
{
FLOAT flu=GetHMPixel(x,y);
FLOAT fru=GetHMPixel(x+idx,y);
FLOAT frd=GetHMPixel(x+idx,y+idx);
FLOAT fld=GetHMPixel(x,y+idx);
if( fdMax<_iRandomDX)
{
SetHMPixel(RandomizePixel((flu+fru)/2.0f,fdMax), x+idx/2, y ); // middle top
SetHMPixel(RandomizePixel((fld+frd)/2.0f,fdMax), x+idx/2, y+idx ); // middle bottom
SetHMPixel(RandomizePixel((fru+frd)/2.0f,fdMax), x+idx, y+idx/2); // right middle
SetHMPixel(RandomizePixel((flu+fld)/2.0f,fdMax), x, y+idx/2); // left middle
SetHMPixel(RandomizePixel((flu+fru+fld+frd)/4.0f,fdMax), x+idx/2, y+idx/2); // middle
}
else
{
SetHMPixel(RandomizePixel(65536.0f/2.0f,fdMax), x+idx/2, y ); // middle top
SetHMPixel(RandomizePixel(65536.0f/2.0f,fdMax), x+idx/2, y+idx ); // middle bottom
SetHMPixel(RandomizePixel(65536.0f/2.0f,fdMax), x+idx, y+idx/2); // right middle
SetHMPixel(RandomizePixel(65536.0f/2.0f,fdMax), x, y+idx/2); // left middle
SetHMPixel(RandomizePixel(65536.0f/2.0f,fdMax), x+idx/2, y+idx/2); // middle
}
fdMax*=0.5f;
if(idx>1)
{
SubdivideAndDisplace(x ,y , idx/2, fdMax);
SubdivideAndDisplace(x+idx/2,y , idx/2, fdMax);
SubdivideAndDisplace(x ,y+idx/2, idx/2, fdMax);
SubdivideAndDisplace(x+idx/2,y+idx/2, idx/2, fdMax);
}
}
Rect GetTerrainRect(void)
{
Rect rect;
rect.rc_iLeft=0;
rect.rc_iRight=0;
rect.rc_iTop=0;
rect.rc_iBottom=0;
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return rect;
rect.rc_iLeft=0;
rect.rc_iRight=ptrTerrain->tr_pixHeightMapWidth;
rect.rc_iTop=0;
rect.rc_iBottom=ptrTerrain->tr_pixHeightMapHeight;
return rect;
}
FLOAT GetWrappedPixelValue( INDEX x, INDEX y)
{
INDEX iWrapX=(x+WNOISE)%WNOISE;
INDEX iWrapY=(y+WNOISE)%WNOISE;
return _pafWhiteNoise[iWrapY*WNOISE+iWrapX];
}
void RandomizeWhiteNoise(void)
{
if(_pafWhiteNoise==NULL)
{
_pafWhiteNoise=(FLOAT *)AllocMemory(WNOISE*WNOISE*sizeof(FLOAT));
}
FLOAT *pfTemp=_pafWhiteNoise;
for(INDEX i=0; i<WNOISE*WNOISE; i++)
{
FLOAT fRnd=FLOAT(rand())/RAND_MAX-0.5f;
*pfTemp=fRnd;
pfTemp++;
}
}
FLOAT *GenerateTerrain_FBMBuffer(PIX pixW, PIX pixH, INDEX ctOctaves, FLOAT fHighFrequencyStep,
FLOAT fStepFactor, FLOAT fMaxAmplitude, FLOAT fAmplitudeDecreaser,
BOOL bAddNegativeValues, BOOL bRandomOffest, FLOAT &fMin, FLOAT &fMax)
{
if(_pafWhiteNoise==NULL)
{
RandomizeWhiteNoise();
}
FLOAT *pfTemp=_pafWhiteNoise;
FLOAT fTmpMaxAmplitude=fMaxAmplitude;
INDEX ctMemory=pixW*pixH*sizeof(FLOAT);
FLOAT *pafFBM=(FLOAT *)AllocMemory(ctMemory);
memset(pafFBM,0,ctMemory);
FLOAT fPixStep=fHighFrequencyStep/pow(fStepFactor,ctOctaves);
fMin=1e6;
fMax=-1e6;
for(INDEX iOctave=ctOctaves-1; iOctave>=0; iOctave--)
{
FLOAT fOctaveOffset=0.0f;
if( bRandomOffest)
{
fOctaveOffset=_pafWhiteNoise[iOctave];
}
for(INDEX y=0; y<pixH; y++)
{
for(INDEX x=0; x<pixW; x++)
{
FLOAT fY=y*fPixStep+fOctaveOffset;
FLOAT fX=x*fPixStep+fOctaveOffset;
// calculate bilinear value
FLOAT fLU=GetWrappedPixelValue( fX , fY);
FLOAT fRU=GetWrappedPixelValue( fX+1, fY);
FLOAT fLD=GetWrappedPixelValue( fX , fY+1);
FLOAT fRD=GetWrappedPixelValue( fX+1, fY+1);
FLOAT fFX=fX-INDEX(fX);
FLOAT fFY=fY-INDEX(fY);
FLOAT fBil=Lerp(Lerp(fLU,fRU,fFX),Lerp(fLD,fRD,fFX),fFY);
INDEX iOffset=pixW*y+x;
FLOAT fValue=pafFBM[iOffset];
FLOAT fAdd=fBil*fTmpMaxAmplitude;
if(bAddNegativeValues || fAdd>0)
{
fValue=fValue+fAdd;
}
pafFBM[iOffset]=fValue;
if(fValue>fMax) fMax=fValue;
if(fValue<fMin) fMin=fValue;
}
}
fPixStep*=fStepFactor;
fTmpMaxAmplitude*=fAmplitudeDecreaser;
}
return pafFBM;
}
void GenerateTerrain_SubdivideAndDisplace(void)
{
// inside subdivide and displace functions we will use these global variables
_iTerrainWidth=_rect.Width();
_puwHeightMap=_puwBuffer;
UWORD uwScrollValue=8.0f-Clamp(theApp.m_iRNDSubdivideAndDisplaceItterations, INDEX(0), INDEX(8));
_iRandomDX=(_iTerrainWidth-1)<<uwScrollValue;
UWORD uwrnd;
FLOAT fdMax=65536.0f;
for (INDEX i=0; i<_rect.Width()*_rect.Height(); i++) {
_puwHeightMap[i] = 65535;
}
uwrnd=RandomizePixel(fdMax/2.0f,fdMax); SetHMPixel(uwrnd, 0, 0);
uwrnd=RandomizePixel(fdMax/2.0f,fdMax); SetHMPixel(uwrnd, _iTerrainWidth-1, 0);
uwrnd=RandomizePixel(fdMax/2.0f,fdMax); SetHMPixel(uwrnd, _iTerrainWidth-1, _iTerrainWidth-1);
uwrnd=RandomizePixel(fdMax/2.0f,fdMax); SetHMPixel(uwrnd, 0, _iTerrainWidth-1);
// generate rest of terrain pixels using recursion
SubdivideAndDisplace(0,0,_iTerrainWidth-1,fdMax/2.0f);
}
void GenerateTerrain(void)
{
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
switch(theApp.m_iTerrainGenerationMethod)
{
case 0:
{
GenerateTerrain_SubdivideAndDisplace();
break;
}
case 1:
{
FLOAT fMin, fMax;
PIX pixTerrainW=ptrTerrain->tr_pixHeightMapWidth;
PIX pixTerrainH=ptrTerrain->tr_pixHeightMapHeight;
FLOAT *pafFBM=GenerateTerrain_FBMBuffer( pixTerrainW, pixTerrainH, theApp.m_iFBMOctaves,
theApp.m_fFBMHighFrequencyStep,theApp.m_fFBMStepFactor, theApp.m_fFBMMaxAmplitude,
theApp.m_fFBMfAmplitudeDecreaser, theApp.m_bFBMAddNegativeValues, theApp.m_bFBMRandomOffset, fMin, fMax);
// convert buffer to height map
FLOAT fSizeY=ptrTerrain->tr_vTerrainSize(2);
FLOAT fConvertFactor=(theApp.m_fFBMMaxAmplitude/fSizeY)*MAX_UWORD;
// set height map
for(INDEX iPix=0; iPix<pixTerrainW*pixTerrainH; iPix++)
{
FLOAT fValue=pafFBM[iPix];
UWORD uwValue=UWORD(Clamp((fValue-fMin)/(fMax-fMin)*fConvertFactor,0.0f,65535.0f));
_puwBuffer[iPix]=uwValue;
}
FreeMemory( pafFBM);
break;
}
}
}
void EqualizeBuffer(void)
{
UWORD uwHeightMax=0;
UWORD uwHeightMin=MAX_UWORD;
INDEX x,y;
for(y=0; y<_rect.Height(); y++)
{
for(x=0; x<_rect.Width(); x++)
{
INDEX iOffset = y*_rect.Width()+x;
UWORD uwHeight = _puwBuffer[iOffset];
if( uwHeight>uwHeightMax) uwHeightMax=uwHeight;
if( uwHeight<uwHeightMin) uwHeightMin=uwHeight;
}
}
FLOAT fFactor=65535.0f/(uwHeightMax-uwHeightMin);
// equalize (normalize from 0 to 65535)
for(y=0; y<_rect.Height(); y++)
{
for(x=0; x<_rect.Width(); x++)
{
INDEX iOffset = y*_rect.Width()+x;
UWORD uwHeight = _puwBuffer[iOffset];
FLOAT fNormalized=(uwHeight-uwHeightMin)*fFactor;
_puwBuffer[iOffset]=fNormalized;
}
}
}
BOOL SetupContinousNoiseTexture( void)
{
try
{
_ptdContinousRandomNoise=_pTextureStock->Obtain_t( theApp.m_fnContinousNoiseTexture);
_ptdContinousRandomNoise->Force(TEX_STATIC|TEX_CONSTANT);
}
catch( char *strError)
{
(void) strError;
WarningMessage("Unable to obtain continous random noise texture!\nError: %s", strError);
return FALSE;
}
return TRUE;
}
void FreeContinousNoiseTexture( void)
{
_pTextureStock->Release( _ptdContinousRandomNoise);
}
BOOL SetupDistributionNoiseTexture( void)
{
try
{
_ptdDistributionRandomNoise=_pTextureStock->Obtain_t( theApp.m_fnDistributionNoiseTexture);
_ptdDistributionRandomNoise->Force(TEX_STATIC|TEX_CONSTANT);
}
catch( char *strError)
{
(void) strError;
WarningMessage("Unable to obtain distribution random noise texture!\nError: %s", strError);
return FALSE;
}
return TRUE;
}
void FreeDistributionNoiseTexture( void)
{
_pTextureStock->Release( _ptdDistributionRandomNoise);
}
FLOAT StepUp(FLOAT fCur, FLOAT fMin, FLOAT fMax)
{
if( fCur<=fMin) return 0.0f;
if( fCur>=fMax) return 1.0f;
return (fCur-fMin)/(fMax-fMin);
}
FLOAT StepDown(FLOAT fCur, FLOAT fMin, FLOAT fMax)
{
if( fCur<=fMin) return 1.0f;
if( fCur>=fMax) return 0.0f;
return (fMax-fCur)/(fMax-fMin);
}
FLOAT3D NormalFrom4Points(const FLOAT3D &v0, const FLOAT3D &v1, const FLOAT3D &v2, const FLOAT3D &v3,
FLOAT fLerpX, FLOAT fLerpZ)
{
FLOAT fHDeltaX = Lerp(v1(2)-v0(2), v3(2)-v2(2), fLerpZ);
FLOAT fHDeltaZ = Lerp(v0(2)-v2(2), v1(2)-v3(2), fLerpX);
FLOAT fDeltaX = v1(1) - v0(1);
FLOAT fDeltaZ = v0(3) - v2(3);
FLOAT3D vNormal;
vNormal(2) = sqrt(1 / (((fHDeltaX*fHDeltaX)/(fDeltaX*fDeltaX)) + ((fHDeltaZ*fHDeltaZ)/(fDeltaZ*fDeltaZ)) + 1));
vNormal(1) = sqrt(vNormal(2)*vNormal(2) * ((fHDeltaX*fHDeltaX) / (fDeltaX*fDeltaX)));
vNormal(3) = sqrt(vNormal(2)*vNormal(2) * ((fHDeltaZ*fHDeltaZ) / (fDeltaZ*fDeltaZ)));
if (fHDeltaX>0) {
vNormal(1) = -vNormal(1);
}
if (fHDeltaZ<0) {
vNormal(3) = -vNormal(3);
}
//ASSERT(Abs(vNormal.Length()-1)<0.01);
return vNormal;
}
FLOAT3D GetPoint(CTerrain *ptrTerrain, INDEX iX, INDEX iY)
{
const FLOAT3D &vStretch = ptrTerrain->tr_vStretch;
iX = Clamp(iX, INDEX(0), ptrTerrain->tr_pixHeightMapWidth);
iY = Clamp(iY, INDEX(0), ptrTerrain->tr_pixHeightMapHeight);
return FLOAT3D(
FLOAT(iX)*vStretch(1),
(FLOAT)ptrTerrain->tr_auwHeightMap[iX+iY*ptrTerrain->tr_pixHeightMapWidth] * vStretch(2),
FLOAT(iY)*vStretch(3));
}
UWORD GetSlope(CTerrain *ptrTerrain, INDEX iX, INDEX iY)
{
FLOAT3D av[9];
INDEX iHMapWidth = ptrTerrain->tr_pixHeightMapWidth;
FLOAT3D vStretch = ptrTerrain->tr_vStretch;
av[0] = GetPoint(ptrTerrain, iX-1, iY-1);
av[1] = GetPoint(ptrTerrain, iX , iY-1);
av[2] = GetPoint(ptrTerrain, iX+1, iY-1);
av[3] = GetPoint(ptrTerrain, iX-1, iY );
av[4] = GetPoint(ptrTerrain, iX , iY );
av[5] = GetPoint(ptrTerrain, iX+1, iY );
av[6] = GetPoint(ptrTerrain, iX-1, iY+1);
av[7] = GetPoint(ptrTerrain, iX , iY+1);
av[8] = GetPoint(ptrTerrain, iX+1, iY+1);
FLOAT3D avN[4];
FLOAT3D vNormal;
avN[0] = NormalFrom4Points(av[0], av[1], av[3], av[4], 1, 1);
avN[1] = NormalFrom4Points(av[1], av[2], av[4], av[5], 0, 1);
avN[2] = NormalFrom4Points(av[3], av[4], av[6], av[7], 1, 0);
avN[3] = NormalFrom4Points(av[4], av[5], av[7], av[8], 0, 0);
vNormal = avN[0]+avN[1]+avN[2]+avN[3];
vNormal.Normalize();
return (1-vNormal(2))*65535;
}
void GenerateLayerDistribution(INDEX iForLayer, Rect rect)
{
if(!SetupDistributionNoiseTexture()) return;
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
// obtain buffer
UWORD *puwAltitude=GetBufferForEditing(ptrTerrain, rect, BT_HEIGHT_MAP, 0);
INDEX ctSize=rect.Width()*rect.Height()*sizeof(UWORD);
UWORD *puwSlope=(UWORD *)AllocMemory(ctSize);
// prepare slope buffer
for(INDEX y=0; y<rect.Height(); y++)
{
for(INDEX x=0; x<rect.Width(); x++)
{
INDEX iOffset = y*rect.Width()+x;
puwSlope[iOffset] = GetSlope(ptrTerrain, x+rect.rc_iLeft, y+rect.rc_iTop);
}
}
// for each layer
for(INDEX iLayer=0; iLayer<ptrTerrain->tr_atlLayers.Count(); iLayer++)
{
if( iForLayer!=-1 && iLayer!=iForLayer) continue;
CTerrainLayer *ptlLayer=GetLayer(iLayer);
if(!ptlLayer->tl_bAutoRegenerated) continue;
// get layer
UWORD *puwMask=GetBufferForEditing(ptrTerrain, rect, BT_LAYER_MASK, iLayer);
for(INDEX y=0; y<rect.Height(); y++)
{
INDEX oy=y+rect.rc_iTop;
for(INDEX x=0; x<rect.Width(); x++)
{
INDEX ox=x+rect.rc_iLeft;
INDEX iOffset = y*rect.Width()+x;
FLOAT fAltitudeRatio = puwAltitude[iOffset]/65535.0f;
FLOAT fSlopeRatio = puwSlope[iOffset]/65535.0f;
FLOAT fAltitudeRange=ptlLayer->tl_fMaxAltitude-ptlLayer->tl_fMinAltitude;
FLOAT fSlopeRange=ptlLayer->tl_fMaxSlope-ptlLayer->tl_fMinSlope;
// get coverage influence
FLOAT fCoverageInfluence=1.0f;
FLOAT fCoverageRandom=GetDistributionNoise( ox, oy, ptlLayer->tl_fCoverageRandom);
fCoverageInfluence=StepDown(fCoverageRandom, ptlLayer->tl_fCoverage, ptlLayer->tl_fCoverage+ptlLayer->tl_fCoverageNoise);
// get min altitude influence
FLOAT fMinAltitudeInfluence=1.0f;
if(ptlLayer->tl_bApplyMinAltitude)
{
FLOAT fMinAltitudeNoise=(GetDistributionNoise( ox, oy, ptlLayer->tl_fMinAltitudeRandom)-0.5f)*ptlLayer->tl_fMinAltitudeNoise;
FLOAT fAltMinFade1=ptlLayer->tl_fMinAltitude+fAltitudeRange*ptlLayer->tl_fMinAltitudeFade;
fMinAltitudeInfluence=StepUp(fAltitudeRatio+fMinAltitudeNoise, ptlLayer->tl_fMinAltitude, fAltMinFade1);
}
// get max altitude influence
FLOAT fMaxAltitudeInfluence=1.0f;
if(ptlLayer->tl_bApplyMaxAltitude)
{
FLOAT fMaxAltitudeNoise=(GetDistributionNoise( ox, oy, ptlLayer->tl_fMaxAltitudeRandom)-0.5f)*ptlLayer->tl_fMaxAltitudeNoise;
FLOAT fAltMaxFade1=ptlLayer->tl_fMaxAltitude-fAltitudeRange*ptlLayer->tl_fMaxAltitudeFade;
fMaxAltitudeInfluence=StepDown(fAltitudeRatio+fMaxAltitudeNoise, fAltMaxFade1, ptlLayer->tl_fMaxAltitude);
}
// get min slope influence
FLOAT fMinSlopeInfluence=1.0f;
if(ptlLayer->tl_bApplyMinSlope)
{
FLOAT fMinSlopeNoise=(GetDistributionNoise( ox, oy, ptlLayer->tl_fMinSlopeRandom)-0.5f)*ptlLayer->tl_fMinSlopeNoise;
FLOAT fSlopeMinFade1=ptlLayer->tl_fMinSlope+fSlopeRange*ptlLayer->tl_fMinSlopeFade;
fMinSlopeInfluence=StepUp(fSlopeRatio+fMinSlopeNoise, ptlLayer->tl_fMinSlope, fSlopeMinFade1);
}
// get max slope influence
FLOAT fMaxSlopeInfluence=1.0f;
if(ptlLayer->tl_bApplyMaxSlope)
{
FLOAT fMaxSlopeNoise=(GetDistributionNoise( ox, oy, ptlLayer->tl_fMaxSlopeRandom)-0.5f)*ptlLayer->tl_fMaxSlopeNoise;
FLOAT fSlopeMaxFade1=ptlLayer->tl_fMaxSlope-fSlopeRange*ptlLayer->tl_fMaxSlopeFade;
fMaxSlopeInfluence=StepDown(fSlopeRatio+fMaxSlopeNoise, fSlopeMaxFade1, ptlLayer->tl_fMaxSlope);
}
// calculate result of all influences
puwMask[iOffset]= 65535.0f*
fCoverageInfluence*
fMinAltitudeInfluence*
fMaxAltitudeInfluence*
fMinSlopeInfluence*
fMaxSlopeInfluence;
}
}
// apply buffer change
SetBufferForEditing(ptrTerrain, puwMask, rect, BT_LAYER_MASK, iLayer);
theApp.GetActiveDocument()->SetModifiedFlag( TRUE);
FreeMemory(puwMask);
}
FreeMemory(puwAltitude);
theApp.m_ctTerrainPageCanvas.MarkChanged();
FreeDistributionNoiseTexture();
}
void GenerateLayerDistribution(INDEX iForLayer)
{
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
Rect rect;
rect.rc_iLeft=0;
rect.rc_iRight=ptrTerrain->tr_pixHeightMapWidth;
rect.rc_iTop=0;
rect.rc_iBottom=ptrTerrain->tr_pixHeightMapHeight;
GenerateLayerDistribution(iForLayer, rect);
}
void RecalculateShadows(void)
{
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
ptrTerrain->UpdateShadowMap();
}
void OptimizeLayers(void)
{
CTerrain *ptrTerrain=GetTerrain();
if( ptrTerrain==NULL) return;
Rect rect;
rect.rc_iLeft=0;
rect.rc_iRight=ptrTerrain->tr_pixHeightMapWidth;
rect.rc_iTop=0;
rect.rc_iBottom=ptrTerrain->tr_pixHeightMapHeight;
CStaticArray<UWORD*> apuwLayers;
// obtain buffer
INDEX ctLayers=ptrTerrain->tr_atlLayers.Count();
apuwLayers.New(ctLayers);
INDEX iLayer, iOffset;
for( iLayer=0; iLayer<ctLayers; iLayer++)
{
UWORD *puw=GetBufferForEditing(ptrTerrain, rect, BT_LAYER_MASK, iLayer);
apuwLayers[iLayer]=puw;
}
// count overdraw before optimisation
INDEX ctDrawnBefore=0;
INDEX ctPixels=rect.Width()*rect.Height();
for(iOffset=0; iOffset<ctPixels; iOffset++)
{
for(INDEX i=0; i<ctLayers; i++)
{
UWORD *puwCurr=apuwLayers[i]+iOffset;
if( (*puwCurr)>>8 != 0) ctDrawnBefore++;
}
}
// optimize for overdraw
for(iOffset=0; iOffset<ctPixels; iOffset++)
{
BOOL bOptimize=FALSE;
for(INDEX i=ctLayers-1; i>=0; i--)
{
UWORD *puwCurr=apuwLayers[i]+iOffset;
// if should optimize
if(bOptimize)
{
// clear mask
*puwCurr=0;
}
else
{
if( (*puwCurr)>>8 == 255)
{
// clear mask for all layers beneath current one
bOptimize=TRUE;
}
}
}
}
// count overdraw after optimisation
INDEX ctDrawnAfter=0;
for(iOffset=0; iOffset<ctPixels; iOffset++)
{
for(INDEX i=0; i<ctLayers; i++)
{
UWORD *puwCurr=apuwLayers[i]+iOffset;
if( (*puwCurr)>>8 != 0) ctDrawnAfter++;
}
}
// make a report about optimisation success
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
CTString strReport;
strReport.PrintF("Overdraw before optimization: %g. Overdraw after optimization: %g",
FLOAT(ctDrawnBefore)/ctPixels, FLOAT(ctDrawnAfter)/ctPixels);
pMainFrame->SetStatusBarMessage( strReport, STATUS_LINE_PANE, 5.0f);
// free buffers
for( iLayer=0; iLayer<ctLayers; iLayer++)
{
SetBufferForEditing(ptrTerrain, apuwLayers[iLayer], rect, BT_LAYER_MASK, iLayer);
FreeMemory(apuwLayers[iLayer]);
}
theApp.GetActiveDocument()->SetModifiedFlag( TRUE);
theApp.m_ctTerrainPageCanvas.MarkChanged();
}
BOOL _bIsUpToDate=TRUE;
Rect _rectDiscarded;
void DiscardLayerDistribution(Rect rect)
{
if(_bIsUpToDate)
{
_rectDiscarded.rc_iLeft=rect.rc_iLeft;
_rectDiscarded.rc_iRight=rect.rc_iRight;
_rectDiscarded.rc_iTop=rect.rc_iTop;
_rectDiscarded.rc_iBottom=rect.rc_iBottom;
}
else
{
if(rect.rc_iLeft < _rectDiscarded.rc_iLeft) _rectDiscarded.rc_iLeft=rect.rc_iLeft;
if(rect.rc_iRight > _rectDiscarded.rc_iRight) _rectDiscarded.rc_iRight=rect.rc_iRight;
if(rect.rc_iTop < _rectDiscarded.rc_iTop) _rectDiscarded.rc_iTop=rect.rc_iTop;
if(rect.rc_iBottom > _rectDiscarded.rc_iBottom) _rectDiscarded.rc_iBottom=rect.rc_iBottom;
}
_bIsUpToDate=FALSE;
}
void UpdateLayerDistribution(void)
{
if(_bIsUpToDate || !theApp.m_Preferences.ap_bAutoUpdateTerrainDistribution) return;
// update layer distribution
GenerateLayerDistribution(-1, _rectDiscarded);
_bIsUpToDate=TRUE;
}
void ApplyFilterOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), theApp.m_fFilterPower*16.0f, TE_ALTITUDE_FILTER);
}
void ApplySmoothOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), theApp.m_fSmoothPower*16.0f, TE_ALTITUDE_SMOOTH);
}
void ApplyEqualizeOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), 1.0f, TE_ALTITUDE_EQUALIZE);
}
void ApplyGenerateTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), 1.0f, TE_GENERATE_TERRAIN);
}
void ApplyRndNoiseOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), theApp.m_fNoiseAltitude, TE_ALTITUDE_RND_NOISE);
}
void ApplyContinousNoiseOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), theApp.m_fNoiseAltitude, TE_ALTITUDE_CONTINOUS_NOISE);
}
void ApplyMinimumOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), 1.0f, TE_ALTITUDE_MINIMUM);
}
void ApplyMaximumOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), 1.0f, TE_ALTITUDE_MAXIMUM);
}
void ApplyFlattenOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), 1.0f, TE_ALTITUDE_FLATTEN);
}
void ApplyPosterizeOntoTerrain(void)
{
EditTerrain(NULL, FLOAT3D(0,0,0), theApp.m_fPosterizeStep, TE_ALTITUDE_POSTERIZE);
}
CEntity *GetEntityForID(ULONG iEntityID)
{
CWorldEditorDoc* pDoc = theApp.GetActiveDocument();
FOREACHINDYNAMICCONTAINER(pDoc->m_woWorld.wo_cenEntities, CEntity, iten)
{
if(iten->en_ulID==iEntityID) return &*iten;
}
return NULL;
}
// constructor
CTerrainUndo::CTerrainUndo()
{
tu_puwUndoBuffer=NULL;
tu_puwRedoBuffer=NULL;
}
void DeleteOneUndo(CTerrainUndo *ptrud)
{
if(ptrud->tu_puwUndoBuffer!=NULL) FreeMemory(ptrud->tu_puwUndoBuffer);
if(ptrud->tu_puwRedoBuffer!=NULL) FreeMemory(ptrud->tu_puwRedoBuffer);
delete ptrud;
}
void DeleteTerrainUndo(CWorldEditorDoc* pDoc)
{
for(INDEX iUndo=0; iUndo<pDoc->m_dcTerrainUndo.Count(); iUndo++)
{
CTerrainUndo *ptu=&pDoc->m_dcTerrainUndo[iUndo];
pDoc->m_dcTerrainUndo.Remove(ptu);
DeleteOneUndo(ptu);
}
}
CTerrain *GetUndoTerrain(ULONG ulEntityID)
{
// obtain terrain entity
CEntity *penTerrain=GetEntityForID(_iTerrainEntityID);
if(penTerrain==NULL)
{
return NULL;
}
// obtain terrain
CTerrain *ptrTerrain=penTerrain->GetTerrain();
return ptrTerrain;
}
void ApplyTerrainUndo(CTerrainUndo *ptrud)
{
CWorldEditorDoc* pDoc = theApp.GetActiveDocument();
CTerrain *ptrTerrain=GetUndoTerrain(ptrud->tu_ulEntityID);
if(ptrTerrain==NULL)
{
DeleteOneUndo(ptrud);
return;
}
// obtain and store redo buffer
if(ptrud->tu_puwRedoBuffer==NULL)
{
ptrud->tu_puwRedoBuffer=GetBufferForEditing(ptrTerrain, ptrud->tu_rcRect,
ptrud->tu_btUndoBufferType, ptrud->tu_iUndoBufferData);
}
// apply buffer change (undo)
SetBufferForEditing(ptrTerrain, ptrud->tu_puwUndoBuffer, ptrud->tu_rcRect,
ptrud->tu_btUndoBufferType, ptrud->tu_iUndoBufferData);
pDoc->m_iCurrentTerrainUndo--;
DiscardLayerDistribution(ptrud->tu_rcRect);
pDoc->SetModifiedFlag( TRUE);
theApp.m_ctTerrainPageCanvas.MarkChanged();
}
void ApplyTerrainRedo(CTerrainUndo *ptrud)
{
CWorldEditorDoc* pDoc = theApp.GetActiveDocument();
CTerrain *ptrTerrain=GetUndoTerrain(ptrud->tu_ulEntityID);
if(ptrTerrain==NULL)
{
DeleteOneUndo(ptrud);
return;
}
if(ptrud->tu_puwRedoBuffer==NULL) return;
// apply buffer change (redo)
SetBufferForEditing(ptrTerrain, ptrud->tu_puwRedoBuffer, ptrud->tu_rcRect,
ptrud->tu_btUndoBufferType, ptrud->tu_iUndoBufferData);
pDoc->m_iCurrentTerrainUndo++;
DiscardLayerDistribution(ptrud->tu_rcRect);
pDoc->SetModifiedFlag( TRUE);
theApp.m_ctTerrainPageCanvas.MarkChanged();
}
UWORD *ExtractUndoRect(PIX pixTerrainWidth)
{