X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=particle.c;h=2e05a1923dc05a5d94560731832d94704a339b1f;hb=304647a7672165dd35ffe54884ed9aedcc9bf363;hp=115c1fe1136f3e6a7dc9f6f896d6bbb35942bca4;hpb=60b814f700fb47b6730d603dd85a5affad67272e;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/particle.c b/particle.c index 115c1fe..2e05a19 100644 --- a/particle.c +++ b/particle.c @@ -1,7 +1,22 @@ +#include "vg/vg_lines.h" +#include "vg/vg_async.h" #include "particle.h" +#include "shaders/particle.h" + +struct particle_system particles_grind = { + .scale = 0.02f, + .velocity_scale = 0.001f, + .width = 0.0125f +}, +particles_env = { + .scale = 0.04f, + .velocity_scale = 0.001f, + .width = 0.25f +}; -static void particle_spawn( particle_system *sys, - v3f co, v3f v, f32 lifetime, u32 colour ){ +void particle_spawn( particle_system *sys, v3f co, v3f v, + f32 lifetime, u32 colour ) +{ if( sys->alive == sys->max ) return; particle *p = &sys->array[ sys->alive ++ ]; @@ -11,7 +26,30 @@ static void particle_spawn( particle_system *sys, p->colour = colour; } -static void particle_system_update( particle_system *sys, f32 dt ){ +void particle_spawn_cone( particle_system *sys, + v3f co, v3f dir, f32 angle, f32 speed, + f32 lifetime, u32 colour ) +{ + if( sys->alive == sys->max ) return; + + particle *p = &sys->array[ sys->alive ++ ]; + + v3f tx, ty; + v3_tangent_basis( dir, tx, ty ); + + v3f rand; + vg_rand_cone( &vg.rand, rand, angle ); + v3_muls( tx, rand[0]*speed, p->v ); + v3_muladds( p->v, ty, rand[1]*speed, p->v ); + v3_muladds( p->v, dir, rand[2]*speed, p->v ); + + p->life = lifetime; + p->colour = colour; + v3_copy( co, p->co ); +} + +void particle_system_update( particle_system *sys, f32 dt ) +{ u32 i = 0; iter: if( i == sys->alive ) return; @@ -30,7 +68,8 @@ iter: if( i == sys->alive ) return; goto iter; } -static void particle_system_debug( particle_system *sys ){ +void particle_system_debug( particle_system *sys ) +{ for( u32 i=0; ialive; i ++ ){ particle *p = &sys->array[i]; v3f p1; @@ -73,20 +112,13 @@ static void async_particle_init( void *payload, u32 size ){ VG_CHECK_GL_ERR(); } -static void particle_init( particle_system *sys, u32 max ){ - static int reg = 1; - if( reg ){ - shader_particle_register(); - reg = 0; - } - +void particle_alloc( particle_system *sys, u32 max ) +{ size_t stride = sizeof(particle_vert); - particles_grind.max = max; - particles_grind.array = - vg_linear_alloc( vg_mem.rtmemory, max*sizeof(particle) ); - particles_grind.vertices = - vg_linear_alloc( vg_mem.rtmemory, max*stride*4 ); + sys->max = max; + sys->array = vg_linear_alloc( vg_mem.rtmemory, max*sizeof(particle) ); + sys->vertices = vg_linear_alloc( vg_mem.rtmemory, max*stride*4 ); vg_async_item *call = vg_async_alloc( sizeof(particle_system *) + max*sizeof(u16)*6 ); @@ -105,17 +137,21 @@ static void particle_init( particle_system *sys, u32 max ){ vg_async_dispatch( call, async_particle_init ); } -static void particle_system_prerender( particle_system *sys ){ +void particle_system_prerender( particle_system *sys ) +{ for( u32 i=0; ialive; i ++ ){ particle *p = &sys->array[i]; particle_vert *vs = &sys->vertices[i*4]; v3f v, right; v3_copy( p->v, v ); - v3_normalize( v ); + + f32 vm = v3_length( p->v ); + v3_muls( v, 1.0f/vm, v ); v3_cross( v, (v3f){0,1,0}, right ); - f32 l = 0.3f, w = 0.025f; + f32 l = (sys->scale+sys->velocity_scale*vm), + w = sys->width; v3f p0, p1; v3_muladds( p->co, p->v, l, p0 ); @@ -139,9 +175,10 @@ static void particle_system_prerender( particle_system *sys ){ glBufferSubData( GL_ARRAY_BUFFER, 0, sys->alive*stride*4, sys->vertices ); } -static void particle_system_render( particle_system *sys, camera *cam ){ +void particle_system_render( particle_system *sys, vg_camera *cam ) +{ glDisable( GL_CULL_FACE ); - glDisable( GL_DEPTH_TEST ); + glEnable( GL_DEPTH_TEST ); shader_particle_use(); shader_particle_uPv( cam->mtx.pv );