OpenBF1942-Plus
Refractor 1, rebuilt from scratch
Loading...
Searching...
No Matches
effects.h
Go to the documentation of this file.
1#pragma once
2// Effects: EffectBundle -> Emitter -> SpriteParticle templates (objects/effects/*/effects.con), simulated on the CPU.
3// Readings of the fields are unverified (docs/formats/effects.md). No GL here: the renderer draws sprites().
4
5#include <array>
6#include <map>
7#include <memory>
8#include <random>
9#include <string>
10#include <vector>
11
12#include "con.h"
13#include "mat.h"
14
15namespace obf {
16
17// CRD_<kind>/a/b/sym: NONE a; UNIFORM between a and b (sym 1: either sign); NORMAL / EXPONENTIAL read as UNIFORM.
18struct Crd {
19 float a = 0, b = 0;
20 bool uniform = false, sym = false;
21 float sample(std::mt19937& rng) const;
22};
23Crd crd_parse(const std::string& s);
24// "t/v|t/v|...": t in percent of the particle's life; at(): linear between keys, 1 with no keys.
25struct Curve {
26 std::vector<std::pair<float, float>> keys;
27 float at(float t01) const;
28};
29Curve curve_parse(const std::string& s);
30
32 std::string texture; // texture/<name>
33 std::string mesh; // a Particle's geometry: standardmesh/<file>.sm (drawn as a mesh, not a sprite)
35 Curve size_over, gravity_t, rotation_speed_t, alpha_t; // multipliers over the life
36 std::vector<std::array<float, 5>> color; // colorRGBAOverTime: percent, r, g, b, a (0-255)
37 bool additive = false; // destBlendMode BMOne
38 int frames = 1; // numAnimationFrames: a square grid of frames in the texture
39};
40
41struct EmitterDef {
42 std::shared_ptr<const ParticleDef> particle;
43 Mat4 local = Mat4::identity(); // the bundle's setPosition / setRotation for it
44 Crd ttl, intensity, delay; // seconds active, particles per second, wait before starting
45 Crd pos[3], speed[3]; // relativePositionIn / positionalSpeedIn Right, Up, Dof
46 bool looping = false, add_speed = false;
47 float speed_scale = 1;
48 bool first = false, third = false; // showInFirstPerson / showInThirdPerson
49 float at_speed = 0; // IntensityAtSpeed: full intensity from this carrier speed (m/s), less below; 0: always
50 bool to_water = false; // moveToWaterSurface: particles start at the water level
51};
52
53struct EffectDef {
54 std::vector<EmitterDef> emitters;
55 float ttl = -1; // the bundle's timeToLive; -1 until stopped
56 std::vector<int> materials; // addWorkOnMaterial: only over these (1: water); empty: anywhere
57};
58
59struct Sprite {
60 float pos[3], size, ratio, rotation, rgba[4]; // size: edge in metres; rotation: degrees; rgba 0-1
61 const std::string* texture;
64};
65
67 Mat4 world; // the emitter's frame turned by the particle's rotation, scaled by its size
68 const std::string* mesh; // standardmesh/<file>.sm
69 float rgba[4];
70};
71
72class Effects {
73public:
74 explicit Effects(const Con& con) : con_(con) {}
75 // An effect at a transform (its z axis is the emitters' "Dof", y "Up", x "Right"); vel: the carrier's velocity.
76 // Returns an id, or -1 for a template with nothing to draw.
77 // view 3: the world's emitters (first-person-only ones left out); 1: the first-person view's (with no flag or
78 // showInFirstPerson).
79 int spawn(const std::string& name, const Mat4& at, const float vel[3] = nullptr, int view = 3);
80 void move(int id, const Mat4& at, const float vel[3] = nullptr); // an attached effect follows its carrier
81 bool alive(int id) const { return live_.count(id) > 0; }
82 const EffectDef* lookup(const std::string& name) { return name.empty() ? nullptr : def(name); }
83 float water_level = -1e9f;
84 void stop(int id); // emitters end; particles live out their time
85 void step(float dt);
86 std::vector<Sprite> sprites() const;
87 std::vector<MeshParticle> meshes() const;
88 size_t particles() const;
89 size_t running() const { return live_.size(); }
90
91private:
92 struct Particle {
93 float pos[3], vel[3], age, ttl, size, gravity, drag, rot, rot_speed, frame, anim_speed, ratio;
94 float axes[9]; // mesh particles: the emitter's x, y, z axes when emitted
95 };
96 struct EmitterRun {
97 const EmitterDef* def;
98 float ttl, delay, acc = 1; // acc: particles owed; the first leaves at once (a muzzle flash cannot wait)
99 std::vector<Particle> ps;
100 };
101 struct Run {
102 std::shared_ptr<const EffectDef> def;
103 Mat4 at;
104 float vel[3] = {}, age = 0;
105 bool stopped = false;
106 std::vector<EmitterRun> emitters;
107 };
108 const EffectDef* def(const std::string& name);
109 std::shared_ptr<const ParticleDef> particle(const std::string& name);
110 const Con& con_;
111 std::map<std::string, std::shared_ptr<const EffectDef>> defs_;
112 std::map<std::string, std::shared_ptr<const ParticleDef>> particles_;
113 std::map<int, Run> live_;
114 int next_ = 0;
115 std::mt19937 rng_{1942};
116};
117
118} // namespace obf
Definition con.h:21
std::vector< MeshParticle > meshes() const
Definition effects.cpp:271
size_t particles() const
Definition effects.cpp:301
void stop(int id)
Definition effects.cpp:187
void step(float dt)
Definition effects.cpp:191
int spawn(const std::string &name, const Mat4 &at, const float vel[3]=nullptr, int view=3)
Definition effects.cpp:166
float water_level
Definition effects.h:83
bool alive(int id) const
Definition effects.h:81
const EffectDef * lookup(const std::string &name)
Definition effects.h:82
std::vector< Sprite > sprites() const
Definition effects.cpp:238
size_t running() const
Definition effects.h:89
void move(int id, const Mat4 &at, const float vel[3]=nullptr)
Definition effects.cpp:180
Effects(const Con &con)
Definition effects.h:74
Definition con.py:1
Definition anim.cpp:7
Mat4 view(float x, float y, float z, float yaw, float pitch)
Definition mat.h:62
Curve curve_parse(const std::string &s)
Definition effects.cpp:52
Crd crd_parse(const std::string &s)
Definition effects.cpp:36
Definition effects.h:18
float sample(std::mt19937 &rng) const
Definition effects.cpp:45
float a
Definition effects.h:19
float b
Definition effects.h:19
bool sym
Definition effects.h:20
bool uniform
Definition effects.h:20
Definition effects.h:25
float at(float t01) const
Definition effects.cpp:60
std::vector< std::pair< float, float > > keys
Definition effects.h:26
Definition effects.h:53
std::vector< int > materials
Definition effects.h:56
float ttl
Definition effects.h:55
std::vector< EmitterDef > emitters
Definition effects.h:54
Definition effects.h:41
bool add_speed
Definition effects.h:46
bool third
Definition effects.h:48
float at_speed
Definition effects.h:49
bool to_water
Definition effects.h:50
Crd intensity
Definition effects.h:44
bool looping
Definition effects.h:46
Crd pos[3]
Definition effects.h:45
Crd delay
Definition effects.h:44
Crd ttl
Definition effects.h:44
Crd speed[3]
Definition effects.h:45
float speed_scale
Definition effects.h:47
std::shared_ptr< const ParticleDef > particle
Definition effects.h:42
Mat4 local
Definition effects.h:43
bool first
Definition effects.h:48
Definition mat.h:8
static Mat4 identity()
Definition mat.h:10
Definition effects.h:66
const std::string * mesh
Definition effects.h:68
Mat4 world
Definition effects.h:67
float rgba[4]
Definition effects.h:69
Definition effects.h:31
Crd anim_speed
Definition effects.h:34
std::string texture
Definition effects.h:32
Curve size_over
Definition effects.h:35
Curve rotation_speed_t
Definition effects.h:35
Crd size
Definition effects.h:34
Crd ratio
Definition effects.h:34
Crd frame
Definition effects.h:34
std::string mesh
Definition effects.h:33
Curve alpha_t
Definition effects.h:35
std::vector< std::array< float, 5 > > color
Definition effects.h:36
int frames
Definition effects.h:38
Crd rotation_speed
Definition effects.h:34
Crd rotation
Definition effects.h:34
Crd gravity
Definition effects.h:34
bool additive
Definition effects.h:37
Curve gravity_t
Definition effects.h:35
Crd ttl
Definition effects.h:34
Crd drag
Definition effects.h:34
Definition effects.h:59
bool additive
Definition effects.h:63
float size
Definition effects.h:60
const std::string * texture
Definition effects.h:61
float pos[3]
Definition effects.h:60
int frame
Definition effects.h:62
float rotation
Definition effects.h:60
float ratio
Definition effects.h:60
int frames
Definition effects.h:62
float rgba[4]
Definition effects.h:60