before the storm
[carveJwlIkooP6JGAAIwe30JlM.git] / player_model.h
1 /*
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef CHARACTER_H
6 #define CHARACTER_H
7
8 #include "common.h"
9 #include "model.h"
10 #include "rigidbody.h"
11 #include "render.h"
12 #include "skeleton.h"
13 #include "world.h"
14 #include "skeleton_animator.h"
15 #include "shaders/viewchar.h"
16
17 static float k_ragdoll_floatyiness = 10.0f,
18 k_ragdoll_floatydrag = 1.0f;
19
20 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
21
22 static void character_register(void)
23 {
24 shader_viewchar_register();
25 }
26
27 static void character_init(void)
28 {
29 vg_tex2d_init( (vg_tex2d *[]){ &tex_characters }, 1 );
30 }
31
32 struct character
33 {
34 glmesh mesh;
35 struct skeleton sk;
36 struct skeleton_anim *anim_stand,
37 *anim_highg,
38 *anim_slide,
39 *anim_air,
40 *anim_push, *anim_push_reverse,
41 *anim_ollie, *anim_ollie_reverse,
42 *anim_grabs, *anim_stop,
43 *anim_walk, *anim_run, *anim_idle;
44
45 u32 id_hip,
46 id_ik_hand_l,
47 id_ik_hand_r,
48 id_ik_elbow_l,
49 id_ik_elbow_r,
50 id_head;
51
52 v3f cam_pos;
53
54 struct ragdoll_part
55 {
56 u32 bone_id;
57 v3f offset;
58
59 u32 use_limits;
60 v3f limits[2];
61
62 rigidbody rb;
63 u32 parent;
64 }
65 *ragdoll;
66 u32 ragdoll_count;
67
68 int shoes[2];
69 };
70
71 static int character_load( struct character *ch, const char *name )
72 {
73 char buf[64];
74
75 snprintf( buf, sizeof(buf)-1, "models/%s.mdl", name );
76 mdl_header *src = mdl_load( buf );
77
78 if( !src )
79 return 0;
80
81 mdl_unpack_glmesh( src, &ch->mesh );
82
83 skeleton_setup( &ch->sk, src );
84 ch->anim_stand = skeleton_get_anim( &ch->sk, "pose_stand" );
85 ch->anim_highg = skeleton_get_anim( &ch->sk, "pose_highg" );
86 ch->anim_slide = skeleton_get_anim( &ch->sk, "pose_slide" );
87 ch->anim_air = skeleton_get_anim( &ch->sk, "pose_air" );
88 ch->anim_push = skeleton_get_anim( &ch->sk, "push" );
89 ch->anim_push_reverse = skeleton_get_anim( &ch->sk, "push_reverse" );
90 ch->anim_ollie = skeleton_get_anim( &ch->sk, "ollie" );
91 ch->anim_ollie_reverse = skeleton_get_anim( &ch->sk, "ollie_reverse" );
92 ch->anim_grabs = skeleton_get_anim( &ch->sk, "grabs" );
93 ch->anim_walk = skeleton_get_anim( &ch->sk, "walk" );
94 ch->anim_run = skeleton_get_anim( &ch->sk, "run" );
95 ch->anim_idle = skeleton_get_anim( &ch->sk, "idle_cycle" );
96
97 ch->id_hip = skeleton_bone_id( &ch->sk, "hips" );
98 ch->id_ik_hand_l = skeleton_bone_id( &ch->sk, "hand.IK.L" );
99 ch->id_ik_hand_r = skeleton_bone_id( &ch->sk, "hand.IK.R" );
100 ch->id_ik_elbow_l = skeleton_bone_id( &ch->sk, "elbow.L" );
101 ch->id_ik_elbow_r = skeleton_bone_id( &ch->sk, "elbow.R" );
102 ch->id_head = skeleton_bone_id( &ch->sk, "head" );
103
104 /* setup ragdoll */
105
106 if( ch->sk.collider_count )
107 {
108 vg_info( "Alloc: %d\n", ch->sk.collider_count );
109 ch->ragdoll = malloc(sizeof(struct ragdoll_part) * ch->sk.collider_count);
110 ch->ragdoll_count = 0;
111
112 for( u32 i=0; i<ch->sk.bone_count; i ++ )
113 {
114 struct skeleton_bone *bone = &ch->sk.bones[i];
115
116 if( bone->collider )
117 {
118 struct ragdoll_part *rp = &ch->ragdoll[ ch->ragdoll_count ++ ];
119 rp->bone_id = i;
120
121 v3f delta;
122 v3_sub( bone->hitbox[1], bone->hitbox[0], delta );
123 v3_muls( delta, 0.5f, delta );
124
125 v3_add( bone->hitbox[0], delta, rp->offset );
126
127 v3_copy( delta, rp->rb.bbx[1] );
128 v3_muls( delta, -1.0f, rp->rb.bbx[0] );
129
130 q_identity( rp->rb.q );
131 v3_add( bone->co, rp->offset, rp->rb.co );
132 rp->rb.type = k_rb_shape_box;
133 rp->rb.is_world = 0;
134 rp->parent = 0xffffffff;
135
136 if( bone->parent )
137 {
138 for( u32 j=0; j<ch->ragdoll_count; j++ )
139 {
140 if( ch->ragdoll[ j ].bone_id == bone->parent )
141 {
142 rp->parent = j;
143 break;
144 }
145 }
146 }
147
148 /* TODO: refactor to use this style elswhere */
149 struct mdl_node *pnode = mdl_node_from_id( src, bone->orig_node );
150 struct classtype_bone *bone_inf = mdl_get_entdata( src, pnode );
151
152 rp->use_limits = bone_inf->use_limits;
153 v3_copy( bone_inf->angle_limits[0], rp->limits[0] );
154 v3_copy( bone_inf->angle_limits[1], rp->limits[1] );
155
156 rb_init( &rp->rb );
157 }
158 }
159 }
160
161 free( src );
162 return 1;
163 }
164
165 static void character_eval( struct character *ch ){}
166 static void character_draw( struct character *ch, float temp, m4x3f camera ){}
167 static void character_init_ragdoll_joints( struct character *ch ){}
168 static void character_init_ragdoll( struct character *ch ){}
169 static void character_ragdoll_go( struct character *ch, v3f pos ){}
170
171 static void character_mimic_ragdoll( struct character *ch )
172 {
173 for( int i=0; i<ch->ragdoll_count; i++ )
174 {
175 struct ragdoll_part *part = &ch->ragdoll[i];
176 m4x3f offset;
177 m3x3_identity(offset);
178 v3_negate( part->offset, offset[3] );
179 m4x3_mul( part->rb.to_world, offset, ch->sk.final_mtx[part->bone_id] );
180 }
181
182 skeleton_apply_inverses( &ch->sk );
183 }
184
185 static void character_ragdoll_copypose( struct character *ch, v3f v )
186 {
187 for( int i=0; i<ch->ragdoll_count; i++ )
188 {
189 struct ragdoll_part *part = &ch->ragdoll[i];
190
191 v3f pos, offset;
192 u32 bone = part->bone_id;
193
194 m4x3_mulv( ch->sk.final_mtx[bone], ch->sk.bones[bone].co, pos );
195 m3x3_mulv( ch->sk.final_mtx[bone], part->offset, offset );
196 v3_add( pos, offset, part->rb.co );
197 m3x3_q( ch->sk.final_mtx[bone], part->rb.q );
198 v3_copy( v, part->rb.v );
199 v3_zero( part->rb.w );
200
201 rb_update_transform( &part->rb );
202 }
203 }
204
205 static void character_debug_ragdoll( struct character *ch )
206 {
207 for( u32 i=0; i<ch->ragdoll_count; i ++ )
208 rb_debug( &ch->ragdoll[i].rb, 0xff00ff00 );
209 }
210
211 static void character_ragdoll_iter( struct character *ch )
212 {
213 rb_solver_reset();
214
215 for( int i=0; i<ch->ragdoll_count; i ++ )
216 rb_collide( &ch->ragdoll[i].rb, &world.rb_geo );
217
218 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
219
220 v3f rv;
221
222 float shoe_vel[2] = {0.0f,0.0f};
223 for( int i=0; i<2; i++ )
224 if( ch->shoes[i] )
225 shoe_vel[i] = v3_length( ch->ragdoll[i].rb.v );
226
227 for( int j=0; j<ch->ragdoll_count; j++ )
228 {
229 struct ragdoll_part *pj = &ch->ragdoll[j];
230 struct skeleton_bone *bj = &ch->sk.bones[pj->bone_id];
231
232 if( pj->parent != 0xffffffff )
233 {
234 struct ragdoll_part *pp = &ch->ragdoll[pj->parent];
235 struct skeleton_bone *bp = &ch->sk.bones[pp->bone_id];
236
237 v3f lca, lcb;
238 v3_negate( pj->offset, lca );
239 v3_add( bp->co, pp->offset, lcb );
240 v3_sub( bj->co, lcb, lcb );
241
242 rb_debug_constraint_position( &pj->rb, lca, &pp->rb, lcb );
243
244 if( pj->use_limits )
245 {
246 rb_debug_constraint_limits( &pj->rb, &pp->rb, lca, pj->limits );
247 }
248 }
249
250 v4f plane = {0.0f,1.0f,0.0f,0.0f};
251 rb_effect_simple_bouyency( &pj->rb, plane, k_ragdoll_floatyiness,
252 k_ragdoll_floatydrag );
253 }
254
255 /* CONSTRAINTS */
256 for( int i=0; i<10; i++ )
257 {
258 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
259
260 for( int j=0; j<ch->ragdoll_count; j++ )
261 {
262 struct ragdoll_part *pj = &ch->ragdoll[j];
263 struct skeleton_bone *bj = &ch->sk.bones[pj->bone_id];
264
265 if( pj->parent != 0xffffffff && pj->use_limits )
266 {
267 struct ragdoll_part *pp = &ch->ragdoll[pj->parent];
268 struct skeleton_bone *bp = &ch->sk.bones[pp->bone_id];
269
270 v3f lca, lcb;
271 v3_negate( pj->offset, lca );
272 v3_add( bp->co, pp->offset, lcb );
273 v3_sub( bj->co, lcb, lcb );
274
275 rb_constraint_position( &pj->rb, lca, &pp->rb, lcb );
276
277 rb_constraint_limits( &pj->rb, lca, &pp->rb, lcb, pj->limits );
278 }
279 }
280 }
281
282 /* INTEGRATION */
283 for( int i=0; i<ch->ragdoll_count; i++ )
284 rb_iter( &ch->ragdoll[i].rb );
285
286 /* SHOES */
287
288 for( int i=0; i<ch->ragdoll_count; i++ )
289 rb_update_transform( &ch->ragdoll[i].rb );
290 }
291
292 #endif