Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_PARTICLEEMITTER 19 : #define INCLUDED_PARTICLEEMITTER 20 : 21 : #include "graphics/ModelAbstract.h" 22 : #include "graphics/ParticleEmitterType.h" 23 : #include "graphics/SColor.h" 24 : #include "lib/posix/posix_types.h" 25 : #include "maths/BoundingBoxAligned.h" 26 : #include "maths/Quaternion.h" 27 : #include "maths/Vector3D.h" 28 : #include "renderer/VertexArray.h" 29 : 30 : #include <cstddef> 31 : #include <map> 32 : #include <memory> 33 : #include <string> 34 : #include <vector> 35 : 36 : class CParticleEmitter; 37 : namespace Renderer::Backend { class IDeviceCommandContext; } 38 : namespace Renderer::Backend { class IShaderProgram; } 39 : namespace Renderer::Backend { class IVertexInputLayout; } 40 : 41 : /** 42 : * Simulation state for a single particle. 43 : */ 44 0 : struct SParticle 45 : { 46 : CVector3D pos; 47 : CVector3D velocity; 48 : float angle; 49 : float angleSpeed; 50 : float size; 51 : float sizeGrowthRate; 52 : SColor4ub color; 53 : float age; 54 : float maxAge; 55 : CVector3D axisX, axisY; 56 : }; 57 : 58 : /** 59 : * Particle emitter. 60 : * 61 : * Emitters store particle data in two forms: 62 : * * m_Particles contains the raw data used for the CPU particle simulation. 63 : * * m_VertexArray contains the data required for rendering. 64 : * Particles are rendered as billboard quads, so the vertex array contains four vertices 65 : * per particle with different UV coordinates. The billboard position computation is 66 : * performed by a vertex shader. 67 : * 68 : * The number of particles is a constant for the entire life of the emitter, 69 : * to simplify the updating and rendering. 70 : * m_Particles acts like a ring buffer, so we don't have to worry about dynamically 71 : * allocating particles. If particles have variable lifetimes, they'll exist in the 72 : * array with alpha=0 until they're overwritten by a new particle after the maximum 73 : * lifetime. 74 : * 75 : * (It's quite likely this could be made more efficient, if the overhead of any added 76 : * complexity is not high.) 77 : */ 78 : class CParticleEmitter 79 : { 80 : public: 81 : CParticleEmitter(const CParticleEmitterType& type); 82 : 83 : /** 84 : * Set the position to be used for emission of new particles. 85 : */ 86 0 : void SetPosition(const CVector3D& pos) 87 : { 88 0 : m_Pos = pos; 89 : } 90 : 91 0 : CVector3D GetPosition() const 92 : { 93 0 : return m_Pos; 94 : } 95 : 96 : /** 97 : * Set the rotation to be used for emission of new particles (note: depends on particles). 98 : */ 99 0 : void SetRotation(const CQuaternion& rot) 100 : { 101 0 : m_Rot = rot; 102 : } 103 : 104 0 : const CQuaternion& GetRotation() const 105 : { 106 0 : return m_Rot; 107 : } 108 : 109 : /** 110 : * Get the bounding box of the center points of particles at their current positions. 111 : */ 112 0 : const CBoundingBoxAligned& GetParticleBounds() const { return m_ParticleBounds; } 113 : 114 : /** 115 : * Push a new particle onto the ring buffer. (May overwrite an old particle.) 116 : */ 117 : void AddParticle(const SParticle& particle); 118 : 119 : /** 120 : * Update particle and vertex array data. Must be called before RenderArray. 121 : * 122 : * If frameNumber is the same as the previous call to UpdateArrayData, 123 : * then the function will do no work and return immediately. 124 : */ 125 : void UpdateArrayData(int frameNumber); 126 : 127 : /** 128 : * Make the vertex data available for subsequent binding and rendering. 129 : */ 130 : void PrepareForRendering(); 131 : 132 : /** 133 : * Upload the vertex data to the backend. 134 : */ 135 : void UploadData( 136 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext); 137 : 138 : /** 139 : * Bind rendering state (textures and blend modes). 140 : */ 141 : void Bind( 142 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 143 : Renderer::Backend::IShaderProgram* shader); 144 : 145 : /** 146 : * Draw the vertex array. 147 : */ 148 : void RenderArray( 149 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext); 150 : 151 : void SetEntityVariable(const std::string& name, float value); 152 : 153 : const CParticleEmitterType& m_Type; 154 : 155 : /// Whether this emitter is still emitting new particles 156 : bool m_Active{true}; 157 : 158 : CVector3D m_Pos; 159 : CQuaternion m_Rot; 160 : 161 : std::map<std::string, float> m_EntityVariables; 162 : 163 : std::vector<SParticle> m_Particles; 164 : uint32_t m_NextParticleIdx{0}; 165 : uint32_t m_NumberOfVisibleParticles{0}; 166 : 167 : float m_LastUpdateTime; 168 : float m_EmissionRoundingError{0.0f}; 169 : 170 : private: 171 : /// Bounding box of the current particle center points 172 : CBoundingBoxAligned m_ParticleBounds; 173 : 174 : VertexIndexArray m_IndexArray; 175 : 176 : VertexArray m_VertexArray; 177 : VertexArray::Attribute m_AttributePos; 178 : VertexArray::Attribute m_AttributeColor; 179 : VertexArray::Attribute m_AttributeAxisX, m_AttributeAxisY; 180 : VertexArray m_VertexUVArray; 181 : VertexArray::Attribute m_AttributeUV; 182 : 183 : Renderer::Backend::IVertexInputLayout* m_VertexInputLayout = nullptr; 184 : 185 : int m_LastFrameNumber; 186 : bool m_UseInstancing{false}; 187 : }; 188 : 189 : /** 190 : * Particle emitter model, for attaching emitters as props on other models. 191 : */ 192 : class CModelParticleEmitter : public CModelAbstract 193 : { 194 : public: 195 : CModelParticleEmitter(const CParticleEmitterType& type); 196 : ~CModelParticleEmitter() override; 197 : 198 : /// Dynamic cast 199 0 : CModelParticleEmitter* ToCModelParticleEmitter() override 200 : { 201 0 : return this; 202 : } 203 : 204 : std::unique_ptr<CModelAbstract> Clone() const override; 205 : 206 0 : void SetTerrainDirty(ssize_t /*i0*/, ssize_t /*j0*/, ssize_t /*i1*/, ssize_t /*j1*/) override 207 : { 208 0 : } 209 : 210 : void SetEntityVariable(const std::string& name, float value) override; 211 : 212 : void CalcBounds() override; 213 : void ValidatePosition() override; 214 : void InvalidatePosition() override; 215 : void SetTransform(const CMatrix3D& transform) override; 216 : 217 : const CParticleEmitterType& m_Type; 218 : std::unique_ptr<CParticleEmitter> m_Emitter; 219 : }; 220 : 221 : #endif // INCLUDED_PARTICLEEMITTER