remove some todos
[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 105
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 GLuint 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_material
128 {
129 u32 pstr_name,
130 shader,
131 flags,
132 surface_prop;
133
134 v4f colour,
135 colour1;
136
137 u32 tex_diffuse, /* Indexes start from 1. 0 if missing. */
138 tex_none0,
139 tex_none1;
140 };
141
142 struct mdl_bone
143 {
144 v3f co, end;
145 u32 parent,
146 collider,
147 ik_target,
148 ik_pole,
149 flags,
150 pstr_name;
151
152 boxf hitbox;
153 v3f conevx, conevy, coneva;
154 float conet;
155 };
156
157 enum bone_flag
158 {
159 k_bone_flag_deform = 0x00000001,
160 k_bone_flag_ik = 0x00000002,
161 k_bone_flag_cone_constraint = 0x00000004
162 };
163
164 enum bone_collider
165 {
166 k_bone_collider_none = 0,
167 k_bone_collider_box = 1,
168 k_bone_collider_capsule = 2
169 };
170
171 struct mdl_armature
172 {
173 mdl_transform transform;
174 u32 bone_start,
175 bone_count,
176 anim_start,
177 anim_count;
178 };
179
180 struct mdl_animation
181 {
182 u32 pstr_name,
183 length;
184 float rate;
185 u32 offset;
186 };
187
188 struct mdl_submesh
189 {
190 u32 indice_start,
191 indice_count,
192 vertex_start,
193 vertex_count;
194
195 boxf bbx;
196 u16 material_id, flags;
197 };
198
199 enum esubmesh_flags
200 {
201 k_submesh_flag_none = 0x0000,
202 k_submesh_flag_consumed = 0x0001
203 };
204
205 struct mdl_mesh
206 {
207 mdl_transform transform;
208 u32 submesh_start,
209 submesh_count,
210 pstr_name,
211 entity_id, /* upper 16 bits: type, lower 16 bits: index */
212 armature_id;
213 };
214
215 struct mdl_file
216 {
217 u32 pstr_path,
218 pack_offset,
219 pack_size;
220 };
221
222 struct mdl_texture
223 {
224 mdl_file file;
225 u32 glname;
226 };
227
228 struct mdl_array
229 {
230 u32 file_offset,
231 item_count,
232 item_size;
233
234 char name[16];
235 };
236
237 struct mdl_header
238 {
239 u32 version;
240 mdl_array index;
241 };
242
243 struct mdl_context{
244 FILE *file;
245 mdl_header info;
246
247 struct mdl_array_ptr
248 {
249 void *data;
250 u32 count, stride;
251 }
252 index,
253
254 /* metadata */
255 strings,
256 meshs,
257 submeshs,
258 materials,
259 textures,
260 armatures,
261 bones,
262 animations,
263
264 /* animation buffers */
265 keyframes,
266
267 /* mesh buffers */
268 verts,
269 indices;
270 u32 pack_base_offset;
271
272 /* runtime */
273 glmesh mesh;
274 };
275
276 void mesh_bind( glmesh *mesh );
277 void mesh_drawn( u32 start, u32 count );
278 void mesh_draw( glmesh *mesh );
279 void mesh_free( glmesh *mesh );
280
281 /* file context management */
282 void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc );
283 void mdl_close( mdl_context *mdl );
284
285 /* array loading */
286 int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
287 const char *name, void *lin_alloc, u32 stride );
288 #define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \
289 _mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) )
290
291 /* array access */
292 void *mdl_arritm( mdl_array_ptr *arr, u32 index );
293 u32 mdl_arrcount( mdl_array_ptr *arr );
294
295 /* pack access */
296 void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst );
297
298 /* standard array groups */
299 int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc );
300 int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc );
301 int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc );
302
303 /* load mesh */
304 void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh, u32 *fixup_table );
305
306 /* load textures and mesh */
307 void mdl_async_full_load_std( mdl_context *mdl );
308
309 /* rendering */
310 void mdl_draw_submesh( mdl_submesh *sm );
311 mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name );
312
313 /* pstrs */
314 const char *mdl_pstr( mdl_context *mdl, u32 pstr );
315 int mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 );
316 #define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\
317 mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) )
318
319 void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx );
320