libpsys

view src/psys.c @ 3:133094e2f5a5

reorganizing
author John Tsiombikas <nuclear@mutantstargoat.com>
date Mon, 26 Sep 2011 18:20:11 +0300
parents 6e5342a2529a
children 0fe624ffcb4f
line source
1 #include <stdlib.h>
2 #include <math.h>
3 #include <assert.h>
4 #include <pthread.h>
5 #include <vmath.h>
6 #include "psys_impl.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 static int init_v3track(struct v3track *v3t);
12 static void destroy_v3track(struct v3track *v3t);
13 static void set_v3value(struct v3track *v3t, anm_time_t tm, vec3_t v);
14 static vec3_t get_v3value(struct v3track *v3t, anm_time_t tm);
16 static float random_val(float x, float range);
17 static vec3_t random_vec3(vec3_t v, vec3_t range);
19 /* particle pool */
20 static struct psys_particle *ppool;
21 static int ppool_size;
22 static pthread_mutex_t pool_lock = PTHREAD_MUTEX_INITIALIZER;
24 static struct psys_particle *palloc(void);
25 static void pfree(struct psys_particle *p);
27 /* --- constructors and shit --- */
29 struct psys_emitter *psys_create(void)
30 {
31 struct psys_emitter *em;
33 if(!(em = malloc(sizeof *em))) {
34 return 0;
35 }
36 if(psys_init(em) == -1) {
37 free(em);
38 return 0;
39 }
40 return em;
41 }
43 void psys_free(struct psys_emitter *em)
44 {
45 psys_destroy(em);
46 free(em);
47 }
49 int psys_init(struct psys_emitter *em)
50 {
51 memset(em, 0, sizeof *em);
53 if(anm_init_node(&em->prs) == -1)
54 goto err;
55 if(init_v3track(&em->pos_range) == -1)
56 goto err;
57 if(anm_init_track(&em->rate) == -1)
58 goto err;
59 if(anm_init_track(&em->life) == -1)
60 goto err;
61 if(anm_init_track(&em->life_range) == -1)
62 goto err;
63 if(anm_init_track(&em->size) == -1)
64 goto err;
65 if(anm_init_track(&em->size_range) == -1)
66 goto err;
67 if(init_v3track(&em->dir) == -1)
68 goto err;
69 if(init_v3track(&em->dir_range) == -1)
70 goto err;
71 if(init_v3track(&em->grav) == -1)
72 goto err;
74 em->spawn = spawn;
75 em->update = update_particle;
77 em->draw = psys_gl_draw;
78 em->draw_start = psys_gl_draw_start;
79 em->draw_end = psys_gl_draw_end;
81 return 0;
82 err:
83 psys_destroy(em);
84 return -1;
85 }
87 void psys_destroy(struct psys_emitter *em)
88 {
89 struct psys_particle *part;
91 part = em->plist;
92 while(part) {
93 struct psys_particle *tmp = part;
94 part = part->next;
95 pfree(tmp);
96 }
98 anm_destroy_node(&em->prs);
99 destroy_v3track(&em->pos_range);
100 anm_destroy_track(&em->rate);
101 anm_destroy_track(&em->life);
102 anm_destroy_track(&em->size);
103 anm_destroy_track(&em->size_range);
104 destroy_v3track(&em->dir);
105 destroy_v3track(&em->dir_range);
106 destroy_v3track(&em->grav);
107 }
109 void psys_set_texture(struct psys_emitter *em, unsigned int tex)
110 {
111 em->tex = tex;
112 }
114 void psys_set_pos(struct psys_emitter *em, vec3_t pos, vec3_t range, float tm)
115 {
116 anm_set_position(&em->prs, pos, ANM_SEC2TM(tm));
117 set_v3value(&em->pos_range, ANM_SEC2TM(tm), range);
118 }
120 void psys_set_rot(struct psys_emitter *em, quat_t rot, float tm)
121 {
122 anm_set_rotation(&em->prs, rot, ANM_SEC2TM(tm));
123 }
125 void psys_set_pivot(struct psys_emitter *em, vec3_t pivot)
126 {
127 anm_set_pivot(&em->prs, pivot);
128 }
130 void psys_set_rate(struct psys_emitter *em, float rate, float tm)
131 {
132 anm_set_value(&em->rate, ANM_SEC2TM(tm), rate);
133 }
135 void psys_set_life(struct psys_emitter *em, float life, float range, float tm)
136 {
137 anm_set_value(&em->life, ANM_SEC2TM(tm), life);
138 anm_set_value(&em->life_range, ANM_SEC2TM(tm), range);
139 }
141 void psys_set_size(struct psys_emitter *em, float size, float range, float tm)
142 {
143 anm_set_value(&em->size, ANM_SEC2TM(tm), size);
144 anm_set_value(&em->size_range, ANM_SEC2TM(tm), range);
145 }
147 void psys_set_dir(struct psys_emitter *em, vec3_t dir, vec3_t range, float tm)
148 {
149 set_v3value(&em->dir, ANM_SEC2TM(tm), dir);
150 set_v3value(&em->dir_range, ANM_SEC2TM(tm), range);
151 }
153 void psys_set_grav(struct psys_emitter *em, vec3_t grav, float tm)
154 {
155 set_v3value(&em->grav, ANM_SEC2TM(tm), grav);
156 }
159 void psys_clear_collision_planes(struct psys_emitter *em)
160 {
161 struct col_plane *plane;
163 plane = em->planes;
164 while(plane) {
165 struct col_plane *tmp = plane;
166 plane = plane->next;
167 free(tmp);
168 }
169 }
171 int psys_add_collision_plane(struct psys_emitter *em, plane_t plane, float elast)
172 {
173 struct col_plane *node;
175 if(!(node = malloc(sizeof *node))) {
176 return -1;
177 }
178 node->p = plane;
179 node->elasticity = elast;
180 node->next = em->planes;
181 em->planes = node;
182 return 0;
183 }
185 void psys_add_particle(struct psys_emitter *em, struct psys_particle *p)
186 {
187 p->next = em->plist;
188 em->plist = p;
189 }
191 void psys_spawn_func(struct psys_emitter *em, psys_spawn_func_t func, void *cls)
192 {
193 em->spawn = func;
194 em->spawn_cls = cls;
195 }
197 void psys_update_func(struct psys_emitter *em, psys_update_func_t func, void *cls)
198 {
199 em->update = func;
200 em->upd_cls = cls;
201 }
203 void psys_draw_func(struct psys_emitter *em, psys_draw_func_t draw,
204 psys_draw_start_func_t start, psys_draw_end_func_t end, void *cls)
205 {
206 em->draw = draw;
207 em->draw_start = start;
208 em->draw_end = end;
209 em->draw_cls = cls;
210 }
212 /* --- query current state --- */
213 unsigned int psys_get_texture(struct psys_emitter *em)
214 {
215 return em->tex;
216 }
218 vec3_t psys_get_pos(struct psys_emitter *em)
219 {
220 return em->cur_pos;
221 }
223 quat_t psys_get_rot(struct psys_emitter *em)
224 {
225 return em->cur_rot;
226 }
228 float psys_get_rate(struct psys_emitter *em)
229 {
230 return em->cur_rate;
231 }
233 float psys_get_life(struct psys_emitter *em)
234 {
235 return em->cur_life;
236 }
238 vec3_t psys_get_dir(struct psys_emitter *em)
239 {
240 return em->cur_dir;
241 }
243 vec3_t psys_get_grav(struct psys_emitter *em)
244 {
245 return em->cur_grav;
246 }
248 /* --- update and render --- */
250 void psys_update(struct psys_emitter *em, float tm)
251 {
252 float dt, spawn_dt, spawn_tm;
253 int i, spawn_count;
254 struct psys_particle *p, pdummy;
255 anm_time_t atm;
257 assert(em->spawn && em->update);
259 atm = ANM_SEC2TM(tm);
261 em->cur_rate = anm_get_value(&em->rate, atm);
262 dt = tm - em->last_update;
264 /* how many particles to spawn for this interval ? */
265 em->spawn_acc += em->cur_rate * dt;
266 if(em->spawn_acc >= 1.0) {
267 spawn_count = em->spawn_acc;
268 em->spawn_acc = fmod(em->spawn_acc, 1.0);
269 } else {
270 spawn_count = 0;
271 }
273 em->cur_dir = get_v3value(&em->dir, atm);
274 em->cur_life = anm_get_value(&em->life, atm);
275 em->cur_grav = get_v3value(&em->grav, atm);
277 spawn_dt = dt / (float)spawn_count;
278 spawn_tm = em->last_update;
279 for(i=0; i<spawn_count; i++) {
280 /* update emitter position for this spawning */
281 em->cur_pos = anm_get_position(&em->prs, ANM_SEC2TM(spawn_tm));
283 if(!(p = palloc())) {
284 return;
285 }
286 if(em->spawn(em, p, em->spawn_cls) == -1) {
287 pfree(p);
288 }
289 spawn_tm += spawn_dt;
290 }
292 /* update all particles */
293 p = em->plist;
294 while(p) {
295 em->update(em, p, tm, dt, em->upd_cls);
296 p = p->next;
297 }
299 /* cleanup dead particles */
300 pdummy.next = em->plist;
301 p = &pdummy;
302 while(p->next) {
303 if(p->next->life <= 0) {
304 struct psys_particle *tmp = p->next;
305 p->next = p->next->next;
306 pfree(tmp);
307 } else {
308 p = p->next;
309 }
310 }
311 em->plist = pdummy.next;
313 em->last_update = tm;
314 }
316 void psys_draw(struct psys_emitter *em)
317 {
318 struct psys_particle *p;
320 if(em->draw_start) {
321 em->draw_start(em, em->draw_cls);
322 }
324 p = em->plist;
325 while(p) {
326 em->draw(em, p, em->draw_cls);
327 p = p->next;
328 }
330 if(em->draw_end) {
331 em->draw_end(em, em->draw_cls);
332 }
333 }
335 static int spawn(struct psys_emitter *em, struct psys_particle *p, void *cls)
336 {
337 p->pos = random_vec3(em->cur_pos, em->cur_pos_range);
338 p->vel = random_vec3(em->cur_dir, em->dir_range);
339 p->size = random_val(em->cur_size, em->cur_size_range);
340 p->life = em->cur_life;
342 psys_add_particle(em, p);
343 return 0;
344 }
346 static void update_particle(struct psys_emitter *em, struct psys_particle *p, float tm, float dt, void *cls)
347 {
348 vec3_t accel;
350 accel.x = em->cur_grav.x - p->vel.x * em->drag;
351 accel.y = em->cur_grav.y - p->vel.y * em->drag;
352 accel.z = em->cur_grav.z - p->vel.z * em->drag;
354 p->vel.x += accel.x * dt;
355 p->vel.y += accel.y * dt;
356 p->vel.z += accel.z * dt;
358 p->pos.x += p->vel.x * dt;
359 p->pos.y += p->vel.y * dt;
360 p->pos.z += p->vel.z * dt;
362 p->life -= dt;
363 }
365 /* --- v3track helper --- */
367 int init_v3track(struct v3track *v3t)
368 {
369 if(anm_init_track(&v3t->x) == -1) {
370 return -1;
371 }
372 if(anm_init_track(&v3t->y) == -1) {
373 anm_destroy_track(&v3t->x);
374 return -1;
375 }
376 if(anm_init_track(&v3t->z) == -1) {
377 anm_destroy_track(&v3t->x);
378 anm_destroy_track(&v3t->y);
379 return -1;
380 }
381 return 0;
382 }
384 static void destroy_v3track(struct v3track *v3t)
385 {
386 anm_destroy_track(&v3t->x);
387 anm_destroy_track(&v3t->y);
388 anm_destroy_track(&v3t->z);
389 }
391 static void set_v3value(struct v3track *v3t, anm_time_t tm, vec3_t v)
392 {
393 anm_set_value(&v3t->x, tm, v.x);
394 anm_set_value(&v3t->y, tm, v.y);
395 anm_set_value(&v3t->z, tm, v.z);
396 }
398 static vec3_t get_v3value(struct v3track *v3t, anm_time_t tm)
399 {
400 vec3_t v;
401 v.x = anm_get_value(&v3t->x, tm);
402 v.y = anm_get_value(&v3t->y, tm);
403 v.z = anm_get_value(&v3t->z, tm);
404 return v;
405 }
407 /* --- particle allocation pool --- */
409 static struct psys_particle *palloc(void)
410 {
411 struct psys_particle *p;
413 pthread_mutex_lock(&pool_lock);
414 if(ppool) {
415 p = ppool;
416 ppool = ppool->next;
417 ppool_size--;
418 } else {
419 p = malloc(sizeof *p);
420 }
421 pthread_mutex_unlock(&pool_lock);
423 if(p) {
424 memset(p, 0, sizeof *p);
425 /*reset_pattr(&p->attr);*/
426 }
427 return p;
428 }
430 static void pfree(struct psys_particle *p)
431 {
432 pthread_mutex_lock(&pool_lock);
433 p->next = ppool;
434 ppool = p;
435 ppool_size++;
436 pthread_mutex_unlock(&pool_lock);
437 }
439 static float random_val(float x, float range)
440 {
441 return x + range * (float)rand() / (float)RAND_MAX - 0.5 * range;
442 }
444 static vec3_t random_vec3(vec3_t v, vec3_t range)
445 {
446 vec3_t res;
447 res.x = random_val(v.x, range.x);
448 res.y = random_val(v.y, range.y);
449 res.z = random_val(v.z, range.z);
450 return res;
451 }