File: GroundFlash.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (393 lines) | stat: -rw-r--r-- 10,884 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "GroundFlash.h"
#include "Map/Ground.h"
#include "Game/Camera.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GroundFlashInfo.h"
#include "Rendering/GL/VertexArray.h"
#include "Rendering/Textures/ColorMap.h"
#include "Rendering/Textures/TextureAtlas.h"
#include "Rendering/Env/Particles/ProjectileDrawer.h"
#include "Sim/Projectiles/ExpGenSpawnableMemberInfo.h"
#include "Sim/Projectiles/ProjectileHandler.h"
#include "Sim/Projectiles/ProjectileMemPool.h"
#include "System/myMath.h"

CR_BIND_DERIVED_POOL(CGroundFlash, CExpGenSpawnable, , projMemPool.alloc, projMemPool.free)
CR_REG_METADATA(CGroundFlash, (
 	CR_MEMBER_BEGINFLAG(CM_Config),
		CR_MEMBER(size),
		CR_MEMBER(depthTest),
		CR_MEMBER(depthMask),
	CR_MEMBER_ENDFLAG(CM_Config)
))

CR_BIND_DERIVED(CStandardGroundFlash, CGroundFlash, )
CR_REG_METADATA(CStandardGroundFlash, (
 	CR_MEMBER_BEGINFLAG(CM_Config),
 		CR_MEMBER(flashSize),
		CR_MEMBER(flashAlpha),
		CR_MEMBER(circleGrowth),
		CR_MEMBER(circleAlpha),
		CR_MEMBER(color),
	CR_MEMBER_ENDFLAG(CM_Config),

	CR_MEMBER(side1),
	CR_MEMBER(side2),
	CR_MEMBER(circleSize),
	CR_MEMBER(flashAge),
	CR_MEMBER(flashAgeSpeed),
	CR_MEMBER(circleAlphaDec),
	CR_MEMBER(ttl)
))

CR_BIND_DERIVED(CSeismicGroundFlash, CGroundFlash, (ZeroVector, 1, 0, 1, 1, 1, ZeroVector))
CR_REG_METADATA(CSeismicGroundFlash, (
	CR_MEMBER(side1),
	CR_MEMBER(side2),
	CR_MEMBER(texture),
	CR_MEMBER(sizeGrowth),
	CR_MEMBER(alpha),
	CR_MEMBER(fade),
	CR_MEMBER(ttl),
	CR_MEMBER(color)
))

CR_BIND_DERIVED(CSimpleGroundFlash, CGroundFlash, )
CR_REG_METADATA(CSimpleGroundFlash, (
	CR_MEMBER(side1),
	CR_MEMBER(side2),
	CR_MEMBER(age),
	CR_MEMBER(agerate),
 	CR_MEMBER_BEGINFLAG(CM_Config),
		CR_MEMBER(sizeGrowth),
		CR_MEMBER(ttl),
		CR_MEMBER(colorMap),
		CR_MEMBER(texture),
	CR_MEMBER_ENDFLAG(CM_Config)
))



// CREG-only
CGroundFlash::CGroundFlash()
{
	size = 0.0f;
	depthTest = true;
	depthMask = false;
	alwaysVisible = false;
}

CGroundFlash::CGroundFlash(const float3& _pos): CExpGenSpawnable()
{
	size = 0.0f;
	depthTest = true;
	depthMask = false;
	alwaysVisible = false;
	pos = _pos;
}

float3 CGroundFlash::CalcNormal(const float3 midPos, const float3 camDir, float quadSize) const
{
	// no degenerate quads, otherwise ANormalize() fails
	// assert(quadSize > 1.0f);
	quadSize = std::max(quadSize, 1.0f);

	float3 p0 = float3(midPos.x + quadSize, 0.0f, midPos.z           );
	float3 p1 = float3(midPos.x - quadSize, 0.0f, midPos.z           );
	float3 p2 = float3(midPos.x,            0.0f, midPos.z + quadSize);
	float3 p3 = float3(midPos.x,            0.0f, midPos.z - quadSize);

	p0.y = CGround::GetApproximateHeight(p0.x, p0.z, false); p0 += camDir;
	p1.y = CGround::GetApproximateHeight(p1.x, p1.z, false); p1 += camDir;
	p2.y = CGround::GetApproximateHeight(p2.x, p2.z, false); p2 += camDir;
	p3.y = CGround::GetApproximateHeight(p3.x, p3.z, false); p3 += camDir;

	const float3 n0 = ((p2 - p0).cross(p3 - p0)).ANormalize();
	const float3 n1 = ((p3 - p1).cross(p2 - p1)).ANormalize();

	return ((n0 + n1).ANormalize());
}


bool CGroundFlash::GetMemberInfo(SExpGenSpawnableMemberInfo& memberInfo)
{
	if (CExpGenSpawnable::GetMemberInfo(memberInfo))
		return true;

	CHECK_MEMBER_INFO_FLOAT(CGroundFlash, size)
	CHECK_MEMBER_INFO_BOOL (CGroundFlash, depthTest)
	CHECK_MEMBER_INFO_BOOL (CGroundFlash, depthMask)

	return false;
}



CStandardGroundFlash::CStandardGroundFlash()
{
	ttl = 0;
	color[0] = 0;
	color[1] = 0;
	color[2] = 0;

	circleAlphaDec = 0.0f;
	flashAgeSpeed = 0.0f;
	flashAge = 0.0f;
	flashAlpha = 0.0f;
	circleAlpha = 0.0f;
	circleGrowth = 0.0f;
	circleSize = 0.0f;
	flashSize = 0.0f;
}

CStandardGroundFlash::CStandardGroundFlash(
	const float3& _pos,
	const GroundFlashInfo& info
)
	: CGroundFlash(_pos)
	, ttl(info.ttl)
	, flashSize(info.flashSize)
	, circleSize(info.circleGrowth)
	, circleGrowth(info.circleGrowth)
	, circleAlpha(info.circleAlpha)
	, flashAlpha(info.flashAlpha)
	, flashAge(0.0f)
	, flashAgeSpeed(ttl ? 1.0f / ttl : 0.0f)
	, circleAlphaDec(ttl ? circleAlpha / ttl : 0.0f)
{
	InitCommon(_pos, info.color);
	projectileHandler->AddGroundFlash(this);
}

CStandardGroundFlash::CStandardGroundFlash(
	const float3& _pos,
	float _circleAlpha,
	float _flashAlpha,
	float _flashSize,
	float _circleGrowth,
	float _ttl,
	const float3& _color
)
	: CGroundFlash(_pos)
	, ttl((int)_ttl)
	, flashSize(_flashSize)
	, circleSize(_circleGrowth)
	, circleGrowth(_circleGrowth)
	, circleAlpha(_circleAlpha)
	, flashAlpha(_flashAlpha)
	, flashAge(0)
	, flashAgeSpeed(ttl ? 1.0f / ttl : 0)
	, circleAlphaDec(ttl ? circleAlpha / ttl : 0)
{
	InitCommon(_pos, _color);
	projectileHandler->AddGroundFlash(this);
}

void CStandardGroundFlash::InitCommon(const float3& _pos, const float3& _color)
{
	pos.y = CGround::GetHeightReal(_pos.x, _pos.z, false) + 1.0f;
	// A is set in Draw
	color.r = _color.x * 255.0f;
	color.g = _color.y * 255.0f;
	color.b = _color.z * 255.0f;

	// flashSize is just backward compability
	size = flashSize;

	const float3 normal = CalcNormal(_pos, camera->GetDir() * -1000.0f, flashSize);

	side1 = (normal.cross(RgtVector)).ANormalize();
	side2 = side1.cross(normal);
}

bool CStandardGroundFlash::Update()
{
	circleSize += circleGrowth;
	circleAlpha -= circleAlphaDec;
	flashAge += flashAgeSpeed;

	return (std::max(--ttl, 0) > 0);
}

void CStandardGroundFlash::Draw(CVertexArray* va)
{
	float iAlpha = Clamp(circleAlpha - (circleAlphaDec * globalRendering->timeOffset), 0.0f, 1.0f);

	const float iSize = circleSize + circleGrowth * globalRendering->timeOffset;
	const float iAge = flashAge + flashAgeSpeed * globalRendering->timeOffset;

	if (iAlpha > 0.0f) {
		const float3 p1 = pos + (-side1 - side2) * iSize;
		const float3 p2 = pos + ( side1 - side2) * iSize;
		const float3 p3 = pos + ( side1 + side2) * iSize;
		const float3 p4 = pos + (-side1 + side2) * iSize;

		color.a = (unsigned char)(iAlpha * 255);

		va->AddVertexQTC(p1, projectileDrawer->groundringtex->xstart, projectileDrawer->groundringtex->ystart, color);
		va->AddVertexQTC(p2, projectileDrawer->groundringtex->xend,   projectileDrawer->groundringtex->ystart, color);
		va->AddVertexQTC(p3, projectileDrawer->groundringtex->xend,   projectileDrawer->groundringtex->yend,   color);
		va->AddVertexQTC(p4, projectileDrawer->groundringtex->xstart, projectileDrawer->groundringtex->yend,   color);
	}

	if (iAge < 1.0f) {
		if (iAge < 0.091f) {
			iAlpha = flashAlpha * iAge * 10.0f;
		} else {
			iAlpha = flashAlpha * (1.0f - iAge);
		}

		color.a = Clamp(iAlpha, 0.0f, 1.0f) * 255;

		const float3 p1 = pos + (-side1 - side2) * size;
		const float3 p2 = pos + ( side1 - side2) * size;
		const float3 p3 = pos + ( side1 + side2) * size;
		const float3 p4 = pos + (-side1 + side2) * size;

		va->AddVertexQTC(p1, projectileDrawer->groundflashtex->xstart, projectileDrawer->groundflashtex->yend,   color);
		va->AddVertexQTC(p2, projectileDrawer->groundflashtex->xend,   projectileDrawer->groundflashtex->yend,   color);
		va->AddVertexQTC(p3, projectileDrawer->groundflashtex->xend,   projectileDrawer->groundflashtex->ystart, color);
		va->AddVertexQTC(p4, projectileDrawer->groundflashtex->xstart, projectileDrawer->groundflashtex->ystart, color);
	}
}


bool CStandardGroundFlash::GetMemberInfo(SExpGenSpawnableMemberInfo& memberInfo)
{
	if (CGroundFlash::GetMemberInfo(memberInfo))
		return true;

	CHECK_MEMBER_INFO_FLOAT(CStandardGroundFlash, flashSize   )
	CHECK_MEMBER_INFO_FLOAT(CStandardGroundFlash, flashAlpha  )
	CHECK_MEMBER_INFO_FLOAT(CStandardGroundFlash, circleGrowth)
	CHECK_MEMBER_INFO_FLOAT(CStandardGroundFlash, circleAlpha )
	CHECK_MEMBER_INFO_SCOLOR(CStandardGroundFlash, color      )

	return false;
}



CSimpleGroundFlash::CSimpleGroundFlash()
{
	texture = nullptr;
	colorMap = nullptr;
	agerate = 0.0f;
	age = 0.0f;
	ttl = 0;
	sizeGrowth = 0.0f;
}

void CSimpleGroundFlash::Init(const CUnit* owner, const float3& offset)
{
	pos += offset;
	pos.y = CGround::GetHeightReal(pos.x, pos.z, false) + 1.0f;

	age = ttl ? 0.0f : 1.0f;
	agerate = ttl ? 1.0f / ttl : 1.0f;

	const float3 normal = CalcNormal(pos, camera->GetDir() * -1000.0f, size + (sizeGrowth * ttl));

	side1 = (normal.cross(RgtVector)).ANormalize();
	side2 = side1.cross(normal);

	projectileHandler->AddGroundFlash(this);
}

void CSimpleGroundFlash::Draw(CVertexArray* va)
{
	unsigned char color[4] = {0, 0, 0, 0};
	colorMap->GetColor(color, age);

	const float3 p1 = pos + (-side1 - side2) * size;
	const float3 p2 = pos + ( side1 - side2) * size;
	const float3 p3 = pos + ( side1 + side2) * size;
	const float3 p4 = pos + (-side1 + side2) * size;

	va->AddVertexQTC(p1, texture->xstart, texture->ystart, color);
	va->AddVertexQTC(p2, texture->xend,   texture->ystart, color);
	va->AddVertexQTC(p3, texture->xend,   texture->yend,   color);
	va->AddVertexQTC(p4, texture->xstart, texture->yend,   color);
}

bool CSimpleGroundFlash::Update()
{
	age += agerate;
	size += sizeGrowth;

	return (age < 1);
}


bool CSimpleGroundFlash::GetMemberInfo(SExpGenSpawnableMemberInfo& memberInfo)
{
	if (CGroundFlash::GetMemberInfo(memberInfo))
		return true;

	CHECK_MEMBER_INFO_FLOAT(CSimpleGroundFlash, sizeGrowth)
	CHECK_MEMBER_INFO_INT  (CSimpleGroundFlash, ttl       )
	CHECK_MEMBER_INFO_PTR  (CSimpleGroundFlash, colorMap, CColorMap::LoadFromDefString)
	CHECK_MEMBER_INFO_PTR  (CSimpleGroundFlash, texture , projectileDrawer->groundFXAtlas->GetTexturePtr)

	return false;
}



CSeismicGroundFlash::CSeismicGroundFlash(
	const float3& _pos,
	int _ttl,
	int _fade,
	float _size,
	float _sizeGrowth,
	float _alpha,
	const float3& _color
)
	: CGroundFlash(_pos)
	, texture(projectileDrawer->seismictex)
	, sizeGrowth(_sizeGrowth)
	, alpha(_alpha)
	, fade(_fade)
	, ttl(_ttl)
{
	pos.y = CGround::GetHeightReal(_pos.x, _pos.z, false) + 1.0f;
	// A is set in Draw
	color.r = _color.x * 255.0f;
	color.g = _color.y * 255.0f;
	color.b = _color.z * 255.0f;

	size = _size;
	alwaysVisible = true;

	const float3 normal = CalcNormal(_pos, camera->GetDir() * -1000.0f, size + (sizeGrowth * ttl));

	side1 = (normal.cross(RgtVector)).SafeANormalize();
	side2 = side1.cross(normal);

	projectileHandler->AddGroundFlash(this);
}

void CSeismicGroundFlash::Draw(CVertexArray* va)
{
	color.a = mix(255, int(255 * (ttl / (1.0f * fade))), (ttl < fade));

	const float3 p1 = pos + (-side1 - side2) * size;
	const float3 p2 = pos + ( side1 - side2) * size;
	const float3 p3 = pos + ( side1 + side2) * size;
	const float3 p4 = pos + (-side1 + side2) * size;

	va->AddVertexQTC(p1, texture->xstart, texture->ystart, color);
	va->AddVertexQTC(p2, texture->xend,   texture->ystart, color);
	va->AddVertexQTC(p3, texture->xend,   texture->yend,   color);
	va->AddVertexQTC(p4, texture->xstart, texture->yend,   color);
}

bool CSeismicGroundFlash::Update()
{
	size += sizeGrowth;
	return (--ttl > 0);
}