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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "ShieldSegmentProjectile.h"
#include "Game/Camera.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Env/Particles/ProjectileDrawer.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/Textures/TextureAtlas.h"
#include "Sim/Projectiles/ProjectileMemPool.h"
#include "Sim/Units/Unit.h"
#include "Sim/Weapons/PlasmaRepulser.h"
#include "Sim/Weapons/WeaponDef.h"
#include "System/EventHandler.h"
#include "System/SpringMath.h"
#include "System/UnorderedMap.hpp"
CR_BIND(ShieldSegmentCollection, )
CR_REG_METADATA(ShieldSegmentCollection, (
CR_MEMBER(shield),
CR_IGNORED(shieldTexture),
CR_IGNORED(lastAllowDrawFrame),
CR_IGNORED(allowDrawing),
CR_MEMBER(shieldSegments),
CR_MEMBER(color),
CR_MEMBER(size),
CR_POSTLOAD(PostLoad)
))
CR_BIND_DERIVED(ShieldSegmentProjectile, CProjectile, )
CR_REG_METADATA(ShieldSegmentProjectile, (
CR_IGNORED(collection),
CR_IGNORED(vertices),
CR_IGNORED(texCoors)
))
static std::vector<float3> spherevertices;
static spring::unsynced_map<const AtlasedTexture*, std::vector<float2> > spheretexcoords;
ShieldSegmentCollection& ShieldSegmentCollection::operator=(ShieldSegmentCollection&& ssc) noexcept {
shield = ssc.shield; ssc.shield = nullptr;
shieldTexture = ssc.shieldTexture; ssc.shieldTexture = nullptr;
lastAllowDrawFrame = ssc.lastAllowDrawFrame;
allowDrawing = ssc.allowDrawing;
size = ssc.size;
color = ssc.color;
shieldSegments = std::move(ssc.shieldSegments);
// update collection-pointers if we are moved into
for (ShieldSegmentProjectile* ssp: shieldSegments) {
ssp->Reload(this, -1, -1);
}
return *this;
}
void ShieldSegmentCollection::Init(CPlasmaRepulser* shield_)
{
shield = shield_;
shieldTexture = nullptr;
lastAllowDrawFrame = -1;
allowDrawing = false;
size = shield->weaponDef->shieldRadius;
color = SColor(255, 255, 255, 0);
const CUnit* u = shield->owner;
const WeaponDef* wd = shield->weaponDef;
if ((allowDrawing = wd->IsVisibleShield())) {
shieldTexture = wd->visuals.texture1;
// Y*X segments, deleted by ProjectileHandler
for (int y = 0; y < NUM_SEGMENTS_Y; ++y) {
for (int x = 0; x < NUM_SEGMENTS_X; ++x) {
shieldSegments.push_back(projMemPool.alloc<ShieldSegmentProjectile>(this, wd, u->pos, x, y));
}
}
// ProjectileDrawer needs to know if any shields use the Perlin-noise texture
if (!UsingPerlinNoise())
return;
projectileDrawer->IncPerlinTexObjectCount();
}
}
void ShieldSegmentCollection::Kill()
{
{
shield = nullptr;
shieldTexture = nullptr;
}
{
for (ShieldSegmentProjectile* seg: shieldSegments) {
seg->PreDelete();
}
shieldSegments.clear();
}
{
if (!UsingPerlinNoise())
return;
projectileDrawer->DecPerlinTexObjectCount();
}
}
void ShieldSegmentCollection::PostLoad()
{
lastAllowDrawFrame = -1;
if (shield == nullptr)
return;
const WeaponDef* wd = shield->weaponDef;
if ((allowDrawing = wd->IsVisibleShield())) {
shieldTexture = wd->visuals.texture1;
int i = 0;
for (int y = 0; y < NUM_SEGMENTS_Y; ++y) {
for (int x = 0; x < NUM_SEGMENTS_X; ++x) {
shieldSegments[i++]->Reload(this, x, y);
}
}
}
}
bool ShieldSegmentCollection::UsingPerlinNoise() const
{
return (projectileDrawer != nullptr && shieldTexture == projectileDrawer->perlintex);
}
bool ShieldSegmentCollection::AllowDrawing()
{
// call eventHandler.DrawShield only once per shield & frame
if (lastAllowDrawFrame == globalRendering->drawFrame)
return allowDrawing;
lastAllowDrawFrame = globalRendering->drawFrame;
if (shield == nullptr)
return (allowDrawing = false);
if (shield->owner == nullptr)
return (allowDrawing = false);
//FIXME if Lua wants to draw the shield itself, we should draw all GL_QUADS in the `va` vertexArray first.
// but doing so for each shield might reduce the performance.
// so might use a branch-predicion? -> save last return value and if it is true draw `va` before calling eventHandler.DrawShield()
if (eventHandler.DrawShield(shield->owner, shield))
return (allowDrawing = false);
if (!shield->IsActive())
return (allowDrawing = false);
if (shieldSegments.empty())
return (allowDrawing = false);
if (!camera->InView(GetShieldDrawPos(), shield->weaponDef->shieldRadius * 2.0f))
return (allowDrawing = false);
// signal the ShieldSegmentProjectile's they can draw
return (allowDrawing = true);
}
void ShieldSegmentCollection::UpdateColor()
{
const WeaponDef* shieldDef = shield->weaponDef;
// lerp between badColor and goodColor based on shield's current power
const float relPower = shield->GetCurPower() / std::max(1.0f, shieldDef->shieldPower);
const float lrpColor = std::min(1.0f, relPower);
float alpha = shieldDef->visibleShield * shieldDef->shieldAlpha;
if (shield->GetHitFrames() > 0 && shieldDef->visibleShieldHitFrames > 0) {
// when a shield is hit, increase segment's opacity to
// min(1, def->alpha + k * def->alpha) with k in [1, 0]
alpha += (shield->GetHitFrames() / float(shieldDef->visibleShieldHitFrames)) * shieldDef->shieldAlpha;
alpha = std::min(alpha, 1.0f);
}
color = SColor(mix(shieldDef->shieldBadColor, shieldDef->shieldGoodColor, lrpColor) * alpha);
}
float3 ShieldSegmentCollection::GetShieldDrawPos() const
{
assert(shield != nullptr);
assert(shield->owner != nullptr);
return shield->owner->GetObjectSpaceDrawPos(shield->relWeaponMuzzlePos);
}
ShieldSegmentProjectile::ShieldSegmentProjectile(
ShieldSegmentCollection* collection_,
const WeaponDef* shieldWeaponDef,
const float3& shieldSegmentPos,
int xpart,
int ypart
)
: CProjectile(
shieldSegmentPos,
ZeroVector,
collection_->GetShield()->owner,
false,
false,
false
)
, collection(collection_)
{
checkCol = false;
deleteMe = false;
alwaysVisible = false;
useAirLos = true;
drawRadius = shieldWeaponDef->shieldRadius * 0.4f;
mygravity = 0.0f;
vertices = GetSegmentVertices(xpart, ypart);
texCoors = GetSegmentTexCoords(collection->GetShieldTexture(), xpart, ypart);
}
void ShieldSegmentProjectile::Reload(ShieldSegmentCollection* collection_, int xpart, int ypart)
{
assert(!deleteMe);
collection = collection_;
if (xpart < 0 && ypart < 0)
return;
vertices = GetSegmentVertices(xpart, ypart);
texCoors = GetSegmentTexCoords(collection->GetShieldTexture(), xpart, ypart);
}
const float3* ShieldSegmentProjectile::GetSegmentVertices(const int xpart, const int ypart)
{
if (spherevertices.empty()) {
spherevertices.resize(ShieldSegmentCollection::NUM_SEGMENTS_Y * ShieldSegmentCollection::NUM_SEGMENTS_X * NUM_VERTICES_Y * NUM_VERTICES_X);
#define NUM_VERTICES_X_M1 (NUM_VERTICES_X - 1)
#define NUM_VERTICES_Y_M1 (NUM_VERTICES_Y - 1)
// add <NUM_SEGMENTS_Y * NUM_SEGMENTS_X * NUM_VERTICES_Y * NUM_VERTICES_X> vertices
for (int ypart_ = 0; ypart_ < ShieldSegmentCollection::NUM_SEGMENTS_Y; ++ypart_) {
for (int xpart_ = 0; xpart_ < ShieldSegmentCollection::NUM_SEGMENTS_X; ++xpart_) {
const int segmentIdx = (xpart_ + ypart_ * ShieldSegmentCollection::NUM_SEGMENTS_X) * (NUM_VERTICES_X * NUM_VERTICES_Y);
for (int y = 0; y < NUM_VERTICES_Y; ++y) {
const float yp = (y + ypart_ * NUM_VERTICES_Y_M1) / float(ShieldSegmentCollection::NUM_SEGMENTS_Y * NUM_VERTICES_Y_M1) * math::PI - math::HALFPI;
for (int x = 0; x < NUM_VERTICES_X; ++x) {
const float xp = (x + xpart_ * NUM_VERTICES_X_M1) / float(ShieldSegmentCollection::NUM_SEGMENTS_X * NUM_VERTICES_X_M1) * math::TWOPI;
const size_t vIdx = segmentIdx + y * NUM_VERTICES_X + x;
spherevertices[vIdx].x = std::sin(xp) * std::cos(yp);
spherevertices[vIdx].y = std::sin(yp);
spherevertices[vIdx].z = std::cos(xp) * std::cos(yp);
}
}
}
}
}
const int segmentIdx = (xpart + ypart * ShieldSegmentCollection::NUM_SEGMENTS_X) * (NUM_VERTICES_X * NUM_VERTICES_Y);
return &spherevertices[segmentIdx];
}
const float2* ShieldSegmentProjectile::GetSegmentTexCoords(const AtlasedTexture* texture, const int xpart, const int ypart)
{
auto fit = spheretexcoords.find(texture);
if (fit == spheretexcoords.end()) {
std::vector<float2>& texcoords = spheretexcoords[texture];
texcoords.resize(ShieldSegmentCollection::NUM_SEGMENTS_Y * ShieldSegmentCollection::NUM_SEGMENTS_X * NUM_VERTICES_Y * NUM_VERTICES_X);
const float xscale = (texture == nullptr)? 0.0f: (texture->xend - texture->xstart) * 0.25f;
const float yscale = (texture == nullptr)? 0.0f: (texture->yend - texture->ystart) * 0.25f;
const float xmid = (texture == nullptr)? 0.0f: (texture->xstart + texture->xend) * 0.5f;
const float ymid = (texture == nullptr)? 0.0f: (texture->ystart + texture->yend) * 0.5f;
for (int ypart_ = 0; ypart_ < ShieldSegmentCollection::NUM_SEGMENTS_Y; ++ypart_) {
for (int xpart_ = 0; xpart_ < ShieldSegmentCollection::NUM_SEGMENTS_X; ++xpart_) {
const int segmentIdx = (xpart_ + ypart_ * ShieldSegmentCollection::NUM_SEGMENTS_X) * (NUM_VERTICES_X * NUM_VERTICES_Y);
for (int y = 0; y < NUM_VERTICES_Y; ++y) {
for (int x = 0; x < NUM_VERTICES_X; ++x) {
const size_t vIdx = segmentIdx + y * NUM_VERTICES_X + x;
texcoords[vIdx].x = (spherevertices[vIdx].x * (2 - std::fabs(spherevertices[vIdx].y))) * xscale + xmid;
texcoords[vIdx].y = (spherevertices[vIdx].z * (2 - std::fabs(spherevertices[vIdx].y))) * yscale + ymid;
}
}
}
}
fit = spheretexcoords.find(texture);
}
const int segmentIdx = (xpart + ypart * ShieldSegmentCollection::NUM_SEGMENTS_X) * (NUM_VERTICES_X * NUM_VERTICES_Y);
return &fit->second[segmentIdx];
}
void ShieldSegmentProjectile::Update()
{
if (collection == nullptr)
return;
// use the "middle" vertex for z-ordering
pos = collection->GetShieldDrawPos() + vertices[(NUM_VERTICES_X * NUM_VERTICES_Y) >> 1] * collection->GetSize();
}
void ShieldSegmentProjectile::Draw(GL::RenderDataBufferTC* va) const
{
if (collection == nullptr)
return;
const SColor color = collection->GetColor();
if (color.a == 0)
return;
if (!collection->AllowDrawing())
return;
const float3 shieldPos = collection->GetShieldDrawPos();
const float size = collection->GetSize();
// draw all quads
for (int y = 0; y < (NUM_VERTICES_Y - 1); ++y) {
for (int x = 0; x < (NUM_VERTICES_X - 1); ++x) {
const int idxTL = (y ) * NUM_VERTICES_X + x, idxTR = (y ) * NUM_VERTICES_X + x + 1;
const int idxBL = (y + 1) * NUM_VERTICES_X + x, idxBR = (y + 1) * NUM_VERTICES_X + x + 1;
va->SafeAppend({shieldPos + vertices[idxTL] * size, texCoors[idxTL].x, texCoors[idxTL].y, color});
va->SafeAppend({shieldPos + vertices[idxTR] * size, texCoors[idxTR].x, texCoors[idxTR].y, color});
va->SafeAppend({shieldPos + vertices[idxBR] * size, texCoors[idxBR].x, texCoors[idxBR].y, color});
va->SafeAppend({shieldPos + vertices[idxBR] * size, texCoors[idxBR].x, texCoors[idxBR].y, color});
va->SafeAppend({shieldPos + vertices[idxBL] * size, texCoors[idxBL].x, texCoors[idxBL].y, color});
va->SafeAppend({shieldPos + vertices[idxTL] * size, texCoors[idxTL].x, texCoors[idxTL].y, color});
}
}
}
|