/
UnitDrawer.cpp
2265 lines (1772 loc) · 61.9 KB
/
UnitDrawer.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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "UnitDrawer.h"
#include "UnitDrawerState.hpp"
#include "Game/Camera.h"
#include "Game/CameraHandler.h"
#include "Game/GameHelper.h"
#include "Game/GameSetup.h"
#include "Game/GlobalUnsynced.h"
#include "Game/Players/Player.h"
#include "Game/UI/MiniMap.h"
#include "Lua/LuaMaterial.h"
#include "Lua/LuaUnitMaterial.h"
#include "Lua/LuaRules.h"
#include "Map/BaseGroundDrawer.h"
#include "Map/Ground.h"
#include "Map/MapInfo.h"
#include "Map/ReadMap.h"
#include "Rendering/Env/ISky.h"
#include "Rendering/Env/IWater.h"
#include "Rendering/Env/CubeMapHandler.h"
#include "Rendering/FarTextureHandler.h"
#include "Rendering/glFont.h"
#include "Rendering/GL/glExtra.h"
#include "Rendering/GL/VertexArray.h"
#include "Rendering/Env/IGroundDecalDrawer.h"
#include "Rendering/IconHandler.h"
#include "Rendering/ShadowHandler.h"
#include "Rendering/Shaders/Shader.h"
#include "Rendering/Textures/Bitmap.h"
#include "Rendering/Textures/3DOTextureHandler.h"
#include "Rendering/Textures/S3OTextureHandler.h"
#include "Rendering/Models/WorldObjectModelRenderer.h"
#include "Sim/Features/Feature.h"
#include "Sim/Misc/LosHandler.h"
#include "Sim/Misc/RadarHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Projectiles/ExplosionGenerator.h"
#include "Sim/Units/CommandAI/BuilderCAI.h"
#include "Sim/Units/Groups/Group.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/UnitDefHandler.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Units/UnitTypes/Building.h"
#include "Sim/Units/UnitTypes/TransportUnit.h"
#include "Sim/Weapons/Weapon.h"
#include "System/Config/ConfigHandler.h"
#include "System/EventHandler.h"
#include "System/Log/ILog.h"
#include "System/myMath.h"
#include "System/Platform/Watchdog.h"
#include "System/TimeProfiler.h"
#include "System/Util.h"
#ifdef USE_GML
#include "lib/gml/gmlsrv.h"
extern gmlClientServer<void, int, CUnit*> *gmlProcessor;
#endif
#define UNIT_SHADOW_ALPHA_MASKING
CUnitDrawer* unitDrawer;
CONFIG(int, UnitLodDist).defaultValue(1000);
CONFIG(int, UnitIconDist).defaultValue(10000);
CONFIG(float, UnitTransparency).defaultValue(0.7f);
CONFIG(bool, ShowHealthBars).defaultValue(true);
CONFIG(bool, MultiThreadDrawUnit).defaultValue(true);
CONFIG(bool, MultiThreadDrawUnitShadow).defaultValue(true);
CONFIG(int, MaxDynamicModelLights)
.defaultValue(1)
.minimumValue(0);
CONFIG(bool, AdvUnitShading).defaultValue(true);
CONFIG(float, LODScale).defaultValue(1.0f);
CONFIG(float, LODScaleShadow).defaultValue(1.0f);
CONFIG(float, LODScaleReflection).defaultValue(1.0f);
CONFIG(float, LODScaleRefraction).defaultValue(1.0f);
static bool LUA_DRAWING = false; // FIXME
static float UNIT_GLOBAL_LOD_FACTOR = 1.0f;
inline static void SetUnitGlobalLODFactor(float value)
{
UNIT_GLOBAL_LOD_FACTOR = (value * camera->GetLPPScale());
}
static float GetLODFloat(const string& name)
{
// NOTE: the inverse of the value is used
const float value = configHandler->GetFloat(name);
if (value <= 0.0f) {
return 1.0f;
}
return (1.0f / value);
}
CUnitDrawer::CUnitDrawer(): CEventClient("[CUnitDrawer]", 271828, false)
{
eventHandler.AddClient(this);
SetUnitDrawDist((float)configHandler->GetInt("UnitLodDist"));
SetUnitIconDist((float)configHandler->GetInt("UnitIconDist"));
LODScale = GetLODFloat("LODScale");
LODScaleShadow = GetLODFloat("LODScaleShadow");
LODScaleReflection = GetLODFloat("LODScaleReflection");
LODScaleRefraction = GetLODFloat("LODScaleRefraction");
unitAmbientColor = mapInfo->light.unitAmbientColor;
unitSunColor = mapInfo->light.unitSunColor;
cloakAlpha = std::max(0.11f, std::min(1.0f, 1.0f - configHandler->GetFloat("UnitTransparency")));
cloakAlpha1 = std::min(1.0f, cloakAlpha + 0.1f);
cloakAlpha2 = std::min(1.0f, cloakAlpha + 0.2f);
cloakAlpha3 = std::min(1.0f, cloakAlpha + 0.4f);
// load unit explosion generators and decals
for (size_t unitDefID = 1; unitDefID < unitDefHandler->unitDefs.size(); unitDefID++) {
UnitDef* ud = unitDefHandler->unitDefs[unitDefID];
for (std::vector<std::string>::const_iterator it = ud->modelCEGTags.begin(); it != ud->modelCEGTags.end(); ++it) {
ud->sfxExplGens.push_back(explGenHandler->LoadGenerator(*it));
}
}
deadGhostBuildings.resize(MODELTYPE_OTHER);
liveGhostBuildings.resize(MODELTYPE_OTHER);
opaqueModelRenderers.resize(MODELTYPE_OTHER, NULL);
cloakedModelRenderers.resize(MODELTYPE_OTHER, NULL);
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
opaqueModelRenderers[modelType] = IWorldObjectModelRenderer::GetInstance(modelType);
cloakedModelRenderers[modelType] = IWorldObjectModelRenderer::GetInstance(modelType);
}
unitRadarIcons.resize(teamHandler->ActiveAllyTeams());
#ifdef USE_GML
showHealthBars = GML::Enabled() && configHandler->GetBool("ShowHealthBars");
multiThreadDrawUnit = configHandler->GetBool("MultiThreadDrawUnit");
multiThreadDrawUnitShadow = configHandler->GetBool("MultiThreadDrawUnitShadow");
#endif
// LH must be initialized before drawer-state is initialized
lightHandler.Init(2U, configHandler->GetInt("MaxDynamicModelLights"));
unitDrawerStateSSP = IUnitDrawerState::GetInstance(globalRendering->haveARB, globalRendering->haveGLSL);
unitDrawerStateFFP = IUnitDrawerState::GetInstance( false, false);
// also set in ::SetupForUnitDrawing, but SunChanged can be
// called first if DynamicSun is enabled --> must be non-NULL
unitDrawerState = unitDrawerStateFFP;
// NOTE:
// advShading can NOT change at runtime if initially false***
// (see AdvModelShadingActionExecutor), so we will always use
// unitDrawerStateFFP (in ::Draw) in that special case and it
// does not matter if unitDrawerStateSSP is initialized
// *** except for DrawCloakedUnits
advFade = GLEW_NV_vertex_program2;
advShading = (unitDrawerStateSSP->Init(this) && cubeMapHandler->Init());
}
CUnitDrawer::~CUnitDrawer()
{
eventHandler.RemoveClient(this);
unitDrawerStateSSP->Kill(); IUnitDrawerState::FreeInstance(unitDrawerStateSSP);
unitDrawerStateFFP->Kill(); IUnitDrawerState::FreeInstance(unitDrawerStateFFP);
cubeMapHandler->Free();
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
std::set<GhostSolidObject*>& ghostSet = deadGhostBuildings[modelType];
std::set<GhostSolidObject*>::iterator ghostSetIt;
for (ghostSetIt = ghostSet.begin(); ghostSetIt != ghostSet.end(); ++ghostSetIt) {
delete *ghostSetIt;
}
deadGhostBuildings[modelType].clear();
liveGhostBuildings[modelType].clear();
}
deadGhostBuildings.clear();
liveGhostBuildings.clear();
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
delete opaqueModelRenderers[modelType];
delete cloakedModelRenderers[modelType];
}
opaqueModelRenderers.clear();
cloakedModelRenderers.clear();
unitRadarIcons.clear();
unsortedUnits.clear();
lightHandler.Kill();
}
void CUnitDrawer::SetUnitDrawDist(float dist)
{
unitDrawDist = dist;
unitDrawDistSqr = dist * dist;
}
void CUnitDrawer::SetUnitIconDist(float dist)
{
unitIconDist = dist;
iconLength = 750 * unitIconDist * unitIconDist;
}
void CUnitDrawer::Update()
{
{
GML_STDMUTEX_LOCK(temp); // Update
while (!tempDrawUnits.empty() && tempDrawUnits.begin()->first < gs->frameNum - 1) {
tempDrawUnits.erase(tempDrawUnits.begin());
}
while (!tempTransparentDrawUnits.empty() && tempTransparentDrawUnits.begin()->first <= gs->frameNum) {
tempTransparentDrawUnits.erase(tempTransparentDrawUnits.begin());
}
}
eventHandler.UpdateDrawUnits();
{
GML_RECMUTEX_LOCK(unit); // Update
drawIcon.clear();
#ifdef USE_GML
drawStat.clear();
#endif
for (std::set<CUnit*>::iterator usi = unsortedUnits.begin(); usi != unsortedUnits.end(); ++usi) {
CUnit* unit = *usi;
UpdateUnitIconState(unit);
UpdateUnitDrawPos(unit);
}
}
useDistToGroundForIcons = (camHandler->GetCurrentController()).GetUseDistToGroundForIcons();
if (useDistToGroundForIcons) {
const float3& camPos = camera->GetPos();
// use the height at the current camera position
//const float groundHeight = ground->GetHeightAboveWater(camPos.x, camPos.z, false);
// use the middle between the highest and lowest position on the map as average
const float groundHeight = (readmap->currMinHeight + readmap->currMaxHeight) / 2;
const float overGround = camPos.y - groundHeight;
sqCamDistToGroundForIcons = overGround * overGround;
}
}
// only called by DrawOpaqueUnit
inline bool CUnitDrawer::DrawUnitLOD(CUnit* unit)
{
GML_LODMUTEX_LOCK(unit); // DrawUnitLOD
if (unit->lodCount > 0) {
if (unit->isCloaked) {
const LuaMatType matType = (water->IsDrawReflection())?
LUAMAT_ALPHA_REFLECT: LUAMAT_ALPHA;
LuaUnitMaterial& unitMat = unit->luaMats[matType];
const unsigned lod = CalcUnitLOD(unit, unitMat.GetLastLOD());
unit->currentLOD = lod;
LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(lod);
if ((lodMat != NULL) && lodMat->IsActive()) {
lodMat->AddUnit(unit); return true;
}
} else {
const LuaMatType matType =
(water->IsDrawReflection()) ? LUAMAT_OPAQUE_REFLECT : LUAMAT_OPAQUE;
LuaUnitMaterial& unitMat = unit->luaMats[matType];
const unsigned lod = CalcUnitLOD(unit, unitMat.GetLastLOD());
unit->currentLOD = lod;
LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(lod);
if ((lodMat != NULL) && lodMat->IsActive()) {
lodMat->AddUnit(unit); return true;
}
}
}
return false;
}
inline void CUnitDrawer::DrawOpaqueUnit(CUnit* unit, const CUnit* excludeUnit, bool drawReflection, bool drawRefraction)
{
if (unit == excludeUnit) {
return;
}
if (unit->noDraw) {
return;
}
if (!camera->InView(unit->drawMidPos, unit->drawRadius)) {
return;
}
if ((unit->losStatus[gu->myAllyTeam] & LOS_INLOS) || gu->spectatingFullView) {
if (drawReflection) {
float3 zeroPos;
if (unit->drawMidPos.y < 0.0f) {
zeroPos = unit->drawMidPos;
} else {
const float dif = unit->drawMidPos.y - camera->GetPos().y;
zeroPos =
camera->GetPos() * (unit->drawMidPos.y / dif) +
unit->drawMidPos * (-camera->GetPos().y / dif);
}
if (ground->GetApproximateHeight(zeroPos.x, zeroPos.z, false) > unit->drawRadius) {
return;
}
}
else if (drawRefraction) {
if (unit->pos.y > 0.0f) {
return;
}
}
#ifdef USE_GML
else
unit->lastDrawFrame = gs->frameNum;
#endif
if (!unit->isIcon) {
if ((unit->pos).SqDistance(camera->GetPos()) > (unit->sqRadius * unitDrawDistSqr)) {
farTextureHandler->Queue(unit);
} else {
if (!DrawUnitLOD(unit)) {
SetTeamColour(unit->team);
DrawUnitNow(unit);
}
}
}
}
}
void CUnitDrawer::Draw(bool drawReflection, bool drawRefraction)
{
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
ISky::SetupFog();
if (drawReflection) {
SetUnitGlobalLODFactor(LODScale * LODScaleReflection);
} else if (drawRefraction) {
SetUnitGlobalLODFactor(LODScale * LODScaleRefraction);
} else {
SetUnitGlobalLODFactor(LODScale);
}
camNorm = camera->forward;
camNorm.y = -0.1f;
camNorm.ANormalize();
const CPlayer* myPlayer = gu->GetMyPlayer();
const CUnit* excludeUnit = drawReflection? NULL: myPlayer->fpsController.GetControllee();
SetupForUnitDrawing();
// lock on the bins
GML_RECMUTEX_LOCK(unit); // Draw
#ifdef USE_GML
mt_drawReflection = drawReflection; // these member vars will be accessed by DrawOpaqueUnitMT
mt_drawRefraction = drawRefraction;
mt_excludeUnit = excludeUnit;
#endif
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
opaqueModelRenderers[modelType]->PushRenderState();
DrawOpaqueUnits(modelType, excludeUnit, drawReflection, drawRefraction);
opaqueModelRenderers[modelType]->PopRenderState();
}
CleanUpUnitDrawing();
DrawOpaqueShaderUnits();
farTextureHandler->Draw();
DrawUnitIcons(drawReflection);
glDisable(GL_FOG);
glDisable(GL_ALPHA_TEST);
glDisable(GL_TEXTURE_2D);
}
void CUnitDrawer::DrawOpaqueUnits(int modelType, const CUnit* excludeUnit, bool drawReflection, bool drawRefraction)
{
typedef std::set<CUnit*> UnitSet;
typedef std::map<int, UnitSet> UnitBin;
const UnitBin& unitBin = opaqueModelRenderers[modelType]->GetUnitBin();
UnitBin::const_iterator unitBinIt;
UnitSet::const_iterator unitSetIt;
for (unitBinIt = unitBin.begin(); unitBinIt != unitBin.end(); ++unitBinIt) {
if (modelType != MODELTYPE_3DO) {
texturehandlerS3O->SetS3oTexture(unitBinIt->first);
}
const UnitSet& unitSet = unitBinIt->second;
#ifdef USE_GML
bool mt = GML_PROFILER(multiThreadDrawUnit) //FIXME DAMN CRAPPY MACRO!!!
if (mt && unitSet.size() >= GML::ThreadCount() * 4) { // small unitSets will add a significant overhead
gmlProcessor->Work( // Profiler results, 4 threads, one single large unitSet: Approximately 20% faster with multiThreadDrawUnit
NULL, NULL, &CUnitDrawer::DrawOpaqueUnitMT, this, GML::ThreadCount(),
FALSE, &unitSet, unitSet.size(), 50, 100, TRUE
);
}
else
#endif
{
for (unitSetIt = unitSet.begin(); unitSetIt != unitSet.end(); ++unitSetIt) {
DrawOpaqueUnit(*unitSetIt, excludeUnit, drawReflection, drawRefraction);
}
}
}
if (modelType == MODELTYPE_3DO) {
DrawOpaqueAIUnits();
}
}
void CUnitDrawer::DrawOpaqueAIUnits()
{
GML_STDMUTEX_LOCK(temp); // DrawOpaqueAIUnits
// non-cloaked AI unit ghosts (FIXME: s3o's + teamcolor)
for (std::multimap<int, TempDrawUnit>::iterator ti = tempDrawUnits.begin(); ti != tempDrawUnits.end(); ++ti) {
if (camera->InView(ti->second.pos, 100)) {
glPushMatrix();
glTranslatef3(ti->second.pos);
glRotatef(ti->second.rotation * 180 / PI, 0, 1, 0);
const UnitDef* udef = ti->second.unitdef;
const S3DModel* model = udef->LoadModel();
model->DrawStatic();
glPopMatrix();
}
}
}
void CUnitDrawer::DrawUnitIcons(bool drawReflection)
{
if (!drawReflection) {
// Draw unit icons and radar blips.
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5f);
for (std::set<CUnit*>::iterator ui = drawIcon.begin(); ui != drawIcon.end(); ++ui) {
DrawIcon(*ui, false);
}
if (!gu->spectatingFullView) {
for (std::set<CUnit*>::const_iterator ui = unitRadarIcons[gu->myAllyTeam].begin(); ui != unitRadarIcons[gu->myAllyTeam].end(); ++ui) {
DrawIcon(*ui, ((*ui)->losStatus[gu->myAllyTeam] & (LOS_PREVLOS | LOS_CONTRADAR)) != (LOS_PREVLOS | LOS_CONTRADAR));
}
}
glDisable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);
#ifdef USE_GML
for (std::set<CUnit*>::iterator ui = drawStat.begin(); ui != drawStat.end(); ++ui) {
DrawUnitStats(*ui);
}
#endif
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
}
/******************************************************************************/
/******************************************************************************/
static void DrawLuaMatBins(LuaMatType type)
{
const LuaMatBinSet& bins = luaMatHandler.GetBins(type);
if (bins.empty()) {
return;
}
LUA_DRAWING = true;
glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_TRANSFORM_BIT);
if (type == LUAMAT_ALPHA || type == LUAMAT_ALPHA_REFLECT) {
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} else {
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5f);
}
const LuaMaterial* currMat = &LuaMaterial::defMat;
LuaMatBinSet::const_iterator it;
for (it = bins.begin(); it != bins.end(); ++it) {
LuaMatBin* bin = *it;
bin->Execute(*currMat);
currMat = bin;
const GML_VECTOR<CUnit*>& units = bin->GetUnits();
const int count = (int)units.size();
for (int i = 0; i < count; i++) {
CUnit* unit = units[i];
GML_LODMUTEX_LOCK(unit); // DrawLuaMatBins
const LuaUnitMaterial& unitMat = unit->luaMats[type];
const LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(unit->currentLOD);
#ifdef USE_GML
if (!lodMat || !lodMat->IsActive())
continue;
#endif
lodMat->uniforms.Execute(unit);
unitDrawer->SetTeamColour(unit->team);
unitDrawer->DrawUnitWithLists(unit, lodMat->preDisplayList, lodMat->postDisplayList);
}
}
LuaMaterial::defMat.Execute(*currMat);
luaMatHandler.ClearBins(type);
glPopAttrib();
LUA_DRAWING = false;
}
/******************************************************************************/
static void SetupShadowDrawing()
{
//FIXME setup face culling for s3o?
glColor3f(1.0f, 1.0f, 1.0f);
glDisable(GL_TEXTURE_2D);
glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);
Shader::IProgramObject* po =
shadowHandler->GetShadowGenProg(CShadowHandler::SHADOWGEN_PROGRAM_MODEL);
po->Enable();
}
static void CleanUpShadowDrawing()
{
Shader::IProgramObject* po =
shadowHandler->GetShadowGenProg(CShadowHandler::SHADOWGEN_PROGRAM_MODEL);
po->Disable();
glDisable(GL_POLYGON_OFFSET_FILL);
}
/******************************************************************************/
#define ud unitDrawer
static void SetupOpaque3DO() {
ud->SetupForUnitDrawing();
ud->GetOpaqueModelRenderer(MODELTYPE_3DO)->PushRenderState();
}
static void ResetOpaque3DO() {
ud->GetOpaqueModelRenderer(MODELTYPE_3DO)->PopRenderState();
ud->CleanUpUnitDrawing();
}
static void SetupOpaqueS3O() {
ud->SetupForUnitDrawing();
ud->GetOpaqueModelRenderer(MODELTYPE_S3O)->PushRenderState();
}
static void ResetOpaqueS3O() {
ud->GetOpaqueModelRenderer(MODELTYPE_S3O)->PopRenderState();
ud->CleanUpUnitDrawing();
}
static void SetupAlpha3DO() {
ud->SetupForGhostDrawing();
ud->GetCloakedModelRenderer(MODELTYPE_3DO)->PushRenderState();
}
static void ResetAlpha3DO() {
ud->GetCloakedModelRenderer(MODELTYPE_3DO)->PopRenderState();
ud->CleanUpGhostDrawing();
}
static void SetupAlphaS3O() {
ud->SetupForGhostDrawing();
ud->GetCloakedModelRenderer(MODELTYPE_S3O)->PushRenderState();
}
static void ResetAlphaS3O() {
ud->GetCloakedModelRenderer(MODELTYPE_S3O)->PopRenderState();
ud->CleanUpGhostDrawing();
}
#undef ud
static void SetupShadow3DO() { SetupShadowDrawing(); }
static void ResetShadow3DO() { CleanUpShadowDrawing(); }
static void SetupShadowS3O() { SetupShadowDrawing(); }
static void ResetShadowS3O() { CleanUpShadowDrawing(); }
/******************************************************************************/
void CUnitDrawer::DrawOpaqueShaderUnits()
{
luaMatHandler.setup3doShader = SetupOpaque3DO;
luaMatHandler.reset3doShader = ResetOpaque3DO;
luaMatHandler.setupS3oShader = SetupOpaqueS3O;
luaMatHandler.resetS3oShader = ResetOpaqueS3O;
const LuaMatType matType = (water->IsDrawReflection())?
LUAMAT_OPAQUE_REFLECT:
LUAMAT_OPAQUE;
DrawLuaMatBins(matType);
}
void CUnitDrawer::DrawCloakedShaderUnits()
{
luaMatHandler.setup3doShader = SetupAlpha3DO;
luaMatHandler.reset3doShader = ResetAlpha3DO;
luaMatHandler.setupS3oShader = SetupAlphaS3O;
luaMatHandler.resetS3oShader = ResetAlphaS3O;
const LuaMatType matType = (water->IsDrawReflection())?
LUAMAT_ALPHA_REFLECT:
LUAMAT_ALPHA;
DrawLuaMatBins(matType);
}
void CUnitDrawer::DrawShadowShaderUnits()
{
luaMatHandler.setup3doShader = SetupShadow3DO;
luaMatHandler.reset3doShader = ResetShadow3DO;
luaMatHandler.setupS3oShader = SetupShadowS3O;
luaMatHandler.resetS3oShader = ResetShadowS3O;
DrawLuaMatBins(LUAMAT_SHADOW);
}
/******************************************************************************/
/******************************************************************************/
inline void CUnitDrawer::DrawOpaqueUnitShadow(CUnit* unit) {
// do shadow alpha-masking for S3O units only
// (3DO's need more setup than it is worth)
#ifdef UNIT_SHADOW_ALPHA_MASKING
#define S3O_TEX(model) \
texturehandlerS3O->GetS3oTex(model->textureType)
#define PUSH_SHADOW_TEXTURE_STATE(model) \
if (model->type != MODELTYPE_3DO) { \
glActiveTexture(GL_TEXTURE0); \
glEnable(GL_TEXTURE_2D); \
glBindTexture(GL_TEXTURE_2D, S3O_TEX(model)->tex2); \
}
#define POP_SHADOW_TEXTURE_STATE(model) \
if (model->type != MODELTYPE_3DO) { \
glBindTexture(GL_TEXTURE_2D, 0); \
glDisable(GL_TEXTURE_2D); \
glActiveTexture(GL_TEXTURE0); \
}
#else
#define PUSH_SHADOW_TEXTURE_STATE(model)
#define POP_SHADOW_TEXTURE_STATE(model)
#endif
const bool unitInLOS = ((unit->losStatus[gu->myAllyTeam] & LOS_INLOS) || gu->spectatingFullView);
// FIXME: test against the shadow projection intersection
if (!(unitInLOS && camera->InView(unit->drawMidPos, unit->drawRadius + 700.0f))) {
return;
}
const float sqDist = (unit->pos - camera->GetPos()).SqLength();
const float farLength = unit->sqRadius * unitDrawDistSqr;
if (unit->noDraw) { return; }
if (sqDist >= farLength) { return; }
if (unit->isCloaked) { return; }
if (DrawAsIcon(unit, sqDist)) { return; }
GML_LODMUTEX_LOCK(unit); // DrawOpaqueUnitShadow
if (unit->lodCount <= 0) {
PUSH_SHADOW_TEXTURE_STATE(unit->model);
DrawUnitNow(unit);
POP_SHADOW_TEXTURE_STATE(unit->model);
} else {
LuaUnitMaterial& unitMat = unit->luaMats[LUAMAT_SHADOW];
const unsigned lod = CalcUnitLOD(unit, unitMat.GetLastLOD());
unit->currentLOD = lod;
LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(lod);
if ((lodMat != NULL) && lodMat->IsActive()) {
lodMat->AddUnit(unit);
} else {
PUSH_SHADOW_TEXTURE_STATE(unit->model);
DrawUnitNow(unit);
POP_SHADOW_TEXTURE_STATE(unit->model);
}
}
#undef PUSH_SHADOW_TEXTURE_STATE
#undef POP_SHADOW_TEXTURE_STATE
}
void CUnitDrawer::DrawOpaqueUnitsShadow(int modelType) {
typedef std::set<CUnit*> UnitSet;
typedef std::map<int, UnitSet> UnitBin;
const UnitBin& unitBin = opaqueModelRenderers[modelType]->GetUnitBin();
UnitBin::const_iterator unitBinIt;
UnitSet::const_iterator unitSetIt;
for (unitBinIt = unitBin.begin(); unitBinIt != unitBin.end(); ++unitBinIt) {
const UnitSet& unitSet = unitBinIt->second;
#ifdef USE_GML
bool mt = GML_PROFILER(multiThreadDrawUnitShadow)
if (mt && unitSet.size() >= GML::ThreadCount() * 4) { // small unitSets will add a significant overhead
gmlProcessor->Work( // Profiler results, 4 threads, one single large unitSet: Approximately 20% faster with multiThreadDrawUnitShadow
NULL, NULL, &CUnitDrawer::DrawOpaqueUnitShadowMT, this, GML::ThreadCount(),
FALSE, &unitSet, unitSet.size(), 50, 100, TRUE
);
}
else
#endif
{
for (unitSetIt = unitSet.begin(); unitSetIt != unitSet.end(); ++unitSetIt) {
DrawOpaqueUnitShadow(*unitSetIt);
}
}
}
}
void CUnitDrawer::DrawShadowPass()
{
glColor3f(1.0f, 1.0f, 1.0f);
glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);
#ifdef UNIT_SHADOW_ALPHA_MASKING
glAlphaFunc(GL_GREATER, 0.5f);
glEnable(GL_ALPHA_TEST);
#endif
Shader::IProgramObject* po =
shadowHandler->GetShadowGenProg(CShadowHandler::SHADOWGEN_PROGRAM_MODEL);
po->Enable();
SetUnitGlobalLODFactor(LODScale * LODScaleShadow);
GML_RECMUTEX_LOCK(unit); // DrawShadowPass
{
// 3DO's have clockwise-wound faces and
// (usually) holes, so disable backface
// culling for them
glDisable(GL_CULL_FACE);
DrawOpaqueUnitsShadow(MODELTYPE_3DO);
glEnable(GL_CULL_FACE);
for (int modelType = MODELTYPE_S3O; modelType < MODELTYPE_OTHER; modelType++) {
// note: just use DrawOpaqueUnits()? would
// save texture switches needed anyway for
// UNIT_SHADOW_ALPHA_MASKING
DrawOpaqueUnitsShadow(modelType);
}
}
po->Disable();
#ifdef UNIT_SHADOW_ALPHA_MASKING
glDisable(GL_ALPHA_TEST);
#endif
glDisable(GL_POLYGON_OFFSET_FILL);
DrawShadowShaderUnits();
}
void CUnitDrawer::DrawIcon(CUnit* unit, bool useDefaultIcon)
{
// If the icon is to be drawn as a radar blip, we want to get the default icon.
const icon::CIconData* iconData = NULL;
if (useDefaultIcon) {
iconData = icon::iconHandler->GetDefaultIconData();
} else {
iconData = unit->unitDef->iconType.GetIconData();
}
// Calculate the icon size. It scales with:
// * The square root of the camera distance.
// * The mod defined 'iconSize' (which acts a multiplier).
// * The unit radius, depending on whether the mod defined 'radiusadjust' is true or false.
float3 pos;
if (gu->spectatingFullView) {
pos = unit->drawMidPos;
} else {
pos = CGameHelper::GetUnitErrorPos(unit, gu->myAllyTeam);
}
float dist = fastmath::sqrt2(fastmath::sqrt2((pos - camera->GetPos()).SqLength()));
float scale = 0.4f * iconData->GetSize() * dist;
if (iconData->GetRadiusAdjust() && !useDefaultIcon) {
scale *= (unit->radius / WORLDOBJECT_DEFAULT_DRAWRADIUS);
}
unit->iconRadius = scale; // store the icon size so that we don't have to calculate it again
// Is the unit selected? Then draw it white.
if (unit->isSelected) {
glColor3ub(255, 255, 255);
} else {
glColor3ubv(teamHandler->Team(unit->team)->color);
}
// If the icon is partly under the ground, move it up.
const float h = ground->GetHeightAboveWater(pos.x, pos.z, false);
if (pos.y < (h + scale)) {
pos.y = (h + scale);
}
// calculate the vertices
const float3 dy = camera->up * scale;
const float3 dx = camera->right * scale;
const float3 vn = pos - dx;
const float3 vp = pos + dx;
const float3 vnn = vn - dy;
const float3 vpn = vp - dy;
const float3 vnp = vn + dy;
const float3 vpp = vp + dy;
// Draw the icon.
iconData->Draw(vnn, vpn, vnp, vpp);
}
void CUnitDrawer::SetupForGhostDrawing() const
{
glEnable(GL_LIGHTING); // Give faded objects same appearance as regular
glLightfv(GL_LIGHT1, GL_POSITION, sky->GetLight()->GetLightDir());
glEnable(GL_LIGHT1);
SetupBasicS3OTexture0();
SetupBasicS3OTexture1(); // This also sets up the transparency
static const float cols[] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, cols);
glColor3f(1.0f, 1.0f, 1.0f);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1f);
glDepthMask(GL_FALSE);
}
void CUnitDrawer::CleanUpGhostDrawing() const
{
glPopAttrib();
glDisable(GL_TEXTURE_2D);
// clean up s3o drawing stuff
// reset texture1 state
CleanupBasicS3OTexture1();
// reset texture0 state
CleanupBasicS3OTexture0();
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT1);
}
void CUnitDrawer::DrawCloakedUnits(bool disableAdvShading)
{
const bool oldAdvShading = advShading;
// don't use shaders if shadows are enabled (WHY?)
// also not safe: should set state instance to FFP
advShading = advShading && !disableAdvShading;
if (advShading) {
SetupForUnitDrawing();
glDisable(GL_ALPHA_TEST);
} else {
SetupForGhostDrawing();
}
glColor4f(1.0f, 1.0f, 1.0f, cloakAlpha);
{
GML_RECMUTEX_LOCK(unit); // DrawCloakedUnits
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
cloakedModelRenderers[modelType]->PushRenderState();
DrawCloakedUnitsHelper(modelType);
cloakedModelRenderers[modelType]->PopRenderState();
}
if (advShading) {
CleanUpUnitDrawing();
glEnable(GL_ALPHA_TEST);
} else {
CleanUpGhostDrawing();
}
advShading = oldAdvShading;
// shader rendering
DrawCloakedShaderUnits();
}
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void CUnitDrawer::DrawCloakedUnitsHelper(int modelType)
{
if (modelType == MODELTYPE_3DO) {
DrawCloakedAIUnits();
}
{
typedef std::set<CUnit*> UnitSet;
typedef std::map<int, UnitSet> UnitRenderBin;
typedef std::map<int, UnitSet>::const_iterator UnitRenderBinIt;
const UnitRenderBin& unitBin = cloakedModelRenderers[modelType]->GetUnitBin();
// cloaked units
for (UnitRenderBinIt it = unitBin.begin(); it != unitBin.end(); ++it) {
if (modelType != MODELTYPE_3DO) {
texturehandlerS3O->SetS3oTexture(it->first);
}
const UnitSet& unitSet = it->second;
for (UnitSet::const_iterator ui = unitSet.begin(); ui != unitSet.end(); ++ui) {
DrawCloakedUnit(*ui, modelType, false);
}
}
}
// living and dead ghosted buildings
DrawGhostedBuildings(modelType);
}
inline void CUnitDrawer::DrawCloakedUnit(CUnit* unit, int modelType, bool drawGhostBuildingsPass) {
if (!camera->InView(unit->drawMidPos, unit->drawRadius)) {