update overlay to have ps controls too
[carveJwlIkooP6JGAAIwe30JlM.git] / model.h
1 /*
2 * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #pragma once
6
7 #define MDL_VERSION_MIN 101
8 #define MDL_VERSION_NR 106
9
10 enum mdl_shader{
11 k_shader_standard = 0,
12 k_shader_standard_cutout = 1,
13 k_shader_terrain_blend = 2,
14 k_shader_standard_vertex_blend = 3,
15 k_shader_water = 4,
16 k_shader_invisible = 5,
17 k_shader_boundary = 6,
18 k_shader_fxglow = 7,
19 k_shader_cubemap = 8,
20 k_shader_walking = 9,
21 k_shader_foliage = 10,
22 k_shader_override = 30000
23 };
24
25 enum mdl_surface_prop{
26 k_surface_prop_concrete = 0,
27 k_surface_prop_wood = 1,
28 k_surface_prop_grass = 2,
29 k_surface_prop_tiles = 3,
30 k_surface_prop_metal = 4,
31 k_surface_prop_snow = 5,
32 k_surface_prop_sand = 6
33 };
34
35 enum material_flag{
36 k_material_flag_skate_target = 0x0001,
37 k_material_flag_collision = 0x0002,
38 k_material_flag_grow_grass = 0x0004,
39 k_material_flag_grindable = 0x0008,
40 k_material_flag_invisible = 0x0010,
41 k_material_flag_boundary = 0x0020,
42 k_material_flag_preview_visibile = 0x0040,
43 k_material_flag_walking = 0x0080,
44
45 k_material_flag_ghosts =
46 k_material_flag_boundary|
47 k_material_flag_invisible|
48 k_material_flag_walking
49 };
50
51 #pragma pack(push,1)
52
53 /* 48 byte */
54 struct mdl_vert
55 {
56 v3f co, /* 3*32 */
57 norm; /* 3*32 */
58 v2f uv; /* 2*32 */
59
60 u8 colour[4]; /* 4*8 */
61 u16 weights[4];/* 4*16 */
62 u8 groups[4]; /* 4*8 */
63 };
64
65 #pragma pack(pop)
66
67 typedef u32 mdl_indice;
68
69 typedef struct mdl_context mdl_context;
70 typedef struct mdl_array_ptr mdl_array_ptr;
71 typedef struct mdl_vert mdl_vert;
72 typedef struct mdl_transform mdl_transform;
73 typedef struct mdl_submesh mdl_submesh;
74 typedef struct mdl_material mdl_material;
75 typedef struct mdl_bone mdl_bone;
76 typedef struct mdl_armature mdl_armature;
77 typedef struct mdl_animation mdl_animation;
78 typedef struct mdl_transform mdl_keyframe;
79 typedef struct mdl_mesh mdl_mesh;
80 typedef struct mdl_file mdl_file;
81 typedef struct mdl_texture mdl_texture;
82 typedef struct mdl_array mdl_array;
83 typedef struct mdl_header mdl_header;
84
85 typedef struct glmesh glmesh;
86 struct glmesh
87 {
88 u32 vao, vbo, ebo;
89 u32 indice_count;
90 u32 loaded;
91 };
92
93 struct mdl_transform
94 {
95 v3f co, s;
96 v4f q;
97 };
98
99 static void transform_identity( mdl_transform *transform )
100 {
101 v3_zero( transform->co );
102 q_identity( transform->q );
103 v3_fill( transform->s, 1.0f );
104 }
105
106 static void mdl_transform_vector( mdl_transform *transform, v3f vec, v3f dest )
107 {
108 v3_mul( transform->s, vec, dest );
109 q_mulv( transform->q, dest, dest );
110 }
111
112 static void mdl_transform_point( mdl_transform *transform, v3f co, v3f dest )
113 {
114 mdl_transform_vector( transform, co, dest );
115 v3_add( transform->co, dest, dest );
116 }
117
118 static void mdl_transform_mul( mdl_transform *a, mdl_transform *b,
119 mdl_transform *d )
120 {
121 mdl_transform_point( a, b->co, d->co );
122 q_mul( a->q, b->q, d->q );
123 q_normalize( d->q );
124 v3_mul( a->s, b->s, d->s );
125 }
126
127 struct mdl_file
128 {
129 u32 pstr_path,
130 pack_offset,
131 pack_size;
132 };
133
134 #if (MDL_VERSION_MIN <= 105)
135 struct mdl_material_v105
136 {
137 u32 pstr_name,
138 shader,
139 flags,
140 surface_prop;
141
142 v4f colour,
143 colour1;
144
145 u32 tex_diffuse, /* Indexes start from 1. 0 if missing. */
146 tex_none0,
147 tex_none1;
148 };
149 #endif
150
151 struct mdl_material
152 {
153 u32 pstr_name,
154 shader,
155 flags,
156 surface_prop;
157
158 union
159 {
160 struct
161 {
162 u32 offset, size;
163 /* -> vg_msg containing KV properties */
164 }
165 kvs;
166 void *compiled; /* -> shader specific structure for render */
167 }
168 props;
169 };
170
171 struct mdl_bone
172 {
173 v3f co, end;
174 u32 parent,
175 collider,
176 ik_target,
177 ik_pole,
178 flags,
179 pstr_name;
180
181 boxf hitbox;
182 v3f conevx, conevy, coneva;
183 float conet;
184 };
185
186 enum bone_flag
187 {
188 k_bone_flag_deform = 0x00000001,
189 k_bone_flag_ik = 0x00000002,
190 k_bone_flag_cone_constraint = 0x00000004
191 };
192
193 enum bone_collider
194 {
195 k_bone_collider_none = 0,
196 k_bone_collider_box = 1,
197 k_bone_collider_capsule = 2
198 };
199
200 struct mdl_armature
201 {
202 mdl_transform transform;
203 u32 bone_start,
204 bone_count,
205 anim_start,
206 anim_count;
207 };
208
209 struct mdl_animation
210 {
211 u32 pstr_name,
212 length;
213 float rate;
214 u32 offset;
215 };
216
217 struct mdl_submesh
218 {
219 u32 indice_start,
220 indice_count,
221 vertex_start,
222 vertex_count;
223
224 boxf bbx;
225 u16 material_id, flags;
226 };
227
228 enum esubmesh_flags
229 {
230 k_submesh_flag_none = 0x0000,
231 k_submesh_flag_consumed = 0x0001
232 };
233
234 struct mdl_mesh
235 {
236 mdl_transform transform;
237 u32 submesh_start,
238 submesh_count,
239 pstr_name,
240 entity_id, /* upper 16 bits: type, lower 16 bits: index */
241 armature_id;
242 };
243
244 struct mdl_texture
245 {
246 mdl_file file;
247 u32 glname;
248 };
249
250 struct mdl_array
251 {
252 u32 file_offset,
253 item_count,
254 item_size;
255
256 char name[16];
257 };
258
259 struct mdl_header
260 {
261 u32 version;
262 mdl_array index;
263 };
264
265 struct mdl_context
266 {
267 FILE *file;
268 mdl_header info;
269
270 struct mdl_array_ptr
271 {
272 void *data;
273 u32 count, stride;
274 }
275 index,
276
277 /* metadata */
278 strings,
279 meshs,
280 submeshs,
281 materials,
282 textures,
283 armatures,
284 bones,
285 animations,
286
287 /* animation buffers */
288 keyframes,
289
290 /* mesh buffers */
291 verts,
292 indices;
293 u32 pack_base_offset;
294
295 /* runtime */
296 glmesh mesh;
297 };
298
299 void mesh_bind( glmesh *mesh );
300 void mesh_drawn( u32 start, u32 count );
301 void mesh_draw( glmesh *mesh );
302 void mesh_free( glmesh *mesh );
303
304 /* file context management */
305 void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc );
306 void mdl_close( mdl_context *mdl );
307
308 /* array loading */
309 int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
310 const char *name, void *lin_alloc, u32 stride );
311 #define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \
312 _mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) )
313
314 /* array access */
315 void *mdl_arritm( mdl_array_ptr *arr, u32 index );
316 u32 mdl_arrcount( mdl_array_ptr *arr );
317
318 /* pack access */
319 void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst );
320
321 /* standard array groups */
322 int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc );
323 int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc );
324 int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc );
325 int mdl_load_materials( mdl_context *mdl, void *lin_alloc );
326
327 /* load mesh */
328 void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh, u32 *fixup_table );
329
330 /* load textures and mesh */
331 void mdl_async_full_load_std( mdl_context *mdl );
332
333 /* rendering */
334 void mdl_draw_submesh( mdl_submesh *sm );
335 mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name );
336 mdl_submesh *mdl_find_submesh( mdl_context *mdl, const char *mesh_name );
337
338 /* pstrs */
339 const char *mdl_pstr( mdl_context *mdl, u32 pstr );
340 int mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 );
341 #define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\
342 mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) )
343
344 void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx );
345