openTRI 0.1
triParticle.h
1/*
2 * triParticle.h: Header for particle engine
3 * This file is part of the "tri Engine".
4 *
5 * Copyright (C) 2007 tri
6 * Copyright (C) 2007 Alexander Berl 'Raphael' <raphael@fx-world.org>
7 *
8 * $Id: $
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25
26#ifndef __TRIPARTICLE_H__
27#define __TRIPARTICLE_H__
28
29#include <pspgu.h>
30#include "triTypes.h"
31
32
33#define ALIGN16 __attribute__((aligned(16)))
34
39typedef struct triParticle
40{
41 triVec4f pos; // (x,y,z,rotation) - rotation only applicable in GU_TRIANGLES mode
42 triVec4f vel; // (x,y,z,rotation) - rotation speed in degree/second
43
44 triColor4f col;
45 triFloat rand; // randomness factor of this particle (to randomize different emitter parameters)
46 triFloat size; // size of the particle, only applicable in GU_SPRITES and GU_TRIANGLES mode
47
48 triFloat age;
49 triFloat lifetime;
50 //triFloat pad16; // padding to 16byte alignment
51} ALIGN16 triParticle; // struct size: 16*4 bytes = 64 bytes
52
53
54#define TRI_VORTEX_RANGE (3.0f)
55typedef struct triVortex
56{
57 triVec4f pos;
58 triVec4f vel;
59
60 triFloat confinement; // vorticity confinement value (how far the vortex ranges)
61 triFloat dir; // vortex rotation direction (+-) and speed
62
63 triFloat age;
64 triFloat lifetime;
65} ALIGN16 triVortex; // struct size: 16*4 bytes = 48 bytes
66
67
68enum triParticleEmitterTypes
69{
70 TRI_EMITTER_MANUAL = 0,
71 TRI_EMITTER_FIRE,
72 TRI_EMITTER_EXPLOSION,
73 TRI_EMITTER_SPRINKLE,
74 TRI_EMITTER_WATERFALL,
75 TRI_EMITTER_SMOKE,
76 TRI_EMITTER_NUM_TYPES
77};
78
79/*
80 * General usage of values with randomness:
81 * result = value + (-1.0,..,1.0) * valueRand
82 *
83 * To generate values in range X - Y, set as follows:
84 * valueRand = (Y-X) / 2.0
85 * value = X + valueRand
86 */
87typedef struct triParticleEmitter
88{
89 triVec4f pos; // position of emitter and initial position of all emitted particles (x,y,z,rotation)
90 triVec4f posRand; // length in all directions of random position (0 for no random placement) (x,y,z,rotation)
91 triVec4f lastpos; // internal: last position of emitter (for moving particles with emitter)
92
93 triVec4f vel; // initial velocity of all emitted particles (x,y,z,rotation)
94 triVec4f velRand; // randomness of particle velocity (x,y,z,rotation)
95
96 triVec4f gravity; // Gravital force on emitter particles
97
98 triVec4f wind; // wind direction vector
99 triVec4f windRand; // wind randomness
100
101 triColor4f cols[8]; // Color shades during lifetime of particle, max 8 currently
102 triS32 numCols; // Number of color fades
103
104 triFloat size; // Mean size of particles
105 triFloat sizeRand; // Random size (0 means no randomness)
106
107 triFloat burnout; // Burnout of the emitter with age. 1.0 means the emitters rate gradually turns towards 0 (and particles life towards ~20%) with age, 0 means no burnout
108
109 triFloat friction; // air friction to damp particle velocity, 0 = no friction, 1.0 = stop immediately (same as vel = 0)
110
111 triFloat growth; // Amount to grow particles (size/second) - Size after end of life = size + rand()*sizeRand + growth*life
112
113 triFloat glitter; // Amount of glitter on particles (sinusform brightening) - 0 means no glitter, 1.0 means glitter in full intensity range
114 triFloat glitterSpeed; // Speed of glitter (number of wavelengths inside particles age)
115
116 triFloat life; // Lifetime of particles to be created (lifeRand is 0.2 by default, ie 20%)
117 triFloat lifeRand; // Lifetime of particles randomness
118
119 triFloat binding; // binding of particles to emitter, 0 = no binding, 1.0 = particles move with emitter
120 triFloat loosen; // loosening of particles with age, 0 = no loosening, 1.0 = particles move completely free at end of life
121
122 triS32 hTexFrames; // number of horizontal texture frames (texture animation)
123 triS32 vTexFrames; // number of vertical texture frames (texture animation)
124 triS32 nTexLoops; // number of loops to do per particle Lifetime (0 means no texture animation at all)
125 triS32 fixedTexRate; // fixed texture animation frame rate in frames/second (0 if rate dependent on life - use nTexLoops*vTexFrames*hTexFrames/life)
126
127 triS32 min; // minimum number of particles at same time
128 triS32 max; // maximum number of particles at same time
129 triS32 minVortex; // minimum number of vortex particles at same time
130 triS32 maxVortex; // maximum number of vortex particles at same time
131
132 triFloat vortexRange; // The squared range of the vortices influence (+- 20%)
133 triFloat vortexDir; // Vortex direction (and speed)
134 triFloat vortexDirRand; // Vortex direction randomness
135
136 triS32 rate; // emission rate (particles/second) - rate*lifetime is amount of emitted particles in total
137 triS32 rateVortex; // vortex emission rate (vortices/second)
138
139 triFloat lifetime; // lifetime of emitter in seconds, 0 if eternal (fueled flame)
140 triFloat age; // internal: age of the emitter, if age > lifetime it will die unless lifetime = 0
141
142 triFloat lastemission; // internal: time of last emission (for proper emittance rate)
143 triS32 emitted; // internal: particles emitted since last emission
144 triS32 emittedVortex; // internal: vortices emitted since last emission
145
146 triFloat padding[3];
147} ALIGN16 triParticleEmitter;
148
149
150
151typedef struct triBlendMode
152{
153 triS32 op;
154 triS32 src_op;
155 triS32 dst_op;
156 triU32 src_fix;
157 triU32 dst_fix;
159
160
161extern triBlendMode TRI_BLEND_MODE_ALPHA /*= { GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0 }*/;
162extern triBlendMode TRI_BLEND_MODE_ADD /*= { GU_ADD, GU_FIX, GU_FIX, 0xFFFFFFFF, 0xFFFFFFFF }*/;
163extern triBlendMode TRI_BLEND_MODE_GLENZ /*= { GU_ADD, GU_FIX, GU_FIX, 0x7F7F7F7F, 0x7F7F7F7F }*/;
164extern triBlendMode TRI_BLEND_MODE_ALPHA_ADD /*= { GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, 0xFFFFFFFF }*/;
165extern triBlendMode TRI_BLEND_MODE_SUB /* = { GU_SUBTRACT, GU_FIX, GU_FIX, 0xFFFFFFFF, 0xFFFFFFFF }*/;
166extern triBlendMode TRI_BLEND_MODE_ALPHA_SUB /* = { GU_SUBTRACT, GU_SRC_ALPHA, GU_FIX, 0, 0xFFFFFFFF }*/;
167
168
169enum triParticleActions {
170 triApplyForce, // apply external force (gravity or similar)
171 triInternalGravity, // calculate internal gravity and move particles accordingly
172 triInternalCollide, // collide particles against each other
173 triEmitterBound, // bind particles to the emitter - ie move them along with it based on age of particle
174 triCollide, // collide particles against exterior mesh
175 triDie, // let particles die (always applied)
176 triNumActions
177 };
178
179
181
187
188
190{
191 triS32 ID; // Particle system ID
192 triS32 typeID; // Particle system type ID - see triParticleEmitterTypes
193
194 triParticleEmitter *emitter; // The emitter attached to this system
195
196 triS32 textureID; // Texture to use for this particle system
197 triS32 texMode; // one of GU_TFX_*
198 triU32 texColor; // sceGuTexEnvColor
199 triBlendMode blendMode;
200 triS32 renderMode; // GU_POINTS, GU_LINES, GU_SPRITES, GU_TRIANGLES
201
202 triS32 useBillboards; // make particles always point towards camera, only applicable in GU_TRIANGLES_MODE
203
204 triU32 actions[triNumActions]; // actions to apply every frame
205
206
207 triParticle* particles; // particle list for update/movement
208 triVortex* vortices; // vortex particle list
209 triS32* particleStack;
210 triS32* vorticesStack;
211 triS32 numParticles;
212 triS32 numVortices;
213
214 triS32 numVerticesPerParticle; // numbers to allocate the right amount of memory
215
216 void* vertices[2]; // particle vertice list for rendering (created during update)
217 triS32 numVertices;
218 triS32 vertexFormat;
219 triS32 vindex;
220
221 triVec3f boundingBox[8];
222 triS32 updateFlag;
223
224
225 triParticleRenderer render; // function pointer to custom particle rendering function
226 struct triParticleSystem* next;
227};
228
229
230
231typedef struct triParticleManager
232{
233 triFloat dt;
234 triParticleSystem* systems;
235 triS32 numSystems;
236
237 triS32 idCounter;
238
239 triU32 numParticles;
240 triU32 numVertices;
242
243
244
245void triParticleSystemConstructor( triParticleSystem* s );
246void triParticleSystemFree( triParticleSystem* s );
247triS32 triParticleSystemRender( triParticleSystem* s );
248void triParticleSystemInitialize( triParticleSystem* s, triParticleEmitter* e );
249void triParticleSystemUpdate( triParticleSystem* s, triCamera* cam, triFloat dt );
250
251
252triS32 triParticleVertexUVCListCreate( triParticleSystem* s, triCamera* cam );
253triS32 triParticleVertexCListCreate( triParticleSystem* s, triCamera* cam );
254
255
256void triParticleEmitterConstructor( triParticleEmitter *e, triS32 emitterType );
257
258
259void triParticleManagerUpdate( triCamera* cam, triFloat dt );
260void triParticleManagerRender();
261void triParticleManagerUpdateRender( triCamera* cam, triFloat dt );
262void triParticleManagerRemove( triS32 id );
263triParticleSystem* triParticleManagerGet( triS32 id );
264void triParticleManagerDestroy();
265triS32 triParticleManagerAdd( triParticleSystem* p, triParticleEmitter* e );
266triS32 triParticleManagerLoadScript( triChar* name );
267
270#endif // __TRIPARTICLE_H__
void(* triParticleRenderer)(triParticleSystem *s, triParticle *p)
Custom particle render callback.
Definition triParticle.h:186
Definition triParticle.h:152
Definition triCamera.h:39
RGBA float color (128bit)
Definition triTypes.h:319
Definition triParticle.h:88
Definition triParticle.h:232
Definition triParticle.h:190
Definition triParticle.h:40
3D float Vector
Definition triTypes.h:156
4D float Vector (quaternion)
Definition triTypes.h:228
Definition triParticle.h:56