vrshoot

annotate libs/psys/psys.c @ 2:334d17aed7de

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