libpsys

view src/psys.c @ 20:0a53b22f7caf

the billboarding was wrong ...
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 09:38:48 +0300
parents fbccc3c0d43e
children
line source
1 #include <stdlib.h>
2 #include <math.h>
3 #include <assert.h>
4 #include <pthread.h>
5 #include "psys.h"
6 #include "psys_gl.h"
8 static int spawn(struct psys_emitter *em, struct psys_particle *p, void *cls);
9 static void update_particle(struct psys_emitter *em, struct psys_particle *p, float tm, float dt, void *cls);
11 /* particle pool */
12 static struct psys_particle *ppool;
13 static int ppool_size;
14 static pthread_mutex_t pool_lock = PTHREAD_MUTEX_INITIALIZER;
16 static struct psys_particle *palloc(void);
17 static void pfree(struct psys_particle *p);
19 /* --- constructors and shit --- */
21 struct psys_emitter *psys_create(void)
22 {
23 struct psys_emitter *em;
25 if(!(em = malloc(sizeof *em))) {
26 return 0;
27 }
28 if(psys_init(em) == -1) {
29 free(em);
30 return 0;
31 }
32 return em;
33 }
35 void psys_free(struct psys_emitter *em)
36 {
37 psys_destroy(em);
38 free(em);
39 }
41 int psys_init(struct psys_emitter *em)
42 {
43 memset(em, 0, sizeof *em);
45 if(anm_init_node(&em->prs) == -1) {
46 return -1;
47 }
48 if(psys_init_attr(&em->attr) == -1) {
49 anm_destroy_node(&em->prs);
50 return -1;
51 }
53 em->spawn = spawn;
54 em->update = update_particle;
56 em->draw = psys_gl_draw;
57 em->draw_start = psys_gl_draw_start;
58 em->draw_end = psys_gl_draw_end;
59 return 0;
60 }
62 void psys_destroy(struct psys_emitter *em)
63 {
64 struct psys_particle *part;
66 part = em->plist;
67 while(part) {
68 struct psys_particle *tmp = part;
69 part = part->next;
70 pfree(tmp);
71 }
73 psys_destroy_attr(&em->attr);
74 }
76 void psys_set_pos(struct psys_emitter *em, vec3_t pos, float tm)
77 {
78 anm_set_position(&em->prs, pos, ANM_SEC2TM(tm));
79 }
81 void psys_set_rot(struct psys_emitter *em, quat_t rot, float tm)
82 {
83 anm_set_rotation(&em->prs, rot, ANM_SEC2TM(tm));
84 }
86 void psys_set_pivot(struct psys_emitter *em, vec3_t pivot)
87 {
88 anm_set_pivot(&em->prs, pivot);
89 }
91 vec3_t psys_get_pos(struct psys_emitter *em, float tm)
92 {
93 return anm_get_node_position(&em->prs, ANM_SEC2TM(tm));
94 }
96 quat_t psys_get_rot(struct psys_emitter *em, float tm)
97 {
98 return anm_get_node_rotation(&em->prs, ANM_SEC2TM(tm));
99 }
101 vec3_t psys_get_pivot(struct psys_emitter *em)
102 {
103 return anm_get_pivot(&em->prs);
104 }
106 void psys_clear_collision_planes(struct psys_emitter *em)
107 {
108 struct psys_plane *plane;
110 plane = em->planes;
111 while(plane) {
112 struct psys_plane *tmp = plane;
113 plane = plane->next;
114 free(tmp);
115 }
116 }
118 int psys_add_collision_plane(struct psys_emitter *em, plane_t plane, float elast)
119 {
120 struct psys_plane *node;
122 if(!(node = malloc(sizeof *node))) {
123 return -1;
124 }
125 node->p = plane;
126 node->elasticity = elast;
127 node->next = em->planes;
128 em->planes = node;
129 return 0;
130 }
132 void psys_add_particle(struct psys_emitter *em, struct psys_particle *p)
133 {
134 p->next = em->plist;
135 em->plist = p;
137 em->pcount++;
138 }
140 void psys_spawn_func(struct psys_emitter *em, psys_spawn_func_t func, void *cls)
141 {
142 em->spawn = func;
143 em->spawn_cls = cls;
144 }
146 void psys_update_func(struct psys_emitter *em, psys_update_func_t func, void *cls)
147 {
148 em->update = func;
149 em->upd_cls = cls;
150 }
152 void psys_draw_func(struct psys_emitter *em, psys_draw_func_t draw,
153 psys_draw_start_func_t start, psys_draw_end_func_t end, void *cls)
154 {
155 em->draw = draw;
156 em->draw_start = start;
157 em->draw_end = end;
158 em->draw_cls = cls;
159 }
161 /* --- update and render --- */
163 void psys_update(struct psys_emitter *em, float tm)
164 {
165 float dt, spawn_dt, spawn_tm;
166 int i, spawn_count;
167 struct psys_particle *p, pdummy;
168 anm_time_t atm = ANM_SEC2TM(tm);
170 assert(em->spawn && em->update);
172 dt = tm - em->last_update;
173 if(dt <= 0.0) {
174 return;
175 }
177 psys_eval_attr(&em->attr, atm);
179 /* how many particles to spawn for this interval ? */
180 em->spawn_acc += psys_get_cur_value(&em->attr.rate) * dt;
181 if(em->spawn_acc >= 1.0) {
182 spawn_count = em->spawn_acc;
183 em->spawn_acc = fmod(em->spawn_acc, 1.0);
184 } else {
185 spawn_count = 0;
186 }
188 spawn_dt = dt / (float)spawn_count;
189 spawn_tm = em->last_update;
190 for(i=0; i<spawn_count; i++) {
191 if(em->attr.max_particles >= 0 && em->pcount >= em->attr.max_particles) {
192 break;
193 }
195 /* update emitter position for this spawning */
196 em->cur_pos = anm_get_position(&em->prs, ANM_SEC2TM(spawn_tm));
198 if(!(p = palloc())) {
199 return;
200 }
201 if(em->spawn(em, p, em->spawn_cls) == -1) {
202 pfree(p);
203 }
204 spawn_tm += spawn_dt;
205 }
207 /* update all particles */
208 p = em->plist;
209 while(p) {
210 em->update(em, p, tm, dt, em->upd_cls);
211 p = p->next;
212 }
214 /* cleanup dead particles */
215 pdummy.next = em->plist;
216 p = &pdummy;
217 while(p->next) {
218 if(p->next->life <= 0) {
219 struct psys_particle *tmp = p->next;
220 p->next = p->next->next;
221 pfree(tmp);
222 em->pcount--;
223 } else {
224 p = p->next;
225 }
226 }
227 em->plist = pdummy.next;
229 em->last_update = tm;
231 /*printf("particles: %5d\r", em->pcount);*/
232 }
234 void psys_draw(struct psys_emitter *em)
235 {
236 struct psys_particle *p;
238 if(em->draw_start) {
239 em->draw_start(em, em->draw_cls);
240 }
242 p = em->plist;
243 while(p) {
244 em->draw(em, p, em->draw_cls);
245 p = p->next;
246 }
248 if(em->draw_end) {
249 em->draw_end(em, em->draw_cls);
250 }
251 }
253 static int spawn(struct psys_emitter *em, struct psys_particle *p, void *cls)
254 {
255 struct psys_rnd3 rpos;
256 rpos.value = em->cur_pos;
257 rpos.range = psys_get_cur_value3(&em->attr.spawn_range);
259 p->pos = psys_eval_rnd3(&rpos);
260 p->vel = psys_eval_anm_rnd3(&em->attr.dir, PSYS_EVAL_CUR);
261 p->base_size = psys_eval_anm_rnd(&em->attr.size, PSYS_EVAL_CUR);
262 p->max_life = p->life = psys_eval_anm_rnd(&em->attr.life, PSYS_EVAL_CUR);
264 p->pattr = &em->attr.part_attr;
266 psys_add_particle(em, p);
267 return 0;
268 }
270 static void update_particle(struct psys_emitter *em, struct psys_particle *p, float tm, float dt, void *cls)
271 {
272 vec3_t accel, grav;
273 anm_time_t t;
275 grav = psys_get_cur_value3(&em->attr.grav);
277 accel.x = grav.x - p->vel.x * em->attr.drag;
278 accel.y = grav.y - p->vel.y * em->attr.drag;
279 accel.z = grav.z - p->vel.z * em->attr.drag;
281 p->vel.x += accel.x * dt;
282 p->vel.y += accel.y * dt;
283 p->vel.z += accel.z * dt;
285 p->pos.x += p->vel.x * dt;
286 p->pos.y += p->vel.y * dt;
287 p->pos.z += p->vel.z * dt;
289 /* update particle attributes */
290 t = (anm_time_t)(1000.0 * (p->max_life - p->life) / p->max_life);
292 p->color = psys_get_value3(&p->pattr->color, t);
293 p->alpha = psys_get_value(&p->pattr->alpha, t);
294 p->size = p->base_size * psys_get_value(&p->pattr->size, t);
296 p->life -= dt;
297 }
299 /* --- particle allocation pool --- */
301 static struct psys_particle *palloc(void)
302 {
303 struct psys_particle *p;
305 pthread_mutex_lock(&pool_lock);
306 if(ppool) {
307 p = ppool;
308 ppool = ppool->next;
309 ppool_size--;
310 } else {
311 p = malloc(sizeof *p);
312 }
313 pthread_mutex_unlock(&pool_lock);
315 return p;
316 }
318 static void pfree(struct psys_particle *p)
319 {
320 pthread_mutex_lock(&pool_lock);
321 p->next = ppool;
322 ppool = p;
323 ppool_size++;
324 pthread_mutex_unlock(&pool_lock);
325 }