added models
[carveJwlIkooP6JGAAIwe30JlM.git] / character.h
1 #ifndef CHARACTER_H
2 #define CHARACTER_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "scene.h"
7 #include "ik.h"
8 #include "rigidbody.h"
9 #include "shaders/character.h"
10
11 vg_tex2d tex_pallet = { .path = "textures/ch_gradient.qoi" };
12
13 static void character_register(void)
14 {
15 shader_character_register();
16 }
17
18 static void character_init(void)
19 {
20 vg_tex2d_init( (vg_tex2d *[]){ &tex_pallet }, 1 );
21 }
22
23 #define FOREACH_PART(FN) \
24 FN( foot_l ) \
25 FN( foot_r ) \
26 FN( sock_l ) \
27 FN( sock_r ) \
28 FN( body0 ) \
29 FN( body1 ) \
30 FN( neck ) \
31 FN( head ) \
32 FN( arm_l0 ) \
33 FN( arm_l1 ) \
34 FN( hand_l ) \
35 FN( arm_r0 ) \
36 FN( arm_r1 ) \
37 FN( hand_r ) \
38 FN( leg_l0 ) \
39 FN( leg_l1 ) \
40 FN( leg_r0 ) \
41 FN( leg_r1 ) \
42 FN( wf ) \
43 FN( wb ) \
44 FN( board ) \
45
46 #define MAKE_ENUM(ENUM) k_chpart_##ENUM,
47 #define MAKE_STRING(STR) #STR,
48 #define ADD_ONE(_) +1
49 #define PART_COUNT FOREACH_PART(ADD_ONE)
50
51
52 enum character_part
53 {
54 FOREACH_PART( MAKE_ENUM )
55 };
56
57 static const char *character_part_strings[] =
58 {
59 FOREACH_PART( MAKE_STRING )
60 };
61
62 struct character
63 {
64 glmesh mesh;
65
66 mdl_submesh parts[ PART_COUNT ];
67 v3f origins[ PART_COUNT ];
68 m4x3f matrices[ PART_COUNT ];
69
70 /* Auxillary information */
71 v3f offsets[ PART_COUNT ];
72 rigidbody ragdoll[ PART_COUNT ];
73
74 /*
75 * Controls
76 * note: - base nodes of IK structures will be filled automatically
77 * - all positions are in local space to the mroot
78 */
79 struct ik_basic ik_arm_l, ik_arm_r,
80 ik_leg_l, ik_leg_r,
81 ik_body;
82 v3f cam_pos;
83
84 v4f qhead;
85 float rhip, rcollar, /* twist of hip and collar controls,
86 these act in the local +y axis of the part */
87 rhead,
88 rfootl, rfootr,
89 rhandl, rhandr;
90
91 v3f ground_normal; /* Feet will be aligned to this */
92 m4x3f mroot;
93
94 int shoes[2];
95 };
96
97 static void character_offset( struct character *ch, enum character_part parent,
98 enum character_part child )
99 {
100 v3_sub( ch->origins[ child ], ch->origins[ parent ],
101 ch->offsets[ child ] );
102 }
103
104 static int character_load( struct character *ch, const char *name )
105 {
106 char buf[64];
107
108 snprintf( buf, sizeof(buf)-1, "models/%s.mdl", name );
109 mdl_header *src = mdl_load( buf );
110
111 if( !src )
112 return 0;
113
114 int error_count = 0;
115
116 for( int i=0; i<PART_COUNT; i++ )
117 {
118 snprintf( buf, sizeof(buf)-1, "%s_%s", name, character_part_strings[i] );
119 mdl_node *pnode = mdl_node_from_name( src, buf );
120
121 memset( &ch->parts[i], 0, sizeof(mdl_submesh) );
122 v3_zero( ch->origins[i] );
123
124 if( !pnode )
125 {
126 vg_warn( "Character file does not contain an '_%s' part.\n",
127 character_part_strings[i] );
128 error_count ++;
129 continue;
130 }
131
132 mdl_submesh *sm = mdl_node_submesh( src, pnode, 0 );
133
134 if( !sm )
135 {
136 vg_warn( "Character file's '_%s' part has no mesh.\n",
137 character_part_strings[i] );
138 error_count ++;
139 continue;
140 }
141
142 ch->parts[i] = *sm;
143 v3_copy( pnode->co, ch->origins[i] );
144 }
145
146 mdl_unpack_glmesh( src, &ch->mesh );
147
148 if( !error_count )
149 vg_success( "Loaded character file '%s' with no errors\n", name );
150
151 /* Create the offsets */
152 character_offset( ch, k_chpart_body0, k_chpart_body1 );
153 character_offset( ch, k_chpart_body1, k_chpart_neck );
154 character_offset( ch, k_chpart_neck, k_chpart_head );
155
156 character_offset( ch, k_chpart_body1, k_chpart_arm_l0 );
157 character_offset( ch, k_chpart_arm_l0, k_chpart_arm_l1 );
158 character_offset( ch, k_chpart_arm_l1, k_chpart_hand_l );
159
160 character_offset( ch, k_chpart_body1, k_chpart_arm_r0 );
161 character_offset( ch, k_chpart_arm_r0, k_chpart_arm_r1 );
162 character_offset( ch, k_chpart_arm_r1, k_chpart_hand_r );
163
164 character_offset( ch, k_chpart_body0, k_chpart_leg_l0 );
165 character_offset( ch, k_chpart_leg_l0, k_chpart_leg_l1 );
166 character_offset( ch, k_chpart_leg_l1, k_chpart_foot_l );
167
168 character_offset( ch, k_chpart_body0, k_chpart_leg_r0 );
169 character_offset( ch, k_chpart_leg_r0, k_chpart_leg_r1 );
170 character_offset( ch, k_chpart_leg_r1, k_chpart_foot_r );
171
172 character_offset( ch, k_chpart_board, k_chpart_wb );
173 character_offset( ch, k_chpart_board, k_chpart_wf );
174
175 ch->ik_arm_l.l1 = v3_length( ch->offsets[ k_chpart_arm_l1 ] );
176 ch->ik_arm_l.l2 = v3_length( ch->offsets[ k_chpart_hand_l ] );
177 ch->ik_arm_r.l1 = v3_length( ch->offsets[ k_chpart_arm_r1 ] );
178 ch->ik_arm_r.l2 = v3_length( ch->offsets[ k_chpart_hand_r ] );
179
180 ch->ik_leg_l.l1 = v3_length( ch->offsets[ k_chpart_leg_l1 ] );
181 ch->ik_leg_l.l2 = v3_length( ch->offsets[ k_chpart_foot_l ] );
182 ch->ik_leg_r.l1 = v3_length( ch->offsets[ k_chpart_leg_r1 ] );
183 ch->ik_leg_r.l2 = v3_length( ch->offsets[ k_chpart_foot_r ] );
184
185 ch->ik_body.l1 = v3_length( ch->offsets[ k_chpart_body1 ] );
186 ch->ik_body.l2 = v3_length( ch->offsets[ k_chpart_neck ] );
187
188 free( src );
189 return 1;
190 }
191
192 static void align_to_board( struct character *ch, enum character_part id )
193 {
194 /* Calculate rotation between board and feet */
195 m4x3f *mats = ch->matrices;
196
197 v3f foot_pos, foot_fwd, foot_target, board_norm, board_origin;
198 v3_copy( mats[id][3], foot_pos );
199 m3x3_mulv( mats[id], (v3f){1.0f,0.0f,0.0f}, foot_fwd );
200 v3_add( foot_fwd, foot_pos, foot_target );
201
202 m3x3_mulv( mats[k_chpart_board], (v3f){0.0f,1.0f,0.0f}, board_norm );
203 m4x3_mulv( mats[k_chpart_board], (v3f){0.0f,0.13f,0.0f}, board_origin );
204
205 vg_line( foot_pos, foot_target, 0xff00ff00 );
206
207 v3f v0;
208 v3_sub( board_origin, foot_target, v0 );
209 float t = v3_dot( v0, board_norm ) / board_norm[1];
210 foot_target[1] += t;
211 vg_line( foot_pos, foot_target, 0xff00ffff );
212
213 v3_sub( foot_target, foot_pos, foot_target );
214 v3_normalize( foot_target );
215 float ang = acosf( v3_dot( foot_target, foot_fwd ) );
216
217 v4f qcorrection; m3x3f correction;
218 q_axis_angle( qcorrection, (v3f){0.0f,0.0f,1.0f}, -ang );
219 q_m3x3( qcorrection, correction );
220 m3x3_mul( mats[id], correction, mats[id] );
221 }
222
223 static void character_eval( struct character *ch )
224 {
225 m4x3f *mats = ch->matrices;
226 v3f *offs = ch->offsets;
227
228 ik_basic( &ch->ik_body, mats[k_chpart_body0], mats[k_chpart_body1],
229 k_ikY, k_ikX );
230
231 m3x3f temp;
232 v4f body_rotation;
233 /* TODO: Do this directly via m3x3 */
234
235 q_axis_angle( body_rotation, (v3f){0.0f,1.0f,0.0f}, ch->rhip );
236 q_m3x3( body_rotation, temp );
237 m3x3_mul( mats[k_chpart_body0], temp, mats[k_chpart_body0] );
238
239 q_axis_angle( body_rotation, (v3f){0.0f,1.0f,0.0f}, ch->rcollar );
240 q_m3x3( body_rotation, temp );
241 m3x3_mul( mats[k_chpart_body1], temp, mats[k_chpart_body1] );
242
243 /* Setup aux */
244 m4x3_mulv( mats[k_chpart_body0], offs[k_chpart_leg_l0], ch->ik_leg_l.base );
245 m4x3_mulv( mats[k_chpart_body0], offs[k_chpart_leg_r0], ch->ik_leg_r.base );
246 m4x3_mulv( mats[k_chpart_body1], offs[k_chpart_arm_l0], ch->ik_arm_l.base );
247 m4x3_mulv( mats[k_chpart_body1], offs[k_chpart_arm_r0], ch->ik_arm_r.base );
248
249 /* IK for arms and legs */
250 ik_basic( &ch->ik_arm_l, mats[k_chpart_arm_l0], mats[k_chpart_arm_l1],
251 k_ikZ, k_ikY );
252 ik_basic( &ch->ik_arm_r, mats[k_chpart_arm_r0], mats[k_chpart_arm_r1],
253 k_iknZ, k_ikY );
254 ik_basic( &ch->ik_leg_l, mats[k_chpart_leg_l0], mats[k_chpart_leg_l1],
255 k_ikY, k_iknX );
256 ik_basic( &ch->ik_leg_r, mats[k_chpart_leg_r0], mats[k_chpart_leg_r1],
257 k_ikY, k_iknX );
258
259 /* Hands */
260 m3x3_copy( mats[k_chpart_arm_l1], mats[k_chpart_hand_l] );
261 m3x3_copy( mats[k_chpart_arm_r1], mats[k_chpart_hand_r] );
262 m4x3_mulv( mats[k_chpart_arm_l1], offs[k_chpart_hand_l],
263 mats[k_chpart_hand_l][3] );
264 m4x3_mulv( mats[k_chpart_arm_r1], offs[k_chpart_hand_r],
265 mats[k_chpart_hand_r][3] );
266
267 /* Neck / Head */
268 m3x3_copy( mats[k_chpart_body1], mats[k_chpart_neck] );
269 m4x3_mulv( mats[k_chpart_body1], offs[k_chpart_neck],
270 mats[k_chpart_neck][3] );
271
272 #if 1
273 v4f qhead;
274 q_axis_angle( qhead, (v3f){ 0.0f,1.0f,0.0f }, ch->rhead );
275 q_m3x3( qhead, mats[k_chpart_head] );
276 m4x3_mulv( mats[k_chpart_neck], offs[k_chpart_head], mats[k_chpart_head][3]);
277 m3x3_mul( mats[k_chpart_neck], mats[k_chpart_head], mats[k_chpart_head] );
278 #else
279 m4x3_mulv( mats[k_chpart_neck], offs[k_chpart_head], mats[k_chpart_head][3]);
280 m3x3_copy( mats[k_chpart_neck], mats[k_chpart_head] );
281 #endif
282
283 /* Feet */
284 m3x3_copy( mats[k_chpart_leg_l1], mats[k_chpart_foot_l] );
285 m3x3_copy( mats[k_chpart_leg_r1], mats[k_chpart_foot_r] );
286 m4x3_mulv( mats[k_chpart_leg_l1], offs[k_chpart_foot_l],
287 mats[k_chpart_foot_l][3] );
288 m4x3_mulv( mats[k_chpart_leg_r1], offs[k_chpart_foot_r],
289 mats[k_chpart_foot_r][3] );
290
291 align_to_board( ch, k_chpart_foot_l );
292 align_to_board( ch, k_chpart_foot_r );
293
294 for( int i=0; i<PART_COUNT; i++ )
295 m4x3_mul( ch->mroot, ch->matrices[i], ch->matrices[i] );
296 }
297
298 #define B3D_CO( X, Y, Z ) (v3f){ X, Z, -Y }
299
300 typedef struct character_pose character_pose;
301 struct character_pose
302 {
303 v3f b0, b1, p, fr, fl, pl, pr, hl, hr, apl, apr, cam;
304 };
305
306 static character_pose pose_aero =
307 {
308 .b0 = {0.0721f, 0.8167f, 0.1365f},
309 .b1 = {-0.0773f, 1.1559f, -0.1699f},
310 .p = {0.0421f, 1.1430f, 0.2803f},
311 .fr = {0.0535f, 0.1312f, -0.3647f},
312 .fl = {-0.0605f, 0.1464f, 0.2917f},
313 .pl = {-0.1704f, 0.6889f, -0.4017f},
314 .pr = {0.0672f, 0.7598f, -0.5963f},
315 .hl = {-0.2153f, 0.7195f, -0.1345f},
316 .hr = {0.1974f, 0.7940f, -0.3522f},
317 .apl = {-0.2008f, 0.9546f, 0.3687f},
318 .apr = {0.3133f, 0.9299f, 0.0181f},
319 .cam = {-0.3394f, 1.2661f, 0.2936f}
320 };
321
322 static character_pose pose_slide =
323 {
324 .b0 = {0.6732f, 0.5565f, -0.0000f},
325 .b1 = {0.8116f, 1.0547f, 0.0613f},
326 .p = {1.0404f, 0.7907f, 0.0186f},
327 .fr = {-0.0030f, 0.1366f, -0.4461f},
328 .fl = {-0.0030f, 0.1366f, 0.3480f},
329 .pl = {-0.0887f, 0.8229f, 0.3826f},
330 .pr = {-0.0887f, 0.8229f, -0.4621f},
331 .hl = {0.7749f, 0.5545f, 0.5310f},
332 .hr = {0.5844f, 1.2445f, -0.5456f},
333 .apl = {1.0999f, 0.5390f, 0.2398f},
334 .apr = {0.9816f, 0.9536f, -0.5463f},
335 .cam = {0.9888f, 1.4037f, 0.6081f}
336 };
337
338 static character_pose pose_slide1 =
339 {
340 .b0 = {-0.2385f, 0.6403f, 0.1368f},
341 .b1 = {-0.5151f, 1.1351f, 0.1380f},
342 .p = {-0.1158f, 1.2118f, 0.3895f},
343 .fr = {-0.0030f, 0.1323f, -0.3190f},
344 .fl = {-0.0030f, 0.1323f, 0.5797f},
345 .pl = {-0.6568f, 0.4305f, 0.2069f},
346 .pr = {-0.6850f, 0.2740f, -0.2969f},
347 .hl = {-0.7029f, 0.6132f, 0.2972f},
348 .hr = {-0.2572f, 1.0104f, -0.4770f},
349 .apl = {-0.4808f, 0.8480f, 0.3731f},
350 .apr = {-0.0836f, 1.0480f, -0.1201f},
351 .cam = {-1.0508f, 1.0769f, 0.0528f}
352 };
353
354 static character_pose pose_aero_reverse =
355 {
356 .b0 = {0.0616f, 0.8167f, -0.1415f},
357 .b1 = {0.0148f, 1.1559f, 0.1861f},
358 .p = {0.0558f, 1.1430f, -0.2779f},
359 .fr = {0.0535f, 0.1312f, -0.3647f},
360 .fl = {0.0730f, 0.1464f, 0.2917f},
361 .pl = {-0.2073f, 0.6889f, 0.3839f},
362 .pr = {-0.3584f, 0.4069f, 0.1032f},
363 .hl = {0.1567f, 0.7195f, 0.1997f},
364 .hr = {-0.3055f, 0.7940f, 0.2639f},
365 .apl = {0.3143f, 0.9546f, -0.2784f},
366 .apr = {-0.2885f, 0.9299f, -0.1236f},
367 .cam = {-0.3394f, 1.2661f, -0.2936f}
368 };
369
370 static character_pose pose_stand =
371 {
372 .b0 = {0.1877f, 1.0663f, 0.0063f},
373 .b1 = {0.0499f, 1.5564f, -0.0584f},
374 .p = {0.5982f, 1.2810f, 0.0842f},
375 .fr = {0.0535f, 0.1312f, -0.3647f},
376 .fl = {0.0354f, 0.1464f, 0.2917f},
377 .pl = {-0.4325f, 0.6889f, 0.1823f},
378 .pr = {-0.4794f, 0.7598f, -0.3610f},
379 .hl = {0.0498f, 1.0058f, 0.2317f},
380 .hr = {0.0188f, 0.9786f, -0.2725f},
381 .apl = {0.2898f, 1.3453f, 0.2303f},
382 .apr = {0.5273f, 1.2876f, -0.1848f},
383 .cam = {-0.3477f, 1.5884f, -0.0019f}
384 };
385
386 static character_pose pose_stand_reverse =
387 {
388 .b0 = {0.1624f, 1.0688f, -0.0632f},
389 .b1 = {0.0499f, 1.5564f, -0.0013f},
390 .p = {0.5423f, 1.2810f, -0.2368f},
391 .fr = {0.0535f, 0.1312f, -0.3647f},
392 .fl = {0.0354f, 0.1464f, 0.2917f},
393 .pl = {-0.4325f, 0.6889f, 0.4591f},
394 .pr = {-0.4794f, 0.7598f, -0.0842f},
395 .hl = {0.0498f, 1.0058f, 0.2317f},
396 .hr = {0.0188f, 0.9786f, -0.2725f},
397 .apl = {0.2898f, 1.3453f, 0.0695f},
398 .apr = {0.4715f, 1.2876f, -0.4982f},
399 .cam = {-0.3477f, 1.5884f, -0.0730f}
400 };
401
402 static character_pose pose_fly =
403 {
404 .b0 = {0.2995f, 0.6819f, -0.1369f},
405 .b1 = {0.1618f, 1.1720f, -0.2016f},
406 .p = {0.7477f, 0.9173f, -0.1885f},
407 .fr = {0.0535f, 0.1312f, -0.3647f},
408 .fl = {0.0354f, 0.1464f, 0.2917f},
409 .pl = {-0.2930f, 0.4849f, 0.5307f},
410 .pr = {-0.4754f, 0.4124f, -0.4874f},
411 .hl = {0.2650f, 1.1897f, 0.4626f},
412 .hr = {0.2494f, 1.2059f, -0.7985f},
413 .apl = {0.5165f, 1.0990f, 0.1655f},
414 .apr = {0.6759f, 1.0661f, -0.6014f},
415 .cam = {-0.2727f, 1.2606f, 0.3564f}
416 };
417
418 static
419 void character_pose_blend( struct character *ch, character_pose *pose, float q )
420 {
421 v3_muladds( ch->ik_body.base, pose->b0, q, ch->ik_body.base );
422 v3_muladds( ch->ik_body.end, pose->b1, q, ch->ik_body.end );
423 v3_muladds( ch->ik_body.pole, pose->p, q, ch->ik_body.pole );
424 v3_muladds( ch->ik_leg_l.end, pose->fl, q, ch->ik_leg_l.end );
425 v3_muladds( ch->ik_leg_l.pole, pose->pl, q, ch->ik_leg_l.pole );
426 v3_muladds( ch->ik_leg_r.end, pose->fr, q, ch->ik_leg_r.end );
427 v3_muladds( ch->ik_leg_r.pole, pose->pr, q, ch->ik_leg_r.pole );
428 v3_muladds( ch->ik_arm_l.pole, pose->apl, q, ch->ik_arm_l.pole );
429 v3_muladds( ch->ik_arm_r.pole, pose->apr, q, ch->ik_arm_r.pole );
430 v3_muladds( ch->ik_arm_l.end, pose->hl, q, ch->ik_arm_l.end );
431 v3_muladds( ch->ik_arm_r.end, pose->hr, q, ch->ik_arm_r.end );
432 v3_muladds( ch->cam_pos, pose->cam, q, ch->cam_pos );
433 }
434
435 static
436 void character_final_pose( struct character *ch, v3f cog,
437 character_pose *pose, float q )
438 {
439 character_pose npose;
440 float dip = vg_clampf(cog[1], -1.0f, 0.3f) * 0.5f,
441 tilt = vg_clampf(cog[2], -1.0f, 1.0f) * 0.3f;
442
443 v4f rz; m4x3f tr;
444 q_axis_angle( rz, (v3f){0.0f,0.0f,1.0f}, -cog[0]*0.6f );
445 q_m3x3( rz, tr );
446 v3_copy( (v3f){0.0f,dip,tilt}, tr[3] );
447
448 m4x3_mulv( tr, pose->b0, npose.b0 );
449 m4x3_mulv( tr, pose->b1, npose.b1 );
450 m4x3_mulv( tr, pose->p, npose.p );
451 m4x3_mulv( tr, pose->pl, npose.pl );
452 m4x3_mulv( tr, pose->pr, npose.pr );
453 m4x3_mulv( tr, pose->hl, npose.hl );
454 m4x3_mulv( tr, pose->hr, npose.hr );
455 m4x3_mulv( tr, pose->apl, npose.apl );
456 m4x3_mulv( tr, pose->apr, npose.apr );
457
458 v3_copy( pose->fr, npose.fr );
459 v3_copy( pose->fl, npose.fl );
460 v3_copy( pose->cam, npose.cam );
461
462 character_pose_blend( ch, &npose, q );
463 }
464
465 static void character_yaw_upper( struct character *ch, float yaw )
466 {
467 m3x3f r;
468 v4f q;
469
470 q_axis_angle( q, (v3f){0.0f,1.0f,0.0f}, yaw );
471 q_m3x3( q, r );
472
473 m3x3_mulv( r, ch->ik_body.pole, ch->ik_body.pole );
474 m3x3_mulv( r, ch->ik_body.end, ch->ik_body.end );
475 }
476
477 static void zero_ik_basic( struct ik_basic *ik )
478 {
479 v3_zero( ik->base );
480 v3_zero( ik->end );
481 v3_zero( ik->pole );
482 }
483
484 static void character_pose_reset( struct character *ch )
485 {
486 zero_ik_basic( &ch->ik_body );
487 zero_ik_basic( &ch->ik_leg_l );
488 zero_ik_basic( &ch->ik_leg_r );
489 zero_ik_basic( &ch->ik_arm_l );
490 zero_ik_basic( &ch->ik_arm_r );
491 v3_zero( ch->cam_pos );
492 }
493
494 static void character_testpose( struct character *ch, float t )
495 {
496 /* Body */
497 float *hips = ch->ik_body.base,
498 *collar = ch->ik_body.end,
499 *pole = ch->ik_body.pole;
500
501 hips[0] = cosf(t*1.325f)*0.25f;
502 hips[1] = (sinf(t)*0.2f+0.6f) * ch->origins[ k_chpart_body0 ][1];
503 hips[2] = 0.0f;
504
505 collar[0] = hips[0];
506 collar[1] = hips[1] + (ch->ik_body.l1+ch->ik_body.l2)*(sinf(t)*0.05f+0.94f);
507 collar[2] = hips[2] + cosf(t*0.42f)*0.01f;
508
509 v3_add( hips, collar, pole );
510 v3_muls( pole, 0.5f, pole );
511 v3_add( pole, (v3f){ 1.0f, 0.0f, 0.0f }, pole );
512
513 /* Legs */
514 float *footl = ch->ik_leg_l.end,
515 *footr = ch->ik_leg_r.end,
516 *polel = ch->ik_leg_l.pole,
517 *poler = ch->ik_leg_r.pole;
518
519 footl[0] = sinf(t*0.563f);
520 footl[1] = 0.0f;
521 footl[2] = 0.0f;
522
523 footr[0] = 0.0f;
524 footr[1] = 0.0f;
525 footr[2] = cosf(t*0.672f);
526
527 v3_add( hips, footl, polel );
528 v3_muls( polel, 0.4f, polel );
529 v3_add( polel, (v3f){ -1.0f,0.0f,0.0f }, polel );
530
531 v3_add( hips, footr, poler );
532 v3_muls( poler, 0.4f, poler );
533 v3_add( poler, (v3f){ -1.0f,0.0f,0.0f }, poler );
534
535 /* Arms */
536 float *arml = ch->ik_arm_l.end,
537 *armr = ch->ik_arm_r.end;
538 polel = ch->ik_arm_l.pole;
539 poler = ch->ik_arm_r.pole;
540
541 v3_copy( (v3f){ 0.0f, 0.0f, 1.0f }, arml );
542 v3_copy( (v3f){ 0.0f, 0.0f,-1.0f }, armr );
543 v3_copy( (v3f){ 1.0f, 1.0f, 0.5f }, polel );
544 v3_copy( (v3f){ 1.0f, 1.0f,-0.5f }, poler );
545
546 /* Other */
547 ch->rhip = sinf(t*0.2f);
548 ch->rcollar = sinf(t*0.35325f);
549 q_identity( ch->qhead );
550
551 m4x3_identity( ch->matrices[k_chpart_board] );
552 m4x3_identity( ch->matrices[k_chpart_wb] );
553 m4x3_identity( ch->matrices[k_chpart_wf] );
554 }
555
556 static float *player_cam_pos(void);
557 static void character_draw( struct character *ch, float temp )
558 {
559 shader_character_use();
560 shader_character_uPv( vg_pv );
561
562 vg_tex2d_bind( &tex_pallet, 0 );
563 shader_character_uTexMain( 0 );
564 shader_character_uOpacity( temp );
565 shader_character_uCamera( player_cam_pos() );
566 shader_link_standard_ub( _shader_character.id, 2 );
567
568 glEnable( GL_CULL_FACE );
569 glCullFace( GL_BACK );
570
571 mesh_bind( &ch->mesh );
572
573 for( int i=4; i<PART_COUNT; i++ )
574 {
575 #if 0
576 if( i == k_chpart_head || i == k_chpart_neck )
577 continue;
578 #endif
579
580 shader_character_uMdl( ch->matrices[i] );
581 mdl_draw_submesh( &ch->parts[i] );
582 }
583
584 for( int i=0; i<2; i++ )
585 {
586 if( ch->shoes[i] )
587 {
588 shader_character_uMdl( ch->matrices[i] );
589 mdl_draw_submesh( &ch->parts[i] );
590 }
591 else
592 {
593 shader_character_uMdl( ch->matrices[i+2] );
594 mdl_draw_submesh( &ch->parts[i] );
595 shader_character_uMdl( ch->matrices[i] );
596 mdl_draw_submesh( &ch->parts[i+2] );
597 }
598 }
599 }
600
601 /*
602 * Ragdoll Stuff
603 */
604
605 static void character_rd_box( struct character *ch, enum character_part id,
606 v3f dims )
607 {
608 v3_muls( dims, -0.5f, ch->ragdoll[id].bbx[0] );
609 v3_muls( dims, 0.5f, ch->ragdoll[id].bbx[1] );
610 }
611
612 struct rd_joint
613 {
614 enum character_part ia, ib;
615 v3f lca, lcb;
616
617 struct rd_joint_axis
618 {
619 v3f va, vb;
620 float spring, ang;
621 }
622 min, maj;
623 };
624
625 static const float k_human_major = 0.5f,
626 k_human_minor = 0.5f,
627 k_human_major_max = 0.4f,
628 k_human_minor_max = 0.1f;
629
630 #define HUMAN_VERTICAL_DEFAULT \
631 .min = { \
632 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
633 .spring = k_human_minor, .ang = k_human_minor_max \
634 }, \
635 .maj = { \
636 .va = {0.0f,1.0f,0.0f}, .vb = {0.0f,1.0f,0.0f}, \
637 .spring = k_human_major, .ang = k_human_major_max \
638 }
639
640 #define HUMAN_ARM_LEFT \
641 .min = { \
642 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
643 .spring = k_human_minor, .ang = k_human_minor_max \
644 }, \
645 .maj = { \
646 .va = {0.0f,0.0f,1.0f}, .vb = {0.0f,0.0f,1.0f}, \
647 .spring = k_human_major, .ang = k_human_major_max \
648 }
649
650 #define HUMAN_ARM_RIGHT \
651 .min = { \
652 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
653 .spring = k_human_minor, .ang = k_human_minor_max \
654 }, \
655 .maj = { \
656 .va = {0.0f,0.0f,-1.0f}, .vb = {0.0f,0.0f,-1.0f}, \
657 .spring = k_human_major, .ang = k_human_major_max \
658 }
659
660 static struct rd_joint rd_joints[] =
661 {
662 { .ia = k_chpart_leg_l1, .ib = k_chpart_foot_l, HUMAN_VERTICAL_DEFAULT },
663 { .ia = k_chpart_leg_r1, .ib = k_chpart_foot_r, HUMAN_VERTICAL_DEFAULT },
664
665 { .ia = k_chpart_body0, .ib = k_chpart_body1, HUMAN_VERTICAL_DEFAULT },
666 { .ia = k_chpart_body1, .ib = k_chpart_neck, HUMAN_VERTICAL_DEFAULT },
667 { .ia = k_chpart_neck, .ib = k_chpart_head, HUMAN_VERTICAL_DEFAULT },
668 { .ia = k_chpart_body0, .ib = k_chpart_leg_l0, HUMAN_VERTICAL_DEFAULT },
669 { .ia = k_chpart_leg_l0, .ib = k_chpart_leg_l1, HUMAN_VERTICAL_DEFAULT },
670 { .ia = k_chpart_body0, .ib = k_chpart_leg_r0, HUMAN_VERTICAL_DEFAULT },
671 { .ia = k_chpart_leg_r0, .ib = k_chpart_leg_r1, HUMAN_VERTICAL_DEFAULT },
672
673 { .ia = k_chpart_body1, .ib = k_chpart_arm_l0, HUMAN_ARM_LEFT },
674 { .ia = k_chpart_arm_l0, .ib = k_chpart_arm_l1, HUMAN_ARM_LEFT },
675 { .ia = k_chpart_arm_l1, .ib = k_chpart_hand_l, HUMAN_ARM_LEFT },
676
677 { .ia = k_chpart_body1, .ib = k_chpart_arm_r0, HUMAN_ARM_RIGHT },
678 { .ia = k_chpart_arm_r0, .ib = k_chpart_arm_r1, HUMAN_ARM_RIGHT },
679 { .ia = k_chpart_arm_r1, .ib = k_chpart_hand_r, HUMAN_ARM_RIGHT }
680 };
681
682 /* Ragdoll should be in rest pose when calling this function */
683 static void character_init_ragdoll_joints( struct character *ch )
684 {
685 for( int i=0; i<vg_list_size(rd_joints); i++ )
686 {
687 struct rd_joint *joint = &rd_joints[i];
688
689 float *hinge = ch->origins[joint->ib];
690 v3_sub( hinge, ch->ragdoll[joint->ia].co, joint->lca );
691 v3_sub( hinge, ch->ragdoll[joint->ib].co, joint->lcb );
692 }
693
694 for( int i=0; i<PART_COUNT; i++ )
695 {
696 float *pivot = ch->origins[i];
697 v3_sub( ch->ragdoll[i].co, pivot, ch->ragdoll[i].delta );
698 }
699 }
700
701 static void character_init_ragdoll( struct character *ch )
702 {
703 v3f *offs = ch->offsets;
704 rigidbody *rbs = ch->ragdoll;
705
706 /* CHest */
707 float chest_width = fabsf(offs[k_chpart_arm_r0][2])*2.0f,
708 chest_depth = chest_width * 0.571f,
709 chest_height = offs[k_chpart_neck][1];
710 v3f chest_dims = { chest_depth, chest_height, chest_width };
711 character_rd_box( ch, k_chpart_body1, chest_dims );
712
713 v3_copy( ch->origins[k_chpart_body1], rbs[k_chpart_body1].co );
714 rbs[k_chpart_body1].co[1] += chest_height*0.5f;
715
716 /* Torso */
717 v3f torso_dims = { chest_depth,
718 offs[k_chpart_body1][1]-offs[k_chpart_leg_l0][1],
719 chest_width*0.85f };
720 v3_copy( ch->origins[k_chpart_body0], rbs[k_chpart_body0].co );
721 character_rd_box( ch, k_chpart_body0, torso_dims );
722
723 /* Neck */
724 v3f neck_dims = { chest_depth*0.5f,
725 offs[k_chpart_head][1],
726 chest_depth*0.5f };
727 v3_copy( ch->origins[k_chpart_neck], rbs[k_chpart_neck].co );
728 rbs[k_chpart_neck].co[1] += neck_dims[1]*0.5f;
729 character_rd_box( ch, k_chpart_neck, neck_dims );
730
731 /* Head */
732 v3f head_dims = { chest_width*0.5f, chest_width*0.5f, chest_width*0.5f };
733 v3_copy( ch->origins[k_chpart_head], rbs[k_chpart_head].co );
734 rbs[k_chpart_head].co[1] += head_dims[1]*0.5f;
735 character_rd_box( ch, k_chpart_head, head_dims );
736
737 /* ARms */
738 v3f ua_dims = { 0.0f, 0.0f, fabsf(offs[k_chpart_arm_l1][2]) };
739 ua_dims[1] = 0.38f*ua_dims[2];
740 ua_dims[0] = 0.38f*ua_dims[2];
741 v3f la_dims = { ua_dims[0], ua_dims[1], fabsf(offs[k_chpart_hand_l][2]) };
742 v3f hand_dims = { ua_dims[1], ua_dims[1]*0.5f, ua_dims[1] };
743
744 character_rd_box( ch, k_chpart_arm_l0, ua_dims );
745 character_rd_box( ch, k_chpart_arm_r0, ua_dims );
746 character_rd_box( ch, k_chpart_arm_l1, la_dims );
747 character_rd_box( ch, k_chpart_arm_r1, la_dims );
748 character_rd_box( ch, k_chpart_hand_l, hand_dims );
749 character_rd_box( ch, k_chpart_hand_r, hand_dims );
750
751 v3_copy( ch->origins[k_chpart_arm_l0], rbs[k_chpart_arm_l0].co );
752 rbs[k_chpart_arm_l0].co[2] += ua_dims[2] * 0.5f;
753 v3_copy( ch->origins[k_chpart_arm_l1], rbs[k_chpart_arm_l1].co );
754 rbs[k_chpart_arm_l1].co[2] += la_dims[2] * 0.5f;
755 v3_copy( ch->origins[k_chpart_hand_l], rbs[k_chpart_hand_l].co );
756 rbs[k_chpart_hand_l].co[2] += hand_dims[2] * 0.5f;
757
758 v3_copy( ch->origins[k_chpart_arm_r0], rbs[k_chpart_arm_r0].co );
759 rbs[k_chpart_arm_r0].co[2] -= ua_dims[2] * 0.5f;
760 v3_copy( ch->origins[k_chpart_arm_r1], rbs[k_chpart_arm_r1].co );
761 rbs[k_chpart_arm_r1].co[2] -= la_dims[2] * 0.5f;
762 v3_copy( ch->origins[k_chpart_hand_r], rbs[k_chpart_hand_r].co );
763 rbs[k_chpart_hand_r].co[2] -= hand_dims[2] * 0.5f;
764
765 /* LEgs */
766 v3f ul_dims = { 0.0f, fabsf(offs[k_chpart_leg_l1][1]), 0.0f };
767 ul_dims[0] = 0.38f*ul_dims[1];
768 ul_dims[2] = 0.38f*ul_dims[1];
769 v3f ll_dims = { ul_dims[0], fabsf(offs[k_chpart_foot_l][1]), ul_dims[2] };
770 v3f foot_dims = { 2.0f*ul_dims[0], ul_dims[0], ul_dims[0] };
771
772 character_rd_box( ch, k_chpart_leg_l0, ul_dims );
773 character_rd_box( ch, k_chpart_leg_r0, ul_dims );
774 character_rd_box( ch, k_chpart_leg_l1, ll_dims );
775 character_rd_box( ch, k_chpart_leg_r1, ll_dims );
776 character_rd_box( ch, k_chpart_foot_l, foot_dims );
777 character_rd_box( ch, k_chpart_foot_r, foot_dims );
778
779 v3_copy( ch->origins[k_chpart_leg_l0], rbs[k_chpart_leg_l0].co );
780 rbs[k_chpart_leg_l0].co[1] -= ul_dims[1] * 0.5f;
781 v3_copy( ch->origins[k_chpart_leg_l1], rbs[k_chpart_leg_l1].co );
782 rbs[k_chpart_leg_l1].co[1] -= ll_dims[1] * 0.5f;
783 v3_copy( ch->origins[k_chpart_foot_l], rbs[k_chpart_foot_l].co );
784 rbs[k_chpart_foot_l].co[1] -= foot_dims[1] * 0.5f;
785 rbs[k_chpart_foot_l].co[0] -= foot_dims[0] * 0.5f;
786
787 v3_copy( ch->origins[k_chpart_leg_r0], rbs[k_chpart_leg_r0].co );
788 rbs[k_chpart_leg_r0].co[1] -= ul_dims[1] * 0.5f;
789 v3_copy( ch->origins[k_chpart_leg_r1], rbs[k_chpart_leg_r1].co );
790 rbs[k_chpart_leg_r1].co[1] -= ll_dims[1] * 0.5f;
791 v3_copy( ch->origins[k_chpart_foot_r], rbs[k_chpart_foot_r].co );
792 rbs[k_chpart_foot_r].co[1] -= foot_dims[1] * 0.5f;
793 rbs[k_chpart_foot_r].co[0] -= foot_dims[0] * 0.5f;
794
795 character_rd_box( ch, k_chpart_sock_l, foot_dims );
796 character_rd_box( ch, k_chpart_sock_r, foot_dims );
797 v3_copy( rbs[k_chpart_foot_l].co, rbs[k_chpart_sock_l].co );
798 v3_copy( rbs[k_chpart_foot_r].co, rbs[k_chpart_sock_r].co );
799
800 box_copy( (boxf){{-0.2f,-0.2f,-0.7f},{0.2f,0.2f,0.7f}},
801 rbs[k_chpart_board].bbx );
802
803 for( int i=0; i<PART_COUNT; i++ )
804 rb_init( &ch->ragdoll[i] );
805
806 character_init_ragdoll_joints( ch );
807 }
808
809 static void character_ragdoll_go( struct character *ch, v3f pos )
810 {
811 character_init_ragdoll( ch );
812 for( int i=0; i<PART_COUNT; i++ )
813 v3_add( pos, ch->ragdoll[i].co, ch->ragdoll[i].co );
814 }
815
816 static void character_ragdoll_copypose( struct character *ch, v3f v )
817 {
818 for( int i=0; i<PART_COUNT; i++ )
819 {
820 rigidbody *rb = &ch->ragdoll[i];
821
822 m4x3_mulv( ch->matrices[i], rb->delta, rb->co );
823 m3x3_q( ch->matrices[i], rb->q );
824 v3_copy( v, rb->v );
825 v3_zero( rb->I );
826 rb->manifold_count = 0; /* ? */
827
828 rb_update_transform( rb );
829 }
830
831 float vel = v3_length(v);
832
833 ch->shoes[0] = 1;
834 ch->shoes[1] = 1;
835 }
836
837 static void character_mimic_ragdoll( struct character *ch )
838 {
839 for( int i=0; i<PART_COUNT; i++ )
840 {
841 rigidbody *rb = &ch->ragdoll[i];
842 v3f *mat = ch->matrices[i];
843
844 m3x3_copy( rb->to_world, mat );
845 v3f inv_delta;
846 v3_negate( rb->delta, inv_delta );
847 m4x3_mulv( rb->to_world, inv_delta, mat[3] );
848 }
849
850 /* Attach wheels to board */
851 m3x3_copy( ch->matrices[k_chpart_board], ch->matrices[k_chpart_wb] );
852 m3x3_copy( ch->matrices[k_chpart_board], ch->matrices[k_chpart_wf] );
853 m4x3_mulv( ch->matrices[k_chpart_board], ch->offsets[k_chpart_wb],
854 ch->matrices[k_chpart_wb][3] );
855 m4x3_mulv( ch->matrices[k_chpart_board], ch->offsets[k_chpart_wf],
856 ch->matrices[k_chpart_wf][3] );
857 }
858
859 static void character_debug_ragdoll( struct character *ch )
860 {
861 rb_debug( &ch->ragdoll[k_chpart_body0], 0xffffffff );
862 rb_debug( &ch->ragdoll[k_chpart_body1], 0xffffffff );
863 rb_debug( &ch->ragdoll[k_chpart_neck], 0xff00ff00 );
864 rb_debug( &ch->ragdoll[k_chpart_head], 0xff00ff00 );
865
866 rb_debug( &ch->ragdoll[k_chpart_arm_l0], 0xffffa500 );
867 rb_debug( &ch->ragdoll[k_chpart_arm_l1], 0xffffa500 );
868 rb_debug( &ch->ragdoll[k_chpart_hand_l], 0xffffa500 );
869
870 rb_debug( &ch->ragdoll[k_chpart_arm_r0], 0xff00a5ff );
871 rb_debug( &ch->ragdoll[k_chpart_arm_r1], 0xff00a5ff );
872 rb_debug( &ch->ragdoll[k_chpart_hand_r], 0xff00a5ff );
873
874 rb_debug( &ch->ragdoll[k_chpart_leg_l0], 0xffffa500 );
875 rb_debug( &ch->ragdoll[k_chpart_leg_l1], 0xffffa500 );
876 rb_debug( &ch->ragdoll[k_chpart_foot_l], 0xffffa500 );
877 rb_debug( &ch->ragdoll[k_chpart_leg_r0], 0xff00a5ff );
878 rb_debug( &ch->ragdoll[k_chpart_leg_r1], 0xff00a5ff );
879 rb_debug( &ch->ragdoll[k_chpart_foot_r], 0xff00a5ff );
880 }
881
882 static void character_ragdoll_iter( struct character *ch )
883 {
884 for( int i=0; i<PART_COUNT; i++ )
885 {
886 rb_manifold_reset( &ch->ragdoll[i] );
887 rb_build_manifold_terrain( &ch->ragdoll[i] );
888
889 u32 colliders[16];
890 int len = bh_select( &world.bhcubes, ch->ragdoll[i].bbx_world,
891 colliders, 16 );
892
893 for( int j=0; j<len; j++ )
894 rb_build_manifold_rb_static( &ch->ragdoll[i],
895 &world.temp_rbs[colliders[j]] );
896 }
897
898 v3f rv;
899
900 float shoe_vel[2];
901 for( int i=0; i<2; i++ )
902 if( ch->shoes[i] )
903 shoe_vel[i] = v3_length( ch->ragdoll[i].v );
904
905 for( int i=0; i<20; i++ )
906 {
907 float const k_springfactor = 1.0f/20.0f;
908
909 for( int j=0; j<PART_COUNT; j++ )
910 rb_constraint_manifold( &ch->ragdoll[j] );
911
912 for( int j=0; j<vg_list_size(rd_joints); j++ )
913 {
914 struct rd_joint *joint = &rd_joints[j];
915 rigidbody *rba = &ch->ragdoll[joint->ia],
916 *rbb = &ch->ragdoll[joint->ib];
917
918 rb_constraint_position( rba, joint->lca, rbb, joint->lcb );
919 rb_constraint_angle( rba, joint->maj.va, rbb, joint->maj.vb,
920 joint->maj.ang,
921 joint->maj.spring * k_springfactor );
922
923 rb_constraint_angle( rba, joint->min.va, rbb, joint->min.vb,
924 joint->min.ang,
925 joint->min.spring * k_springfactor );
926 }
927 }
928
929 for( int j=0; j<vg_list_size(rd_joints); j++ )
930 {
931 struct rd_joint *joint = &rd_joints[j];
932 rigidbody *rba = &ch->ragdoll[joint->ia],
933 *rbb = &ch->ragdoll[joint->ib];
934 rb_angle_limit_force( rba, joint->min.va, rbb, joint->min.vb,
935 joint->min.ang );
936 rb_angle_limit_force( rba, joint->maj.va, rbb, joint->maj.vb,
937 joint->maj.ang );
938 }
939
940 for( int i=0; i<PART_COUNT; i++ )
941 rb_iter( &ch->ragdoll[i] );
942
943 for( int i=0; i<2; i++ )
944 {
945 if( ch->shoes[i] )
946 {
947 float a = v3_length( ch->ragdoll[i].v ) - shoe_vel[i];
948
949 if( a > 2.0f )
950 {
951 ch->shoes[i] = 0;
952
953 rigidbody *src = &ch->ragdoll[k_chpart_foot_l];
954 rigidbody *dst = &ch->ragdoll[k_chpart_sock_l];
955
956 v3_copy( src->co, dst->co );
957 v3_copy( src->v, dst->v );
958 v3_copy( src->q, dst->q );
959 v3_copy( src->I, dst->I );
960 }
961 }
962 }
963
964 for( int i=0; i<PART_COUNT; i++ )
965 rb_update_transform( &ch->ragdoll[i] );
966 }
967
968 #endif