# HG changeset patch # User John Tsiombikas # Date 1316914011 -10800 # Node ID 6e5342a2529a70de2ab888a528b8b21906b47ca6 # Parent 874a942853ad019b982177491baa86417e65dab9 more stuff done diff -r 874a942853ad -r 6e5342a2529a examples/simple/Makefile --- a/examples/simple/Makefile Sat Sep 24 20:44:42 2011 +0300 +++ b/examples/simple/Makefile Sun Sep 25 04:26:51 2011 +0300 @@ -9,7 +9,7 @@ CC = gcc CFLAGS = -pedantic -Wall -g -I$(incdir) `pkg-config --cflags vmath` -LDFLAGS = -L$(libdir) $(liba) $(libgl) -lanim `pkg-config --libs vmath` -lm +LDFLAGS = -L$(libdir) $(liba) $(libgl) -lanim -limago `pkg-config --libs vmath` -lm ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT diff -r 874a942853ad -r 6e5342a2529a examples/simple/simple.c --- a/examples/simple/simple.c Sat Sep 24 20:44:42 2011 +0300 +++ b/examples/simple/simple.c Sun Sep 25 04:26:51 2011 +0300 @@ -1,5 +1,6 @@ #include #include +#include #ifndef __APPLE__ #include @@ -8,6 +9,7 @@ #endif #include +#include #include "psys.h" void disp(void); @@ -17,8 +19,10 @@ void mouse(int bn, int state, int x, int y); void motion(int x, int y); vec3_t get_mouse_hit(float x, float y); +unsigned int load_texture(const char *fname); struct psys_emitter *ps; +unsigned int tex; int main(int argc, char **argv) { @@ -34,13 +38,19 @@ glutMotionFunc(motion); glutIdleFunc(idle); - glEnable(GL_CULL_FACE); + glClearColor(0.05, 0.05, 0.05, 1); + + if(!(tex = load_texture("pimg.png"))) { + fprintf(stderr, "failed to load the FUCKING TEXTURE GOD DAMN IT\n"); + return 1; + } if(!(ps = psys_create())) { return 1; } - psys_set_grav(ps, v3_cons(0, -1, 0), 0); + psys_set_grav(ps, v3_cons(0, -9, 0), 0); psys_set_life(ps, 2, 0); + psys_set_texture(ps, tex); glutMainLoop(); return 0; @@ -61,6 +71,7 @@ psys_draw(ps); glutSwapBuffers(); + assert(glGetError() == GL_NO_ERROR); } void idle(void) @@ -90,7 +101,7 @@ { bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN; if(bn == GLUT_LEFT_BUTTON) { - psys_set_rate(ps, state == GLUT_DOWN ? 1.0 : 0.0, 0); + psys_set_rate(ps, state == GLUT_DOWN ? 30.0 : 0.0, 0); psys_set_pos(ps, get_mouse_hit(x, y), 0); } } @@ -131,3 +142,25 @@ return res; } + +unsigned int load_texture(const char *fname) +{ + void *pixels; + int xsz, ysz; + unsigned int tex; + + if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) { + return 0; + } + + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + img_free_pixels(pixels); + + return tex; +} diff -r 874a942853ad -r 6e5342a2529a src/psys.c --- a/src/psys.c Sat Sep 24 20:44:42 2011 +0300 +++ b/src/psys.c Sun Sep 25 04:26:51 2011 +0300 @@ -94,6 +94,11 @@ destroy_v3track(&em->dir); } +void psys_set_texture(struct psys_emitter *em, unsigned int tex) +{ + em->tex = tex; +} + void psys_set_pos(struct psys_emitter *em, vec3_t pos, float tm) { anm_set_position(&em->prs, pos, ANM_SEC2TM(tm)); @@ -184,6 +189,11 @@ } /* --- query current state --- */ +unsigned int psys_get_texture(struct psys_emitter *em) +{ + return em->tex; +} + vec3_t psys_get_pos(struct psys_emitter *em) { return em->cur_pos; @@ -218,7 +228,7 @@ void psys_update(struct psys_emitter *em, float tm) { - float dt, spawn_dt; + float dt, spawn_dt, spawn_tm; int i, spawn_count; struct psys_particle *p, pdummy; anm_time_t atm; @@ -231,21 +241,23 @@ dt = tm - em->last_update; /* how many particles to spawn for this interval ? */ - spawn_count = em->cur_rate * dt; + em->spawn_acc += em->cur_rate * dt; + if(em->spawn_acc >= 1.0) { + spawn_count = em->spawn_acc; + em->spawn_acc = fmod(em->spawn_acc, 1.0); + } else { + spawn_count = 0; + } -#ifndef SUB_UPDATE_POS - em->cur_pos = anm_get_position(&em->prs, atm); -#endif em->cur_dir = get_v3value(&em->dir, atm); em->cur_life = anm_get_value(&em->life, atm); em->cur_grav = get_v3value(&em->grav, atm); spawn_dt = dt / (float)spawn_count; + spawn_tm = em->last_update; for(i=0; icur_pos = anm_get_position(&em->prs, ANM_SEC2TM(em->last_update + spawn_dt)); -#endif + em->cur_pos = anm_get_position(&em->prs, ANM_SEC2TM(spawn_tm)); if(!(p = palloc())) { return; @@ -253,6 +265,7 @@ if(em->spawn(em, p, em->spawn_cls) == -1) { pfree(p); } + spawn_tm += spawn_dt; } /* update all particles */ @@ -275,6 +288,8 @@ } } em->plist = pdummy.next; + + em->last_update = tm; } void psys_draw(struct psys_emitter *em) @@ -322,6 +337,8 @@ p->pos.x += p->vel.x * dt; p->pos.y += p->vel.y * dt; p->pos.z += p->vel.z * dt; + + p->life -= dt; } /* --- v3track helper --- */ diff -r 874a942853ad -r 6e5342a2529a src/psys.h --- a/src/psys.h Sat Sep 24 20:44:42 2011 +0300 +++ b/src/psys.h Sun Sep 25 04:26:51 2011 +0300 @@ -1,14 +1,6 @@ #ifndef LIBPSYS_H_ #define LIBPSYS_H_ -/* emitter properties: - * - p/r/s (anim) - * - spawn rate (anim) - * - direction (anim) - * - * - collision planes - */ - struct psys_emitter; struct psys_particle; @@ -27,6 +19,7 @@ void psys_destroy(struct psys_emitter *em); /* set properties */ +void psys_set_texture(struct psys_emitter *em, unsigned int tex); void psys_set_pos(struct psys_emitter *em, vec3_t pos, float tm); void psys_set_rot(struct psys_emitter *em, quat_t rot, float tm); @@ -49,6 +42,7 @@ /* query emitter state */ +unsigned int psys_get_texture(struct psys_emitter *em); vec3_t psys_get_pos(struct psys_emitter *em); quat_t psys_get_rot(struct psys_emitter *em); float psys_get_rate(struct psys_emitter *em); diff -r 874a942853ad -r 6e5342a2529a src/psys_gl.c --- a/src/psys_gl.c Sat Sep 24 20:44:42 2011 +0300 +++ b/src/psys_gl.c Sun Sep 25 04:26:51 2011 +0300 @@ -10,27 +10,26 @@ { float xform[16]; - vec3_t pos = psys_get_pos(em); - - glPointSize(5.0); - glBegin(GL_POINTS); - glColor3f(1, 0, 0); - glVertex3f(pos.x, pos.y, pos.z); - glColor3f(1, 1, 1); - glEnd(); - glMatrixMode(GL_MODELVIEW); glPushMatrix(); glGetFloatv(GL_MODELVIEW_MATRIX, xform); - xform[3] = xform[7] = xform[11] = xform[12] = xform[13] = xform[14] = 0.0f; - xform[15] = 1.0f; + xform[0] = xform[5] = xform[10] = 1.0; + xform[1] = xform[2] = xform[4] = xform[6] = xform[8] = xform[9] = 0.0; glLoadMatrixf(xform); glPushAttrib(GL_ENABLE_BIT); glDisable(GL_LIGHTING); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + if(em->tex) { + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, em->tex); + } + glDepthMask(0); glBegin(GL_QUADS); diff -r 874a942853ad -r 6e5342a2529a src/psys_impl.h --- a/src/psys_impl.h Sat Sep 24 20:44:42 2011 +0300 +++ b/src/psys_impl.h Sun Sep 25 04:26:51 2011 +0300 @@ -21,6 +21,8 @@ struct anm_node prs; + unsigned int tex; + struct anm_track rate; struct anm_track life; struct v3track dir; @@ -54,6 +56,9 @@ float cur_rate, cur_life; vec3_t cur_dir; vec3_t cur_grav; + + /* partial spawn accumulator */ + float spawn_acc; };