libpsys

changeset 9:9c24273f211b

first test is done
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 28 Sep 2011 03:42:01 +0300
parents a10f19674147
children 0f83e882fd04
files Makefile.in configure examples/simple/simple.c src/pattr.c src/pattr.h src/psys.c src/psys.h src/psys_gl.c
diffstat 8 files changed, 90 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/Makefile.in	Tue Sep 27 21:47:27 2011 +0300
     1.2 +++ b/Makefile.in	Wed Sep 28 03:42:01 2011 +0300
     1.3 @@ -18,7 +18,7 @@
     1.4  
     1.5  CC = gcc
     1.6  AR = ar
     1.7 -CFLAGS = -std=c89 -pedantic -Wall -g -fPIC -Isrc
     1.8 +CFLAGS = -std=c89 -pedantic -Wall $(dbg) $(opt) -fPIC -Isrc
     1.9  LDFLAGS = -lanim -limago -lvmath
    1.10  
    1.11  .PHONY: all
     2.1 --- a/configure	Tue Sep 27 21:47:27 2011 +0300
     2.2 +++ b/configure	Wed Sep 28 03:42:01 2011 +0300
     2.3 @@ -63,9 +63,4 @@
     2.4  fi
     2.5  cat Makefile.in >>Makefile
     2.6  
     2.7 -echo 'creating pkg-config file ...'
     2.8 -echo "prefix=$PREFIX" >psys.pc
     2.9 -echo "ver=$VERSION" >>psys.pc
    2.10 -cat psys.pc.in >>psys.pc
    2.11 -
    2.12  echo 'configuration completed, type make (or gmake) to build.'
     3.1 --- a/examples/simple/simple.c	Tue Sep 27 21:47:27 2011 +0300
     3.2 +++ b/examples/simple/simple.c	Wed Sep 28 03:42:01 2011 +0300
     3.3 @@ -25,6 +25,8 @@
     3.4  struct psys_emitter *ps;
     3.5  unsigned int tex;
     3.6  
     3.7 +#define RATE	300.0
     3.8 +
     3.9  int main(int argc, char **argv)
    3.10  {
    3.11  	glutInitWindowSize(800, 600);
    3.12 @@ -49,11 +51,18 @@
    3.13  	if(!(ps = psys_create())) {
    3.14  		return 1;
    3.15  	}
    3.16 -	psys_set_value3(&ps->attr.grav, 0, v3_cons(0, -9, 0));
    3.17 +	ps->attr.tex = tex;
    3.18 +	ps->attr.drag = 2;
    3.19 +	psys_set_value3(&ps->attr.grav, 0, v3_cons(0, -4, 0));
    3.20  	psys_set_anm_rnd(&ps->attr.life, 0, 2, 0);
    3.21 -	psys_set_value3(&ps->attr.spawn_range, 0, v3_cons(0.2, 0.2, 0.2));
    3.22 -	psys_set_anm_rnd3(&ps->attr.dir, 0, v3_cons(0, 0, 0), v3_cons(2, 2, 2));
    3.23 -	ps->attr.tex = tex;
    3.24 +	psys_set_value3(&ps->attr.spawn_range, 0, v3_cons(0.3, 0.3, 0.3));
    3.25 +	psys_set_anm_rnd3(&ps->attr.dir, 0, v3_cons(0, 0, 0), v3_cons(4, 4, 4));
    3.26 +
    3.27 +	psys_set_value3(&ps->attr.part_attr.color, 0, v3_cons(1.0, 0.6, 0.4));
    3.28 +	psys_set_value3(&ps->attr.part_attr.color, 1000,  v3_cons(0.6, 0.3, 1.0));
    3.29 +	psys_set_value(&ps->attr.part_attr.alpha, 0, 1);
    3.30 +	psys_set_value(&ps->attr.part_attr.alpha, 700, 1);
    3.31 +	psys_set_value(&ps->attr.part_attr.alpha, 1000, 0);
    3.32  
    3.33  	atexit(cleanup);
    3.34  
    3.35 @@ -111,7 +120,7 @@
    3.36  {
    3.37  	bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
    3.38  	if(bn == GLUT_LEFT_BUTTON) {
    3.39 -		psys_set_value(&ps->attr.rate, 0, state == GLUT_DOWN ? 30.0 : 0.0);
    3.40 +		psys_set_value(&ps->attr.rate, 0, state == GLUT_DOWN ? RATE : 0.0);
    3.41  		psys_set_pos(ps, get_mouse_hit(x, y), 0);
    3.42  	}
    3.43  }
     4.1 --- a/src/pattr.c	Tue Sep 27 21:47:27 2011 +0300
     4.2 +++ b/src/pattr.c	Wed Sep 28 03:42:01 2011 +0300
     4.3 @@ -5,6 +5,9 @@
     4.4  #include "pattr.h"
     4.5  #include "psys_gl.h"
     4.6  
     4.7 +static int init_particle_attr(struct psys_particle_attributes *pattr);
     4.8 +static void destroy_particle_attr(struct psys_particle_attributes *pattr);
     4.9 +
    4.10  static void *tex_cls;
    4.11  static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture;
    4.12  static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture;
    4.13 @@ -33,6 +36,9 @@
    4.14  	if(psys_init_track3(&attr->grav) == -1)
    4.15  		goto err;
    4.16  
    4.17 +	if(init_particle_attr(&attr->part_attr) == -1)
    4.18 +		goto err;
    4.19 +
    4.20  	attr->max_particles = -1;
    4.21  
    4.22  	anm_set_track_default(&attr->size.value.trk, 1.0);
    4.23 @@ -45,6 +51,31 @@
    4.24  	return -1;
    4.25  }
    4.26  
    4.27 +
    4.28 +static int init_particle_attr(struct psys_particle_attributes *pattr)
    4.29 +{
    4.30 +	if(psys_init_track3(&pattr->color) == -1) {
    4.31 +		return -1;
    4.32 +	}
    4.33 +	if(psys_init_track(&pattr->alpha) == -1) {
    4.34 +		psys_destroy_track3(&pattr->color);
    4.35 +		return -1;
    4.36 +	}
    4.37 +	if(psys_init_track(&pattr->size) == -1) {
    4.38 +		psys_destroy_track3(&pattr->color);
    4.39 +		psys_destroy_track(&pattr->alpha);
    4.40 +		return -1;
    4.41 +	}
    4.42 +
    4.43 +	anm_set_track_default(&pattr->color.x, 1.0);
    4.44 +	anm_set_track_default(&pattr->color.y, 1.0);
    4.45 +	anm_set_track_default(&pattr->color.z, 1.0);
    4.46 +	anm_set_track_default(&pattr->alpha.trk, 1.0);
    4.47 +	anm_set_track_default(&pattr->size.trk, 1.0);
    4.48 +	return 0;
    4.49 +}
    4.50 +
    4.51 +
    4.52  void psys_destroy_attr(struct psys_attributes *attr)
    4.53  {
    4.54  	psys_destroy_track3(&attr->spawn_range);
    4.55 @@ -54,11 +85,20 @@
    4.56  	psys_destroy_anm_rnd3(&attr->dir);
    4.57  	psys_destroy_track3(&attr->grav);
    4.58  
    4.59 +	destroy_particle_attr(&attr->part_attr);
    4.60 +
    4.61  	if(attr->tex && unload_texture) {
    4.62  		unload_texture(attr->tex, tex_cls);
    4.63  	}
    4.64  }
    4.65  
    4.66 +static void destroy_particle_attr(struct psys_particle_attributes *pattr)
    4.67 +{
    4.68 +	psys_destroy_track3(&pattr->color);
    4.69 +	psys_destroy_track(&pattr->alpha);
    4.70 +	psys_destroy_track(&pattr->size);
    4.71 +}
    4.72 +
    4.73  void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
    4.74  {
    4.75  	psys_eval_track3(&attr->spawn_range, tm);
     5.1 --- a/src/pattr.h	Tue Sep 27 21:47:27 2011 +0300
     5.2 +++ b/src/pattr.h	Wed Sep 28 03:42:01 2011 +0300
     5.3 @@ -4,6 +4,13 @@
     5.4  #include "pstrack.h"
     5.5  #include "rndval.h"
     5.6  
     5.7 +/* the particle attributes vary from 0 to 1 during its lifetime */
     5.8 +struct psys_particle_attributes {
     5.9 +	struct psys_track3 color;
    5.10 +	struct psys_track alpha;
    5.11 +	struct psys_track size;
    5.12 +};
    5.13 +
    5.14  struct psys_attributes {
    5.15  	unsigned int tex;	/* OpenGL texture to use for the billboard */
    5.16  
    5.17 @@ -14,8 +21,10 @@
    5.18  	struct psys_anm_rnd3 dir;		/* particle shoot direction */
    5.19  
    5.20  	struct psys_track3 grav;		/* external force (usually gravity) */
    5.21 +	float drag;	/* I don't think this needs to animate */
    5.22  
    5.23 -	float drag;	/* I don't think this needs to animate */
    5.24 +	/* particle attributes */
    5.25 +	struct psys_particle_attributes part_attr;
    5.26  
    5.27  	/* limits */
    5.28  	int max_particles;
     6.1 --- a/src/psys.c	Tue Sep 27 21:47:27 2011 +0300
     6.2 +++ b/src/psys.c	Wed Sep 28 03:42:01 2011 +0300
     6.3 @@ -204,6 +204,7 @@
     6.4  			struct psys_particle *tmp = p->next;
     6.5  			p->next = p->next->next;
     6.6  			pfree(tmp);
     6.7 +			em->pcount--;
     6.8  		} else {
     6.9  			p = p->next;
    6.10  		}
    6.11 @@ -211,6 +212,8 @@
    6.12  	em->plist = pdummy.next;
    6.13  
    6.14  	em->last_update = tm;
    6.15 +
    6.16 +	printf("particles: %5d\r", em->pcount);
    6.17  }
    6.18  
    6.19  void psys_draw(struct psys_emitter *em)
    6.20 @@ -240,8 +243,10 @@
    6.21  
    6.22  	p->pos = psys_eval_rnd3(&rpos);
    6.23  	p->vel = psys_eval_anm_rnd3(&em->attr.dir, PSYS_EVAL_CUR);
    6.24 -	p->size = psys_eval_anm_rnd(&em->attr.size, PSYS_EVAL_CUR);
    6.25 -	p->life = psys_eval_anm_rnd(&em->attr.life, PSYS_EVAL_CUR);
    6.26 +	p->base_size = psys_eval_anm_rnd(&em->attr.size, PSYS_EVAL_CUR);
    6.27 +	p->max_life = p->life = psys_eval_anm_rnd(&em->attr.life, PSYS_EVAL_CUR);
    6.28 +
    6.29 +	p->pattr = &em->attr.part_attr;
    6.30  
    6.31  	psys_add_particle(em, p);
    6.32  	return 0;
    6.33 @@ -250,6 +255,7 @@
    6.34  static void update_particle(struct psys_emitter *em, struct psys_particle *p, float tm, float dt, void *cls)
    6.35  {
    6.36  	vec3_t accel, grav;
    6.37 +	anm_time_t t;
    6.38  
    6.39  	grav = psys_get_cur_value3(&em->attr.grav);
    6.40  
    6.41 @@ -265,6 +271,13 @@
    6.42  	p->pos.y += p->vel.y * dt;
    6.43  	p->pos.z += p->vel.z * dt;
    6.44  
    6.45 +	/* update particle attributes */
    6.46 +	t = (anm_time_t)(1000.0 * (p->max_life - p->life) / p->max_life);
    6.47 +
    6.48 +	p->color = psys_get_value3(&p->pattr->color, t);
    6.49 +	p->alpha = psys_get_value(&p->pattr->alpha, t);
    6.50 +	p->size = p->base_size * psys_get_value(&p->pattr->size, t);
    6.51 +
    6.52  	p->life -= dt;
    6.53  }
    6.54  
     7.1 --- a/src/psys.h	Tue Sep 27 21:47:27 2011 +0300
     7.2 +++ b/src/psys.h	Wed Sep 28 03:42:01 2011 +0300
     7.3 @@ -58,7 +58,14 @@
     7.4  
     7.5  struct psys_particle {
     7.6  	vec3_t pos, vel;
     7.7 -	float life, size;
     7.8 +	float life, max_life;
     7.9 +	float base_size;
    7.10 +
    7.11 +	struct psys_particle_attributes *pattr;
    7.12 +
    7.13 +	/* current particle attr values calculated during update */
    7.14 +	vec3_t color;
    7.15 +	float alpha, size;
    7.16  
    7.17  	struct psys_particle *next;
    7.18  };
     8.1 --- a/src/psys_gl.c	Tue Sep 27 21:47:27 2011 +0300
     8.2 +++ b/src/psys_gl.c	Wed Sep 28 03:42:01 2011 +0300
     8.3 @@ -49,6 +49,8 @@
     8.4  {
     8.5  	float hsz = p->size / 2.0;
     8.6  
     8.7 +	glColor4f(p->color.x, p->color.y, p->color.z, p->alpha);
     8.8 +
     8.9  	glTexCoord2f(0, 0);
    8.10  	glVertex3f(p->pos.x - hsz, p->pos.y - hsz, p->pos.z);
    8.11