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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef SHADER_GROUND_DECAL_DRAWER_H
#define SHADER_GROUND_DECAL_DRAWER_H
#include <vector>
#include <array>
#include "Rendering/Env/IGroundDecalDrawer.h"
#include "Rendering/Env/Decals/LegacyTrackHandler.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/VBO.h"
#include "Rendering/GL/FBO.h"
#include "System/EventClient.h"
#include "Sim/Projectiles/ExplosionListener.h"
#include "System/type2.h"
#include "System/float3.h"
#include "System/float4.h"
#if !defined(GL_VERSION_4_3) || HEADLESS
class CDecalsDrawerGL4: public IGroundDecalDrawer
{
public:
struct Decal {
Decal()
: rot(0.0f)
, alpha(0.0f)
, owner(nullptr)
, type(EXPLOSION)
{}
void Free() const {};
bool IsValid() const { return false; }
void Invalidate() const {}
bool InvalidateExtents() const { return false; }
void SetTexture(const std::string& name) {}
int GetIdx() const { return -1; }
std::string GetTexture() const { return ""; }
float3 pos;
float2 size;
float rot;
float alpha;
const void* owner;
enum { EXPLOSION, BUILDING, LUA } type;
};
CDecalsDrawerGL4();
void Draw() override {}
void OnDecalLevelChanged() override {}
void ForceRemoveSolidObject(CSolidObject* object) override {}
void GhostDestroyed(GhostSolidObject* gb) override {}
void GhostCreated(CSolidObject* object, GhostSolidObject* gb) override {}
int CreateLuaDecal() { return 0;}
const std::vector<Decal>& GetAllDecals() const { return decals; }
Decal& GetDecalByIdx(unsigned idx) { assert(false); static Decal tmp; return tmp; }
private:
std::vector<Decal> decals;
};
#else
namespace Shader {
struct IProgramObject;
}
class CDecalsDrawerGL4: public IGroundDecalDrawer, public CEventClient, public IExplosionListener
{
public:
CDecalsDrawerGL4();
~CDecalsDrawerGL4();
void Draw() override;
//FIXME make eventClient!!!
void GhostDestroyed(GhostSolidObject* gb) override;
void GhostCreated(CSolidObject* object, GhostSolidObject* gb) override;
void ForceRemoveSolidObject(CSolidObject* object) override;
void OnDecalLevelChanged() override;
void ExplosionOccurred(const CExplosionParams&) override;
public:
bool WantsEvent(const std::string& eventName) override {
return
(eventName == "RenderUnitCreated")
|| (eventName == "RenderUnitDestroyed")
|| (eventName == "RenderFeatureCreated")
|| (eventName == "RenderFeatureDestroyed")
|| (eventName == "ViewResize")
|| (eventName == "Update")
|| (eventName == "GameFrame")
|| (eventName == "SunChanged");
;
/*return
(eventName == "UnitMoved")*/
}
bool GetFullRead() const override { return true; }
int GetReadAllyTeam() const override { return AllAccessTeam; }
void RenderUnitCreated(const CUnit* unit, int cloaked) override;
void RenderUnitDestroyed(const CUnit* unit) override;
void RenderFeatureCreated(const CFeature* feature) override;
void RenderFeatureDestroyed(const CFeature* feature) override;
//FIXME
/*void FeatureMoved(const CFeature* feature, const float3& oldpos);
void UnitMoved(const CUnit* unit);
void UnitLoaded(const CUnit* unit, const CUnit* transport);
void UnitUnloaded(const CUnit* unit, const CUnit* transport);*/
void SunChanged() override;
void ViewResize() override;
void Update() override;
void GameFrame(int n) override;
public:
static constexpr int MAX_DECALS_PER_GROUP = 4;
static constexpr int MAX_OVERLAP = 3;
static constexpr int OVERLAP_TEST_TEXTURE_SIZE = 256;
struct Decal {
public:
Decal()
: pos(-1e6, -1e6, -1e6)
, size(-1.0f, -1.0f)
, rot(0.0f)
, alpha(0.0f)
, alphaFalloff(0.0f)
, texOffsets(0.0f, 0.0f, 0.0f, 0.0f)
, texNormalOffsets(0.0f, 0.0f, 0.0f, 0.0f)
, owner(nullptr)
, generation(0)
, type(EXPLOSION)
{}
void Free() const;
void Invalidate() const; ///< call after alpha & texture changes (to upload changes to GPU)
bool InvalidateExtents() const; ///< call after pos, size, rot changes (does same as Invalidate() + searches a new `DecalGroup`), returns true whena group was found (else it isn't rendered)
int GetIdx() const;
bool IsValid() const;
bool InView() const;
float GetRating(bool inview_test) const;
void SetOwner(const void* owner);
void SetTexture(const std::string& name);
std::string GetTexture() const;
public:
float3 pos;
float2 size;
float rot;
float alpha;
float alphaFalloff;
float4 texOffsets;
float4 texNormalOffsets;
const void* owner;
int generation;
enum { EXPLOSION, BUILDING, LUA } type; //FIXME merge with owner?
};
struct SDecalGroup {
std::array<float4, 2> boundAABB;
std::array<int, MAX_DECALS_PER_GROUP> ids;
int size() const {
for (int i = 0; i < MAX_DECALS_PER_GROUP; ++i) {
if (ids[i] == 0) {
return i;
}
}
return MAX_DECALS_PER_GROUP;
}
};
friend Decal;
public:
int CreateLuaDecal();
int NewDecal(const Decal& d);
void FreeDecal(int idx);
Decal& GetDecalOwnedBy(const void* owner);
const std::vector<Decal>& GetAllDecals() const { return decals; }
Decal& GetDecalByIdx(unsigned idx) {
if (idx < decals.size()) {
return decals[idx];
}
return decals.front();
}
private:
void AddExplosion(float3 pos, float damage, float radius);
void CreateBuildingDecal(const CSolidObject* unit);
void DeownBuildingDecal(const CSolidObject* object);
bool FindAndAddToGroup(int decalIdx);
void RemoveFromGroup(int decalIdx);
bool AddDecalToGroup(SDecalGroup& g, const Decal& d, const int decalIdx);
bool TryToCombineDecalGroups(SDecalGroup& g1, SDecalGroup& g2);
void UpdateBoundingBox(SDecalGroup& g);
void GetWorstRatedDecal(int* idx, float* rating, const bool inview_test) const;
bool AnyDecalsInView() const;
void DetectMaxDecals();
void LoadShaders();
void GenerateAtlasTexture();
void CreateBoundingBoxVBOs();
void CreateStructureVBOs();
void UpdateDecalsVBO();
void OptimizeGroups();
void UpdateOverlap();
std::vector<int> UpdateOverlap_PreCheck();
void UpdateOverlap_Initialize();
std::vector<int> UpdateOverlap_CheckQueries();
void UpdateOverlap_GenerateQueries(const std::vector<int>& candidatesForOverlap);
std::vector<int> CandidatesForOverlap() const;
void DrawDecals();
//void DrawTracks();
private:
std::vector<Decal> decals;
std::vector<SDecalGroup> groups;
int curWorstDecalIdx;
float curWorstDecalRating;
std::vector<int> freeIds;
std::vector<int> decalsToUpdate;
std::vector<int> alphaDecayingDecals;
int maxDecals;
int maxDecalGroups;
bool useSSBO;
// used to turn off parallax mapping when fps drops too low
int laggedFrames;
// used to remove decals that are totally overlapped by others
int overlapStage;
std::vector<std::pair<int,GLuint>> waitingOverlapGlQueries;
std::vector<int> waitingDecalsForOverlapTest;
FBO fboOverlap;
VBO vboVertices;
VBO vboIndices;
VBO uboGroundLighting;
VBO uboDecalsStructures;
VBO uboDecalGroups;
GLuint depthTex;
GLuint atlasTex;
Shader::IProgramObject* decalShader;
LegacyTrackHandler trackHandler;
};
#endif // !defined(GL_VERSION_4_0) || HEADLESS
#endif // SHADER_GROUND_DECAL_DRAWER_H
|