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