actually render trails
[carveJwlIkooP6JGAAIwe30JlM.git] / particle.c
index 115c1fe1136f3e6a7dc9f6f896d6bbb35942bca4..e1f6ac85b0f31270b75c0fafb91160aa0c767233 100644 (file)
@@ -1,4 +1,5 @@
 #include "particle.h"
+#include "shaders/trail.h"
 
 static void particle_spawn( particle_system *sys, 
                             v3f co, v3f v, f32 lifetime, u32 colour ){
@@ -11,6 +12,27 @@ static void particle_spawn( particle_system *sys,
    p->colour = colour;
 }
 
+static 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 );
+}
+
 static void particle_system_update( particle_system *sys, f32 dt ){
    u32 i = 0;
 iter: if( i == sys->alive ) return;
@@ -73,20 +95,19 @@ static void async_particle_init( void *payload, u32 size ){
    VG_CHECK_GL_ERR();
 }
 
-static void particle_init( particle_system *sys, u32 max ){
+static void particle_alloc( particle_system *sys, u32 max ){
    static int reg = 1;
    if( reg ){
       shader_particle_register();
+      shader_trail_register();
       reg = 0;
    }
 
    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 );
@@ -112,10 +133,13 @@ static void particle_system_prerender( particle_system *sys ){
 
       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 );
@@ -141,7 +165,7 @@ static void particle_system_prerender( particle_system *sys ){
 
 static void particle_system_render( particle_system *sys, camera *cam ){
    glDisable( GL_CULL_FACE );
-   glDisable( GL_DEPTH_TEST );
+   glEnable( GL_DEPTH_TEST );
 
    shader_particle_use();
    shader_particle_uPv( cam->mtx.pv );