/
UnitDrawer.h
301 lines (222 loc) · 9.78 KB
/
UnitDrawer.h
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef UNIT_DRAWER_H
#define UNIT_DRAWER_H
#include <array>
#include <vector>
#include "Rendering/GL/LightHandler.h"
#include "Rendering/Models/3DModel.h"
#include "Rendering/UnitDrawerState.hpp"
#include "System/EventClient.h"
#include "System/type2.h"
#include "System/UnorderedMap.hpp"
struct SolidObjectDef;
struct UnitDef;
struct S3DModel;
class IModelRenderContainer;
class CSolidObject;
class CUnit;
class CVertexArray;
struct Command;
struct BuildInfo;
struct SolidObjectGroundDecal;
struct IUnitDrawerState;
namespace icon {
class CIconData;
}
namespace GL {
struct GeometryBuffer;
}
struct GhostSolidObject {
public:
void IncRef() { ( refCount++ ); }
bool DecRef() { return ((refCount--) > 1); }
public:
SolidObjectGroundDecal* decal; //FIXME defined in legacy decal handler with a lot legacy stuff
S3DModel* model;
float3 pos;
float3 dir;
int facing; //FIXME replaced with dir-vector just legacy decal drawer uses this
int team;
int refCount;
int lastDrawFrame;
};
class CUnitDrawer: public CEventClient
{
public:
// CEventClient interface
bool WantsEvent(const std::string& eventName) {
return
eventName == "RenderUnitCreated" || eventName == "RenderUnitDestroyed" ||
eventName == "UnitCloaked" || eventName == "UnitDecloaked" ||
eventName == "UnitEnteredRadar" || eventName == "UnitEnteredLos" ||
eventName == "UnitLeftRadar" || eventName == "UnitLeftLos" ||
eventName == "PlayerChanged" || eventName == "SunChanged";
}
bool GetFullRead() const { return true; }
int GetReadAllyTeam() const { return AllAccessTeam; }
void RenderUnitCreated(const CUnit*, int cloaked);
void RenderUnitDestroyed(const CUnit*);
void UnitEnteredRadar(const CUnit* unit, int allyTeam);
void UnitEnteredLos(const CUnit* unit, int allyTeam);
void UnitLeftRadar(const CUnit* unit, int allyTeam);
void UnitLeftLos(const CUnit* unit, int allyTeam);
void UnitCloaked(const CUnit* unit);
void UnitDecloaked(const CUnit* unit);
void PlayerChanged(int playerNum);
void SunChanged();
public:
CUnitDrawer();
~CUnitDrawer();
void Update();
void UpdateGhostedBuildings();
void Draw(bool drawReflection, bool drawRefraction = false);
void DrawOpaquePass(bool deferredPass, bool drawReflection, bool drawRefraction);
void DrawShadowPass();
void DrawAlphaPass();
void SetDrawForwardPass(bool b) { drawForward = b; }
void SetDrawDeferredPass(bool b) { drawDeferred = b; }
static void DrawUnitModel(const CUnit* unit, bool noLuaCall);
static void DrawUnitModelBeingBuiltShadow(const CUnit* unit, bool noLuaCall);
static void DrawUnitModelBeingBuiltOpaque(const CUnit* unit, bool noLuaCall);
// note: make these static?
void DrawUnitNoTrans(const CUnit* unit, unsigned int preList, unsigned int postList, bool lodCall, bool noLuaCall);
void DrawUnitTrans(const CUnit* unit, unsigned int preList, unsigned int postList, bool lodCall, bool noLuaCall);
void PushIndividualOpaqueState(const S3DModel* model, int teamID, bool deferredPass);
void PushIndividualAlphaState(const S3DModel* model, int teamID, bool deferredPass);
void PopIndividualOpaqueState(const S3DModel* model, int teamID, bool deferredPass);
void PopIndividualAlphaState(const S3DModel* model, int teamID, bool deferredPass);
/// LuaOpenGL::Unit{Raw}: draw a single unit with full state setup
void PushIndividualOpaqueState(const CUnit* unit, bool deferredPass);
void PopIndividualOpaqueState(const CUnit* unit, bool deferredPass);
void DrawIndividual(const CUnit* unit, bool noLuaCall);
void DrawIndividualNoTrans(const CUnit* unit, bool noLuaCall);
// alpha.x := alpha-value
// alpha.y := alpha-pass (true or false)
void SetTeamColour(int team, const float2 alpha = float2(1.0f, 0.0f)) const;
void SetupOpaqueDrawing(bool deferredPass);
void ResetOpaqueDrawing(bool deferredPass);
void SetupAlphaDrawing(bool deferredPass);
void ResetAlphaDrawing(bool deferredPass);
void SetUnitDrawDist(float dist);
void SetUnitIconDist(float dist);
bool ShowUnitBuildSquare(const BuildInfo& buildInfo);
bool ShowUnitBuildSquare(const BuildInfo& buildInfo, const std::vector<Command>& commands);
void DrawUnitMiniMapIcons() const;
const std::vector<CUnit*>& GetUnsortedUnits() const { return unsortedUnits; }
IModelRenderContainer* GetOpaqueModelRenderer(int modelType) { return opaqueModelRenderers[modelType]; }
IModelRenderContainer* GetAlphaModelRenderer(int modelType) { return alphaModelRenderers[modelType]; }
const GL::LightHandler* GetLightHandler() const { return &lightHandler; }
GL::LightHandler* GetLightHandler() { return &lightHandler; }
const GL::GeometryBuffer* GetGeometryBuffer() const { return geomBuffer; }
GL::GeometryBuffer* GetGeometryBuffer() { return geomBuffer; }
const IUnitDrawerState* GetWantedDrawerState(bool alphaPass) const;
IUnitDrawerState* GetDrawerState(unsigned int idx) { return unitDrawerStates[idx]; }
bool DrawForward() const { return drawForward; }
bool DrawDeferred() const { return drawDeferred; }
bool UseAdvShading() const { return advShading; }
bool& UseAdvShadingRef() { return advShading; }
bool& WireFrameModeRef() { return wireFrameMode; }
public:
struct TempDrawUnit {
const UnitDef* unitDef;
int team;
int facing;
int timeout;
float3 pos;
float rotation;
bool drawAlpha;
bool drawBorder;
};
void AddTempDrawUnit(const TempDrawUnit& tempDrawUnit);
void UpdateTempDrawUnits(std::vector<TempDrawUnit>& tempDrawUnits);
private:
/// Returns true if the given unit should be drawn as icon in the current frame.
bool DrawAsIcon(const CUnit* unit, const float sqUnitCamDist) const;
bool CanDrawOpaqueUnit(const CUnit* unit, bool drawReflection, bool drawRefraction) const;
bool CanDrawOpaqueUnitShadow(const CUnit* unit) const;
void DrawOpaqueUnit(CUnit* unit, bool drawReflection, bool drawRefraction);
void DrawOpaqueUnitShadow(CUnit* unit);
void DrawOpaqueUnitsShadow(int modelType);
void DrawOpaqueUnits(int modelType, bool drawReflection, bool drawRefraction);
void DrawAlphaUnits(int modelType);
void DrawAlphaUnit(CUnit* unit, int modelType, bool drawGhostBuildingsPass);
void DrawOpaqueAIUnits(int modelType);
void DrawOpaqueAIUnit(const TempDrawUnit& unit);
void DrawAlphaAIUnits(int modelType);
void DrawAlphaAIUnit(const TempDrawUnit& unit);
void DrawAlphaAIUnitBorder(const TempDrawUnit& unit);
void DrawGhostedBuildings(int modelType);
public:
void DrawUnitIcons();
void DrawUnitMiniMapIcon(const CUnit* unit, CVertexArray* va) const;
private:
void UpdateUnitMiniMapIcon(const CUnit* unit, bool forced, bool killed);
void UpdateUnitIconState(CUnit* unit);
static void DrawIcon(CUnit* unit, bool asRadarBlip);
static void UpdateUnitDrawPos(CUnit* unit);
public:
static void BindModelTypeTexture(int mdlType, int texType);
static void PushModelRenderState(int mdlType);
static void PopModelRenderState(int mdlType);
// never called directly; combined with PushModelRenderState(S3DModel*)
// static void BindModelTypeTexture(const S3DModel* m) { BindModelTypeTexture(m->type, m->textureType); }
static void PushModelRenderState(const S3DModel* m);
static void PopModelRenderState(const S3DModel* m);
// never called directly; combined with PushModelRenderState(CSolidObject*)
// static void BindModelTypeTexture(const CSolidObject* o) { BindModelTypeTexture(o->model); }
static void PushModelRenderState(const CSolidObject* o);
static void PopModelRenderState(const CSolidObject* o);
static void DrawIndividualDefOpaque(const SolidObjectDef* objectDef, int teamID, bool rawState, bool toScreen = false);
static void DrawIndividualDefAlpha(const SolidObjectDef* objectDef, int teamID, bool rawState, bool toScreen = false);
// needed by FFP drawer-state
static void SetupBasicS3OTexture0();
static void SetupBasicS3OTexture1();
static void CleanupBasicS3OTexture1();
static void CleanupBasicS3OTexture0();
static bool ObjectVisibleReflection(const float3 objPos, const float3 camPos, float maxRadius);
public:
float unitDrawDist;
float unitDrawDistSqr;
float unitIconDist;
float iconLength;
float sqCamDistToGroundForIcons;
// .x := regular unit alpha
// .y := ghosted unit alpha (out of radar)
// .z := ghosted unit alpha (inside radar)
// .w := AI-temp unit alpha
float4 alphaValues;
private:
bool drawForward;
bool drawDeferred;
bool wireFrameMode;
bool advShading;
bool drawBeingBuiltModels;
bool useDistToGroundForIcons;
private:
typedef void (*DrawModelFunc)(const CUnit*, bool);
std::array<IModelRenderContainer*, MODELTYPE_OTHER> opaqueModelRenderers;
std::array<IModelRenderContainer*, MODELTYPE_OTHER> alphaModelRenderers;
/// units being rendered (note that this is a completely
/// unsorted set of 3DO, S3O, opaque, and cloaked models!)
std::vector<CUnit*> unsortedUnits;
/// AI unit ghosts
std::array< std::vector<TempDrawUnit>, MODELTYPE_OTHER> tempOpaqueUnits;
std::array< std::vector<TempDrawUnit>, MODELTYPE_OTHER> tempAlphaUnits;
/// buildings that were in LOS_PREVLOS when they died and not in LOS since
std::vector<std::array<std::vector<GhostSolidObject*>, MODELTYPE_OTHER>> deadGhostBuildings;
/// buildings that left LOS but are still alive
std::vector<std::array<std::vector<CUnit*>, MODELTYPE_OTHER>> liveGhostBuildings;
/// units that are only rendered as icons this frame
std::vector<CUnit*> iconUnits;
spring::unordered_map<icon::CIconData*, std::vector<const CUnit*> > unitsByIcon;
// [0] := fallback shader-less rendering path
// [1] := default shader-driven rendering path
// [2] := currently selected state
std::array<IUnitDrawerState*, DRAWER_STATE_CNT> unitDrawerStates;
std::array<DrawModelFunc, 3> drawModelFuncs;
private:
GL::LightHandler lightHandler;
GL::GeometryBuffer* geomBuffer;
};
extern CUnitDrawer* unitDrawer;
#endif // UNIT_DRAWER_H