11 #include "shaders/character.h"
13 vg_tex2d tex_pallet
= { .path
= "textures/ch_gradient.qoi" };
15 static void character_register(void)
17 shader_character_register();
20 static void character_init(void)
22 vg_tex2d_init( (vg_tex2d
*[]){ &tex_pallet
}, 1 );
25 #define FOREACH_PART(FN) \
48 #define MAKE_ENUM(ENUM) k_chpart_##ENUM,
49 #define MAKE_STRING(STR) #STR,
51 #define PART_COUNT FOREACH_PART(ADD_ONE)
56 FOREACH_PART( MAKE_ENUM
)
59 static const char *character_part_strings
[] =
61 FOREACH_PART( MAKE_STRING
)
68 mdl_submesh parts
[ PART_COUNT
];
69 v3f origins
[ PART_COUNT
];
70 m4x3f matrices
[ PART_COUNT
];
72 /* Auxillary information */
73 v3f offsets
[ PART_COUNT
];
74 rigidbody ragdoll
[ PART_COUNT
];
78 * note: - base nodes of IK structures will be filled automatically
79 * - all positions are in local space to the mroot
81 struct ik_basic ik_arm_l
, ik_arm_r
,
87 float rhip
, rcollar
, /* twist of hip and collar controls,
88 these act in the local +y axis of the part */
93 v3f ground_normal
; /* Feet will be aligned to this */
99 static void character_offset( struct character
*ch
, enum character_part parent
,
100 enum character_part child
)
102 v3_sub( ch
->origins
[ child
], ch
->origins
[ parent
],
103 ch
->offsets
[ child
] );
106 static int character_load( struct character
*ch
, const char *name
)
110 snprintf( buf
, sizeof(buf
)-1, "models/%s.mdl", name
);
111 mdl_header
*src
= mdl_load( buf
);
118 for( int i
=0; i
<PART_COUNT
; i
++ )
120 snprintf( buf
, sizeof(buf
)-1, "%s_%s", name
, character_part_strings
[i
] );
121 mdl_node
*pnode
= mdl_node_from_name( src
, buf
);
123 memset( &ch
->parts
[i
], 0, sizeof(mdl_submesh
) );
124 v3_zero( ch
->origins
[i
] );
128 vg_warn( "Character file does not contain an '_%s' part.\n",
129 character_part_strings
[i
] );
134 mdl_submesh
*sm
= mdl_node_submesh( src
, pnode
, 0 );
138 vg_warn( "Character file's '_%s' part has no mesh.\n",
139 character_part_strings
[i
] );
145 v3_copy( pnode
->co
, ch
->origins
[i
] );
148 mdl_unpack_glmesh( src
, &ch
->mesh
);
151 vg_success( "Loaded character file '%s' with no errors\n", name
);
153 /* Create the offsets */
154 character_offset( ch
, k_chpart_body0
, k_chpart_body1
);
155 character_offset( ch
, k_chpart_body1
, k_chpart_neck
);
156 character_offset( ch
, k_chpart_neck
, k_chpart_head
);
158 character_offset( ch
, k_chpart_body1
, k_chpart_arm_l0
);
159 character_offset( ch
, k_chpart_arm_l0
, k_chpart_arm_l1
);
160 character_offset( ch
, k_chpart_arm_l1
, k_chpart_hand_l
);
162 character_offset( ch
, k_chpart_body1
, k_chpart_arm_r0
);
163 character_offset( ch
, k_chpart_arm_r0
, k_chpart_arm_r1
);
164 character_offset( ch
, k_chpart_arm_r1
, k_chpart_hand_r
);
166 character_offset( ch
, k_chpart_body0
, k_chpart_leg_l0
);
167 character_offset( ch
, k_chpart_leg_l0
, k_chpart_leg_l1
);
168 character_offset( ch
, k_chpart_leg_l1
, k_chpart_foot_l
);
170 character_offset( ch
, k_chpart_body0
, k_chpart_leg_r0
);
171 character_offset( ch
, k_chpart_leg_r0
, k_chpart_leg_r1
);
172 character_offset( ch
, k_chpart_leg_r1
, k_chpart_foot_r
);
174 character_offset( ch
, k_chpart_board
, k_chpart_wb
);
175 character_offset( ch
, k_chpart_board
, k_chpart_wf
);
177 ch
->ik_arm_l
.l1
= v3_length( ch
->offsets
[ k_chpart_arm_l1
] );
178 ch
->ik_arm_l
.l2
= v3_length( ch
->offsets
[ k_chpart_hand_l
] );
179 ch
->ik_arm_r
.l1
= v3_length( ch
->offsets
[ k_chpart_arm_r1
] );
180 ch
->ik_arm_r
.l2
= v3_length( ch
->offsets
[ k_chpart_hand_r
] );
182 ch
->ik_leg_l
.l1
= v3_length( ch
->offsets
[ k_chpart_leg_l1
] );
183 ch
->ik_leg_l
.l2
= v3_length( ch
->offsets
[ k_chpart_foot_l
] );
184 ch
->ik_leg_r
.l1
= v3_length( ch
->offsets
[ k_chpart_leg_r1
] );
185 ch
->ik_leg_r
.l2
= v3_length( ch
->offsets
[ k_chpart_foot_r
] );
187 ch
->ik_body
.l1
= v3_length( ch
->offsets
[ k_chpart_body1
] );
188 ch
->ik_body
.l2
= v3_length( ch
->offsets
[ k_chpart_neck
] );
194 static void align_to_board( struct character
*ch
, enum character_part id
)
196 /* Calculate rotation between board and feet */
197 m4x3f
*mats
= ch
->matrices
;
199 v3f foot_pos
, foot_fwd
, foot_target
, board_norm
, board_origin
;
200 v3_copy( mats
[id
][3], foot_pos
);
201 m3x3_mulv( mats
[id
], (v3f
){1.0f
,0.0f
,0.0f
}, foot_fwd
);
202 v3_add( foot_fwd
, foot_pos
, foot_target
);
204 m3x3_mulv( mats
[k_chpart_board
], (v3f
){0.0f
,1.0f
,0.0f
}, board_norm
);
205 m4x3_mulv( mats
[k_chpart_board
], (v3f
){0.0f
,0.13f
,0.0f
}, board_origin
);
207 vg_line( foot_pos
, foot_target
, 0xff00ff00 );
210 v3_sub( board_origin
, foot_target
, v0
);
211 float t
= v3_dot( v0
, board_norm
) / board_norm
[1];
213 vg_line( foot_pos
, foot_target
, 0xff00ffff );
215 v3_sub( foot_target
, foot_pos
, foot_target
);
216 v3_normalize( foot_target
);
217 float ang
= acosf( v3_dot( foot_target
, foot_fwd
) );
219 v4f qcorrection
; m3x3f correction
;
220 q_axis_angle( qcorrection
, (v3f
){0.0f
,0.0f
,1.0f
}, -ang
);
221 q_m3x3( qcorrection
, correction
);
222 m3x3_mul( mats
[id
], correction
, mats
[id
] );
225 static void character_eval( struct character
*ch
)
227 m4x3f
*mats
= ch
->matrices
;
228 v3f
*offs
= ch
->offsets
;
230 ik_basic( &ch
->ik_body
, mats
[k_chpart_body0
], mats
[k_chpart_body1
],
235 /* TODO: Do this directly via m3x3 */
237 q_axis_angle( body_rotation
, (v3f
){0.0f
,1.0f
,0.0f
}, ch
->rhip
);
238 q_m3x3( body_rotation
, temp
);
239 m3x3_mul( mats
[k_chpart_body0
], temp
, mats
[k_chpart_body0
] );
241 q_axis_angle( body_rotation
, (v3f
){0.0f
,1.0f
,0.0f
}, ch
->rcollar
);
242 q_m3x3( body_rotation
, temp
);
243 m3x3_mul( mats
[k_chpart_body1
], temp
, mats
[k_chpart_body1
] );
246 m4x3_mulv( mats
[k_chpart_body0
], offs
[k_chpart_leg_l0
], ch
->ik_leg_l
.base
);
247 m4x3_mulv( mats
[k_chpart_body0
], offs
[k_chpart_leg_r0
], ch
->ik_leg_r
.base
);
248 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_arm_l0
], ch
->ik_arm_l
.base
);
249 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_arm_r0
], ch
->ik_arm_r
.base
);
251 /* IK for arms and legs */
252 ik_basic( &ch
->ik_arm_l
, mats
[k_chpart_arm_l0
], mats
[k_chpart_arm_l1
],
254 ik_basic( &ch
->ik_arm_r
, mats
[k_chpart_arm_r0
], mats
[k_chpart_arm_r1
],
256 ik_basic( &ch
->ik_leg_l
, mats
[k_chpart_leg_l0
], mats
[k_chpart_leg_l1
],
258 ik_basic( &ch
->ik_leg_r
, mats
[k_chpart_leg_r0
], mats
[k_chpart_leg_r1
],
262 m3x3_copy( mats
[k_chpart_arm_l1
], mats
[k_chpart_hand_l
] );
263 m3x3_copy( mats
[k_chpart_arm_r1
], mats
[k_chpart_hand_r
] );
264 m4x3_mulv( mats
[k_chpart_arm_l1
], offs
[k_chpart_hand_l
],
265 mats
[k_chpart_hand_l
][3] );
266 m4x3_mulv( mats
[k_chpart_arm_r1
], offs
[k_chpart_hand_r
],
267 mats
[k_chpart_hand_r
][3] );
270 m3x3_copy( mats
[k_chpart_body1
], mats
[k_chpart_neck
] );
271 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_neck
],
272 mats
[k_chpart_neck
][3] );
276 q_axis_angle( qhead
, (v3f
){ 0.0f
,1.0f
,0.0f
}, ch
->rhead
);
277 q_m3x3( qhead
, mats
[k_chpart_head
] );
278 m4x3_mulv( mats
[k_chpart_neck
], offs
[k_chpart_head
], mats
[k_chpart_head
][3]);
279 m3x3_mul( mats
[k_chpart_neck
], mats
[k_chpart_head
], mats
[k_chpart_head
] );
281 m4x3_mulv( mats
[k_chpart_neck
], offs
[k_chpart_head
], mats
[k_chpart_head
][3]);
282 m3x3_copy( mats
[k_chpart_neck
], mats
[k_chpart_head
] );
286 m3x3_copy( mats
[k_chpart_leg_l1
], mats
[k_chpart_foot_l
] );
287 m3x3_copy( mats
[k_chpart_leg_r1
], mats
[k_chpart_foot_r
] );
288 m4x3_mulv( mats
[k_chpart_leg_l1
], offs
[k_chpart_foot_l
],
289 mats
[k_chpart_foot_l
][3] );
290 m4x3_mulv( mats
[k_chpart_leg_r1
], offs
[k_chpart_foot_r
],
291 mats
[k_chpart_foot_r
][3] );
293 align_to_board( ch
, k_chpart_foot_l
);
294 align_to_board( ch
, k_chpart_foot_r
);
296 for( int i
=0; i
<PART_COUNT
; i
++ )
297 m4x3_mul( ch
->mroot
, ch
->matrices
[i
], ch
->matrices
[i
] );
300 #define B3D_CO( X, Y, Z ) (v3f){ X, Z, -Y }
302 typedef struct character_pose character_pose
;
303 struct character_pose
305 v3f b0
, b1
, p
, fr
, fl
, pl
, pr
, hl
, hr
, apl
, apr
, cam
;
308 static character_pose pose_aero
=
310 .b0
= {0.0721f
, 0.8167f
, 0.1365f
},
311 .b1
= {-0.0773f
, 1.1559f
, -0.1699f
},
312 .p
= {0.0421f
, 1.1430f
, 0.2803f
},
313 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
314 .fl
= {-0.0605f
, 0.1464f
, 0.2917f
},
315 .pl
= {-0.1704f
, 0.6889f
, -0.4017f
},
316 .pr
= {0.0672f
, 0.7598f
, -0.5963f
},
317 .hl
= {-0.2153f
, 0.7195f
, -0.1345f
},
318 .hr
= {0.1974f
, 0.7940f
, -0.3522f
},
319 .apl
= {-0.2008f
, 0.9546f
, 0.3687f
},
320 .apr
= {0.3133f
, 0.9299f
, 0.0181f
},
321 .cam
= {-0.3394f
, 1.2661f
, 0.2936f
}
324 static character_pose pose_slide
=
326 .b0
= {0.6732f
, 0.5565f
, -0.0000f
},
327 .b1
= {0.8116f
, 1.0547f
, 0.0613f
},
328 .p
= {1.0404f
, 0.7907f
, 0.0186f
},
329 .fr
= {-0.0030f
, 0.1366f
, -0.4461f
},
330 .fl
= {-0.0030f
, 0.1366f
, 0.3480f
},
331 .pl
= {-0.0887f
, 0.8229f
, 0.3826f
},
332 .pr
= {-0.0887f
, 0.8229f
, -0.4621f
},
333 .hl
= {0.7749f
, 0.5545f
, 0.5310f
},
334 .hr
= {0.5844f
, 1.2445f
, -0.5456f
},
335 .apl
= {1.0999f
, 0.5390f
, 0.2398f
},
336 .apr
= {0.9816f
, 0.9536f
, -0.5463f
},
337 .cam
= {0.9888f
, 1.4037f
, 0.6081f
}
340 static character_pose pose_slide1
=
342 .b0
= {-0.2385f
, 0.6403f
, 0.1368f
},
343 .b1
= {-0.5151f
, 1.1351f
, 0.1380f
},
344 .p
= {-0.1158f
, 1.2118f
, 0.3895f
},
345 .fr
= {-0.0030f
, 0.1323f
, -0.3190f
},
346 .fl
= {-0.0030f
, 0.1323f
, 0.5797f
},
347 .pl
= {-0.6568f
, 0.4305f
, 0.2069f
},
348 .pr
= {-0.6850f
, 0.2740f
, -0.2969f
},
349 .hl
= {-0.7029f
, 0.6132f
, 0.2972f
},
350 .hr
= {-0.2572f
, 1.0104f
, -0.4770f
},
351 .apl
= {-0.4808f
, 0.8480f
, 0.3731f
},
352 .apr
= {-0.0836f
, 1.0480f
, -0.1201f
},
353 .cam
= {-1.0508f
, 1.0769f
, 0.0528f
}
356 static character_pose pose_aero_reverse
=
358 .b0
= {0.0616f
, 0.8167f
, -0.1415f
},
359 .b1
= {0.0148f
, 1.1559f
, 0.1861f
},
360 .p
= {0.0558f
, 1.1430f
, -0.2779f
},
361 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
362 .fl
= {0.0730f
, 0.1464f
, 0.2917f
},
363 .pl
= {-0.2073f
, 0.6889f
, 0.3839f
},
364 .pr
= {-0.3584f
, 0.4069f
, 0.1032f
},
365 .hl
= {0.1567f
, 0.7195f
, 0.1997f
},
366 .hr
= {-0.3055f
, 0.7940f
, 0.2639f
},
367 .apl
= {0.3143f
, 0.9546f
, -0.2784f
},
368 .apr
= {-0.2885f
, 0.9299f
, -0.1236f
},
369 .cam
= {-0.3394f
, 1.2661f
, -0.2936f
}
372 static character_pose pose_stand
=
374 .b0
= {0.1877f
, 1.0663f
, 0.0063f
},
375 .b1
= {0.0499f
, 1.5564f
, -0.0584f
},
376 .p
= {0.5982f
, 1.2810f
, 0.0842f
},
377 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
378 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
379 .pl
= {-0.4325f
, 0.6889f
, 0.1823f
},
380 .pr
= {-0.4794f
, 0.7598f
, -0.3610f
},
381 .hl
= {0.0498f
, 1.0058f
, 0.2317f
},
382 .hr
= {0.0188f
, 0.9786f
, -0.2725f
},
383 .apl
= {0.2898f
, 1.3453f
, 0.2303f
},
384 .apr
= {0.5273f
, 1.2876f
, -0.1848f
},
385 .cam
= {-0.3477f
, 1.5884f
, -0.0019f
}
388 static character_pose pose_stand_reverse
=
390 .b0
= {0.1624f
, 1.0688f
, -0.0632f
},
391 .b1
= {0.0499f
, 1.5564f
, -0.0013f
},
392 .p
= {0.5423f
, 1.2810f
, -0.2368f
},
393 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
394 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
395 .pl
= {-0.4325f
, 0.6889f
, 0.4591f
},
396 .pr
= {-0.4794f
, 0.7598f
, -0.0842f
},
397 .hl
= {0.0498f
, 1.0058f
, 0.2317f
},
398 .hr
= {0.0188f
, 0.9786f
, -0.2725f
},
399 .apl
= {0.2898f
, 1.3453f
, 0.0695f
},
400 .apr
= {0.4715f
, 1.2876f
, -0.4982f
},
401 .cam
= {-0.3477f
, 1.5884f
, -0.0730f
}
404 static character_pose pose_fly
=
406 .b0
= {0.2995f
, 0.6819f
, -0.1369f
},
407 .b1
= {0.1618f
, 1.1720f
, -0.2016f
},
408 .p
= {0.7477f
, 0.9173f
, -0.1885f
},
409 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
410 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
411 .pl
= {-0.2930f
, 0.4849f
, 0.5307f
},
412 .pr
= {-0.4754f
, 0.4124f
, -0.4874f
},
413 .hl
= {0.2650f
, 1.1897f
, 0.4626f
},
414 .hr
= {0.2494f
, 1.2059f
, -0.7985f
},
415 .apl
= {0.5165f
, 1.0990f
, 0.1655f
},
416 .apr
= {0.6759f
, 1.0661f
, -0.6014f
},
417 .cam
= {-0.2727f
, 1.2606f
, 0.3564f
}
421 void character_pose_blend( struct character
*ch
, character_pose
*pose
, float q
)
423 v3_muladds( ch
->ik_body
.base
, pose
->b0
, q
, ch
->ik_body
.base
);
424 v3_muladds( ch
->ik_body
.end
, pose
->b1
, q
, ch
->ik_body
.end
);
425 v3_muladds( ch
->ik_body
.pole
, pose
->p
, q
, ch
->ik_body
.pole
);
426 v3_muladds( ch
->ik_leg_l
.end
, pose
->fl
, q
, ch
->ik_leg_l
.end
);
427 v3_muladds( ch
->ik_leg_l
.pole
, pose
->pl
, q
, ch
->ik_leg_l
.pole
);
428 v3_muladds( ch
->ik_leg_r
.end
, pose
->fr
, q
, ch
->ik_leg_r
.end
);
429 v3_muladds( ch
->ik_leg_r
.pole
, pose
->pr
, q
, ch
->ik_leg_r
.pole
);
430 v3_muladds( ch
->ik_arm_l
.pole
, pose
->apl
, q
, ch
->ik_arm_l
.pole
);
431 v3_muladds( ch
->ik_arm_r
.pole
, pose
->apr
, q
, ch
->ik_arm_r
.pole
);
432 v3_muladds( ch
->ik_arm_l
.end
, pose
->hl
, q
, ch
->ik_arm_l
.end
);
433 v3_muladds( ch
->ik_arm_r
.end
, pose
->hr
, q
, ch
->ik_arm_r
.end
);
434 v3_muladds( ch
->cam_pos
, pose
->cam
, q
, ch
->cam_pos
);
439 void character_final_pose( struct character
*ch
, v3f cog
,
440 character_pose
*pose
, float q
)
442 character_pose npose
;
443 float dip
= vg_clampf(cog
[1], -1.0f
, 0.3f
) * 0.5f
,
444 tilt
= vg_clampf(cog
[2], -1.0f
, 1.0f
) * 0.3f
;
447 q_axis_angle( rz
, (v3f
){0.0f
,0.0f
,1.0f
}, -cog
[0]*0.3f
);
450 //v3_copy( (v3f){0.0f,dip,tilt}, tr[3] );
451 v3_copy( cog
, tr
[3] );
453 v3_muladd( pose
->b0
, tr
[3], (v3f
){0.85f
,1.0f
,1.0f
}, npose
.b0
);
454 //m4x3_mulv( tr, pose->b0, npose.b0 );
455 m4x3_mulv( tr
, pose
->b1
, npose
.b1
);
456 m4x3_mulv( tr
, pose
->p
, npose
.p
);
457 m4x3_mulv( tr
, pose
->pl
, npose
.pl
);
458 m4x3_mulv( tr
, pose
->pr
, npose
.pr
);
459 m4x3_mulv( tr
, pose
->hl
, npose
.hl
);
460 m4x3_mulv( tr
, pose
->hr
, npose
.hr
);
461 m4x3_mulv( tr
, pose
->apl
, npose
.apl
);
462 m4x3_mulv( tr
, pose
->apr
, npose
.apr
);
464 v3_copy( pose
->fr
, npose
.fr
);
465 v3_copy( pose
->fl
, npose
.fl
);
466 v3_copy( pose
->cam
, npose
.cam
);
468 character_pose_blend( ch
, &npose
, q
);
472 void character_final_pose( struct character
*ch
, v4f rot
,
473 character_pose
*pose
, float q
)
475 character_pose npose
;
481 m4x3_mulv( tr
, pose
->b0
, npose
.b0
);
482 m4x3_mulv( tr
, pose
->b1
, npose
.b1
);
483 m4x3_mulv( tr
, pose
->p
, npose
.p
);
484 m4x3_mulv( tr
, pose
->pl
, npose
.pl
);
485 m4x3_mulv( tr
, pose
->pr
, npose
.pr
);
486 m4x3_mulv( tr
, pose
->hl
, npose
.hl
);
487 m4x3_mulv( tr
, pose
->hr
, npose
.hr
);
488 m4x3_mulv( tr
, pose
->apl
, npose
.apl
);
489 m4x3_mulv( tr
, pose
->apr
, npose
.apr
);
491 v3_copy( pose
->fr
, npose
.fr
);
492 v3_copy( pose
->fl
, npose
.fl
);
493 v3_copy( pose
->cam
, npose
.cam
);
495 character_pose_blend( ch
, &npose
, q
);
499 static void character_yaw_upper( struct character
*ch
, float yaw
)
504 q_axis_angle( q
, (v3f
){0.0f
,1.0f
,0.0f
}, yaw
);
507 m3x3_mulv( r
, ch
->ik_body
.pole
, ch
->ik_body
.pole
);
508 m3x3_mulv( r
, ch
->ik_body
.end
, ch
->ik_body
.end
);
511 static void zero_ik_basic( struct ik_basic
*ik
)
518 static void character_pose_reset( struct character
*ch
)
520 zero_ik_basic( &ch
->ik_body
);
521 zero_ik_basic( &ch
->ik_leg_l
);
522 zero_ik_basic( &ch
->ik_leg_r
);
523 zero_ik_basic( &ch
->ik_arm_l
);
524 zero_ik_basic( &ch
->ik_arm_r
);
525 v3_zero( ch
->cam_pos
);
528 static void character_testpose( struct character
*ch
, float t
)
531 float *hips
= ch
->ik_body
.base
,
532 *collar
= ch
->ik_body
.end
,
533 *pole
= ch
->ik_body
.pole
;
535 hips
[0] = cosf(t
*1.325f
)*0.25f
;
536 hips
[1] = (sinf(t
)*0.2f
+0.6f
) * ch
->origins
[ k_chpart_body0
][1];
540 collar
[1] = hips
[1] + (ch
->ik_body
.l1
+ch
->ik_body
.l2
)*(sinf(t
)*0.05f
+0.94f
);
541 collar
[2] = hips
[2] + cosf(t
*0.42f
)*0.01f
;
543 v3_add( hips
, collar
, pole
);
544 v3_muls( pole
, 0.5f
, pole
);
545 v3_add( pole
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, pole
);
548 float *footl
= ch
->ik_leg_l
.end
,
549 *footr
= ch
->ik_leg_r
.end
,
550 *polel
= ch
->ik_leg_l
.pole
,
551 *poler
= ch
->ik_leg_r
.pole
;
553 footl
[0] = sinf(t
*0.563f
);
559 footr
[2] = cosf(t
*0.672f
);
561 v3_add( hips
, footl
, polel
);
562 v3_muls( polel
, 0.4f
, polel
);
563 v3_add( polel
, (v3f
){ -1.0f
,0.0f
,0.0f
}, polel
);
565 v3_add( hips
, footr
, poler
);
566 v3_muls( poler
, 0.4f
, poler
);
567 v3_add( poler
, (v3f
){ -1.0f
,0.0f
,0.0f
}, poler
);
570 float *arml
= ch
->ik_arm_l
.end
,
571 *armr
= ch
->ik_arm_r
.end
;
572 polel
= ch
->ik_arm_l
.pole
;
573 poler
= ch
->ik_arm_r
.pole
;
575 v3_copy( (v3f
){ 0.0f
, 0.0f
, 1.0f
}, arml
);
576 v3_copy( (v3f
){ 0.0f
, 0.0f
,-1.0f
}, armr
);
577 v3_copy( (v3f
){ 1.0f
, 1.0f
, 0.5f
}, polel
);
578 v3_copy( (v3f
){ 1.0f
, 1.0f
,-0.5f
}, poler
);
581 ch
->rhip
= sinf(t
*0.2f
);
582 ch
->rcollar
= sinf(t
*0.35325f
);
583 q_identity( ch
->qhead
);
585 m4x3_identity( ch
->matrices
[k_chpart_board
] );
586 m4x3_identity( ch
->matrices
[k_chpart_wb
] );
587 m4x3_identity( ch
->matrices
[k_chpart_wf
] );
590 static void character_draw( struct character
*ch
, float temp
, m4x3f camera
)
592 shader_character_use();
593 shader_character_uPv( vg_pv
);
595 vg_tex2d_bind( &tex_pallet
, 0 );
596 shader_character_uTexMain( 0 );
597 shader_character_uOpacity( temp
);
598 shader_character_uCamera( camera
[3] );
599 shader_link_standard_ub( _shader_character
.id
, 2 );
601 glEnable( GL_CULL_FACE
);
602 glCullFace( GL_BACK
);
604 mesh_bind( &ch
->mesh
);
606 for( int i
=4; i
<PART_COUNT
; i
++ )
609 if( i
== k_chpart_head
|| i
== k_chpart_neck
)
613 shader_character_uMdl( ch
->matrices
[i
] );
614 mdl_draw_submesh( &ch
->parts
[i
] );
617 for( int i
=0; i
<2; i
++ )
621 shader_character_uMdl( ch
->matrices
[i
] );
622 mdl_draw_submesh( &ch
->parts
[i
] );
626 shader_character_uMdl( ch
->matrices
[i
+2] );
627 mdl_draw_submesh( &ch
->parts
[i
] );
628 shader_character_uMdl( ch
->matrices
[i
] );
629 mdl_draw_submesh( &ch
->parts
[i
+2] );
638 static void character_rd_box( struct character
*ch
, enum character_part id
,
641 v3_muls( dims
, -0.5f
, ch
->ragdoll
[id
].bbx
[0] );
642 v3_muls( dims
, 0.5f
, ch
->ragdoll
[id
].bbx
[1] );
647 enum character_part ia
, ib
;
658 static const float k_human_major
= 0.5f
,
659 k_human_minor
= 0.5f
,
660 k_human_major_max
= 0.4f
,
661 k_human_minor_max
= 0.1f
;
663 #define HUMAN_VERTICAL_DEFAULT \
665 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
666 .spring = k_human_minor, .ang = k_human_minor_max \
669 .va = {0.0f,1.0f,0.0f}, .vb = {0.0f,1.0f,0.0f}, \
670 .spring = k_human_major, .ang = k_human_major_max \
673 #define HUMAN_ARM_LEFT \
675 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
676 .spring = k_human_minor, .ang = k_human_minor_max \
679 .va = {0.0f,0.0f,1.0f}, .vb = {0.0f,0.0f,1.0f}, \
680 .spring = k_human_major, .ang = k_human_major_max \
683 #define HUMAN_ARM_RIGHT \
685 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
686 .spring = k_human_minor, .ang = k_human_minor_max \
689 .va = {0.0f,0.0f,-1.0f}, .vb = {0.0f,0.0f,-1.0f}, \
690 .spring = k_human_major, .ang = k_human_major_max \
693 static struct rd_joint rd_joints
[] =
695 { .ia
= k_chpart_leg_l1
, .ib
= k_chpart_foot_l
, HUMAN_VERTICAL_DEFAULT
},
696 { .ia
= k_chpart_leg_r1
, .ib
= k_chpart_foot_r
, HUMAN_VERTICAL_DEFAULT
},
698 { .ia
= k_chpart_body0
, .ib
= k_chpart_body1
, HUMAN_VERTICAL_DEFAULT
},
699 { .ia
= k_chpart_body1
, .ib
= k_chpart_neck
, HUMAN_VERTICAL_DEFAULT
},
700 { .ia
= k_chpart_neck
, .ib
= k_chpart_head
, HUMAN_VERTICAL_DEFAULT
},
701 { .ia
= k_chpart_body0
, .ib
= k_chpart_leg_l0
, HUMAN_VERTICAL_DEFAULT
},
702 { .ia
= k_chpart_leg_l0
, .ib
= k_chpart_leg_l1
, HUMAN_VERTICAL_DEFAULT
},
703 { .ia
= k_chpart_body0
, .ib
= k_chpart_leg_r0
, HUMAN_VERTICAL_DEFAULT
},
704 { .ia
= k_chpart_leg_r0
, .ib
= k_chpart_leg_r1
, HUMAN_VERTICAL_DEFAULT
},
706 { .ia
= k_chpart_body1
, .ib
= k_chpart_arm_l0
, HUMAN_ARM_LEFT
},
707 { .ia
= k_chpart_arm_l0
, .ib
= k_chpart_arm_l1
, HUMAN_ARM_LEFT
},
708 { .ia
= k_chpart_arm_l1
, .ib
= k_chpart_hand_l
, HUMAN_ARM_LEFT
},
710 { .ia
= k_chpart_body1
, .ib
= k_chpart_arm_r0
, HUMAN_ARM_RIGHT
},
711 { .ia
= k_chpart_arm_r0
, .ib
= k_chpart_arm_r1
, HUMAN_ARM_RIGHT
},
712 { .ia
= k_chpart_arm_r1
, .ib
= k_chpart_hand_r
, HUMAN_ARM_RIGHT
}
715 /* Ragdoll should be in rest pose when calling this function */
716 static void character_init_ragdoll_joints( struct character
*ch
)
718 for( int i
=0; i
<vg_list_size(rd_joints
); i
++ )
720 struct rd_joint
*joint
= &rd_joints
[i
];
722 float *hinge
= ch
->origins
[joint
->ib
];
723 v3_sub( hinge
, ch
->ragdoll
[joint
->ia
].co
, joint
->lca
);
724 v3_sub( hinge
, ch
->ragdoll
[joint
->ib
].co
, joint
->lcb
);
727 for( int i
=0; i
<PART_COUNT
; i
++ )
729 float *pivot
= ch
->origins
[i
];
730 v3_sub( ch
->ragdoll
[i
].co
, pivot
, ch
->ragdoll
[i
].delta
);
734 static void character_init_ragdoll( struct character
*ch
)
736 v3f
*offs
= ch
->offsets
;
737 rigidbody
*rbs
= ch
->ragdoll
;
740 float chest_width
= fabsf(offs
[k_chpart_arm_r0
][2])*2.0f
,
741 chest_depth
= chest_width
* 0.571f
,
742 chest_height
= offs
[k_chpart_neck
][1];
743 v3f chest_dims
= { chest_depth
, chest_height
, chest_width
};
744 character_rd_box( ch
, k_chpart_body1
, chest_dims
);
746 v3_copy( ch
->origins
[k_chpart_body1
], rbs
[k_chpart_body1
].co
);
747 rbs
[k_chpart_body1
].co
[1] += chest_height
*0.5f
;
750 v3f torso_dims
= { chest_depth
,
751 offs
[k_chpart_body1
][1]-offs
[k_chpart_leg_l0
][1],
753 v3_copy( ch
->origins
[k_chpart_body0
], rbs
[k_chpart_body0
].co
);
754 character_rd_box( ch
, k_chpart_body0
, torso_dims
);
757 v3f neck_dims
= { chest_depth
*0.5f
,
758 offs
[k_chpart_head
][1],
760 v3_copy( ch
->origins
[k_chpart_neck
], rbs
[k_chpart_neck
].co
);
761 rbs
[k_chpart_neck
].co
[1] += neck_dims
[1]*0.5f
;
762 character_rd_box( ch
, k_chpart_neck
, neck_dims
);
765 v3f head_dims
= { chest_width
*0.5f
, chest_width
*0.5f
, chest_width
*0.5f
};
766 v3_copy( ch
->origins
[k_chpart_head
], rbs
[k_chpart_head
].co
);
767 rbs
[k_chpart_head
].co
[1] += head_dims
[1]*0.5f
;
768 character_rd_box( ch
, k_chpart_head
, head_dims
);
771 v3f ua_dims
= { 0.0f
, 0.0f
, fabsf(offs
[k_chpart_arm_l1
][2]) };
772 ua_dims
[1] = 0.38f
*ua_dims
[2];
773 ua_dims
[0] = 0.38f
*ua_dims
[2];
774 v3f la_dims
= { ua_dims
[0], ua_dims
[1], fabsf(offs
[k_chpart_hand_l
][2]) };
775 v3f hand_dims
= { ua_dims
[1], ua_dims
[1]*0.5f
, ua_dims
[1] };
777 character_rd_box( ch
, k_chpart_arm_l0
, ua_dims
);
778 character_rd_box( ch
, k_chpart_arm_r0
, ua_dims
);
779 character_rd_box( ch
, k_chpart_arm_l1
, la_dims
);
780 character_rd_box( ch
, k_chpart_arm_r1
, la_dims
);
781 character_rd_box( ch
, k_chpart_hand_l
, hand_dims
);
782 character_rd_box( ch
, k_chpart_hand_r
, hand_dims
);
784 v3_copy( ch
->origins
[k_chpart_arm_l0
], rbs
[k_chpart_arm_l0
].co
);
785 rbs
[k_chpart_arm_l0
].co
[2] += ua_dims
[2] * 0.5f
;
786 v3_copy( ch
->origins
[k_chpart_arm_l1
], rbs
[k_chpart_arm_l1
].co
);
787 rbs
[k_chpart_arm_l1
].co
[2] += la_dims
[2] * 0.5f
;
788 v3_copy( ch
->origins
[k_chpart_hand_l
], rbs
[k_chpart_hand_l
].co
);
789 rbs
[k_chpart_hand_l
].co
[2] += hand_dims
[2] * 0.5f
;
791 v3_copy( ch
->origins
[k_chpart_arm_r0
], rbs
[k_chpart_arm_r0
].co
);
792 rbs
[k_chpart_arm_r0
].co
[2] -= ua_dims
[2] * 0.5f
;
793 v3_copy( ch
->origins
[k_chpart_arm_r1
], rbs
[k_chpart_arm_r1
].co
);
794 rbs
[k_chpart_arm_r1
].co
[2] -= la_dims
[2] * 0.5f
;
795 v3_copy( ch
->origins
[k_chpart_hand_r
], rbs
[k_chpart_hand_r
].co
);
796 rbs
[k_chpart_hand_r
].co
[2] -= hand_dims
[2] * 0.5f
;
799 v3f ul_dims
= { 0.0f
, fabsf(offs
[k_chpart_leg_l1
][1]), 0.0f
};
800 ul_dims
[0] = 0.38f
*ul_dims
[1];
801 ul_dims
[2] = 0.38f
*ul_dims
[1];
802 v3f ll_dims
= { ul_dims
[0], fabsf(offs
[k_chpart_foot_l
][1]), ul_dims
[2] };
803 v3f foot_dims
= { 2.0f
*ul_dims
[0], ul_dims
[0], ul_dims
[0] };
805 character_rd_box( ch
, k_chpart_leg_l0
, ul_dims
);
806 character_rd_box( ch
, k_chpart_leg_r0
, ul_dims
);
807 character_rd_box( ch
, k_chpart_leg_l1
, ll_dims
);
808 character_rd_box( ch
, k_chpart_leg_r1
, ll_dims
);
809 character_rd_box( ch
, k_chpart_foot_l
, foot_dims
);
810 character_rd_box( ch
, k_chpart_foot_r
, foot_dims
);
812 v3_copy( ch
->origins
[k_chpart_leg_l0
], rbs
[k_chpart_leg_l0
].co
);
813 rbs
[k_chpart_leg_l0
].co
[1] -= ul_dims
[1] * 0.5f
;
814 v3_copy( ch
->origins
[k_chpart_leg_l1
], rbs
[k_chpart_leg_l1
].co
);
815 rbs
[k_chpart_leg_l1
].co
[1] -= ll_dims
[1] * 0.5f
;
816 v3_copy( ch
->origins
[k_chpart_foot_l
], rbs
[k_chpart_foot_l
].co
);
817 rbs
[k_chpart_foot_l
].co
[1] -= foot_dims
[1] * 0.5f
;
818 rbs
[k_chpart_foot_l
].co
[0] -= foot_dims
[0] * 0.5f
;
820 v3_copy( ch
->origins
[k_chpart_leg_r0
], rbs
[k_chpart_leg_r0
].co
);
821 rbs
[k_chpart_leg_r0
].co
[1] -= ul_dims
[1] * 0.5f
;
822 v3_copy( ch
->origins
[k_chpart_leg_r1
], rbs
[k_chpart_leg_r1
].co
);
823 rbs
[k_chpart_leg_r1
].co
[1] -= ll_dims
[1] * 0.5f
;
824 v3_copy( ch
->origins
[k_chpart_foot_r
], rbs
[k_chpart_foot_r
].co
);
825 rbs
[k_chpart_foot_r
].co
[1] -= foot_dims
[1] * 0.5f
;
826 rbs
[k_chpart_foot_r
].co
[0] -= foot_dims
[0] * 0.5f
;
828 character_rd_box( ch
, k_chpart_sock_l
, foot_dims
);
829 character_rd_box( ch
, k_chpart_sock_r
, foot_dims
);
830 v3_copy( rbs
[k_chpart_foot_l
].co
, rbs
[k_chpart_sock_l
].co
);
831 v3_copy( rbs
[k_chpart_foot_r
].co
, rbs
[k_chpart_sock_r
].co
);
833 box_copy( (boxf
){{-0.2f
,-0.2f
,-0.7f
},{0.2f
,0.2f
,0.7f
}},
834 rbs
[k_chpart_board
].bbx
);
836 for( int i
=0; i
<PART_COUNT
; i
++ )
837 rb_init( &ch
->ragdoll
[i
] );
839 character_init_ragdoll_joints( ch
);
842 static void character_ragdoll_go( struct character
*ch
, v3f pos
)
844 character_init_ragdoll( ch
);
845 for( int i
=0; i
<PART_COUNT
; i
++ )
846 v3_add( pos
, ch
->ragdoll
[i
].co
, ch
->ragdoll
[i
].co
);
849 static void character_ragdoll_copypose( struct character
*ch
, v3f v
)
851 for( int i
=0; i
<PART_COUNT
; i
++ )
853 rigidbody
*rb
= &ch
->ragdoll
[i
];
855 m4x3_mulv( ch
->matrices
[i
], rb
->delta
, rb
->co
);
856 m3x3_q( ch
->matrices
[i
], rb
->q
);
860 rb_update_transform( rb
);
863 float vel
= v3_length(v
);
869 static void character_mimic_ragdoll( struct character
*ch
)
871 for( int i
=0; i
<PART_COUNT
; i
++ )
873 rigidbody
*rb
= &ch
->ragdoll
[i
];
874 v3f
*mat
= ch
->matrices
[i
];
876 m3x3_copy( rb
->to_world
, mat
);
878 v3_negate( rb
->delta
, inv_delta
);
879 m4x3_mulv( rb
->to_world
, inv_delta
, mat
[3] );
882 /* Attach wheels to board */
883 m3x3_copy( ch
->matrices
[k_chpart_board
], ch
->matrices
[k_chpart_wb
] );
884 m3x3_copy( ch
->matrices
[k_chpart_board
], ch
->matrices
[k_chpart_wf
] );
885 m4x3_mulv( ch
->matrices
[k_chpart_board
], ch
->offsets
[k_chpart_wb
],
886 ch
->matrices
[k_chpart_wb
][3] );
887 m4x3_mulv( ch
->matrices
[k_chpart_board
], ch
->offsets
[k_chpart_wf
],
888 ch
->matrices
[k_chpart_wf
][3] );
891 static void character_debug_ragdoll( struct character
*ch
)
893 rb_debug( &ch
->ragdoll
[k_chpart_body0
], 0xffffffff );
894 rb_debug( &ch
->ragdoll
[k_chpart_body1
], 0xffffffff );
895 rb_debug( &ch
->ragdoll
[k_chpart_neck
], 0xff00ff00 );
896 rb_debug( &ch
->ragdoll
[k_chpart_head
], 0xff00ff00 );
898 rb_debug( &ch
->ragdoll
[k_chpart_arm_l0
], 0xffffa500 );
899 rb_debug( &ch
->ragdoll
[k_chpart_arm_l1
], 0xffffa500 );
900 rb_debug( &ch
->ragdoll
[k_chpart_hand_l
], 0xffffa500 );
902 rb_debug( &ch
->ragdoll
[k_chpart_arm_r0
], 0xff00a5ff );
903 rb_debug( &ch
->ragdoll
[k_chpart_arm_r1
], 0xff00a5ff );
904 rb_debug( &ch
->ragdoll
[k_chpart_hand_r
], 0xff00a5ff );
906 rb_debug( &ch
->ragdoll
[k_chpart_leg_l0
], 0xffffa500 );
907 rb_debug( &ch
->ragdoll
[k_chpart_leg_l1
], 0xffffa500 );
908 rb_debug( &ch
->ragdoll
[k_chpart_foot_l
], 0xffffa500 );
909 rb_debug( &ch
->ragdoll
[k_chpart_leg_r0
], 0xff00a5ff );
910 rb_debug( &ch
->ragdoll
[k_chpart_leg_r1
], 0xff00a5ff );
911 rb_debug( &ch
->ragdoll
[k_chpart_foot_r
], 0xff00a5ff );
914 static void character_ragdoll_iter( struct character
*ch
)
918 for( int i
=0; i
<PART_COUNT
; i
++ )
920 rb_collide( &ch
->ragdoll
[i
], &world
.rb_geo
);
923 rb_presolve_contacts( rb_contact_buffer
, rb_contact_count
);
927 float shoe_vel
[2] = {0.0f
,0.0f
};
928 for( int i
=0; i
<2; i
++ )
930 shoe_vel
[i
] = v3_length( ch
->ragdoll
[i
].v
);
932 /* This used to be 20 iterations */
933 for( int i
=0; i
<10; i
++ )
935 float const k_springfactor
= 1.0f
/20.0f
;
937 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
939 for( int j
=0; j
<vg_list_size(rd_joints
); j
++ )
941 struct rd_joint
*joint
= &rd_joints
[j
];
942 rigidbody
*rba
= &ch
->ragdoll
[joint
->ia
],
943 *rbb
= &ch
->ragdoll
[joint
->ib
];
945 rb_constraint_position( rba
, joint
->lca
, rbb
, joint
->lcb
);
946 rb_constraint_angle( rba
, joint
->maj
.va
, rbb
, joint
->maj
.vb
,
948 joint
->maj
.spring
* k_springfactor
);
950 rb_constraint_angle( rba
, joint
->min
.va
, rbb
, joint
->min
.vb
,
952 joint
->min
.spring
* k_springfactor
);
956 for( int j
=0; j
<vg_list_size(rd_joints
); j
++ )
958 struct rd_joint
*joint
= &rd_joints
[j
];
959 rigidbody
*rba
= &ch
->ragdoll
[joint
->ia
],
960 *rbb
= &ch
->ragdoll
[joint
->ib
];
961 rb_angle_limit_force( rba
, joint
->min
.va
, rbb
, joint
->min
.vb
,
963 rb_angle_limit_force( rba
, joint
->maj
.va
, rbb
, joint
->maj
.vb
,
967 for( int i
=0; i
<PART_COUNT
; i
++ )
968 rb_iter( &ch
->ragdoll
[i
] );
970 for( int i
=0; i
<2; i
++ )
974 float a
= v3_length( ch
->ragdoll
[i
].v
) - shoe_vel
[i
];
980 rigidbody
*src
= &ch
->ragdoll
[k_chpart_foot_l
];
981 rigidbody
*dst
= &ch
->ragdoll
[k_chpart_sock_l
];
983 v3_copy( src
->co
, dst
->co
);
984 v3_copy( src
->v
, dst
->v
);
985 v3_copy( src
->q
, dst
->q
);
986 v3_copy( src
->w
, dst
->w
);
991 for( int i
=0; i
<PART_COUNT
; i
++ )
992 rb_update_transform( &ch
->ragdoll
[i
] );