fix quality profile water/shadows
[carveJwlIkooP6JGAAIwe30JlM.git] / player_glide.c
1 #ifndef PLAYER_GLIDE_C
2 #define PLAYER_GLIDE_C
3
4 #include "player_glide.h"
5 #include "input.h"
6
7 #include "vg/vg_rigidbody.h"
8 #include "scene_rigidbody.h"
9 #include "shaders/model_board_view.h"
10
11 static f32 k_glide_steer = 2.0f,
12 k_glide_cl = 0.04f,
13 k_glide_cs = 0.02f,
14 k_glide_drag = 0.0001f,
15 k_glide_slip_yaw = 0.1f,
16 k_glide_lift_pitch = 0.0f,
17 k_glide_wing_orient = -0.1f,
18 k_glide_balance = 1.0f;
19
20 static i32 k_glide_pause = 0;
21
22 static void player_glide_pre_update(void){
23 if( button_down(k_srbind_use) ){
24 localplayer.subsystem = k_player_subsystem_skate;
25 localplayer.glider_orphan = 1;
26
27 player_skate.state.activity = k_skate_activity_air;
28 player_skate.state.activity_prev = k_skate_activity_air;
29
30 q_mulv( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f}, player_skate.state.cog );
31 v3_add( player_skate.state.cog, localplayer.rb.co,
32 player_skate.state.cog );
33 v3_copy( localplayer.rb.v, player_skate.state.cog_v );
34
35 player__begin_holdout( (v3f){0.0f,0.0f,0.0f} );
36 player__skate_reset_animator();
37 player__skate_clear_mechanics();
38 v3_copy( (v3f){0.0f,0.0f,0.0f}, player_skate.state.trick_euler );
39
40 player__approximate_best_trajectory();
41 }
42 }
43
44 static void massless_accel( rigidbody *rb, v3f delta, v3f impulse ){
45 /* linear */
46 v3_muladds( rb->v, impulse, k_rb_delta, rb->v );
47
48 /* Angular velocity */
49 v3f wa;
50 v3_cross( delta, impulse, wa );
51 v3_muladds( rb->w, wa, k_rb_delta, rb->w );
52 }
53
54 static void calculate_lift( v3f vl, f32 aoa_bias,
55 v3f axis, v3f back, f32 power,
56 v3f out_force ){
57 v3f up;
58 v3_cross( back, axis, up );
59
60 v3f wind;
61 v3_muladds( vl, axis, -v3_dot(axis,vl), wind );
62
63 f32 windv2 = v3_length2(wind),
64 aoa = atan2f( v3_dot( up, wind ), v3_dot( back, wind ) ) + aoa_bias,
65 cl = aoa / VG_PIf, /* TODO: make it a curve */
66 L = windv2 * cl * power;
67
68 v3f lift_dir;
69 v3_normalize( wind );
70 v3_cross( wind, axis, lift_dir );
71
72 /* this is where induced drag (from the flappy things) would go */
73
74 v3_muls( lift_dir, L, out_force );
75 }
76
77 static void calculate_drag( v3f vl, f32 cd, v3f out_force ){
78 f32 v2 = v3_length2( vl );
79 v3f dir;
80 v3_copy( vl, dir );
81 v3_normalize( dir );
82 v3_muls( vl, -cd*v2, out_force );
83 }
84
85 /*
86 * Returns true if the bottom sphere is hit
87 */
88 static bool glider_physics( v2f steer ){
89 rigidbody *rb = &player_glide.rb;
90
91 /* lift */
92 v3f vl, wl;
93 m3x3_mulv( rb->to_local, rb->v, vl );
94 m3x3_mulv( rb->to_local, rb->w, wl );
95
96 v3f F, Flift, Fslip, Fdrag, FslipW, FliftW;
97
98 calculate_lift( vl, steer[1]*k_glide_steer,
99 (v3f){1,0,0},
100 (v3f){0,sinf(k_glide_wing_orient),cosf(k_glide_wing_orient)},
101 k_glide_cl, Flift );
102 v3_copy( Flift, player_glide.info_lift );
103 v3_cross( (v3f){0,0,0}, Flift, FliftW );
104
105 calculate_lift( vl, 0.0f,
106 (v3f){0,1,0},(v3f){0,0,1},
107 k_glide_cs, Fslip );
108 v3_copy( Fslip, player_glide.info_slip );
109 v3_cross( (v3f){0,k_glide_lift_pitch,k_glide_slip_yaw}, Fslip, FslipW );
110
111 calculate_drag( vl, k_glide_drag, Fdrag );
112 v3_copy( Fdrag, player_glide.info_drag );
113
114 v3f balance = {0.0f,-k_glide_balance,0.0f};
115 m3x3_mulv( rb->to_local, balance, balance );
116
117 v3f Fw = {
118 steer[1]*k_glide_steer - balance[2],
119 0.0f,
120 -steer[0]*k_glide_steer + balance[0],
121 };
122
123 if( player_glide.ticker ){
124 player_glide.ticker --;
125 return 0;
126 }
127 player_glide.ticker += k_glide_pause;
128
129 /* apply forces */
130 v3_add( Flift, Fslip, F );
131 v3_add( F, Fdrag, F );
132
133 m3x3_mulv( rb->to_world, F, F );
134 v3_muladds( rb->v, F, k_rb_delta, rb->v );
135
136 v3_add( Fw, FslipW, Fw );
137 v3_add( Fw, FliftW, Fw );
138 m3x3_mulv( rb->to_world, Fw, Fw );
139 v3_muladds( rb->w, Fw, k_rb_delta, rb->w );
140
141
142 /*
143 * collisions & constraints
144 */
145 world_instance *world = world_current_instance();
146 rb_solver_reset();
147
148 bool bottom_hit = 0;
149
150 rigidbody _null = {0};
151 _null.inv_mass = 0.0f;
152 m3x3_zero( _null.iI );
153 for( u32 i=0; i < vg_list_size(player_glide.parts); i ++ ){
154 m4x3f mmdl;
155 m4x3_mul( rb->to_world, player_glide.parts[i].mdl, mmdl );
156
157 if( player_glide.parts[i].shape == k_rb_shape_capsule ){
158 vg_line_capsule( mmdl,
159 player_glide.parts[i].inf.r,
160 player_glide.parts[i].inf.h,
161 VG__BLACK );
162 }
163 else if( player_glide.parts[i].shape == k_rb_shape_sphere ){
164 vg_line_sphere( mmdl, player_glide.parts[i].r, 0 );
165 }
166
167 if( rb_global_has_space() ){
168 rb_ct *buf = rb_global_buffer();
169
170 u32 l = 0;
171
172 if( player_glide.parts[i].shape == k_rb_shape_capsule ){
173 l = rb_capsule__scene( mmdl, &player_glide.parts[i].inf,
174 NULL, world->geo_bh, buf,
175 k_material_flag_ghosts );
176 }
177 else if( player_glide.parts[i].shape == k_rb_shape_sphere ){
178 l = rb_sphere__scene( mmdl, player_glide.parts[i].r,
179 NULL, world->geo_bh, buf,
180 k_material_flag_ghosts );
181 }
182
183 if( player_glide.parts[i].is_damage && l ){
184 bottom_hit = 1;
185 }
186
187 for( u32 j=0; j<l; j ++ ){
188 buf[j].rba = rb;
189 buf[j].rbb = &_null;
190 }
191
192 rb_contact_count += l;
193 }
194 }
195
196 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
197 for( u32 i=0; i<10; i ++ )
198 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
199
200 rb_iter( rb );
201 rb_update_matrices( rb );
202
203 return bottom_hit;
204 }
205
206 static void player_glide_update(void){
207 v2f steer;
208 joystick_state( k_srjoystick_steer, steer );
209
210 if( glider_physics( steer ) ){
211 player__dead_transition( k_player_die_type_generic );
212 localplayer.glider_orphan = 1;
213 }
214 }
215
216 static void player_glide_post_update(void){
217 v3_copy( player_glide.rb.co, localplayer.rb.co );
218 v4_copy( player_glide.rb.q, localplayer.rb.q );
219 v3_copy( player_glide.rb.v, localplayer.rb.v );
220 v3_copy( player_glide.rb.w, localplayer.rb.w );
221 rb_update_matrices( &localplayer.rb );
222 }
223
224 static void player_glide_animate(void){
225 struct player_glide *g = &player_glide;
226 struct player_glide_animator *animator = &g->animator;
227 rb_extrapolate( &localplayer.rb, animator->root_co, animator->root_q );
228 }
229
230 static void player_glide_pose( void *_animator, player_pose *pose ){
231 struct skeleton *sk = &localplayer.skeleton;
232 struct player_glide_animator *animator = _animator;
233
234 skeleton_sample_anim( sk, player_glide.anim_glide, 0.0f, pose->keyframes );
235
236 /* TODO: again the offset is wrong */
237 v3f temp;
238 q_mulv( pose->root_q, (v3f){0,-0.5f,0}, temp );
239
240 v3_add( animator->root_co, temp, pose->root_co );
241 v4_copy( animator->root_q, pose->root_q );
242 }
243
244 static void player_glide_post_animate(void){
245 if( localplayer.cam_control.camera_mode == k_cam_firstperson )
246 localplayer.cam_velocity_influence = 0.0f;
247 else
248 localplayer.cam_velocity_influence = 0.0f;
249
250 v3f fwd;
251 v3_muls( localplayer.rb.to_world[2], -1.0f, fwd );
252 v3_angles( fwd, localplayer.angles );
253
254 localplayer.cam_dist = 2.0f + v3_length( localplayer.rb.v )*0.2f;
255 }
256
257 static void player_glide_im_gui(void){
258 player__debugtext( 1, "Nothing here" );
259 player__debugtext( 1, " lift: %.2f %.2f %.2f",
260 player_glide.info_lift[0],
261 player_glide.info_lift[1],
262 player_glide.info_lift[2] );
263 player__debugtext( 1, " slip: %.2f %.2f %.2f",
264 player_glide.info_slip[0],
265 player_glide.info_slip[1],
266 player_glide.info_slip[2] );
267 player__debugtext( 1, " drag: %.2f %.2f %.2f",
268 player_glide.info_drag[0],
269 player_glide.info_drag[1],
270 player_glide.info_drag[2] );
271 }
272
273 static void player_glide_bind(void){
274
275 u32 mask = VG_VAR_CHEAT|VG_VAR_PERSISTENT;
276 VG_VAR_F32( k_glide_steer, flags=mask );
277 VG_VAR_F32( k_glide_cl, flags=mask );
278 VG_VAR_F32( k_glide_cs, flags=mask );
279 VG_VAR_F32( k_glide_drag, flags=mask );
280 VG_VAR_F32( k_glide_slip_yaw, flags=mask );
281 VG_VAR_F32( k_glide_lift_pitch, flags=mask );
282 VG_VAR_I32( k_glide_pause, flags=mask );
283 VG_VAR_F32( k_glide_balance, flags=mask );
284 VG_VAR_F32( k_glide_wing_orient, flags=mask );
285
286 f32 mass = 0.0f,
287 k_density = 8.0f,
288 k_inertia_scale = 1.0f;
289 m3x3f I;
290 m3x3_zero( I );
291
292 for( u32 i=0; i<vg_list_size(player_glide.parts); i ++ ){
293 /* create part transform matrix */
294 v4f qp, qy, qr, q;
295 q_axis_angle( qp, (v3f){1,0,0}, player_glide.parts[i].euler[0] );
296 q_axis_angle( qy, (v3f){0,1,0}, player_glide.parts[i].euler[1] );
297 q_axis_angle( qr, (v3f){0,0,1}, player_glide.parts[i].euler[2] );
298
299 q_mul( qr, qy, q );
300 q_mul( q, qp, q );
301
302 q_m3x3( q, player_glide.parts[i].mdl );
303 v3_copy( player_glide.parts[i].co, player_glide.parts[i].mdl[3] );
304
305 /* add it to inertia model */
306
307 if( player_glide.parts[i].shape == k_rb_shape_capsule ){
308 f32 r = player_glide.parts[i].inf.r,
309 h = player_glide.parts[i].inf.h,
310 pv = vg_capsule_volume( r, h ),
311 pm = pv * k_density;
312
313 mass += pm;
314
315 m3x3f pI;
316 vg_capsule_inertia( r, h, pm, pI );
317 vg_rotate_inertia( pI, player_glide.parts[i].mdl );
318 vg_translate_inertia( pI, pm, player_glide.parts[i].co );
319 m3x3_add( I, pI, I );
320 }
321 else if( player_glide.parts[i].shape == k_rb_shape_sphere ){
322 f32 r = player_glide.parts[i].r,
323 pv = vg_sphere_volume( r ),
324 pm = pv * k_density;
325
326 mass += pm;
327 m3x3f pI;
328 vg_sphere_inertia( r, pm, pI );
329 vg_translate_inertia( pI, pm, player_glide.parts[i].co );
330 m3x3_add( I, pI, I );
331 }
332 }
333
334 /* set inverses */
335 m3x3_inv( I, player_glide.rb.iI );
336 player_glide.rb.inv_mass = 1.0f / mass;
337
338 /* resources */
339 struct skeleton *sk = &localplayer.skeleton;
340 player_glide.anim_glide = skeleton_get_anim( sk, "glide_pose" );
341
342
343 void *alloc = vg_mem.rtmemory;
344 mdl_context *mdl = &player_glide.glider;
345
346 mdl_open( mdl, "models/glider.mdl", alloc );
347 mdl_load_metadata_block( mdl, alloc );
348
349 vg_linear_clear( vg_mem.scratch );
350
351 u32 count = mdl_arrcount( &mdl->textures );
352 player_glide.glider_textures =
353 vg_linear_alloc(alloc,vg_align8(sizeof(GLuint)*(count+1)));
354 player_glide.glider_textures[0] = vg.tex_missing;
355
356 mdl_async_load_glmesh( mdl, &player_glide.glider_mesh, NULL );
357
358 for( u32 i=0; i<count; i ++ ){
359 vg_linear_clear( vg_mem.scratch );
360 player_glide.glider_textures[i+1] = vg.tex_missing;
361
362 mdl_texture *tex = mdl_arritm( &mdl->textures, i );
363 void *data = vg_linear_alloc( vg_mem.scratch, tex->file.pack_size );
364 mdl_fread_pack_file( mdl, &tex->file, data );
365 vg_tex2d_load_qoi_async( data, tex->file.pack_size,
366 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
367 &player_glide.glider_textures[i+1] );
368 }
369
370 mdl_close( mdl );
371 }
372
373 static void player_glide_transition(void){
374 localplayer.subsystem = k_player_subsystem_glide;
375 localplayer.have_glider = 0;
376
377 v3_copy( localplayer.rb.co, player_glide.rb.co );
378
379 f32 dir = v3_dot( localplayer.rb.v, localplayer.rb.to_world[2] );
380
381 if( dir > 0.0f ){
382 v4f qyaw;
383 q_axis_angle( qyaw, (v3f){0,1,0}, VG_TAUf*0.5f );
384 q_mul( qyaw, localplayer.rb.q, player_glide.rb.q );
385 q_normalize( player_glide.rb.q );
386 }
387 else
388 v4_copy( localplayer.rb.q, player_glide.rb.q );
389
390 v3_copy( localplayer.rb.v, player_glide.rb.v );
391 v3_copy( localplayer.rb.w, player_glide.rb.w );
392 rb_update_matrices( &player_glide.rb );
393
394 player__begin_holdout( (v3f){0,0,0} );
395 }
396
397 static void player_glide_render( camera *cam, world_instance *world,
398 player_pose *pose ){
399 if( !((localplayer.subsystem == k_player_subsystem_glide) ||
400 localplayer.have_glider ||
401 localplayer.glider_orphan) )
402 return;
403
404 shader_model_board_view_use();
405 shader_model_board_view_uTexMain( 0 );
406 shader_model_board_view_uCamera( cam->transform[3] );
407 shader_model_board_view_uPv( cam->mtx.pv );
408
409 shader_model_board_view_uDepthCompare(1);
410 depth_compare_bind(
411 shader_model_board_view_uTexSceneDepth,
412 shader_model_board_view_uInverseRatioDepth,
413 shader_model_board_view_uInverseRatioMain,
414 cam );
415
416 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_board_view );
417
418 mdl_keyframe kf_res;
419 if( localplayer.glider_orphan ){
420 rb_extrapolate( &player_glide.rb, kf_res.co, kf_res.q );
421 v3_fill( kf_res.s, 1.0f );
422
423 v3f temp;
424 q_mulv( kf_res.q, (v3f){0,-0.5f,0}, temp );
425 v3_add( temp, kf_res.co, kf_res.co );
426 }
427 else {
428 f32 target;
429 if( localplayer.subsystem == k_player_subsystem_glide ) target = 1.0f;
430 else target = 0.0f;
431 vg_slewf( &player_glide.t, target, vg.time_frame_delta * 4.0f );
432
433 mdl_keyframe kf_backpack;
434
435 struct skeleton *sk = &localplayer.skeleton;
436 m4x3_mulv( localplayer.final_mtx[localplayer.id_chest ],
437 sk->bones[localplayer.id_chest].co,
438 kf_backpack.co );
439
440 v4f qyaw, qpitch, qchest, q;
441 q_axis_angle( qyaw, (v3f){0,1,0}, VG_TAUf*0.25f );
442 q_axis_angle( qpitch, (v3f){1,0,0}, VG_TAUf*0.25f );
443 m3x3_q( localplayer.final_mtx[ localplayer.id_chest ], qchest );
444
445 q_mul( qyaw, qpitch, q );
446 q_mul( qchest, q, kf_backpack.q );
447 q_normalize( kf_backpack.q );
448
449 f32 scale;
450 if( player_glide.t <= 0.0f ){
451 f32 st = player_glide.t + 1.0f,
452 sst = vg_smoothstepf(st),
453 isst= 1.0f - sst;
454 scale = vg_lerpf( 0.0f, 0.2f, sst );
455
456 v4f qspin;
457 q_axis_angle( qspin, (v3f){0,0,1}, VG_TAUf * isst * 0.5f );
458 q_mul( kf_backpack.q, qspin, kf_backpack.q );
459 kf_backpack.co[1] += isst * 1.0f;
460 v3_muladds( kf_backpack.co,
461 localplayer.final_mtx[ localplayer.id_chest ][0],
462 isst * 0.25f,
463 kf_backpack.co );
464 }
465 else{
466 scale = vg_lerpf( 0.2f, 1.0f, vg_smoothstepf(player_glide.t) );
467 }
468
469
470 v3_fill( kf_backpack.s, scale );
471
472 v3_copy( pose->root_co, kf_res.co );
473 v4_copy( pose->root_q, kf_res.q );
474 v3_fill( kf_res.s, scale );
475
476 f32 blend = vg_smoothstepf( vg_maxf( 0, player_glide.t ) );
477 keyframe_lerp( &kf_backpack, &kf_res, blend, &kf_res );
478 }
479
480 m4x3f mmdl;
481 q_m3x3( kf_res.q, mmdl );
482 m3x3_scale( mmdl, kf_res.s );
483 v3_copy( kf_res.co, mmdl[3] );
484
485 u32 current_tex = 0xffffffff;
486 glActiveTexture( GL_TEXTURE0 );
487
488 mdl_context *mdl = &player_glide.glider;
489 mesh_bind( &player_glide.glider_mesh );
490
491 for( u32 i=0; i<mdl_arrcount(&mdl->meshs); i ++ ){
492 mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
493
494 m4x3f mmmdl;
495 mdl_transform_m4x3( &mesh->transform, mmmdl );
496 m4x3_mul( mmdl, mmmdl, mmmdl );
497
498 shader_model_board_view_uMdl( mmmdl );
499
500 for( u32 j=0; j<mesh->submesh_count; j ++ ){
501 mdl_submesh *sm = mdl_arritm( &mdl->submeshs, mesh->submesh_start+j );
502 if( !sm->material_id ) {
503 vg_error( "Invalid material ID 0\n" );
504 continue;
505 }
506
507 mdl_material *mat = mdl_arritm( &mdl->materials, sm->material_id-1 );
508 if( mat->tex_diffuse != current_tex ){
509 glBindTexture( GL_TEXTURE_2D,
510 player_glide.glider_textures[ mat->tex_diffuse ] );
511 current_tex = mat->tex_diffuse;
512 }
513
514 mdl_draw_submesh( sm );
515 }
516 }
517 }
518
519 #endif /* PLAYER_GLIDE_C */