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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef GROUND_DECAL_HANDLER_H
#define GROUND_DECAL_HANDLER_H
#include <vector>
#include <string>
#include "Rendering/Env/IGroundDecalDrawer.h"
#include "Rendering/Env/Decals/LegacyTrackHandler.h"
#include "Rendering/GL/VertexArray.h"
#include "System/float3.h"
#include "System/EventClient.h"
#include "Sim/Projectiles/ExplosionListener.h"
class CSolidObject;
class CUnit;
class CVertexArray;
struct SolidObjectGroundDecal;
struct SolidObjectDecalType;
namespace Shader {
struct IProgramObject;
}
struct SolidObjectGroundDecal {
public:
SolidObjectGroundDecal()
: owner(nullptr)
, gbOwner(nullptr)
, posx(0)
, posy(0)
, xsize(0)
, ysize(0)
, facing(-1)
, pos(ZeroVector)
, radius(0.0f)
, alpha(1.0f)
, alphaFalloff(1.0f)
{}
SolidObjectGroundDecal(const SolidObjectGroundDecal& d) = delete;
SolidObjectGroundDecal(SolidObjectGroundDecal&& d) { *this = std::move(d); }
SolidObjectGroundDecal& operator = (const SolidObjectGroundDecal& d) = delete;
SolidObjectGroundDecal& operator = (SolidObjectGroundDecal&& d) {
va = std::move(d.va);
owner = d.owner; d.owner = nullptr;
gbOwner = d.gbOwner; d.gbOwner = nullptr;
posx = d.posx;
posy = d.posy;
xsize = d.xsize;
ysize = d.ysize;
facing = d.facing;
pos = d.pos;
radius = d.radius;
alpha = d.alpha;
alphaFalloff = d.alphaFalloff;
return *this;
}
public:
CVertexArray va;
CSolidObject* owner;
GhostSolidObject* gbOwner;
int posx;
int posy;
int xsize;
int ysize;
int facing;
float3 pos;
float radius;
float alpha;
float alphaFalloff;
};
class CGroundDecalHandler: public IGroundDecalDrawer, public CEventClient, public IExplosionListener
{
public:
CGroundDecalHandler();
~CGroundDecalHandler();
void Draw() override;
void GhostCreated(CSolidObject* object, GhostSolidObject* gb) override;
void GhostDestroyed(GhostSolidObject* gb) override;
void RemoveSolidObject(CSolidObject* object, GhostSolidObject* gb);
void ForceRemoveSolidObject(CSolidObject* object) override;
static void RemoveTrack(CUnit* unit);
void OnDecalLevelChanged() override {}
private:
void BindTextures();
void KillTextures();
void BindShader(const float3& ambientColor);
void DrawDecals();
void AddExplosion(float3 pos, float damage, float radius);
void MoveSolidObject(CSolidObject* object, const float3& pos);
int GetSolidObjectDecalType(const std::string& name);
public:
// CEventClient
bool WantsEvent(const std::string& eventName) override {
return
(eventName == "SunChanged") ||
(eventName == "RenderUnitCreated") ||
(eventName == "RenderUnitDestroyed") ||
(eventName == "UnitMoved") ||
(eventName == "RenderFeatureCreated") ||
(eventName == "RenderFeatureDestroyed") ||
(eventName == "FeatureMoved") ||
(eventName == "UnitLoaded") ||
(eventName == "UnitUnloaded");
}
bool GetFullRead() const override { return true; }
int GetReadAllyTeam() const override { return AllAccessTeam; }
void SunChanged() override;
void RenderUnitCreated(const CUnit*, int cloaked) override;
void RenderUnitDestroyed(const CUnit*) override;
void RenderFeatureCreated(const CFeature* feature) override;
void RenderFeatureDestroyed(const CFeature* feature) override;
void FeatureMoved(const CFeature* feature, const float3& oldpos) override;
void UnitMoved(const CUnit* unit) override;
void UnitLoaded(const CUnit* unit, const CUnit* transport) override;
void UnitUnloaded(const CUnit* unit, const CUnit* transport) override;
// IExplosionListener
void ExplosionOccurred(const CExplosionParams& event) override;
public:
struct SolidObjectDecalType {
SolidObjectDecalType(): texture(0) {}
std::string name;
std::vector<SolidObjectGroundDecal*> objectDecals;
unsigned int texture;
};
struct Scar {
public:
Scar(): va(2048) {}
Scar(const Scar& s) = delete;
Scar(Scar&& s) { *this = std::move(s); }
Scar& operator = (const Scar& s) = delete;
Scar& operator = (Scar&& s) {
id = s.id;
x1 = s.x1; x2 = s.x2;
y1 = s.y1; y2 = s.y2;
creationTime = s.creationTime;
lifeTime = s.lifeTime;
lastTest = s.lastTest;
lastDraw = s.lastDraw;
pos = s.pos;
radius = s.radius;
basesize = s.basesize;
overdrawn = s.overdrawn;
alphaDecay = s.alphaDecay;
startAlpha = s.startAlpha;
texOffsetX = s.texOffsetX;
texOffsetY = s.texOffsetY;
va = std::move(s.va);
return *this;
}
void Reset() {
id = -1;
x1 = 0; x2 = 0;
y1 = 0; y2 = 0;
creationTime = 0;
lifeTime = 0;
lastTest = 0;
lastDraw = -1;
pos = ZeroVector;
radius = 0.0f;
basesize = 0.0f;
overdrawn = 0.0f;
alphaDecay = 0.0f;
startAlpha = 1.0f;
texOffsetX = 0.0f;
texOffsetY = 0.0f;
va.Initialize();
}
public:
int id;
int x1, x2;
int y1, y2;
int creationTime;
int lifeTime;
int lastTest;
int lastDraw;
float3 pos;
float radius;
float basesize;
float overdrawn;
float alphaDecay;
float startAlpha;
float texOffsetX;
float texOffsetY;
CVertexArray va;
};
private:
void LoadScarTextures();
void LoadDecalShaders();
void DrawObjectDecals();
void AddScars();
void DrawScars();
void GatherDecalsForType(SolidObjectDecalType& decalType);
void AddDecal(CUnit* unit, const float3& newPos);
void DrawObjectDecal(SolidObjectGroundDecal* decal);
void DrawGroundScar(Scar& scar);
int GetScarID() const;
int ScarOverlapSize(const Scar& s1, const Scar& s2);
void TestScarOverlaps(const Scar& scar);
void RemoveScar(Scar& scar);
void LoadScarTexture(const std::string& file, uint8_t* buf, int xoffset, int yoffset);
private:
enum DecalShaderProgram {
DECAL_SHADER_ARB,
DECAL_SHADER_GLSL,
DECAL_SHADER_CURR,
DECAL_SHADER_LAST
};
std::vector<SolidObjectDecalType> objectDecalTypes;
std::vector<Shader::IProgramObject*> decalShaders;
std::vector<SolidObjectGroundDecal*> decalsToDraw;
std::vector<int> addedScars;
// stores indices into <scars> of reserved slots, per quad
std::vector< std::vector<int> > scarField;
int scarFieldX;
int scarFieldY;
unsigned int scarTex;
// number of calls made to TestScarOverlaps
int lastScarOverlapTest;
float maxScarOverlapSize;
bool groundScarAlphaFade;
LegacyTrackHandler trackHandler;
};
#endif // GROUND_DECAL_HANDLER_H
|