yeah yeah yeah
[carveJwlIkooP6JGAAIwe30JlM.git] / player_interface.h
1 #ifndef PLAYER_INTERFACE_H
2 #define PLAYER_INTERFACE_H
3
4 #include "model.h"
5 #include "camera.h"
6 #include "rigidbody.h"
7 #include "player_ragdoll.h"
8 #include "player_model.h"
9 #include "world.h"
10
11 typedef struct player_device player_device;
12 typedef struct player_interface player_interface;
13 typedef struct player_attachment player_attachment;
14 typedef mdl_keyframe player_pose[32];
15
16 #define PLAYER_DEVICE_API VG_STATIC
17
18 struct player_interface
19 {
20 rigidbody rb;
21 camera cam;
22
23 struct player_attachment
24 {
25 player_device *device;
26 void *storage;
27
28 /* animation driven */
29 player_pose pose;
30 v3f pose_root_co;
31 v4f pose_root_q;
32 camera cam_1st, cam_3rd;
33 }
34 dev,
35 dev_previous;
36
37 enum camera_mode
38 {
39 k_camera_mode_firstperson,
40 k_camera_mode_thirdperson
41 }
42 camera_mode;
43
44 float camera_type_blend;
45
46 /* TODO: have an automated system for crossing the thirdperson camera
47 * across portal boundaries. if it fails the check with the gate plane, then
48 * transform root_co and root_q, as well as final camera, BACK across the
49 * division, using the inverse of the transport matrix
50 */
51
52 int device_blend;
53 float device_blend_time;
54
55 struct input_binding *input_js1h,
56 *input_js1v,
57 *input_js2h,
58 *input_js2v,
59 *input_jump,
60 *input_push,
61 *input_walk,
62 *input_walkh,
63 *input_walkv,
64 *input_use,
65 *input_reset,
66 *input_grab,
67 *input_camera;
68
69 #if 0
70 v3f prev_position;
71 #endif
72
73 struct player_avatar *playeravatar;
74 glmesh *playermesh;
75 struct player_ragdoll ragdoll;
76
77
78 /* FIXME: eventually store animation state here when we have more than 1
79 * player. since currently its written into the avatar
80 *
81 * struct avatar_anim_state anim_state;
82 */
83 };
84
85 /* FIXME: yo */
86 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
87
88 struct player_device
89 {
90 void (* bind ) ( player_interface *player, player_attachment *at );
91
92 /*
93 * Regular updates
94 */
95 void (* pre_update) ( player_interface *player, player_attachment *at );
96 void (* update) ( player_interface *player, player_attachment *at );
97 void (* post_update)( player_interface *player, player_attachment *at );
98
99 #if 0
100 /*
101 * Get current pose, and root transform
102 */
103 void (* pose) ( player_interface *player, player_attachment *at,
104 player_pose pose, v3f root_co, v4f root_q );
105 #endif
106
107 /*
108 * Use this to fill out animation state
109 */
110 void (* animate) ( player_interface *player, player_attachment *at );
111
112 /* Get current camera, required fields to be filled are:
113 * fov
114 * pos
115 * angles
116 *
117 * They may be blended with other systems
118 */
119 void (* post_animate) ( player_interface *player, player_attachment *at );
120
121 /*
122 This is called when a player is forced back to a spawnpoint.
123 */
124 void (* reset ) ( player_interface *player, player_attachment *at,
125 struct respawn_point *spawn );
126
127 /*
128 * make calls into player_debugtext( .. ) in this function
129 */
130 void (* debug_ui) ( player_interface *player, player_attachment *at );
131
132 #if 0
133 /*
134 * Called when going through a gate, it should modify any direction and
135 * position sensitive things, as well as store context here. it may be
136 * restored later.
137 */
138 void (* gate_transport)( player_interface *player, player_attachment *at,
139 teleport_gate *gate );
140 #endif
141
142 /*
143 * Load the state previously saved when gate_transport was called
144 */
145 void (* load_state) ( player_interface *player, player_attachment *at );
146
147
148
149
150 #if 0
151 void (* store_state)( player_interface *player, player_attachment *at );
152 void (* attatch ) ( player_interface *player, player_attachment *at,
153 void *storage );
154 #endif
155
156 };
157
158 VG_STATIC void player_interface_create_player( player_interface *inst )
159 {
160 static int only_once = 0;
161 assert( only_once == 0 );
162 only_once ++;
163
164 inst->input_js1h = vg_create_named_input( "steer-h", k_input_type_axis );
165 inst->input_js1v = vg_create_named_input( "steer-v", k_input_type_axis );
166 inst->input_grab = vg_create_named_input( "grab", k_input_type_axis_norm);
167 inst->input_js2h = vg_create_named_input( "grab-h", k_input_type_axis );
168 inst->input_js2v = vg_create_named_input( "grab-v", k_input_type_axis );
169 inst->input_jump = vg_create_named_input( "jump", k_input_type_button );
170 inst->input_push = vg_create_named_input( "push", k_input_type_button );
171 inst->input_walk = vg_create_named_input( "walk", k_input_type_button );
172 inst->input_walkh= vg_create_named_input( "walk-h", k_input_type_axis );
173 inst->input_walkv= vg_create_named_input( "walk-v", k_input_type_axis );
174 inst->input_use = vg_create_named_input( "use", k_input_type_button );
175 inst->input_reset= vg_create_named_input( "reset", k_input_type_button );
176 inst->input_camera=vg_create_named_input( "camera", k_input_type_button );
177
178 const char *default_cfg[] =
179 {
180 "bind steer-h gp-ls-h",
181 "bind -steer-h a",
182 "bind +steer-h d",
183
184 "bind steer-v gp-ls-v",
185 "bind -steer-v w",
186 "bind +steer-v s",
187
188 "bind grab gp-rt",
189 "bind +grab shift",
190 "bind grab-h gp-rs-h",
191 "bind grab-v gp-rs-v",
192
193 "bind jump space",
194 "bind jump gp-a",
195
196 "bind push gp-b",
197 "bind push w",
198
199 "bind walk shift",
200 "bind walk gp-ls",
201
202 "bind walk-h gp-ls-h",
203 "bind walk-v -gp-ls-v",
204 "bind +walk-h d",
205 "bind -walk-h a",
206 "bind +walk-v w",
207 "bind -walk-v s",
208
209 "bind reset gp-lb",
210 "bind reset r",
211
212 "bind use gp-y",
213 "bind use e",
214 "bind camera c"
215 };
216
217 for( int i=0; i<vg_list_size(default_cfg); i++ )
218 vg_execute_console_input(default_cfg[i]);
219
220 v3_zero( inst->rb.co );
221 v3_zero( inst->rb.w );
222 v3_zero( inst->rb.v );
223 q_identity( inst->rb.q );
224 m4x3_identity( inst->rb.to_world );
225 m4x3_identity( inst->rb.to_local );
226 }
227
228 VG_STATIC void player_use_avatar( player_interface *player,
229 struct player_avatar *av )
230 {
231 player->playeravatar = av;
232 player_setup_ragdoll_from_avatar( &player->ragdoll, av );
233 }
234
235 VG_STATIC void player_use_mesh( player_interface *player, glmesh *mesh )
236 {
237 player->playermesh = mesh;
238 }
239
240 /* FIXME: Seperate concepts for binding and equip.
241 */
242 VG_STATIC void player_use_device( player_interface *player, player_device *dev,
243 void *storage )
244 {
245 player->dev.device = dev;
246 player->dev.storage = storage;
247
248 player->dev.device->bind( player, &player->dev );
249 }
250
251 VG_STATIC void player_pre_update( player_interface *player )
252 {
253 assert( player->dev.device );
254
255 if( vg_input_button_down( player->input_camera ) )
256 {
257 if( player->camera_mode == k_camera_mode_firstperson )
258 player->camera_mode = k_camera_mode_thirdperson;
259 else
260 player->camera_mode = k_camera_mode_firstperson;
261 }
262
263 #if 0
264 v3_copy( player->rb.co, player->prev_position );
265 #endif
266
267 if( player->dev.device->pre_update )
268 player->dev.device->pre_update( player, &player->dev );
269 }
270
271 VG_STATIC void player_update( player_interface *player )
272 {
273 assert( player->dev.device );
274
275 if( player->dev.device->update )
276 player->dev.device->update( player, &player->dev );
277 }
278
279 VG_STATIC void player_apply_transport_to_cam( m4x3f transport )
280 {
281 /* FIXME: Applies to main_camera directly! */
282
283 /* Pre-emptively edit the camera matrices so that the motion vectors
284 * are correct */
285 m4x3f transport_i;
286 m4x4f transport_4;
287 m4x3_invert_affine( transport, transport_i );
288 m4x3_expand( transport_i, transport_4 );
289 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
290 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
291 }
292
293 /*
294 * Applies gate transport to a player_interface
295 */
296 PLAYER_DEVICE_API
297 void player_pass_gate( player_interface *player, teleport_gate *gate )
298 {
299
300 }
301
302 VG_STATIC void player_post_update( player_interface *player )
303 {
304 assert( player->dev.device );
305
306 if( player->dev.device->post_update )
307 player->dev.device->post_update( player, &player->dev );
308 }
309
310 VG_STATIC void player_pre_render( player_interface *player )
311 {
312 player->dev.device->animate( player, &player->dev );
313
314 /* TODO: eventually, blending code goes here */
315
316 m4x3f transform;
317 q_m3x3( player->dev.pose_root_q, transform );
318 v3_copy( player->dev.pose_root_co, transform[3] );
319
320 struct skeleton *sk = &player->playeravatar->sk;
321
322 skeleton_apply_pose( sk, player->dev.pose, k_anim_apply_defer_ik );
323 skeleton_apply_ik_pass( sk );
324 skeleton_apply_pose( sk, player->dev.pose, k_anim_apply_deffered_only );
325 skeleton_apply_inverses( sk );
326 skeleton_apply_transform( sk, transform );
327 skeleton_debug( sk );
328
329 #if 0
330 if( player->dev.device->pose )
331 {
332 player->dev.device->pose( player, &player->dev, pose, transform );
333
334 struct skeleton *sk = &player->playeravatar->sk;
335
336 skeleton_apply_pose( sk, pose, k_anim_apply_defer_ik );
337 skeleton_apply_ik_pass( sk );
338 skeleton_apply_pose( sk, pose, k_anim_apply_deffered_only );
339 skeleton_apply_inverses( sk );
340 skeleton_apply_transform( sk, transform );
341 skeleton_debug( sk );
342 }
343 #endif
344
345 player->dev.device->post_animate( player, &player->dev );
346
347 /* TODO: eventually, blending code goes here */
348
349 float camera_blend_target = 1.0f;
350 if( player->camera_mode == k_camera_mode_firstperson )
351 camera_blend_target = 0.0f;
352
353 player->camera_type_blend = vg_lerpf( player->camera_type_blend,
354 camera_blend_target,
355 5.0f * vg.frame_delta );
356
357 float t = player->camera_type_blend;
358 camera_lerp( &player->dev.cam_1st, &player->dev.cam_3rd, t, &player->cam );
359
360
361 #if 0
362 v3_copy( player->dev.cam_1st.pos, player->cam.pos );
363 v3_copy( player->dev.cam_1st.angles, player->cam.angles );
364 player->cam.fov = player->dev.cam_1st.fov;
365 #endif
366 }
367
368 VG_STATIC void player_render( camera *cam, player_interface *player )
369 {
370 shader_viewchar_use();
371 vg_tex2d_bind( &tex_characters, 0 );
372 shader_viewchar_uTexMain( 0 );
373 shader_viewchar_uCamera( cam->transform[3] );
374 shader_viewchar_uPv( cam->mtx.pv );
375 shader_link_standard_ub( _shader_viewchar.id, 2 );
376 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
377 player->playeravatar->sk.bone_count,
378 0,
379 (float *)player->playeravatar->sk.final_mtx );
380
381 mesh_bind( player->playermesh );
382 mesh_draw( player->playermesh );
383 }
384
385 VG_STATIC void player_debugtext( int size, const char *fmt, ... )
386 {
387 char buffer[ 1024 ];
388
389 va_list args;
390 va_start( args, fmt );
391 vsnprintf( buffer, 1024, fmt, args );
392 va_end( args );
393
394 ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
395 vg_uictx.cursor[1] += 14*size;
396 }
397
398 VG_STATIC void player_ui( player_interface *player )
399 {
400 /* TODO: if debugger enabled */
401
402 if( player->dev.device->debug_ui )
403 {
404 vg_uictx.cursor[0] = vg.window_x - 200;
405 vg_uictx.cursor[1] = 0;
406 vg_uictx.cursor[2] = 200;
407 vg_uictx.cursor[3] = 200;
408
409 struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 );
410
411 vg_uictx.cursor[0] = vg.window_x;
412 player->dev.device->debug_ui( player, &player->dev );
413
414 b[2].co[1] = vg_uictx.cursor[1];
415 b[3].co[1] = vg_uictx.cursor[1];
416 }
417 }
418
419 VG_STATIC void player_spawn( player_interface *player,
420 struct respawn_point *rp )
421 {
422 v3_copy( rp->co, player->rb.co );
423 #if 0
424 v3_copy( rp->co, player->prev_position );
425 #endif
426 v3_zero( player->rb.v );
427 v3_zero( player->rb.w );
428 q_identity( player->rb.q );
429 rb_update_transform( &player->rb );
430
431 if( player->dev.device->reset )
432 player->dev.device->reset( player, &player->dev, rp );
433 }
434
435
436 VG_STATIC void player_kill( player_interface *player )
437 {
438
439 }
440
441 /*
442 * Apply per render-frame mouse look from player to angles
443 */
444 PLAYER_DEVICE_API
445 void player_look( player_interface *player, v3f angles )
446 {
447 angles[2] = 0.0f;
448 v2_muladds( angles, vg.mouse_delta, 0.0025f, angles );
449
450 if( vg_input.controller_should_use_trackpad_look )
451 {
452 static v2f last_input;
453 static v2f vel;
454 static v2f vel_smooth;
455
456 v2f input = { player->input_js2h->axis.value,
457 player->input_js2v->axis.value };
458
459 if( (v2_length2(last_input) > 0.001f) && (v2_length2(input) > 0.001f) )
460 {
461 v2_sub( input, last_input, vel );
462 v2_muls( vel, 1.0f/vg.time_delta, vel );
463 }
464 else
465 {
466 v2_zero( vel );
467 }
468
469 v2_lerp( vel_smooth, vel, vg.time_delta*8.0f, vel_smooth );
470
471 v2_muladds( angles, vel_smooth, vg.time_delta, angles );
472 v2_copy( input, last_input );
473 }
474 else
475 {
476 angles[0] += player->input_js2h->axis.value * vg.time_delta * 4.0f;
477 angles[1] += player->input_js2v->axis.value * vg.time_delta * 4.0f;
478 }
479
480 angles[1] = vg_clampf( angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
481 }
482
483 #endif /* PLAYER_INTERFACE_H */