libpsys
changeset 2:6e5342a2529a
more stuff done
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Sun, 25 Sep 2011 04:26:51 +0300 |
parents | 874a942853ad |
children | 133094e2f5a5 |
files | examples/simple/Makefile examples/simple/simple.c src/psys.c src/psys.h src/psys_gl.c src/psys_impl.h |
diffstat | 6 files changed, 79 insertions(+), 31 deletions(-) [+] |
line diff
1.1 --- a/examples/simple/Makefile Sat Sep 24 20:44:42 2011 +0300 1.2 +++ b/examples/simple/Makefile Sun Sep 25 04:26:51 2011 +0300 1.3 @@ -9,7 +9,7 @@ 1.4 1.5 CC = gcc 1.6 CFLAGS = -pedantic -Wall -g -I$(incdir) `pkg-config --cflags vmath` 1.7 -LDFLAGS = -L$(libdir) $(liba) $(libgl) -lanim `pkg-config --libs vmath` -lm 1.8 +LDFLAGS = -L$(libdir) $(liba) $(libgl) -lanim -limago `pkg-config --libs vmath` -lm 1.9 1.10 ifeq ($(shell uname -s), Darwin) 1.11 libgl = -framework OpenGL -framework GLUT
2.1 --- a/examples/simple/simple.c Sat Sep 24 20:44:42 2011 +0300 2.2 +++ b/examples/simple/simple.c Sun Sep 25 04:26:51 2011 +0300 2.3 @@ -1,5 +1,6 @@ 2.4 #include <stdio.h> 2.5 #include <stdlib.h> 2.6 +#include <assert.h> 2.7 2.8 #ifndef __APPLE__ 2.9 #include <GL/glut.h> 2.10 @@ -8,6 +9,7 @@ 2.11 #endif 2.12 2.13 #include <vmath.h> 2.14 +#include <imago2.h> 2.15 #include "psys.h" 2.16 2.17 void disp(void); 2.18 @@ -17,8 +19,10 @@ 2.19 void mouse(int bn, int state, int x, int y); 2.20 void motion(int x, int y); 2.21 vec3_t get_mouse_hit(float x, float y); 2.22 +unsigned int load_texture(const char *fname); 2.23 2.24 struct psys_emitter *ps; 2.25 +unsigned int tex; 2.26 2.27 int main(int argc, char **argv) 2.28 { 2.29 @@ -34,13 +38,19 @@ 2.30 glutMotionFunc(motion); 2.31 glutIdleFunc(idle); 2.32 2.33 - glEnable(GL_CULL_FACE); 2.34 + glClearColor(0.05, 0.05, 0.05, 1); 2.35 + 2.36 + if(!(tex = load_texture("pimg.png"))) { 2.37 + fprintf(stderr, "failed to load the FUCKING TEXTURE GOD DAMN IT\n"); 2.38 + return 1; 2.39 + } 2.40 2.41 if(!(ps = psys_create())) { 2.42 return 1; 2.43 } 2.44 - psys_set_grav(ps, v3_cons(0, -1, 0), 0); 2.45 + psys_set_grav(ps, v3_cons(0, -9, 0), 0); 2.46 psys_set_life(ps, 2, 0); 2.47 + psys_set_texture(ps, tex); 2.48 2.49 glutMainLoop(); 2.50 return 0; 2.51 @@ -61,6 +71,7 @@ 2.52 psys_draw(ps); 2.53 2.54 glutSwapBuffers(); 2.55 + assert(glGetError() == GL_NO_ERROR); 2.56 } 2.57 2.58 void idle(void) 2.59 @@ -90,7 +101,7 @@ 2.60 { 2.61 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN; 2.62 if(bn == GLUT_LEFT_BUTTON) { 2.63 - psys_set_rate(ps, state == GLUT_DOWN ? 1.0 : 0.0, 0); 2.64 + psys_set_rate(ps, state == GLUT_DOWN ? 30.0 : 0.0, 0); 2.65 psys_set_pos(ps, get_mouse_hit(x, y), 0); 2.66 } 2.67 } 2.68 @@ -131,3 +142,25 @@ 2.69 2.70 return res; 2.71 } 2.72 + 2.73 +unsigned int load_texture(const char *fname) 2.74 +{ 2.75 + void *pixels; 2.76 + int xsz, ysz; 2.77 + unsigned int tex; 2.78 + 2.79 + if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) { 2.80 + return 0; 2.81 + } 2.82 + 2.83 + glGenTextures(1, &tex); 2.84 + glBindTexture(GL_TEXTURE_2D, tex); 2.85 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 2.86 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 2.87 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 2.88 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 2.89 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 2.90 + img_free_pixels(pixels); 2.91 + 2.92 + return tex; 2.93 +}
3.1 --- a/src/psys.c Sat Sep 24 20:44:42 2011 +0300 3.2 +++ b/src/psys.c Sun Sep 25 04:26:51 2011 +0300 3.3 @@ -94,6 +94,11 @@ 3.4 destroy_v3track(&em->dir); 3.5 } 3.6 3.7 +void psys_set_texture(struct psys_emitter *em, unsigned int tex) 3.8 +{ 3.9 + em->tex = tex; 3.10 +} 3.11 + 3.12 void psys_set_pos(struct psys_emitter *em, vec3_t pos, float tm) 3.13 { 3.14 anm_set_position(&em->prs, pos, ANM_SEC2TM(tm)); 3.15 @@ -184,6 +189,11 @@ 3.16 } 3.17 3.18 /* --- query current state --- */ 3.19 +unsigned int psys_get_texture(struct psys_emitter *em) 3.20 +{ 3.21 + return em->tex; 3.22 +} 3.23 + 3.24 vec3_t psys_get_pos(struct psys_emitter *em) 3.25 { 3.26 return em->cur_pos; 3.27 @@ -218,7 +228,7 @@ 3.28 3.29 void psys_update(struct psys_emitter *em, float tm) 3.30 { 3.31 - float dt, spawn_dt; 3.32 + float dt, spawn_dt, spawn_tm; 3.33 int i, spawn_count; 3.34 struct psys_particle *p, pdummy; 3.35 anm_time_t atm; 3.36 @@ -231,21 +241,23 @@ 3.37 dt = tm - em->last_update; 3.38 3.39 /* how many particles to spawn for this interval ? */ 3.40 - spawn_count = em->cur_rate * dt; 3.41 + em->spawn_acc += em->cur_rate * dt; 3.42 + if(em->spawn_acc >= 1.0) { 3.43 + spawn_count = em->spawn_acc; 3.44 + em->spawn_acc = fmod(em->spawn_acc, 1.0); 3.45 + } else { 3.46 + spawn_count = 0; 3.47 + } 3.48 3.49 -#ifndef SUB_UPDATE_POS 3.50 - em->cur_pos = anm_get_position(&em->prs, atm); 3.51 -#endif 3.52 em->cur_dir = get_v3value(&em->dir, atm); 3.53 em->cur_life = anm_get_value(&em->life, atm); 3.54 em->cur_grav = get_v3value(&em->grav, atm); 3.55 3.56 spawn_dt = dt / (float)spawn_count; 3.57 + spawn_tm = em->last_update; 3.58 for(i=0; i<spawn_count; i++) { 3.59 -#ifdef SUB_UPDATE_POS 3.60 /* update emitter position for this spawning */ 3.61 - em->cur_pos = anm_get_position(&em->prs, ANM_SEC2TM(em->last_update + spawn_dt)); 3.62 -#endif 3.63 + em->cur_pos = anm_get_position(&em->prs, ANM_SEC2TM(spawn_tm)); 3.64 3.65 if(!(p = palloc())) { 3.66 return; 3.67 @@ -253,6 +265,7 @@ 3.68 if(em->spawn(em, p, em->spawn_cls) == -1) { 3.69 pfree(p); 3.70 } 3.71 + spawn_tm += spawn_dt; 3.72 } 3.73 3.74 /* update all particles */ 3.75 @@ -275,6 +288,8 @@ 3.76 } 3.77 } 3.78 em->plist = pdummy.next; 3.79 + 3.80 + em->last_update = tm; 3.81 } 3.82 3.83 void psys_draw(struct psys_emitter *em) 3.84 @@ -322,6 +337,8 @@ 3.85 p->pos.x += p->vel.x * dt; 3.86 p->pos.y += p->vel.y * dt; 3.87 p->pos.z += p->vel.z * dt; 3.88 + 3.89 + p->life -= dt; 3.90 } 3.91 3.92 /* --- v3track helper --- */
4.1 --- a/src/psys.h Sat Sep 24 20:44:42 2011 +0300 4.2 +++ b/src/psys.h Sun Sep 25 04:26:51 2011 +0300 4.3 @@ -1,14 +1,6 @@ 4.4 #ifndef LIBPSYS_H_ 4.5 #define LIBPSYS_H_ 4.6 4.7 -/* emitter properties: 4.8 - * - p/r/s (anim) 4.9 - * - spawn rate (anim) 4.10 - * - direction (anim) 4.11 - * 4.12 - * - collision planes 4.13 - */ 4.14 - 4.15 struct psys_emitter; 4.16 struct psys_particle; 4.17 4.18 @@ -27,6 +19,7 @@ 4.19 void psys_destroy(struct psys_emitter *em); 4.20 4.21 /* set properties */ 4.22 +void psys_set_texture(struct psys_emitter *em, unsigned int tex); 4.23 4.24 void psys_set_pos(struct psys_emitter *em, vec3_t pos, float tm); 4.25 void psys_set_rot(struct psys_emitter *em, quat_t rot, float tm); 4.26 @@ -49,6 +42,7 @@ 4.27 4.28 4.29 /* query emitter state */ 4.30 +unsigned int psys_get_texture(struct psys_emitter *em); 4.31 vec3_t psys_get_pos(struct psys_emitter *em); 4.32 quat_t psys_get_rot(struct psys_emitter *em); 4.33 float psys_get_rate(struct psys_emitter *em);
5.1 --- a/src/psys_gl.c Sat Sep 24 20:44:42 2011 +0300 5.2 +++ b/src/psys_gl.c Sun Sep 25 04:26:51 2011 +0300 5.3 @@ -10,27 +10,26 @@ 5.4 { 5.5 float xform[16]; 5.6 5.7 - vec3_t pos = psys_get_pos(em); 5.8 - 5.9 - glPointSize(5.0); 5.10 - glBegin(GL_POINTS); 5.11 - glColor3f(1, 0, 0); 5.12 - glVertex3f(pos.x, pos.y, pos.z); 5.13 - glColor3f(1, 1, 1); 5.14 - glEnd(); 5.15 - 5.16 glMatrixMode(GL_MODELVIEW); 5.17 glPushMatrix(); 5.18 5.19 glGetFloatv(GL_MODELVIEW_MATRIX, xform); 5.20 - xform[3] = xform[7] = xform[11] = xform[12] = xform[13] = xform[14] = 0.0f; 5.21 - xform[15] = 1.0f; 5.22 + xform[0] = xform[5] = xform[10] = 1.0; 5.23 + xform[1] = xform[2] = xform[4] = xform[6] = xform[8] = xform[9] = 0.0; 5.24 5.25 glLoadMatrixf(xform); 5.26 5.27 glPushAttrib(GL_ENABLE_BIT); 5.28 glDisable(GL_LIGHTING); 5.29 5.30 + glEnable(GL_BLEND); 5.31 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 5.32 + 5.33 + if(em->tex) { 5.34 + glEnable(GL_TEXTURE_2D); 5.35 + glBindTexture(GL_TEXTURE_2D, em->tex); 5.36 + } 5.37 + 5.38 glDepthMask(0); 5.39 5.40 glBegin(GL_QUADS);
6.1 --- a/src/psys_impl.h Sat Sep 24 20:44:42 2011 +0300 6.2 +++ b/src/psys_impl.h Sun Sep 25 04:26:51 2011 +0300 6.3 @@ -21,6 +21,8 @@ 6.4 6.5 struct anm_node prs; 6.6 6.7 + unsigned int tex; 6.8 + 6.9 struct anm_track rate; 6.10 struct anm_track life; 6.11 struct v3track dir; 6.12 @@ -54,6 +56,9 @@ 6.13 float cur_rate, cur_life; 6.14 vec3_t cur_dir; 6.15 vec3_t cur_grav; 6.16 + 6.17 + /* partial spawn accumulator */ 6.18 + float spawn_acc; 6.19 }; 6.20 6.21