audio occlusion
authorhgn <hgodden00@gmail.com>
Fri, 23 Sep 2022 16:26:24 +0000 (17:26 +0100)
committerhgn <hgodden00@gmail.com>
Fri, 23 Sep 2022 16:26:24 +0000 (17:26 +0100)
audio.h
blender_export.py
main.c
player.h
player_model.h
rigidbody.h
sound_src/reverb.ogg [new file with mode: 0644]

diff --git a/audio.h b/audio.h
index 498acc35f616680b65d5950522bf99f37f543ccd..0572d6dcf52b623fc7a3f313c370e0d70e0f4d7c 100644 (file)
--- a/audio.h
+++ b/audio.h
@@ -2,6 +2,12 @@
 #define AUDIO_H
 
 #include "common.h"
+#include "world.h"
+
+static float audio_occlusion_current = 0.0f,
+             k_audio_occlusion_rate  = 1.0f;
+
+static int   k_audio_debug_soundscape = 0;
 
 sfx_vol_control audio_vol_all = { .val = 1.0f, .name = "All" };
 
@@ -10,6 +16,7 @@ sfx_set audio_board =
    .sources = "sound/skate.ogg\0"
               "sound/wheel.ogg\0"
               "sound/slide.ogg\0"
+              "sound/reverb.ogg\0"
 };
 
 sfx_system audio_player0 =
@@ -39,12 +46,38 @@ sfx_system audio_player2 =
    .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
 };
 
+sfx_system audio_player3 =
+{
+   .vol = 0.0f,
+   .ch = 1,
+   .vol_src = &audio_vol_all,
+   .name = "Player3",
+   .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
+};
+
 static void audio_init(void)
 {
    sfx_set_init( &audio_board, NULL );
    sfx_set_play( &audio_board, &audio_player0, 0 );
    sfx_set_play( &audio_board, &audio_player1, 1 );
    sfx_set_play( &audio_board, &audio_player2, 2 );
+   sfx_set_play( &audio_board, &audio_player3, 3 );
+
+   vg_convar_push( (struct vg_convar){
+      .name = "aud_debug_soundscape",
+      .data = &k_audio_debug_soundscape,
+      .data_type = k_convar_dtype_i32,
+      .opt_i32 = { .min=0, .max=1, .clamp=0 },
+      .persistent = 1
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "aud_occlusion_rate",
+      .data = &k_audio_occlusion_rate,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
 }
 
 static void audio_free(void)
@@ -52,4 +85,65 @@ static void audio_free(void)
    sfx_set_free( &audio_board );
 }
 
+static void audio_sample_occlusion( v3f origin )
+{
+   float d = 0.0f,
+         sample_dist = 880.0f;
+
+   int sample_count = 8;
+
+   for( int i=0; i<sample_count; i++ )
+   {
+      v3f dir;
+      dir[0] = vg_randf();
+      dir[1] = vg_randf();
+      dir[2] = vg_randf();
+
+      v3_muls( dir, 2.0f, dir );
+      v3_sub( dir, (v3f){1.0f,1.0f,1.0f}, dir );
+
+      v3_normalize( dir );
+
+      ray_hit contact;
+      contact.dist = 15.0f;
+      
+      if( ray_world( origin, dir, &contact ) )
+      {
+         d += contact.dist;
+
+         vg_line( origin, contact.pos, 0xff0000ff );
+         vg_line_pt3( contact.pos, 0.1f, 0xff0000ff );
+      }
+      else
+      {
+         v3f p1;
+         v3_muladds( origin, dir, sample_dist, p1 );
+         vg_line( origin, p1, 0xffcccccc );
+
+         d += sample_dist;
+      }
+   }
+
+   float occlusion = 1.0f - (d * (1.0f/(sample_dist*(float)sample_count))),
+         rate = ktimestep * k_audio_occlusion_rate,
+         target = powf( occlusion, 6.0f );
+   audio_occlusion_current = vg_lerpf( audio_occlusion_current, target, rate );
+}
+
+static void audio_debug_soundscapes(void)
+{
+   if( !k_audio_debug_soundscape ) return;
+
+   char buf[64];
+   snprintf( buf, 31, "occlusion: %.5f", audio_occlusion_current );
+
+   ui_global_ctx.cursor[0] = 10;
+   ui_global_ctx.cursor[1] = 10;
+   ui_global_ctx.cursor[2] = audio_occlusion_current * 200.0f;
+   ui_global_ctx.cursor[3] = 20;
+
+   gui_fill_rect( ui_global_ctx.cursor, 0x55cccccc );
+   gui_text( ui_global_ctx.cursor, buf, 1, 0 );
+}
+
 #endif /* AUDIO_H */
index acd28ec3bcfcadbd96bc17cc539bd1508a78f86d..05d1ec06aa5482a1ff35a40ac5cd5771dfca55a0 100644 (file)
@@ -158,6 +158,20 @@ class classtype_bone(Structure):
                ("angle_limits",(c_float*3)*2),
                ("hitbox",(c_float*3)*2)]
 
+class subclass_audio_channel(Structure):
+   _pack_ = 1
+   _fields_ = [("sound",c_uint32),
+               ("target",c_uint32),
+               ("")]
+
+class classtype_audio_system(Structure):
+   _pack_ = 1
+   _fields_ = [("")]
+
+class classtype_audio_zone(Structure):
+   _pack_ = 1
+   _fields_ = [("system",c_uint32)]
+
 # Exporter
 # ==============================================================================
 
diff --git a/main.c b/main.c
index b46f3771b5a565abba28504e852f745fe97f5bfc..266e080803cc4b50190e795b33eb39854151d0cd 100644 (file)
--- a/main.c
+++ b/main.c
@@ -171,6 +171,22 @@ void vg_start(void)
       .persistent = 1
    });
 
+   vg_convar_push( (struct vg_convar){
+      .name = "rd_floaty",
+      .data = &k_ragdoll_floatyiness,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
+
+   vg_convar_push( (struct vg_convar){
+      .name = "rd_floatd",
+      .data = &k_ragdoll_floatydrag,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
+
    vg_convar_push( (struct vg_convar){
       .name = "dt",
       .data = &ktimestep,
@@ -484,6 +500,8 @@ void vg_ui(void)
       render_world_routes_ui();
    }
    //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
+   
+   audio_debug_soundscapes();
 
 #if 0
    static double last_b_press = 0.0;
index 99cb6c8105af173d585f941ee7ad542442d721a2..8619308a0cffe686a929574701353ca2d3be4c23 100644 (file)
--- a/player.h
+++ b/player.h
@@ -1137,12 +1137,14 @@ static void player_animate_camera(void)
  */
 static void player_audio(void)
 {
+   audio_sample_occlusion( player.camera[3] );
+
    float speed = vg_minf(v3_length( player.rb.v )*0.1f,1.0f),
          attn = v3_dist( player.rb.co, player.camera[3] )+1.0f;
    attn = (1.0f/(attn*attn)) * speed;
 
    static float air = 0.0f;
-   air = vg_lerpf(air, player.in_air? 1.0f: 0.0f, 0.7f);
+   air = vg_lerpf(air, player.in_air? 1.0f: 0.0f, 5.0f*ktimestep);
    
    v3f ears = { 1.0f,0.0f,0.0f };
    v3f delta;
@@ -1150,12 +1152,14 @@ static void player_audio(void)
    v3_sub( player.rb.co, player.camera[3], delta );
    v3_normalize( delta );
    m3x3_mulv( player.camera, ears, ears );
-
+   
    float pan = v3_dot( ears, delta );
    audio_player0.pan = pan;
    audio_player1.pan = pan;
    audio_player2.pan = pan;
 
+   audio_player3.pan = 0.0f;
+
    if( freecam )
    {
       audio_player0.vol = 0.0f;
@@ -1178,6 +1182,8 @@ static void player_audio(void)
          audio_player2.vol = (1.0f-air)*attn*slide;
       }
    }
+
+   audio_player3.vol = audio_player0.vol * audio_occlusion_current * 2.0f;
 }
 
 /*
@@ -1282,7 +1288,8 @@ static void player_update(void)
       player.on_board ^= 0x1;
    }
 
-   if( glfwGetKey( vg_window, GLFW_KEY_O ) )
+   if( (glfwGetKey( vg_window, GLFW_KEY_O ) || (player.rb.co[1] < 0.0f)) &&
+       !player.is_dead)
    {
       character_ragdoll_copypose( &player.mdl, player.rb.v );
       player.is_dead = 1;
index 652753c81d71a23335f7c13dd2d65381a169b26c..a4a626c666cfe7db46d171d571d4251d66efccca 100644 (file)
@@ -10,6 +10,9 @@
 #include "skeleton_animator.h"
 #include "shaders/viewchar.h"
 
+static float k_ragdoll_floatyiness = 10.0f,
+             k_ragdoll_floatydrag  = 1.0f;
+
 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
 
 static void character_register(void)
@@ -239,6 +242,10 @@ static void character_ragdoll_iter( struct character *ch )
             rb_debug_constraint_limits( &pj->rb, &pp->rb, lca, pj->limits );
          }
       }
+
+      v4f plane = {0.0f,1.0f,0.0f,0.0f};
+      rb_effect_simple_bouyency( &pj->rb, plane, k_ragdoll_floatyiness,
+                                                 k_ragdoll_floatydrag );
    }
 
    /* CONSTRAINTS */
index 05d2db1100fe78310d8a548691d99138c90a9d3a..026d8676dbd0cbccab6b7174c2a1498626ce2e77 100644 (file)
@@ -1860,6 +1860,23 @@ static void rb_constraint_position( rigidbody *ra, v3f lca,
    }
 }
 
+/* 
+ * Effectors
+ */
+
+static void rb_effect_simple_bouyency( rigidbody *ra, v4f plane, 
+                                       float amt, float drag )
+{
+   /* float */
+   float depth  = v3_dot( plane, ra->co ) - plane[3],
+         lambda = vg_clampf( -depth, 0.0f, 1.0f ) * amt;
+
+   v3_muladds( ra->v, plane, lambda * ktimestep, ra->v );
+
+   if( depth < 0.0f )
+      v3_muls( ra->v, 1.0f-(drag*ktimestep), ra->v );
+}
+
 /*
  * -----------------------------------------------------------------------------
  * BVH implementation, this is ONLY for static rigidbodies, its to slow for
diff --git a/sound_src/reverb.ogg b/sound_src/reverb.ogg
new file mode 100644 (file)
index 0000000..b8bb4d8
Binary files /dev/null and b/sound_src/reverb.ogg differ