acd9e1e9082daace25308df87a95cfa0a8076159
[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 struct player_interface
17 {
18 rigidbody rb;
19 camera cam;
20
21 struct player_attachment
22 {
23 player_device *device;
24 void *storage;
25 }
26 dev;
27
28 struct input_binding *input_js1h,
29 *input_js1v,
30 *input_js2h,
31 *input_js2v,
32 *input_jump,
33 *input_push,
34 *input_walk,
35 *input_walkh,
36 *input_walkv,
37 *input_use,
38 *input_reset,
39 *input_grab;
40
41 v3f prev_position;
42
43 struct player_avatar *playeravatar;
44 glmesh *playermesh;
45 struct player_ragdoll ragdoll;
46 };
47
48 /* FIXME: yo */
49 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
50
51 struct player_device
52 {
53 void (* bind ) ( player_interface *player, player_attachment *at );
54 void (* pre_update) ( player_interface *player, player_attachment *at );
55 void (* update) ( player_interface *player, player_attachment *at );
56 void (* post_update)( player_interface *player, player_attachment *at );
57 void (* pose) ( player_interface *player, player_attachment *at,
58 player_pose pose, m4x3f transform );
59
60 void (* get_camera) ( player_interface *player, player_attachment *at,
61 camera *cam );
62
63 void (* attatch ) ( player_interface *player, player_attachment *at,
64 void *storage );
65
66 void (* reset ) ( player_interface *player, player_attachment *at,
67 struct respawn_point *spawn );
68
69 void (* store_state)( player_interface *player, player_attachment *at );
70 void (* load_state) ( player_interface *player, player_attachment *at );
71 void (* debug_ui) ( player_interface *player, player_attachment *at );
72 void (* gate_transport)( player_interface *player, player_attachment *at,
73 teleport_gate *gate );
74 };
75
76 VG_STATIC void player_interface_create_player( player_interface *inst )
77 {
78 static int only_once = 0;
79 assert( only_once == 0 );
80 only_once ++;
81
82 inst->input_js1h = vg_create_named_input( "steer-h", k_input_type_axis );
83 inst->input_js1v = vg_create_named_input( "steer-v", k_input_type_axis );
84 inst->input_grab = vg_create_named_input( "grab", k_input_type_axis_norm);
85 inst->input_js2h = vg_create_named_input( "grab-h", k_input_type_axis );
86 inst->input_js2v = vg_create_named_input( "grab-v", k_input_type_axis );
87 inst->input_jump = vg_create_named_input( "jump", k_input_type_button );
88 inst->input_push = vg_create_named_input( "push", k_input_type_button );
89 inst->input_walk = vg_create_named_input( "walk", k_input_type_button );
90 inst->input_walkh= vg_create_named_input( "walk-h", k_input_type_axis );
91 inst->input_walkv= vg_create_named_input( "walk-v", k_input_type_axis );
92 inst->input_use = vg_create_named_input( "use", k_input_type_button );
93 inst->input_reset= vg_create_named_input( "reset", k_input_type_button );
94
95 const char *default_cfg[] =
96 {
97 "bind steer-h gp-ls-h",
98 "bind -steer-h a",
99 "bind +steer-h d",
100
101 "bind steer-v gp-ls-v",
102 "bind -steer-v w",
103 "bind +steer-v s",
104
105 "bind grab gp-rt",
106 "bind +grab shift",
107 "bind grab-h gp-rs-h",
108 "bind grab-v gp-rs-v",
109
110 "bind jump space",
111 "bind jump gp-a",
112
113 "bind push gp-b",
114 "bind push w",
115
116 "bind walk shift",
117 "bind walk gp-ls",
118
119 "bind walk-h gp-ls-h",
120 "bind walk-v -gp-ls-v",
121 "bind +walk-h d",
122 "bind -walk-h a",
123 "bind +walk-v w",
124 "bind -walk-v s",
125
126 "bind reset gp-lb",
127 "bind reset r",
128
129 "bind use gp-y",
130 "bind use e",
131 };
132
133 for( int i=0; i<vg_list_size(default_cfg); i++ )
134 vg_execute_console_input(default_cfg[i]);
135
136 v3_zero( inst->rb.co );
137 v3_zero( inst->rb.w );
138 v3_zero( inst->rb.v );
139 q_identity( inst->rb.q );
140 m4x3_identity( inst->rb.to_world );
141 m4x3_identity( inst->rb.to_local );
142 }
143
144 VG_STATIC void player_use_avatar( player_interface *player,
145 struct player_avatar *av )
146 {
147 player->playeravatar = av;
148 player_setup_ragdoll_from_avatar( &player->ragdoll, av );
149 }
150
151 VG_STATIC void player_use_mesh( player_interface *player, glmesh *mesh )
152 {
153 player->playermesh = mesh;
154 }
155
156 /* FIXME: Seperate concepts for binding and equip.
157 */
158 VG_STATIC void player_use_device( player_interface *player, player_device *dev,
159 void *storage )
160 {
161 player->dev.device = dev;
162 player->dev.storage = storage;
163
164 player->dev.device->bind( player, &player->dev );
165 }
166
167 VG_STATIC void player_pre_update( player_interface *player )
168 {
169 assert( player->dev.device );
170
171 v3_copy( player->rb.co, player->prev_position );
172
173 if( player->dev.device->pre_update )
174 player->dev.device->pre_update( player, &player->dev );
175 }
176
177 VG_STATIC void player_update( player_interface *player )
178 {
179 assert( player->dev.device );
180
181 if( player->dev.device->update )
182 player->dev.device->update( player, &player->dev );
183 }
184
185 VG_STATIC void player_apply_transport_to_cam( m4x3f transport )
186 {
187 /* FIXME: Applies to main_camera directly! */
188
189 /* Pre-emptively edit the camera matrices so that the motion vectors
190 * are correct */
191 m4x3f transport_i;
192 m4x4f transport_4;
193 m4x3_invert_affine( transport, transport_i );
194 m4x3_expand( transport_i, transport_4 );
195 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
196 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
197 }
198
199 VG_STATIC void player_post_update( player_interface *player )
200 {
201 /* FIXME: Applies to main_camera directly! */
202
203 assert( player->dev.device );
204
205 if( player->dev.device->post_update )
206 player->dev.device->post_update( player, &player->dev );
207
208 /* FIXME: only need to test against the visible gate....
209 * OR... bvh */
210
211 for( int i=0; i<world.gate_count; i++ )
212 {
213 struct route_gate *rg = &world.gates[i];
214 teleport_gate *gate = &rg->gate;
215
216 if( gate_intersect( gate, player->rb.co, player->prev_position ) )
217 {
218 player->dev.device->gate_transport( player, &player->dev, gate );
219 v3_copy( player->rb.co, player->prev_position );
220 }
221 }
222
223 #if 0
224 camera_update_transform( &player->cam );
225 camera_update_view( &player->cam );
226 camera_update_projection( &player->cam );
227 camera_finalize( &player->cam );
228 #endif
229 }
230
231 #if 0
232 VG_STATIC void player_pre_render( player_interface *player )
233 {
234 assert( player->dev.device );
235
236 if( player->dev.device->pre_render )
237 player->dev.device->pre_render( player );
238 }
239 #endif
240
241 VG_STATIC void player_pre_render( player_interface *player )
242 {
243 player_pose pose;
244 m4x3f transform;
245
246 /* FIXME: Give devices more control over these render stages, and have
247 * 'API calls'
248 * for this kindof crap instead of it dictating order... */
249
250 if( player->dev.device->pose )
251 {
252 player->dev.device->pose( player, &player->dev, pose, transform );
253
254 struct skeleton *sk = &player->playeravatar->sk;
255
256 skeleton_apply_pose( sk, pose, k_anim_apply_defer_ik );
257 skeleton_apply_ik_pass( sk );
258 skeleton_apply_pose( sk, pose, k_anim_apply_deffered_only );
259 skeleton_apply_inverses( sk );
260 skeleton_apply_transform( sk, transform );
261 skeleton_debug( sk );
262 }
263
264 player->dev.device->get_camera( player, &player->dev, &player->cam );
265 /* TODO: if dead copy ragdoll.. . */
266 }
267
268 VG_STATIC void player_render( camera *cam, player_interface *player )
269 {
270 shader_viewchar_use();
271 vg_tex2d_bind( &tex_characters, 0 );
272 shader_viewchar_uTexMain( 0 );
273 shader_viewchar_uCamera( cam->transform[3] );
274 shader_viewchar_uPv( cam->mtx.pv );
275 shader_link_standard_ub( _shader_viewchar.id, 2 );
276 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
277 player->playeravatar->sk.bone_count,
278 0,
279 (float *)player->playeravatar->sk.final_mtx );
280
281 mesh_bind( player->playermesh );
282 mesh_draw( player->playermesh );
283 }
284
285 VG_STATIC void player_debugtext( int size, const char *fmt, ... )
286 {
287 char buffer[ 1024 ];
288
289 va_list args;
290 va_start( args, fmt );
291 vsnprintf( buffer, 1024, fmt, args );
292 va_end( args );
293
294 ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
295 vg_uictx.cursor[1] += 14*size;
296 }
297
298 VG_STATIC void player_ui( player_interface *player )
299 {
300 /* TODO: if debugger enabled */
301
302 if( player->dev.device->debug_ui )
303 {
304 vg_uictx.cursor[0] = vg.window_x - 200;
305 vg_uictx.cursor[1] = 0;
306 vg_uictx.cursor[2] = 200;
307 vg_uictx.cursor[3] = 200;
308
309 struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 );
310
311 vg_uictx.cursor[0] = vg.window_x;
312 player->dev.device->debug_ui( player, &player->dev );
313
314 b[2].co[1] = vg_uictx.cursor[1];
315 b[3].co[1] = vg_uictx.cursor[1];
316 }
317 }
318
319 VG_STATIC void player_spawn( player_interface *player,
320 struct respawn_point *rp )
321 {
322 v3_copy( rp->co, player->rb.co );
323 v3_copy( rp->co, player->prev_position );
324 v3_zero( player->rb.v );
325 v3_zero( player->rb.w );
326 q_identity( player->rb.q );
327 rb_update_transform( &player->rb );
328
329 if( player->dev.device->reset )
330 player->dev.device->reset( player, &player->dev, rp );
331 }
332
333 VG_STATIC void player_kill( player_interface *player )
334 {
335
336 }
337
338 /*
339 * Apply per render-frame mouse look from player to angles
340 */
341 VG_STATIC void player_look( player_interface *player, v3f angles )
342 {
343 angles[2] = 0.0f;
344 v2_muladds( angles, vg.mouse_delta, 0.0025f, angles );
345
346 if( vg_input.controller_should_use_trackpad_look )
347 {
348 static v2f last_input;
349 static v2f vel;
350 static v2f vel_smooth;
351
352 v2f input = { player->input_js2h->axis.value,
353 player->input_js2v->axis.value };
354
355 if( (v2_length2(last_input) > 0.001f) && (v2_length2(input) > 0.001f) )
356 {
357 v2_sub( input, last_input, vel );
358 v2_muls( vel, 1.0f/vg.time_delta, vel );
359 }
360 else
361 {
362 v2_zero( vel );
363 }
364
365 v2_lerp( vel_smooth, vel, vg.time_delta*8.0f, vel_smooth );
366
367 v2_muladds( angles, vel_smooth, vg.time_delta, angles );
368 v2_copy( input, last_input );
369 }
370 else
371 {
372 angles[0] += player->input_js2h->axis.value * vg.time_delta * 4.0f;
373 angles[1] += player->input_js2v->axis.value * vg.time_delta * 4.0f;
374 }
375
376 angles[1] = vg_clampf( angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
377 }
378
379 #endif /* PLAYER_INTERFACE_H */