add skybox editor
[carveJwlIkooP6JGAAIwe30JlM.git] / world_audio.c
1 #ifndef WORLD_AUDIO_C
2 #define WORLD_AUDIO_C
3
4 #include "audio.h"
5 #include "world_audio.h"
6
7 /* finds any active playing in world and fades them out, we can only do this
8 * while unloading */
9 static void world_fadeout_audio( world_instance *world )
10 {
11 if( world->status != k_world_status_unloading ){
12 vg_fatal_error( "World status must be set to 'unloading', to fadeout"
13 " audio.\n" );
14 }
15
16 u8 world_id = (world - world_static.instances) + 1;
17
18 audio_lock();
19 for( u32 i=0; i<AUDIO_CHANNELS; i++ ){
20 audio_channel *ch = &vg_audio.channels[i];
21
22 if( ch->allocated && (ch->world_id == world_id) ){
23 ch = audio_channel_fadeout( ch, 1.0f );
24 }
25 }
26 audio_unlock();
27 }
28
29 /*
30 * Trace out a random point, near the player to try and determine water areas
31 */
32 static
33 enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output){
34 v3f chance = { (vg_randf64(&vg.rand)-0.5f) * 30.0f,
35 8,
36 (vg_randf64(&vg.rand)-0.5f) * 30.0f };
37
38 v3f pos;
39 v3_add( chance, origin, pos );
40
41 ray_hit contact;
42 contact.dist = vg_minf( 16.0f, pos[1] );
43
44 world_instance *world = world_current_instance();
45
46 if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &contact,
47 k_material_flag_ghosts ) ){
48 struct world_surface *mat = ray_hit_surface( world, &contact );
49
50 if( mat->info.surface_prop == k_surface_prop_grass){
51 v3_copy( contact.pos, output );
52 return k_audio_sprite_type_grass;
53 }
54 else{
55 return k_audio_sprite_type_none;
56 }
57 }
58
59 output[0] = pos[0];
60 output[1] = 0.0f;
61 output[2] = pos[2];
62
63 float dist = fabsf(output[1] - origin[1]);
64
65 if( world->water.enabled && dist<=40.0f )
66 return k_audio_sprite_type_water;
67 else
68 return k_audio_sprite_type_none;
69 }
70
71 static void world_audio_sample_distances( v3f co, int *index, float *value )
72 {
73 float inr3 = 0.57735027,
74 inr2 = 0.70710678118;
75
76 v3f sample_directions[] = {
77 { -1.0f, 0.0f, 0.0f },
78 { 1.0f, 0.0f, 0.0f },
79 { 0.0f, 0.0f, 1.0f },
80 { 0.0f, 0.0f, -1.0f },
81 { 0.0f, 1.0f, 0.0f },
82 { 0.0f, -1.0f, 0.0f },
83 { -inr3, inr3, inr3 },
84 { inr3, inr3, inr3 },
85 { -inr3, inr3, -inr3 },
86 { inr3, inr3, -inr3 },
87 { -inr2, 0.0f, inr2 },
88 { inr2, 0.0f, inr2 },
89 { -inr2, 0.0f, -inr2 },
90 { inr2, 0.0f, -inr2 },
91 };
92
93 static int si = 0;
94 static float distances[16];
95
96 ray_hit ray;
97 ray.dist = 5.0f;
98
99 v3f rc, rd, ro;
100 v3_copy( sample_directions[ si ], rd );
101 v3_add( co, (v3f){0.0f,1.5f,0.0f}, ro );
102 v3_copy( ro, rc );
103
104 float dist = 200.0f;
105
106 for( int i=0; i<10; i++ ){
107 if( ray_world( world_current_instance(), rc, rd, &ray,
108 k_material_flag_ghosts ) ){
109 dist = (float)i*5.0f + ray.dist;
110 break;
111 }
112 else{
113 v3_muladds( rc, rd, ray.dist, rc );
114 }
115 }
116
117 distances[si] = dist;
118
119 if( vg_audio.debug_ui && vg_lines.draw ){
120 for( int i=0; i<14; i++ ){
121 if( distances[i] != 200.0f ){
122 u32 colours[] = { VG__RED, VG__BLUE, VG__GREEN,
123 VG__CYAN, VG__YELOW, VG__PINK,
124 VG__WHITE };
125
126 u32 colour = colours[i%7];
127
128 v3f p1;
129 v3_muladds( ro, sample_directions[i], distances[i], p1 );
130 vg_line( ro, p1, colour );
131 vg_line_point( p1, 0.1f, colour );
132 }
133 }
134 }
135
136 *index = si;
137 *value = dist;
138
139 si ++;
140 if( si >= 14 )
141 si = 0;
142 }
143
144 #endif /* WORLD_AUDIO_C */