Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
particles.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <vector>
8#include <unordered_map>
10#include "irr_ptr.h"
11#include "../particles.h"
12
13struct ClientEvent;
14class ParticleManager;
16struct MapNode;
17struct ContentFeatures;
18class LocalPlayer;
19class ITextureSource;
20class IGameDef;
21class Client;
22
24{
25 /* per-spawner structure used to store the ParticleTexture structs
26 * that spawned particles will refer to through ClientParticleTexRef */
28 video::ITexture *ref = nullptr;
29
32};
33
35{
36 /* per-particle structure used to avoid massively duplicating the
37 * fairly large ParticleTexture struct */
38 ParticleTexture *tex = nullptr;
39 video::ITexture *ref = nullptr;
40
42
43 /* constructor used by particles spawned from a spawner */
46
47 /* constructor used for node particles */
48 explicit ClientParticleTexRef(video::ITexture *tp): ref(tp) {};
49};
50
51class ParticleSpawner;
52class ParticleBuffer;
53
55{
56public:
58 const ParticleParameters &p,
59 const ClientParticleTexRef &texture,
60 v2f texpos,
61 v2f texsize,
62 video::SColor color,
63 ParticleSpawner *parent = nullptr,
64 std::unique_ptr<ClientParticleTexture> owned_texture = nullptr
65 );
66
67 ~Particle();
68
70
71 void step(float dtime, ClientEnvironment *env);
72
73 bool isExpired () const
74 { return m_expiration < m_time; }
75
76 ParticleSpawner *getParent() const { return m_parent; }
77
78 const ClientParticleTexRef &getTextureRef() const { return m_texture; }
79
80 ParticleBuffer *getBuffer() const { return m_buffer; }
81 bool attachToBuffer(ParticleBuffer *buffer);
82
83private:
84 video::SColor updateLight(ClientEnvironment *env);
85 void updateVertices(ClientEnvironment *env, video::SColor color);
86
88 u16 m_index; // index in m_buffer
89
90 float m_time = 0.0f;
92
93 // Color without lighting
94 video::SColor m_base_color;
95
102
104
105 float m_animation_time = 0.0f;
107
109 // Used if not spawned from a particlespawner
110 std::unique_ptr<ClientParticleTexture> m_owned_texture;
111};
112
114{
115public:
117 const ParticleSpawnerParameters &params,
118 u16 attached_id,
119 std::vector<ClientParticleTexture> &&texpool,
120 ParticleManager *p_manager);
121
122 void step(float dtime, ClientEnvironment *env);
123
124 bool getExpired() const
125 { return p.amount <= 0 && p.time != 0; }
126
127 bool hasActive() const { return m_active != 0; }
128 void decrActive() { m_active -= 1; }
129
130private:
131 void spawnParticle(ClientEnvironment *env, float radius,
132 const core::matrix4 *attached_absolute_pos_rot_matrix);
133
134 size_t m_active;
136 float m_time;
139 std::vector<ClientParticleTexture> m_texpool;
140 std::vector<float> m_spawntimes;
142};
143
144class ParticleBuffer : public scene::ISceneNode
145{
146 friend class ParticleManager;
147public:
148 ParticleBuffer(ClientEnvironment *env, const video::SMaterial &material);
149
150 // for pointer stability
152
153
155 std::optional<u16> allocate();
157 void release(u16 index);
158
160 video::S3DVertex *getVertices(u16 index);
161
162 inline bool isEmpty() const {
163 return m_free_list.size() == m_count;
164 }
165
166 virtual video::SMaterial &getMaterial(u32 num) override {
167 return m_mesh_buffer->getMaterial();
168 }
169 virtual u32 getMaterialCount() const override {
170 return 1;
171 }
172
173 virtual const core::aabbox3df &getBoundingBox() const override;
174
175 virtual void render() override;
176
177 virtual void OnRegisterSceneNode() override;
178
179 // we have 16-bit indices
180 static constexpr u16 MAX_PARTICLES_PER_BUFFER = 16000;
181
182private:
183 irr_ptr<scene::SMeshBuffer> m_mesh_buffer;
184 // unused (e.g. expired) particle indices for re-use
185 std::vector<u16> m_free_list;
186 // for automatic deletion when unused for a while. is reset on allocate().
187 float m_usage_timer = 0;
188 // total count of contained particles
189 u16 m_count = 0;
190 mutable bool m_bounding_box_dirty = true;
191};
192
197{
198 friend class ParticleSpawner;
199public:
203
204 void step (float dtime);
205
207 LocalPlayer *player);
208
209 void addDiggingParticles(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
210 const MapNode &n, const ContentFeatures &f);
211
212 void addNodeParticle(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
213 const MapNode &n, const ContentFeatures &f);
214
215 void reserveParticleSpace(size_t max_estimate);
216
225 {
227 }
228
229protected:
230 static bool getNodeParticleParams(const MapNode &n, const ContentFeatures &f,
231 ParticleParameters &p, video::ITexture **texture, v2f &texpos,
232 v2f &texsize, video::SColor *color, u8 tilenum = 0);
233
234 static video::SMaterial getMaterialForParticle(const ClientParticleTexRef &texture);
235
236 bool addParticle(std::unique_ptr<Particle> toadd);
237
238private:
239 void addParticleSpawner(u64 id, std::unique_ptr<ParticleSpawner> toadd);
240 void deleteParticleSpawner(u64 id);
241
242 void stepParticles(float dtime);
243 void stepSpawners(float dtime);
244 void stepBuffers(float dtime);
245
246 void clearAll();
247
248 std::vector<std::unique_ptr<Particle>> m_particles;
249 std::unordered_map<u64, std::unique_ptr<ParticleSpawner>> m_particle_spawners;
250 std::vector<std::unique_ptr<ParticleSpawner>> m_dying_particle_spawners;
251 std::vector<irr_ptr<ParticleBuffer>> m_particle_buffers;
252
253 // Start the particle spawner ids generated from here after u32_max.
254 // lower values are for server sent spawners.
255 u64 m_next_particle_spawner_id = static_cast<u64>(U32_MAX) + 1;
256
258
260
263};
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition clientenvironment.h:54
Definition client.h:105
Definition gamedef.h:36
Definition texturesource.h:36
Definition numeric.h:347
Definition localplayer.h:49
Definition particles.h:145
virtual void render() override
Definition particles.cpp:632
bool m_bounding_box_dirty
Definition particles.h:190
ParticleBuffer(ClientEnvironment *env, const video::SMaterial &material)
Definition particles.cpp:545
irr_ptr< scene::SMeshBuffer > m_mesh_buffer
Definition particles.h:183
u16 m_count
Definition particles.h:189
static constexpr u16 MAX_PARTICLES_PER_BUFFER
Definition particles.h:180
void release(u16 index)
Frees the particle at index
Definition particles.cpp:587
float m_usage_timer
Definition particles.h:187
virtual void OnRegisterSceneNode() override
Definition particles.cpp:604
virtual u32 getMaterialCount() const override
Definition particles.h:169
std::optional< u16 > allocate()
Reserves one more slot for a particle (4 vertices, 6 indices)
Definition particles.cpp:556
video::S3DVertex * getVertices(u16 index)
Definition particles.cpp:596
virtual video::SMaterial & getMaterial(u32 num) override
Definition particles.h:166
bool isEmpty() const
Definition particles.h:162
virtual const core::aabbox3df & getBoundingBox() const override
Definition particles.cpp:611
std::vector< u16 > m_free_list
Definition particles.h:185
Class doing particle as well as their spawners handling.
Definition particles.h:197
u64 m_next_particle_spawner_id
Definition particles.h:255
std::mutex m_particle_list_lock
Definition particles.h:261
void stepSpawners(float dtime)
Definition particles.cpp:664
static bool getNodeParticleParams(const MapNode &n, const ContentFeatures &f, ParticleParameters &p, video::ITexture **texture, v2f &texpos, v2f &texsize, video::SColor *color, u8 tilenum=0)
Definition particles.cpp:841
void clearAll()
Definition particles.cpp:744
~ParticleManager()
Definition particles.cpp:652
IntervalLimiter m_buffer_gc
Definition particles.h:259
void addParticleSpawner(u64 id, std::unique_ptr< ParticleSpawner > toadd)
Definition particles.cpp:1040
void stepParticles(float dtime)
Definition particles.cpp:693
void addNodeParticle(IGameDef *gamedef, LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
Definition particles.cpp:898
static video::SMaterial getMaterialForParticle(const ClientParticleTexRef &texture)
Definition particles.cpp:943
void step(float dtime)
Definition particles.cpp:657
std::vector< irr_ptr< ParticleBuffer > > m_particle_buffers
Definition particles.h:251
void reserveParticleSpace(size_t max_estimate)
Definition particles.cpp:936
u64 generateSpawnerId()
This function is only used by client particle spawners.
Definition particles.h:224
void deleteParticleSpawner(u64 id)
Definition particles.cpp:1054
void addDiggingParticles(IGameDef *gamedef, LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
Definition particles.cpp:883
void handleParticleEvent(ClientEvent *event, Client *client, LocalPlayer *player)
Definition particles.cpp:760
std::vector< std::unique_ptr< ParticleSpawner > > m_dying_particle_spawners
Definition particles.h:250
bool addParticle(std::unique_ptr< Particle > toadd)
Definition particles.cpp:1003
ParticleManager(ClientEnvironment *env)
Definition particles.cpp:648
void stepBuffers(float dtime)
Definition particles.cpp:715
std::unordered_map< u64, std::unique_ptr< ParticleSpawner > > m_particle_spawners
Definition particles.h:249
std::vector< std::unique_ptr< Particle > > m_particles
Definition particles.h:248
ClientEnvironment * m_env
Definition particles.h:257
std::mutex m_spawner_list_lock
Definition particles.h:262
Definition particles.h:114
ParticleSpawnerParameters p
Definition particles.h:138
float m_time
Definition particles.h:136
size_t m_active
Definition particles.h:134
std::vector< float > m_spawntimes
Definition particles.h:140
void decrActive()
Definition particles.h:128
bool hasActive() const
Definition particles.h:127
ParticleSpawner(LocalPlayer *player, const ParticleSpawnerParameters &params, u16 attached_id, std::vector< ClientParticleTexture > &&texpool, ParticleManager *p_manager)
Definition particles.cpp:249
std::vector< ClientParticleTexture > m_texpool
Definition particles.h:139
void step(float dtime, ClientEnvironment *env)
Definition particles.cpp:494
u16 m_attached_id
Definition particles.h:141
void spawnParticle(ClientEnvironment *env, float radius, const core::matrix4 *attached_absolute_pos_rot_matrix)
Definition particles.cpp:290
LocalPlayer * m_player
Definition particles.h:137
bool getExpired() const
Definition particles.h:124
ParticleManager * m_particlemanager
Definition particles.h:135
Definition particles.h:55
v3f m_velocity
Definition particles.h:100
void updateVertices(ClientEnvironment *env, video::SColor color)
Definition particles.cpp:181
ParticleSpawner * getParent() const
Definition particles.h:76
Particle(const ParticleParameters &p, const ClientParticleTexRef &texture, v2f texpos, v2f texsize, video::SColor color, ParticleSpawner *parent=nullptr, std::unique_ptr< ClientParticleTexture > owned_texture=nullptr)
Definition particles.cpp:36
u16 m_index
Definition particles.h:88
ParticleBuffer * m_buffer
Definition particles.h:87
v3f m_pos
Definition particles.h:99
float m_animation_time
Definition particles.h:105
const ParticleParameters m_p
Definition particles.h:103
bool attachToBuffer(ParticleBuffer *buffer)
Definition particles.cpp:68
v2f m_texpos
Definition particles.h:97
ParticleSpawner * m_parent
Definition particles.h:108
float m_time
Definition particles.h:90
const ClientParticleTexRef & getTextureRef() const
Definition particles.h:78
video::SColor updateLight(ClientEnvironment *env)
Definition particles.cpp:157
v3f m_acceleration
Definition particles.h:101
float m_expiration
Definition particles.h:91
v2f m_texsize
Definition particles.h:98
video::SColor m_base_color
Definition particles.h:94
~Particle()
Definition particles.cpp:62
int m_animation_frame
Definition particles.h:106
ClientParticleTexRef m_texture
Definition particles.h:96
void step(float dtime, ClientEnvironment *env)
Definition particles.cpp:79
bool isExpired() const
Definition particles.h:73
ParticleBuffer * getBuffer() const
Definition particles.h:80
std::unique_ptr< ClientParticleTexture > m_owned_texture
Definition particles.h:110
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
#define U32_MAX
Definition irrlichttypes.h:29
Definition activeobjectmgr.cpp:11
Definition clientevent.h:67
Definition particles.h:35
ClientParticleTexRef(ClientParticleTexture &t)
Definition particles.h:44
ClientParticleTexRef(video::ITexture *tp)
Definition particles.h:48
ClientParticleTexRef()=default
video::ITexture * ref
Definition particles.h:39
ParticleTexture * tex
Definition particles.h:38
Definition particles.h:24
ClientParticleTexture()=default
ParticleTexture tex
Definition particles.h:27
video::ITexture * ref
Definition particles.h:28
Definition nodedef.h:295
Definition mapnode.h:124
Definition particles.h:303
Definition particles.h:314
u16 amount
Definition particles.h:315
f32 time
Definition particles.h:316
Definition particles.h:253
Definition particles.h:262
static std::string p(std::string path)
Definition test_filesys.cpp:53