542c7b2f3a4efad3457cacdc07ec6307ff6eff7c
[carveJwlIkooP6JGAAIwe30JlM.git] / player_model.h
1 #ifndef CHARACTER_H
2 #define CHARACTER_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "rigidbody.h"
7 #include "render.h"
8 #include "skeleton.h"
9 #include "world.h"
10 #include "skeleton_animator.h"
11 #include "shaders/viewchar.h"
12
13 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
14
15 static void character_register(void)
16 {
17 shader_viewchar_register();
18 }
19
20 static void character_init(void)
21 {
22 vg_tex2d_init( (vg_tex2d *[]){ &tex_characters }, 1 );
23 }
24
25 struct character
26 {
27 glmesh mesh;
28 struct skeleton sk;
29 struct skeleton_anim *anim_stand,
30 *anim_highg,
31 *anim_slide,
32 *anim_air,
33 *anim_push, *anim_push_reverse,
34 *anim_ollie;
35
36 u32 id_hip,
37 id_ik_hand_l,
38 id_ik_hand_r,
39 id_ik_elbow_l,
40 id_ik_elbow_r,
41 id_head;
42
43 v3f cam_pos;
44
45 struct ragdoll_part
46 {
47 u32 bone_id;
48 v3f offset;
49 rigidbody rb;
50 }
51 *ragdoll;
52 u32 ragdoll_count;
53
54 int shoes[2];
55 };
56
57 static int character_load( struct character *ch, const char *name )
58 {
59 char buf[64];
60
61 snprintf( buf, sizeof(buf)-1, "models/%s.mdl", name );
62 mdl_header *src = mdl_load( buf );
63
64 if( !src )
65 return 0;
66
67 mdl_unpack_glmesh( src, &ch->mesh );
68
69 skeleton_setup( &ch->sk, src );
70 ch->anim_stand = skeleton_get_anim( &ch->sk, "pose_stand" );
71 ch->anim_highg = skeleton_get_anim( &ch->sk, "pose_highg" );
72 ch->anim_slide = skeleton_get_anim( &ch->sk, "pose_slide" );
73 ch->anim_air = skeleton_get_anim( &ch->sk, "pose_air" );
74 ch->anim_push = skeleton_get_anim( &ch->sk, "push" );
75 ch->anim_push_reverse = skeleton_get_anim( &ch->sk, "push_reverse" );
76 ch->anim_ollie = skeleton_get_anim( &ch->sk, "ollie" );
77
78 ch->id_hip = skeleton_bone_id( &ch->sk, "hips" );
79 ch->id_ik_hand_l = skeleton_bone_id( &ch->sk, "hand.IK.L" );
80 ch->id_ik_hand_r = skeleton_bone_id( &ch->sk, "hand.IK.R" );
81 ch->id_ik_elbow_l = skeleton_bone_id( &ch->sk, "elbow.L" );
82 ch->id_ik_elbow_r = skeleton_bone_id( &ch->sk, "elbow.R" );
83 ch->id_head = skeleton_bone_id( &ch->sk, "head" );
84
85 /* setup ragdoll */
86
87 if( ch->sk.collider_count )
88 {
89 vg_info( "Alloc: %d\n", ch->sk.collider_count );
90 ch->ragdoll = malloc(sizeof(struct ragdoll_part) * ch->sk.collider_count);
91 ch->ragdoll_count = 0;
92
93 for( u32 i=0; i<ch->sk.bone_count; i ++ )
94 {
95 struct skeleton_bone *bone = &ch->sk.bones[i];
96
97 if( bone->collider )
98 {
99 struct ragdoll_part *rp = &ch->ragdoll[ ch->ragdoll_count ++ ];
100 rp->bone_id = i;
101
102 v3f delta;
103 v3_sub( bone->hitbox[1], bone->hitbox[0], delta );
104 v3_muls( delta, 0.5f, delta );
105
106 v3_add( bone->hitbox[0], delta, rp->offset );
107
108 v3_copy( delta, rp->rb.bbx[1] );
109 v3_muls( delta, -1.0f, rp->rb.bbx[0] );
110
111 q_identity( rp->rb.q );
112 v3_add( bone->co, rp->offset, rp->rb.co );
113 rp->rb.type = k_rb_shape_box;
114 rp->rb.is_world = 0;
115
116 rb_init( &rp->rb );
117 }
118 }
119 }
120
121 free( src );
122 return 1;
123 }
124
125 static void character_eval( struct character *ch ){}
126 static void character_draw( struct character *ch, float temp, m4x3f camera ){}
127 static void character_init_ragdoll_joints( struct character *ch ){}
128 static void character_init_ragdoll( struct character *ch ){}
129 static void character_ragdoll_go( struct character *ch, v3f pos ){}
130 static void character_ragdoll_copypose( struct character *ch, v3f v )
131 {
132 for( int i=0; i<ch->ragdoll_count; i++ )
133 {
134 struct ragdoll_part *part = &ch->ragdoll[i];
135
136 v3f pos, offset;
137 u32 bone = part->bone_id;
138
139 m4x3_mulv( ch->sk.final_mtx[bone], ch->sk.bones[bone].co, pos );
140 m3x3_mulv( ch->sk.final_mtx[bone], part->offset, offset );
141 v3_add( pos, offset, part->rb.co );
142 m3x3_q( ch->sk.final_mtx[bone], part->rb.q );
143 v3_copy( v, part->rb.v );
144 v3_zero( part->rb.w );
145
146 rb_update_transform( &part->rb );
147 }
148 }
149
150 static void character_debug_ragdoll( struct character *ch )
151 {
152 for( u32 i=0; i<ch->ragdoll_count; i ++ )
153 rb_debug( &ch->ragdoll[i].rb, 0xff00ff00 );
154 }
155
156 static void character_ragdoll_iter( struct character *ch )
157 {
158 rb_solver_reset();
159
160 for( int i=0; i<ch->ragdoll_count; i ++ )
161 rb_collide( &ch->ragdoll[i].rb, &world.rb_geo );
162
163 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
164
165 v3f rv;
166
167 float shoe_vel[2] = {0.0f,0.0f};
168 for( int i=0; i<2; i++ )
169 if( ch->shoes[i] )
170 shoe_vel[i] = v3_length( ch->ragdoll[i].rb.v );
171
172 /* CONSTRAINTS */
173 for( int i=0; i<5; i++ )
174 {
175 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
176 }
177
178 /* INTEGRATION */
179 for( int i=0; i<ch->ragdoll_count; i++ )
180 rb_iter( &ch->ragdoll[i].rb );
181
182 /* SHOES */
183
184 for( int i=0; i<ch->ragdoll_count; i++ )
185 rb_update_transform( &ch->ragdoll[i].rb );
186 }
187
188 #endif