forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHeightMap.cpp
More file actions
2455 lines (2192 loc) · 86.1 KB
/
HeightMap.cpp
File metadata and controls
2455 lines (2192 loc) · 86.1 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
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** 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, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: Heightmap.cpp ////////////////////////////////////////////////
//-----------------------------------------------------------------------------
//
// Westwood Studios Pacific.
//
// Confidential Information
// Copyright (C) 2001 - All Rights Reserved
//
//-----------------------------------------------------------------------------
//
// Project: RTS3
//
// File name: Heightmap.cpp
//
// Created: Mark W., John Ahlquist, April/May 2001
//
// Desc: Draw the terrain and scorchmarks in a scene.
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "W3DDevice/GameClient/HeightMap.h"
#ifndef USE_FLAT_HEIGHT_MAP // Flat height map uses flattened textures. jba. [3/20/2003]
#include <stdlib.h>
#include <assetmgr.h>
#include <texture.h>
#include <tri.h>
#include <colmath.h>
#include <coltest.h>
#include <rinfo.h>
#include <camera.h>
#include <d3dx8core.h>
#include "Common/GlobalData.h"
#include "Common/PerfTimer.h"
#include "GameClient/TerrainVisual.h"
#include "GameClient/View.h"
#include "GameClient/Water.h"
#include "GameLogic/AIPathfind.h"
#include "GameLogic/TerrainLogic.h"
#include "W3DDevice/GameClient/TerrainTex.h"
#include "W3DDevice/GameClient/W3DDynamicLight.h"
#include "W3DDevice/GameClient/W3DScene.h"
#include "W3DDevice/GameClient/W3DTerrainTracks.h"
#include "W3DDevice/GameClient/W3DBibBuffer.h"
#include "W3DDevice/GameClient/W3DTreeBuffer.h"
#include "W3DDevice/GameClient/W3DPropBuffer.h"
#include "W3DDevice/GameClient/W3DRoadBuffer.h"
#include "W3DDevice/GameClient/W3DBridgeBuffer.h"
#include "W3DDevice/GameClient/W3DWaypointBuffer.h"
#include "W3DDevice/GameClient/W3DCustomEdging.h"
#include "W3DDevice/GameClient/WorldHeightMap.h"
#include "W3DDevice/GameClient/W3DShaderManager.h"
#include "W3DDevice/GameClient/W3DShadow.h"
#include "W3DDevice/GameClient/W3DWater.h"
#include "W3DDevice/GameClient/W3DShroud.h"
#include "WW3D2/dx8wrapper.h"
#include "WW3D2/light.h"
#include "WW3D2/scene.h"
#include "W3DDevice/GameClient/W3DPoly.h"
#include "W3DDevice/GameClient/W3DCustomScene.h"
#include "Common/PerfTimer.h"
#include "Common/UnitTimings.h" //Contains the DO_UNIT_TIMINGS define jba.
#define no_OPTIMIZED_HEIGHTMAP_LIGHTING 01
// Doesn't work well. jba.
const Bool HALF_RES_MESH = false;
HeightMapRenderObjClass *TheHeightMap = NULL;
//-----------------------------------------------------------------------------
// Private Data
//-----------------------------------------------------------------------------
#define SC_DETAIL_BLEND ( SHADE_CNST(ShaderClass::PASS_LEQUAL, ShaderClass::DEPTH_WRITE_ENABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_ONE, \
ShaderClass::DSTBLEND_ZERO, ShaderClass::FOG_DISABLE, ShaderClass::GRADIENT_MODULATE, ShaderClass::SECONDARY_GRADIENT_DISABLE, ShaderClass::TEXTURING_ENABLE, \
ShaderClass::ALPHATEST_DISABLE, ShaderClass::CULL_MODE_ENABLE, ShaderClass::DETAILCOLOR_SCALE, ShaderClass::DETAILALPHA_DISABLE) )
static ShaderClass detailOpaqueShader(SC_DETAIL_BLEND);
#define DEFAULT_MAX_FRAME_EXTRABLEND_TILES 256 //default number of terrain tiles rendered per call (must fit in one VB)
#define DEFAULT_MAX_MAP_EXTRABLEND_TILES 2048 //default size of array allocated to hold all map extra blend tiles.
#define DEFAULT_MAX_BATCH_SHORELINE_TILES 512 //maximum number of terrain tiles rendered per call (must fit in one VB)
#define DEFAULT_MAX_MAP_SHORELINE_TILES 4096 //default size of array allocated to hold all map shoreline tiles.
#define ADJUST_FROM_INDEX_TO_REAL(k) ((k-m_map->getBorderSizeInline())*MAP_XY_FACTOR)
inline Int IABS(Int x) { if (x>=0) return x; return -x;};
//-----------------------------------------------------------------------------
// Private Functions
//-----------------------------------------------------------------------------
//=============================================================================
// HeightMapRenderObjClass::freeIndexVertexBuffers
//=============================================================================
/** Frees the w3d resources used to draw the terrain. */
//=============================================================================
void HeightMapRenderObjClass::freeIndexVertexBuffers(void)
{
REF_PTR_RELEASE(m_indexBuffer);
if (m_vertexBufferTiles) {
for (int i=0; i<m_numVertexBufferTiles; i++)
REF_PTR_RELEASE(m_vertexBufferTiles[i]);
delete[] m_vertexBufferTiles;
m_vertexBufferTiles = NULL;
}
if (m_vertexBufferBackup) {
for (int i=0; i<m_numVertexBufferTiles; i++)
delete[] m_vertexBufferBackup[i];
delete[] m_vertexBufferBackup;
m_vertexBufferBackup = NULL;
}
m_numVertexBufferTiles = 0;
}
//=============================================================================
// HeightMapRenderObjClass::freeMapResources
//=============================================================================
/** Frees the w3d resources used to draw the terrain. */
//=============================================================================
Int HeightMapRenderObjClass::freeMapResources(void)
{
BaseHeightMapRenderObjClass::freeMapResources();
freeIndexVertexBuffers();
return 0;
}
//=============================================================================
// HeightMapRenderObjClass::doTheDynamicLight
//=============================================================================
/** Calculates the diffuse lighting as affected by dynamic lighting. */
//=============================================================================
UnsignedInt HeightMapRenderObjClass::doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX_FORMAT *vbMirror, Vector3*light, Vector3*normal, W3DDynamicLight *pLights[], Int numLights)
{
#ifdef USE_NORMALS
return;
#else
Real shadeR, shadeG, shadeB;
Int diffuse = vbMirror->diffuse;
#ifdef RTS_DEBUG
//vbMirror->diffuse += 30; // Shows which vertexes are geting touched by dynamic light. debug only.
#endif
// (gth) avoiding the extra divides (compiler unfortunately didn't do this automatically...)
const float oo255 = (1.0f/255.0f);
shadeR = ((diffuse>>16)&0x00FF) * oo255;
shadeG = ((diffuse>>8)&0x00FF) * oo255;
shadeB = (diffuse&0x00FF) * oo255;
Int alpha = (diffuse>>24)&0x00FF;
Int k;
for (k=0; k<numLights; k++) {
W3DDynamicLight *pLight = pLights[k];
if (!pLight->isEnabled()) {
continue; // he is turned off.
}
Vector3 lightDirection(vbMirror->x, vbMirror->y, vbMirror->z);
Real factor = 1.0f;
switch(pLight->Get_Type()) {
case LightClass::POINT:
case LightClass::SPOT: {
Vector3 lightLoc = pLight->Get_Position();
lightDirection -= lightLoc;
double range, midRange;
pLight->Get_Far_Attenuation_Range(midRange, range);
Real dist = lightDirection.Length();
if (dist >= range) continue;
if (midRange < 0.1) continue;
factor = 1.0f - (dist - midRange) / (range - midRange);
factor = WWMath::Clamp(factor,0.0f,1.0f);
// (gth) normalize here since we have the length
lightDirection /= dist;
}
break;
case LightClass::DIRECTIONAL:
pLight->Get_Spot_Direction(lightDirection);
factor = 1.0;
break;
};
// (gth) unneeded due to above normalization
//lightDirection.Normalize();
Vector3 lightRay(-lightDirection.X, -lightDirection.Y, -lightDirection.Z);
Real shade = Vector3::Dot_Product(lightRay, *normal);
shade *= factor;
Vector3 diffuse;
pLight->Get_Diffuse(&diffuse);
Vector3 ambient;
pLight->Get_Ambient(&ambient);
if (shade > 1.0) shade = 1.0;
if(shade < 0.0f) shade = 0.0f;
shadeR += shade*diffuse.X;
shadeG += shade*diffuse.Y;
shadeB += shade*diffuse.Z;
shadeR += factor*ambient.X;
shadeG += factor*ambient.Y;
shadeB += factor*ambient.Z;
}
if (shadeR > 1.0) shadeR = 1.0;
if(shadeR < 0.0f) shadeR = 0.0f;
if (shadeG > 1.0) shadeG = 1.0;
if(shadeG < 0.0f) shadeG = 0.0f;
if (shadeB > 1.0) shadeB = 1.0;
if(shadeB < 0.0f) shadeB = 0.0f;
shadeR*=255.0f;
shadeG*=255.0f;
shadeB*=255.0f;
// (gth) faster float to int conversion, return the result so we can re-use it.
// vb->diffuse=REAL_TO_INT(shadeB) | (REAL_TO_INT(shadeG) << 8) | (REAL_TO_INT(shadeR) << 16) | ((int)alpha << 24);
UnsignedInt light_val = WWMath::Float_To_Int_Chop(shadeB) | (WWMath::Float_To_Int_Chop(shadeG) << 8) | (WWMath::Float_To_Int_Chop(shadeR) << 16) | ((int)alpha << 24);
vb->diffuse = light_val;
return light_val;
#endif
}
//=============================================================================
// HeightMapRenderObjClass::getXWithOrigin
//=============================================================================
/** Gets the x index that corresponds to the data. For example, if the columns
are shifted by 3, index 3 is actually the first row of polygons, or 0. Yes it
is confusing, but it makes sliding the map 10x faster. */
//=============================================================================
Int HeightMapRenderObjClass::getXWithOrigin(Int x)
{
x -= m_originX;
if (x<0) x+= m_x-1;
if (x>= m_x-1) x-=m_x-1;
#ifdef RTS_DEBUG
DEBUG_ASSERTCRASH (x>=0, ("X out of range."));
DEBUG_ASSERTCRASH (x<m_x-1, ("X out of range."));
#endif
if (x<0) x = 0;
if (x>= m_x-1) x=m_x-1;
return x;
}
//=============================================================================
// HeightMapRenderObjClass::getYWithOrigin
//=============================================================================
/** Gets the y index that corresponds to the data. For example, if the rows
are shifted by 3, index 3 is actually the first row of polygons, or 0. Yes it
is confusing, but it makes sliding the map 10x faster. */
//=============================================================================
Int HeightMapRenderObjClass::getYWithOrigin(Int y)
{
y -= m_originY;
if (y<0) y+= m_y-1;
if (y>= m_y-1) y-=m_y-1;
#ifdef RTS_DEBUG
DEBUG_ASSERTCRASH (y>=0, ("Y out of range."));
DEBUG_ASSERTCRASH (y<m_y-1, ("Y out of range."));
#endif
if (y<0) y = 0;
if (y>= m_y-1) y=m_y-1;
return y;
}
//=============================================================================
// HeightMapRenderObjClass::updateVB
//=============================================================================
/** Update a rectangular block of the given Vertex Buffer.
data is expected to be an array same dimensions as current heightmap
mapped into this VB.
*/
//=============================================================================
Int HeightMapRenderObjClass::updateVB(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, WorldHeightMap *pMap, RefRenderObjListIterator *pLightsIterator)
{
Int i,j;
Vector3 lightRay[MAX_GLOBAL_LIGHTS];
const Coord3D *lightPos;
Int xCoord, yCoord;
Int vn0,un0,vp1,up1;
Vector3 l2r,n2f,normalAtTexel;
Int vertsPerRow=(VERTEX_BUFFER_TILE_LENGTH)*4; //vertices per row of VB
Int cellOffset = 1;
if (HALF_RES_MESH) {
cellOffset = 2;
}
REF_PTR_SET(m_map, pMap); //update our heightmap pointer in case it changed since last call.
if (m_vertexBufferTiles && pMap)
{
#ifdef RTS_DEBUG
assert(x0 >= originX && y0 >= originY && x1>x0 && y1>y0 && x1<=originX+VERTEX_BUFFER_TILE_LENGTH && y1<=originY+VERTEX_BUFFER_TILE_LENGTH);
#endif
DX8VertexBufferClass::WriteLockClass lockVtxBuffer(pVB);
VERTEX_FORMAT *vbHardware = (VERTEX_FORMAT*)lockVtxBuffer.Get_Vertex_Array();
VERTEX_FORMAT *vBase = (VERTEX_FORMAT*)data;
// Note that we are building the vertex buffer data in the memory buffer, data.
// At the bottom, we will copy the final vertex data for one cell into the
// hardware vertex buffer.
for (j=y0; j<y1; j++)
{
VERTEX_FORMAT *vb = vBase;
if (HALF_RES_MESH) {
if (j&1) continue;
vb += ((j-originY)/2)*vertsPerRow/2; //skip to correct row in vertex buffer
vb += ((x0-originX)/2)*4; //skip to correct vertex in row.
} else {
vb += (j-originY)*vertsPerRow; //skip to correct row in vertex buffer
vb += (x0-originX)*4; //skip to correct vertex in row.
}
vn0 = getYWithOrigin(j)-cellOffset;
if (vn0 < -pMap->getDrawOrgY())
vn0=-pMap->getDrawOrgY();
vp1 = getYWithOrigin(j+cellOffset)+cellOffset;
if (vp1 >= pMap->getYExtent()-pMap->getDrawOrgY())
vp1=pMap->getYExtent()-pMap->getDrawOrgY()-1;
yCoord = getYWithOrigin(j)+pMap->getDrawOrgY();
for (i=x0; i<x1; i++)
{
if (HALF_RES_MESH) {
if (i&1) continue;
}
un0 = getXWithOrigin(i)-cellOffset;
if (un0 < -pMap->getDrawOrgX())
un0=-pMap->getDrawOrgX();
up1 = getXWithOrigin(i+cellOffset)+cellOffset;
if (up1 >= pMap->getXExtent()-pMap->getDrawOrgX())
up1=pMap->getXExtent()-pMap->getDrawOrgX()-1;
xCoord = getXWithOrigin(i)+pMap->getDrawOrgX();
//update the 4 vertices in this block
float U[4], V[4];
UnsignedByte alpha[4];
float UA[4], VA[4];
Bool flipForBlend = false; // True if the blend needs the triangles flipped.
if (pMap) {
pMap->getUVData(getXWithOrigin(i),getYWithOrigin(j),U, V, HALF_RES_MESH);
pMap->getAlphaUVData(getXWithOrigin(i),getYWithOrigin(j), UA, VA, alpha, &flipForBlend, HALF_RES_MESH);
}
for (Int lightIndex=0; lightIndex < TheGlobalData->m_numGlobalLights; lightIndex++)
{
lightPos=&TheGlobalData->m_terrainLightPos[lightIndex];
lightRay[lightIndex].Set(-lightPos->x,-lightPos->y, -lightPos->z);
}
//top-left sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset, getYWithOrigin(j)) - pMap->getDisplayHeight(un0, getYWithOrigin(j))));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(getXWithOrigin(i), (getYWithOrigin(j)+cellOffset)) - pMap->getDisplayHeight(getXWithOrigin(i), vn0)));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
vb->x=xCoord;
vb->y=yCoord;
vb->z= ((float)pMap->getDisplayHeight(getXWithOrigin(i), getYWithOrigin(j)))*MAP_HEIGHT_SCALE;
vb->x = ADJUST_FROM_INDEX_TO_REAL(vb->x);
vb->y = ADJUST_FROM_INDEX_TO_REAL(vb->y);
vb->u1=U[0];
vb->v1=V[0];
vb->u2=UA[0];
vb->v2=VA[0];
doTheLight(vb, lightRay, &normalAtTexel, pLightsIterator, alpha[0]);
vb++;
//top-right sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(up1 , getYWithOrigin(j) ) - pMap->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset , (getYWithOrigin(j)+cellOffset) ) - pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset , vn0 )));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
vb->x=xCoord+cellOffset;
vb->y=yCoord;
vb->z= ((float)pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset, getYWithOrigin(j)))*MAP_HEIGHT_SCALE;
vb->x = ADJUST_FROM_INDEX_TO_REAL(vb->x);
vb->y = ADJUST_FROM_INDEX_TO_REAL(vb->y);
vb->u1=U[1];
vb->v1=V[1];
vb->u2=UA[1];
vb->v2=VA[1];
doTheLight(vb, lightRay, &normalAtTexel, pLightsIterator, alpha[1]);
vb++;
//bottom-right sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(up1 , (getYWithOrigin(j)+cellOffset) ) - pMap->getDisplayHeight(getXWithOrigin(i) , (getYWithOrigin(j)+cellOffset) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset , vp1 ) - pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset , getYWithOrigin(j) )));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
vb->x=xCoord+cellOffset;
if (yCoord + 1 == pMap->getDrawOrgY() + m_y - 1) {
vb->y=yCoord+1;
} else {
vb->y=yCoord+cellOffset;
}
vb->z= ((float)pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset, getYWithOrigin(j)+cellOffset))*MAP_HEIGHT_SCALE;
vb->x = ADJUST_FROM_INDEX_TO_REAL(vb->x);
vb->y = ADJUST_FROM_INDEX_TO_REAL(vb->y);
vb->u1=U[2];
vb->v1=V[2];
vb->u2=UA[2];
vb->v2=VA[2];
doTheLight(vb, lightRay, &normalAtTexel, pLightsIterator, alpha[2]);
vb++;
//bottom-left sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(getXWithOrigin(i)+cellOffset , (getYWithOrigin(j)+cellOffset) ) - pMap->getDisplayHeight(un0 , (getYWithOrigin(j)+cellOffset) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(getXWithOrigin(i) , vp1 ) - pMap->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
if (xCoord == pMap->getDrawOrgX()) {
vb->x=xCoord;
//if (vb->x < 0) vb->x = 0;
} else {
vb->x=xCoord;
}
if (yCoord + 1 == pMap->getDrawOrgY() + m_y - 1) {
vb->y=yCoord+1;
} else {
vb->y=yCoord+cellOffset;
}
vb->z= ((float)pMap->getDisplayHeight(getXWithOrigin(i), getYWithOrigin(j)+cellOffset))*MAP_HEIGHT_SCALE;
vb->x = ADJUST_FROM_INDEX_TO_REAL(vb->x);
vb->y = ADJUST_FROM_INDEX_TO_REAL(vb->y);
vb->u1=U[3];
vb->v1=V[3];
vb->u2=UA[3];
vb->v2=VA[3];
doTheLight(vb, lightRay, &normalAtTexel, pLightsIterator, alpha[3]);
vb++;
VERTEX_FORMAT *pCurVertices = vb-4;
#ifdef FLIP_TRIANGLES // jba - reduces "diamonding" in some cases, not others. Better cliffs, though.
VERTEX_FORMAT tmpVertex;
if (flipForBlend) {
tmpVertex = pCurVertices[0];
pCurVertices[0] = pCurVertices[1];
pCurVertices[1] = pCurVertices[2];
pCurVertices[2] = pCurVertices[3];
pCurVertices[3] = tmpVertex;
}
#endif
if (m_showImpassableAreas) {
// Color impassable cells "red"
DEBUG_ASSERTCRASH(PATHFIND_CELL_SIZE_F == MAP_XY_FACTOR, ("Pathfind must be terrain cell size, or this code needs reworking. John A."));
Real borderHiX = (pMap->getXExtent()-2*pMap->getBorderSizeInline())*MAP_XY_FACTOR;
Real borderHiY = (pMap->getYExtent()-2*pMap->getBorderSizeInline())*MAP_XY_FACTOR;
Bool border = pCurVertices[0].x == -MAP_XY_FACTOR || pCurVertices[0].y == -MAP_XY_FACTOR;
Bool cliffMapped = pMap->isCliffMappedTexture(getXWithOrigin(i), getYWithOrigin(j));
if (pCurVertices[0].x == borderHiX) {
border = true;
}
if (pCurVertices[0].y == borderHiY) {
border = true;
}
Bool isCliff = pMap->getCliffState(getXWithOrigin(i)+pMap->getDrawOrgX(), getYWithOrigin(j)+pMap->getDrawOrgY())
|| showAsVisibleCliff(getXWithOrigin(i) + pMap->getDrawOrgX(), getYWithOrigin(j)+pMap->getDrawOrgY());
if ( isCliff || border || cliffMapped) {
Int cellX, cellY;
for (cellX=0; cellX<2; cellX++) {
for (cellY=0; cellY<2; cellY++) {
Int vertex = cellX+2*cellY;
if (border) {
Bool doBorder = false;
if (pCurVertices[vertex].y >= 0 && pCurVertices[vertex].y <= borderHiY) {
if (pCurVertices[vertex].x == 0 || pCurVertices[vertex].x == borderHiX) {
doBorder = true;
}
}
if (pCurVertices[vertex].x >= 0 && pCurVertices[vertex].x <= borderHiX) {
if (pCurVertices[vertex].y == 0 || pCurVertices[vertex].y == borderHiY) {
doBorder = true;
}
}
if (doBorder) {
pCurVertices[vertex].diffuse &= 0xFF0000ff; // blue with alpha.
}
} else if (isCliff) {
pCurVertices[vertex].diffuse &= 0xFFFF0000; // red with alpha.
}
if (cliffMapped && vertex==0) {
pCurVertices[vertex].diffuse &= 0xFF000000; // Black.
pCurVertices[vertex].diffuse |= 0xff00; // Add green.
}
}
}
}
}
// Note - We have been building the vertex buffer in the memory location.
// Now copy the set of vertices into the hardware buffer.
// We don't copy the whole vertex buffer because we often update only
// a couple of rows and its a lot faster to just copy the ones that change.
Int offset = pCurVertices - vBase;
memcpy(vbHardware+offset, pCurVertices, 4*sizeof(VERTEX_FORMAT));
}
}
return 0; //success.
}
return -1;
}
//=============================================================================
// HeightMapRenderObjClass::updateVBForLight
//=============================================================================
/** Update the dynamic lighting values only in a rectangular block of the given Vertex Buffer.
The vertex locations and texture coords are unchanged.
*/
Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights)
{
#if (OPTIMIZED_HEIGHTMAP_LIGHTING) // (gth) if optimizations are enabled, jump over to the "optimized" version of this function.
return updateVBForLightOptimized( pVB, data, x0, y0, x1, y1, originX, originY, pLights, numLights );
#endif
Int i,j,k;
Int vn0,un0,vp1,up1;
Vector3 l2r,n2f,normalAtTexel;
Int vertsPerRow=(VERTEX_BUFFER_TILE_LENGTH)*4; //vertices per row of VB
if (m_vertexBufferTiles && m_map)
{
#ifdef RTS_DEBUG
assert(x0 >= originX && y0 >= originY && x1>x0 && y1>y0 && x1<=originX+VERTEX_BUFFER_TILE_LENGTH && y1<=originY+VERTEX_BUFFER_TILE_LENGTH);
#endif
DX8VertexBufferClass::WriteLockClass lockVtxBuffer(pVB);
VERTEX_FORMAT *vBase = (VERTEX_FORMAT*)lockVtxBuffer.Get_Vertex_Array();
VERTEX_FORMAT *vb;
for (j=y0; j<y1; j++)
{
if (HALF_RES_MESH) {
if (j&1) continue;
}
Int yCoord = getYWithOrigin(j)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
Bool intersect = false;
for (k=0; k<numLights; k++) {
if (pLights[k]->m_minY <= yCoord+1 &&
pLights[k]->m_maxY >= yCoord) {
intersect = true;
}
if (pLights[k]->m_prevMinY <= yCoord+1 &&
pLights[k]->m_prevMaxY >= yCoord) {
intersect = true;
}
}
if (!intersect) {
continue;
}
vn0 = getYWithOrigin(j)-1;
if (vn0 < -m_map->getDrawOrgY())
vn0=-m_map->getDrawOrgY();
vp1 = getYWithOrigin(j+1)+1;
if (vp1 >= m_map->getYExtent()-m_map->getDrawOrgY())
vp1=m_map->getYExtent()-m_map->getDrawOrgY()-1;
for (i=x0; i<x1; i++)
{
if (HALF_RES_MESH) {
if (i&1) continue;
}
Int xCoord = getXWithOrigin(i)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
Bool intersect = false;
for (k=0; k<numLights; k++) {
if (pLights[k]->m_minX <= xCoord+1 &&
pLights[k]->m_maxX >= xCoord &&
pLights[k]->m_minY <= yCoord+1 &&
pLights[k]->m_maxY >= yCoord) {
intersect = true;
}
if (pLights[k]->m_prevMinX <= xCoord+1 &&
pLights[k]->m_prevMaxX >= xCoord &&
pLights[k]->m_prevMinY <= yCoord+1 &&
pLights[k]->m_prevMaxY >= yCoord) {
intersect = true;
}
}
if (!intersect) {
continue;
}
// vb is the pointer to the vertex in the hardware dx8 vertex buffer.
Int offset = (j-originY)*vertsPerRow+4*(i-originX);
if (HALF_RES_MESH) {
offset = (j-originY)*vertsPerRow/4+2*(i-originX);
}
vb = vBase + offset; //skip to correct row in vertex buffer
// vbMirror is the pointer to the vertex in our memory based copy.
// The important point is that we can read out of our copy to get the original
// diffuse color, and xyz location. It is VERY SLOW to read out of the
// hardware vertex buffer, possibly worse... jba.
VERTEX_FORMAT *vbMirror = ((VERTEX_FORMAT*)data) + offset;
un0 = getXWithOrigin(i)-1;
if (un0 < -m_map->getDrawOrgX())
un0=-m_map->getDrawOrgX();
up1 = getXWithOrigin(i+1)+1;
if (up1 >= m_map->getXExtent()-m_map->getDrawOrgX())
up1=m_map->getXExtent()-m_map->getDrawOrgX()-1;
Vector3 lightRay(0,0,0);
//top-left sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1, getYWithOrigin(j)) - m_map->getDisplayHeight(un0, getYWithOrigin(j))));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i), (getYWithOrigin(j)+1)) - m_map->getDisplayHeight(getXWithOrigin(i), vn0)));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
vb++; vbMirror++;
//top-right sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(up1 , getYWithOrigin(j) ) - m_map->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(getXWithOrigin(i)+1 , vn0 )));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
vb++; vbMirror++;
//bottom-right sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(up1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(getXWithOrigin(i) , (getYWithOrigin(j)+1) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , vp1 ) - m_map->getDisplayHeight(getXWithOrigin(i)+1 , getYWithOrigin(j) )));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
vb++; vbMirror++;
//bottom-left sample
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(un0 , (getYWithOrigin(j)+1) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i) , vp1 ) - m_map->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
#ifdef ALLOW_TEMPORARIES
normalAtTexel= Normalize(Vector3::Cross_Product(l2r,n2f));
#else
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
vb++; vbMirror++;
}
}
return 0; //success.
}
return -1;
}
Int HeightMapRenderObjClass::updateVBForLightOptimized(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights)
{
Int i,j,k;
Int vn0,un0,vp1,up1;
Vector3 l2r,n2f,normalAtTexel;
Int vertsPerRow=(VERTEX_BUFFER_TILE_LENGTH)*4; //vertices per row of VB
if (m_vertexBufferTiles && m_map)
{
#ifdef RTS_DEBUG
assert(x0 >= originX && y0 >= originY && x1>x0 && y1>y0 && x1<=originX+VERTEX_BUFFER_TILE_LENGTH && y1<=originY+VERTEX_BUFFER_TILE_LENGTH);
#endif
DX8VertexBufferClass::WriteLockClass lockVtxBuffer(pVB);
VERTEX_FORMAT *vBase = (VERTEX_FORMAT*)lockVtxBuffer.Get_Vertex_Array();
VERTEX_FORMAT *vb;
//
// (gth) the optimization in this function is to take advantage of verts in the same
// x,y position who have already computed their lighting. To do this, we need to set up
// some offsets in the vertex buffer. I've computed these offsets to be consistent with
// the formula's that Generals is using but in the case of the "half-res-mesh" I'm not
// sure things are correct...
//
Int quad_right_offset;
Int quad_below_offset;
Int quad_below_right_offset;
if (HALF_RES_MESH == false) {
// offset = (j-originY)*vertsPerRow+4*(i-originX);
quad_right_offset = 4;
quad_below_offset = vertsPerRow;
quad_below_right_offset = vertsPerRow + 4;
} else {
// offset = (j-originY)*vertsPerRow/4+2*(i-originX);
quad_right_offset = 2;
quad_below_offset = vertsPerRow/4;
quad_below_right_offset = vertsPerRow/4 + 2;
}
//
// i,j loop over the quads affected by the light. Each quad has its *own* 4 vertices. This
// means that for any vertex position on the map, there are actually 4 copies of the vertex.
//
for (j=y0; j<y1; j++)
{
if (HALF_RES_MESH) {
if (j&1) continue;
}
Int yCoord = getYWithOrigin(j)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
Bool intersect = false;
for (k=0; k<numLights; k++) {
if (pLights[k]->m_minY <= yCoord+1 &&
pLights[k]->m_maxY >= yCoord) {
intersect = true;
}
if (pLights[k]->m_prevMinY <= yCoord+1 &&
pLights[k]->m_prevMaxY >= yCoord) {
intersect = true;
}
}
if (!intersect) {
continue;
}
vn0 = getYWithOrigin(j)-1;
if (vn0 < -m_map->getDrawOrgY())
vn0=-m_map->getDrawOrgY();
vp1 = getYWithOrigin(j+1)+1;
if (vp1 >= m_map->getYExtent()-m_map->getDrawOrgY())
vp1=m_map->getYExtent()-m_map->getDrawOrgY()-1;
for (i=x0; i<x1; i++)
{
if (HALF_RES_MESH) {
if (i&1) continue;
}
Int xCoord = getXWithOrigin(i)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
Bool intersect = false;
for (k=0; k<numLights; k++) {
if (pLights[k]->m_minX <= xCoord+1 &&
pLights[k]->m_maxX >= xCoord &&
pLights[k]->m_minY <= yCoord+1 &&
pLights[k]->m_maxY >= yCoord) {
intersect = true;
}
if (pLights[k]->m_prevMinX <= xCoord+1 &&
pLights[k]->m_prevMaxX >= xCoord &&
pLights[k]->m_prevMinY <= yCoord+1 &&
pLights[k]->m_prevMaxY >= yCoord) {
intersect = true;
}
}
if (!intersect) {
continue;
}
// vb is the pointer to the vertex in the hardware dx8 vertex buffer.
Int offset = (j-originY)*vertsPerRow+4*(i-originX);
if (HALF_RES_MESH) {
offset = (j-originY)*vertsPerRow/4+2*(i-originX);
}
vb = vBase + offset; //skip to correct row in vertex buffer
// vbMirror is the pointer to the vertex in our memory based copy.
// The important point is that we can read out of our copy to get the original
// diffuse color, and xyz location. It is VERY SLOW to read out of the
// hardware vertex buffer, possibly worse... jba.
VERTEX_FORMAT *vbMirror = ((VERTEX_FORMAT*)data) + offset;
VERTEX_FORMAT *vbaseMirror = ((VERTEX_FORMAT*)data);
un0 = getXWithOrigin(i)-1;
if (un0 < -m_map->getDrawOrgX())
un0=-m_map->getDrawOrgX();
up1 = getXWithOrigin(i+1)+1;
if (up1 >= m_map->getXExtent()-m_map->getDrawOrgX())
up1=m_map->getXExtent()-m_map->getDrawOrgX()-1;
Vector3 lightRay(0,0,0);
//
// (gth) Following the set of rules below lets us take advantage of lighting values that have
// been previously computed. The idea is to copy them ahead to future quads that will need them
// and then not compute them when we get to those quads. This also avoids having to read-back
// from the vertex buffer but we do jump around in memory... probably bad anyway, maybe we should
// compute into a temporary buffer and copy all at once...
//
unsigned long light_copy;
// top-left sample -> only compute when i==0 and j==0
if ((i==x0) && (j==y0)) {
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1, getYWithOrigin(j)) - m_map->getDisplayHeight(un0, getYWithOrigin(j))));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i), (getYWithOrigin(j)+1)) - m_map->getDisplayHeight(getXWithOrigin(i), vn0)));
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
}
vb++; vbMirror++;
//top-right sample -> compute when j==0, then copy to (right,0)
if (j==y0) {
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(up1 , getYWithOrigin(j) ) - m_map->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(getXWithOrigin(i)+1 , vn0 )));
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
light_copy = doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
if (i < x1-1) {
// copy light to (right,0)
(vBase + offset + quad_right_offset)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset)->diffuse&0xff000000) ;
}
}
vb++; vbMirror++;
//bottom-right sample -> always compute, then copy to (right,3), (down,1), (down+right,0)
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(up1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(getXWithOrigin(i) , (getYWithOrigin(j)+1) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , vp1 ) - m_map->getDisplayHeight(getXWithOrigin(i)+1 , getYWithOrigin(j) )));
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
light_copy = doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
if (i < x1-1) {
// copy light to (right,3)
//(vBase + offset + quad_right_offset + 3)->diffuse = light_copy;
(vBase + offset + quad_right_offset + 3)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset + 3)->diffuse&0xff000000) ;
}
if (j < y1-1) {
// copy light to (down,1)
//(vBase + offset + quad_below_offset + 1)->diffuse = light_copy;
(vBase + offset + quad_right_offset + 1)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset + 1)->diffuse&0xff000000) ;
}
if ((i < x1-1) && (j < y1-1)) {
// copy light to (right+down,0)
//(vBase + offset + quad_below_right_offset)->diffuse = light_copy;
(vBase + offset + quad_right_offset)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset)->diffuse&0xff000000) ;
}
vb++; vbMirror++;
//bottom-left sample -> compute when i==0, otherwise copy from (left,2)
if (i==x0) {
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(un0 , (getYWithOrigin(j)+1) )));
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i) , vp1 ) - m_map->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
light_copy = doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, pLights, numLights);
if (j < y1-1) {
// copy light to (down,0)
//(vBase + offset + quad_below_offset)->diffuse = light_copy;
(vBase + offset + quad_below_offset)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_below_offset)->diffuse&0xff000000) ;
}
}
vb++; vbMirror++;
}
}
return 0; //success.
}
return -1;
}
//=============================================================================
// HeightMapRenderObjClass::doPartialUpdate
//=============================================================================
/** Updates a partial block of vertices from [x0,y0 to x1,y1]
The coordinates in partialRange are map cell coordinates, relative to the entire map.
The vertex coordinates and texture coordinates, as well as static lighting are updated.
*/
void HeightMapRenderObjClass::doPartialUpdate(const IRegion2D &partialRange, WorldHeightMap *htMap, RefRenderObjListIterator *pLightsIterator)
{
// Adjust range into the current drawn map range.
Int minX = partialRange.lo.x - htMap->getDrawOrgX();
Int maxX = partialRange.hi.x - htMap->getDrawOrgX();
Int minY = partialRange.lo.y - htMap->getDrawOrgY();
Int maxY = partialRange.hi.y - htMap->getDrawOrgY();
if (minX<0) minX = 0;
if (minY<0) minY = 0;
if (maxX > m_x-1) maxX = m_x-1;
if (maxY > m_y-1) maxY = m_y-1;
if (maxX < minX) return;
if (maxY < minY) return;
if (m_originX == 0 && m_originY == 0) {
// simple case.
updateBlock(minX, minY, maxX, maxY,
htMap, pLightsIterator);
}
else
{
minY = minY+m_originY;
maxY = maxY+m_originY;
if (minY> m_y-1) {
minY -= m_y-1;
maxY -= m_y-1;
}
if (maxY > m_y-1) {
maxY -= m_y-1;
updateBlock(0, minY, m_x-1, m_y-1, htMap, pLightsIterator);
updateBlock(0, 0, m_x-1, maxY, htMap, pLightsIterator);
} else {
updateBlock(0, minY, m_x-1, maxY, htMap, pLightsIterator);
}
}
if (!m_extraBlendTilePositions)
{ //Need to allocate memory
m_extraBlendTilePositions = NEW Int[DEFAULT_MAX_MAP_EXTRABLEND_TILES];
m_extraBlendTilePositionsSize = DEFAULT_MAX_MAP_EXTRABLEND_TILES;
}
//Find list of all extra blend tiles used on map. These are tiles with 3 materials/textures
//over the same tile and require an extra render pass.
Int i, j;
//First remove any existing extra blend tiles within this partial region
for (j=0; j<m_numExtraBlendTiles; j++)
{ Int x = m_extraBlendTilePositions[j] & 0xffff;
Int y = m_extraBlendTilePositions[j] >> 16;
if (x >= partialRange.lo.x && x < partialRange.hi.x &&
y >= partialRange.lo.y && y < partialRange.hi.y)
{ //this tile is inside region being updated so remove it by shifting tile array
memcpy(m_extraBlendTilePositions+j,m_extraBlendTilePositions+j+1,(m_numExtraBlendTiles-1-j)*sizeof(Int));
m_numExtraBlendTiles--;
j--; //need to look at index j again because this tile was removed
}
}
for (j=partialRange.lo.y; j<partialRange.hi.y; j++)
for (i=partialRange.lo.x; i<partialRange.hi.x; i++)
{
if (j<0 || i<0) continue;
Real U[4],V[4];
UnsignedByte alpha[4];
Bool flipState,cliffState;
if (htMap->getExtraAlphaUVData(i,j,U,V,alpha,&flipState, &cliffState))
{ if (m_numExtraBlendTiles >= m_extraBlendTilePositionsSize)
{ //no more room to store extra blend tiles so enlarge the buffer.
Int *tempPositions=NEW Int[m_extraBlendTilePositionsSize+512];
memcpy(tempPositions, m_extraBlendTilePositions, m_extraBlendTilePositionsSize*sizeof(Int));
delete [] m_extraBlendTilePositions;
//enlarge by more tiles to reduce memory trashing
m_extraBlendTilePositions = tempPositions;
m_extraBlendTilePositionsSize += 512;
}
//Pack x and y position into single integer since maps are limited in size
m_extraBlendTilePositions[m_numExtraBlendTiles]=i | (j <<16);
m_numExtraBlendTiles++;
}
}
updateShorelineTiles(partialRange.lo.x,partialRange.lo.y,partialRange.hi.x,partialRange.hi.y,htMap);