File: particle.cpp

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (538 lines) | stat: -rw-r--r-- 16,324 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/



#include "bmpman/bmpman.h"
#include "particle/particle.h"
#include "particle/ParticleManager.h"
#include "particle/ParticleEffect.h"
#include "debugconsole/console.h"
#include "globalincs/systemvars.h"
#include "graphics/2d.h"
#include "lighting/lighting.h"
#include "math/curve.h"
#include "render/3d.h"
#include "render/batching.h"
#include "tracing/tracing.h"
#include "tracing/Monitor.h"
#include "utils/Random.h"
#include "nebula/neb.h"
#include "mission/missionparse.h"
#include "mod_table/mod_table.h"

using namespace particle;

namespace
{
	SCP_vector<::particle::particle> Particles;
	SCP_vector<ParticlePtr> Persistent_particles;

	static int Particles_enabled = 1;

	float get_current_alpha(vec3d* pos, float rad)
	{
		float dist;
		float alpha = 0.99999f;

		const float inner_radius = MIN(30.0f, rad);
		const float magic_num = MIN(2.75f, rad / 10.0f);

		// determine what alpha to draw this bitmap with
		// higher alpha the closer the bitmap gets to the eye
		dist = vm_vec_dist_quick(&Eye_position, pos);

		// if the point is inside the inner radius, alpha is based on distance to the player's eye,
		// becoming more transparent as it gets close
		if (dist <= inner_radius)
		{
			// alpha per meter between the magic # and the inner radius
			alpha /= (inner_radius - magic_num);

			// above value times the # of meters away we are
			alpha *= (dist - magic_num);
			if (alpha < 0.05f)
				return 0.0f;
		}

		if (Neb_affects_particles)
			nebula_handle_alpha(alpha, pos, 
				Neb2_fog_visibility_particle_const + (rad * Neb2_fog_visibility_particle_scaled_factor));

		return alpha;
	}
}

namespace particle
{
	int Anim_bitmap_id_fire = -1;
	int Anim_num_frames_fire = -1;

	int Anim_bitmap_id_smoke = -1;
	int Anim_num_frames_smoke = -1;

	int Anim_bitmap_id_smoke2 = -1;
	int Anim_num_frames_smoke2 = -1;

	// Reset everything between levels
	void init()
	{
		if (Is_standalone)
		{
			Particles_enabled = 0;
			return;
		}

		// FIRE!!!
		if (Anim_bitmap_id_fire < 0)
		{
			Anim_bitmap_id_fire = bm_load_animation("particleexp01", &Anim_num_frames_fire, nullptr, NULL, 0);
			if (Anim_bitmap_id_fire < 0)
				Warning(LOCATION, "Could not load legacy fire particle bitmap (particleexp01)!");
		}

		// Cough, cough
		if (Anim_bitmap_id_smoke < 0)
		{
			Anim_bitmap_id_smoke = bm_load_animation("particlesmoke01", &Anim_num_frames_smoke, nullptr, NULL, 0);
			if (Anim_bitmap_id_smoke < 0)
				Warning(LOCATION, "Could not load legacy smoke particle bitmap (particlesmoke01)!");
		}

		// wheeze
		if (Anim_bitmap_id_smoke2 < 0)
		{
			Anim_bitmap_id_smoke2 = bm_load_animation("particlesmoke02", &Anim_num_frames_smoke2, nullptr, NULL, 0);
			if (Anim_bitmap_id_smoke2 < 0)
				Warning(LOCATION, "Could not load legacy smoke2 particle bitmap (particlesmoke02)!");
		}
	}

	const ParticleEffect& ParticleSubeffectHandle::getParticleEffect() const {
		//TODO possibly cache this!
		return ParticleManager::get()->getEffect(handle)[subeffect];
	}

	// only call from game_shutdown()!!!
	void close()
	{
		Persistent_particles.clear();
		Particles.clear();
	}

	size_t get_particle_count() {
		return Particles.size() + Persistent_particles.size();
	}

	void page_in()
	{
		if (!Particles_enabled)
		{
			return;
		}

		bm_page_in_texture(Anim_bitmap_id_fire);
		bm_page_in_texture(Anim_bitmap_id_smoke);
		bm_page_in_texture(Anim_bitmap_id_smoke2);

		ParticleManager::get()->pageIn();
	}

	DCF_BOOL2(particles, Particles_enabled, "Turns particles on/off",
			  "Usage: particles [bool]\nTurns particle system on/off.  If nothing passed, then toggles it.\n");

	static bool maybe_cull_particle(const particle& new_particle) {
		if (!Particles_enabled)
		{
			return true;
		}

		vec3d world_pos = new_particle.pos;
		if (new_particle.attached_objnum >= 0) {
			vm_vec_unrotate(&world_pos, &world_pos, &Objects[new_particle.attached_objnum].orient);
			world_pos += Objects[new_particle.attached_objnum].pos;
		}
		// treat particles on lower detail levels as 'further away' for the purposes of culling
		float adjusted_dist = vm_vec_dist(&Eye_position, &world_pos) * powf(2.5f, (float)(static_cast<int>(DefaultDetailPreset::Num_detail_presets) - Detail.num_particles));
		// treat bigger particles as 'closer'
		adjusted_dist /= new_particle.radius;
		float cull_start_dist = 1000.f;
		if (adjusted_dist > cull_start_dist) {
			if (frand() > 1.0f / (log2(adjusted_dist / cull_start_dist) + 1.0f))
				return true;
		}

		if (new_particle.nframes >= 0 && new_particle.bitmap < 0)
				return true;

		return false;
	}

	void create(particle&& new_particle) {
		if (maybe_cull_particle(new_particle))
			return;

		Particles.push_back(new_particle);
	}

	// Creates a single particle. See the PARTICLE_?? defines for types.
	WeakParticlePtr createPersistent(particle&& new_particle)
	{
		if (maybe_cull_particle(new_particle))
			return {};

		ParticlePtr new_particle_ptr = std::make_shared<particle>(new_particle);

		Persistent_particles.push_back(new_particle_ptr);

		return {new_particle_ptr};
	}

	float getPixelSize(const particle& subject_particle) {
		vec3d world_pos = subject_particle.pos;

		if (subject_particle.attached_objnum >= 0) {
			vm_vec_unrotate(&world_pos, &world_pos, &Objects[subject_particle.attached_objnum].orient);
			world_pos += Objects[subject_particle.attached_objnum].pos;
		}

		float distance_to_eye = vm_vec_dist(&Eye_position, &world_pos);

		return convert_distance_and_diameter_to_pixel_size(
			distance_to_eye,
			subject_particle.radius * 2.f,
			g3_get_hfov(Eye_fov),
			gr_screen.max_w);
	}

	/**
	 * @brief Moves a single particle
	 * @param frametime The length of the current frame
	 * @param part The particle to process for movement
	 * @return @c true if the particle has expired and should be removed, @c false otherwise
	 */
	bool move_particle(float frametime, particle* part) {
		if (part->age == 0.0f)
		{
			part->age = 0.00001f;
		}
		else
		{
			part->age += frametime;
		}

		bool remove_particle = false;

		// if its time expired, remove it. If the particle is looping then it will never be removed due to age
		if (part->age > part->max_life && !part->looping)
		{
			// special case, if max_life is 0 then we want it to render at least once
			if ((part->age > frametime) || (part->max_life > 0.0f))
			{
				remove_particle = true;
			}
		}

		// if the particle is attached to an object which has become invalid, kill it
		if (part->attached_objnum >= 0)
		{
			// if the signature has changed, or it's bogus, kill it
			if ((part->attached_objnum >= MAX_OBJECTS) ||
				(part->attached_sig != Objects[part->attached_objnum].signature))
			{
				remove_particle = true;
			}
		}

		if (remove_particle)
		{
			return true;
		}

		const auto& source_effect = part->parent_effect.getParticleEffect();

		float part_velocity =  vm_vec_mag_quick(&part->velocity);
		float vel_scalar = source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::VELOCITY_MULT, std::forward_as_tuple(*part, part_velocity) );

		// move as a regular particle
		vec3d prev_pos = part->pos;
		part->pos += (part->velocity * vel_scalar) * frametime;

		const auto& curve_input = std::forward_as_tuple(*part, part_velocity * vel_scalar);

		if (Detail.lighting > 3 && source_effect.m_light_source) {
			const auto& light_source = *source_effect.m_light_source;

			vec3d p_pos;
			if (part->attached_objnum >= 0)
			{
				vm_vec_unrotate(&p_pos, &part->pos, &Objects[part->attached_objnum].orient);
				vm_vec_add2(&p_pos, &Objects[part->attached_objnum].pos);
			}
			else
			{
				p_pos = part->pos;
			}
			
			float light_radius = light_source.light_radius * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_RADIUS_MULT, curve_input);
			float source_radius = light_source.source_radius * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_SOURCE_RADIUS_MULT, curve_input);
			float intensity = light_source.intensity * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_INTENSITY_MULT, curve_input);
			float r = light_source.r * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_R_MULT, curve_input);
			float g = light_source.g * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_G_MULT, curve_input);
			float b = light_source.b * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_B_MULT, curve_input);

			// after this point it is only lighting code, so we can early return
			if (light_radius <= 0.0f || intensity <= 0.0f) {
				return false;
			}

			switch (light_source.light_source_mode) {
			case ParticleEffect::LightInformation::LightSourceMode::POINT:
				light_add_point(&p_pos, light_radius, light_radius, intensity, r, g, b, source_radius);
				break;
			case ParticleEffect::LightInformation::LightSourceMode::TO_LAST_POS: {
				vec3d p_prev_pos;
				if (part->attached_objnum >= 0)
				{
					vm_vec_unrotate(&p_prev_pos, &prev_pos, &Objects[part->attached_objnum].last_orient);
					vm_vec_add2(&p_prev_pos, &Objects[part->attached_objnum].last_pos);
				}
				else
				{
					p_prev_pos = prev_pos;
				}
				light_add_tube(&p_prev_pos, &p_pos, light_radius, light_radius, intensity, r, g, b, source_radius);
			}
			break;
			case ParticleEffect::LightInformation::LightSourceMode::AS_PARTICLE:
				if (part->length != 0.0f) {
					vec3d p1;
					vm_vec_copy_normalize_safe(&p1, &part->velocity);
					if (part->attached_objnum >= 0) {
						vm_vec_unrotate(&p1, &p1, &Objects[part->attached_objnum].orient);
					}
					p1 *= part->length * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LENGTH_MULT, curve_input);
					p1 += p_pos;
					light_add_tube(&p_pos, &p1, light_radius, light_radius, intensity, r, g, b, source_radius);
				}
				else {
					light_add_point(&p_pos, light_radius, light_radius, intensity, r, g, b, source_radius);
				}
				break;
			case ParticleEffect::LightInformation::LightSourceMode::CONE: {
				float cone_angle = light_source.cone_angle * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_CONE_ANGLE_MULT, curve_input);
				float cone_inner_angle = light_source.cone_inner_angle * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LIGHT_CONE_INNER_ANGLE_MULT, curve_input);
				vec3d p1;
				vm_vec_copy_normalize_safe(&p1, &part->velocity);
				if (part->attached_objnum >= 0) {
					vm_vec_unrotate(&p1, &p1, &Objects[part->attached_objnum].orient);
				}

				light_add_cone(&p_pos, &p1, cone_angle, cone_inner_angle, false, light_radius, light_radius, intensity, r, g, b, source_radius);
			}
			break;
			}
		}

		return false;
	}

	void move_all(float frametime)
	{
		TRACE_SCOPE(tracing::ParticlesMoveAll);

		if (!Particles_enabled)
			return;

		if (Persistent_particles.empty() && Particles.empty())
			return;

		for (auto p = Persistent_particles.begin(); p != Persistent_particles.end();)
		{
			ParticlePtr part = *p;
			if (move_particle(frametime, part.get()))
			{
				// if we're sitting on the very last particle, popping-back will invalidate the iterator!
				if (p + 1 == Persistent_particles.end())
				{
					Persistent_particles.pop_back();
					break;
				}

				*p = Persistent_particles.back();
				Persistent_particles.pop_back();
				continue;
			}

			// next particle
			++p;
		}

		for (auto p = Particles.begin(); p != Particles.end();)
		{
			if (move_particle(frametime, &(*p)))
			{
				// if we're sitting on the very last particle, popping-back will invalidate the iterator!
				if (p + 1 == Particles.end())
				{
					Particles.pop_back();
					break;
				}

				*p = Particles.back();
				Particles.pop_back();
				continue;
			}

			// next particle
			++p;
		}
	}

	// kill all active particles
	void kill_all()
	{
		// kill all active particles
		Particles.clear();
		Persistent_particles.clear();
	}

	/**
	 * @brief Renders a single particle
	 * @param part The particle to render
	 * @return @c true if the particle has been added to the rendering batch, @c false otherwise
	 */
	static bool render_particle(particle* part) {
		// skip back-facing particles (ripped from fullneb code)
		// Wanderer - add support for attached particles
		vec3d p_pos;
		if (part->attached_objnum >= 0)
		{
			vm_vec_unrotate(&p_pos, &part->pos, &Objects[part->attached_objnum].orient);
			vm_vec_add2(&p_pos, &Objects[part->attached_objnum].pos);
		}
		else
		{
			p_pos = part->pos;
		}

		bool part_has_length = part->length != 0.0f;

		if (!part_has_length && vm_vec_dot_to_point(&Eye_matrix.vec.fvec, &Eye_position, &p_pos) <= 0.0f)
		{
			return false;
		}
		
		const auto& source_effect = part->parent_effect.getParticleEffect();

		//For anything apart from the velocity curve, "Post-Curves Velocity" is well defined. This is needed to facilitate complex but common particle scaling and appearance curves.
		const auto& curve_input = std::forward_as_tuple(*part,
			vm_vec_mag_quick(&part->velocity) * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::VELOCITY_MULT, std::forward_as_tuple(*part, vm_vec_mag_quick(&part->velocity))));
			
		vec3d p1 = vmd_x_vector;

		if (part_has_length) {
			vm_vec_copy_normalize_safe(&p1, &part->velocity);
			if (part->attached_objnum >= 0) {
				vm_vec_unrotate(&p1, &p1, &Objects[part->attached_objnum].orient);
			}
			p1 *= part->length * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::LENGTH_MULT, curve_input);
			p1 += p_pos;

			float dot0 = vm_vec_dot_to_point(&Eye_matrix.vec.fvec, &Eye_position, &p_pos);
			float dot1 = vm_vec_dot_to_point(&Eye_matrix.vec.fvec, &Eye_position, &p1);

			if (dot0 <= 0.0f && dot1 <= 0.0f) {
				return false;
			}
		}

		// calculate the alpha to draw at
		auto alpha = get_current_alpha(&p_pos, part->radius);

		// if it's transparent then just skip it
		if (alpha <= 0.0f)
		{
			return false;
		}

		vertex pos;
		auto flags = g3_rotate_vertex(&pos, &p_pos);

		if (flags)
		{
			if (part_has_length) {
				vertex pos2;
				auto flags2 = g3_rotate_vertex(&pos2, &p1);
				if (flags & flags2) {
					return false;
				}
			} else {
				return false;
			}
		}

		g3_transfer_vertex(&pos, &p_pos);

		// figure out which frame we should be using
		int framenum;
		int cur_frame;
		if (part->nframes > 1) {
			if (source_effect.m_lifetime_curves.has_curve(ParticleEffect::ParticleLifetimeCurvesOutput::ANIM_STATE)) {
				cur_frame = fl2i(i2fl(part->nframes - 1) * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::ANIM_STATE, curve_input));
			}
			else {
				framenum = bm_get_anim_frame(part->bitmap, part->age, part->max_life, part->looping);
				cur_frame = part->reverse ? (part->nframes - framenum - 1) : framenum;
			}
		}
		else
		{
			cur_frame = 0;
		}

		framenum = part->bitmap;

		Assert( (cur_frame < part->nframes) || (part->nframes == 0 && cur_frame == 0) );

		float radius = part->radius * source_effect.m_lifetime_curves.get_output(ParticleEffect::ParticleLifetimeCurvesOutput::RADIUS_MULT, curve_input);

		if (part_has_length) {
			vec3d p0 = p_pos;
			batching_add_laser(framenum + cur_frame, &p0, radius, &p1, radius);
		}
		else {
			// it will subtract Physics_viewer_bank, so without the flag we counter that and make it screen-aligned again
			batching_add_volume_bitmap_rotated(framenum + cur_frame, &pos, part->use_angle ? part->angle : Physics_viewer_bank, radius, alpha);
		}

		return true;
	}

	void render_all()
	{
		GR_DEBUG_SCOPE("Render Particles");
		TRACE_SCOPE(tracing::ParticlesRenderAll);

		if (!Particles_enabled)
			return;

		if (Persistent_particles.empty() && Particles.empty())
			return;

		for (auto& part : Persistent_particles) {
			render_particle(part.get());
		}

		for (auto& part : Particles) {
			render_particle(&part);
		}

	}
}