2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 typedef struct glmesh glmesh
;
12 typedef struct mdl_vert mdl_vert
;
13 typedef struct mdl_submesh mdl_submesh
;
14 typedef struct mdl_material mdl_material
;
15 typedef struct mdl_node mdl_node
;
16 typedef struct mdl_header mdl_header
;
17 typedef struct mdl_animation mdl_animation
;
18 typedef struct mdl_keyframe mdl_keyframe
;
20 #define MDL_SIZE_MAX 0x1000000
21 #define MDL_VERT_MAX 1000000
22 #define MDL_INDICE_MAX 1000000
23 #define MDL_MATERIAL_MAX 32
24 #define MDL_NODE_MAX 4000
25 #define MDL_SUBMESH_MAX 8000
26 #define MDL_STRING_LENGTH_MAX 64
32 k_classtype_block
= 2,
33 k_classtype_spawn
= 3,
34 k_classtype_water
= 4,
35 k_classtype_car_path
= 5,
36 k_classtype_instance
= 6,
37 k_classtype_capsule
= 7,
38 k_classtype_route_node
= 8,
39 k_classtype_route
= 9,
40 k_classtype_bone
= 10,
41 k_classtype_skeleton
= 11,
42 k_classtype_skin
= 12,
43 k_classtype_achievement_box
= 13
81 union{ u32 submesh_start
, sub_uid
; };
110 u32 identifier
, version
, file_length
;
112 u32 vertex_count
, vertex_offset
,
113 indice_count
, indice_offset
,
114 submesh_count
, submesh_offset
,
115 material_count
, material_offset
,
116 node_count
, node_offset
,
117 anim_count
, anim_offset
,
118 strings_offset
, entdata_offset
, animdata_offset
;
122 * Entity data structures
125 struct classtype_block
130 struct classtype_gate
136 struct classtype_spawn
141 struct classtype_water
146 struct classtype_car_path
151 struct classtype_instance
156 struct classtype_capsule
158 float height
, radius
;
161 struct classtype_route_node
166 struct classtype_route
172 struct classtype_bone
184 struct classtype_skeleton
193 struct classtype_skin
198 struct classtype_achievement_box
206 * Simple mesh interface for OpenGL
211 GLuint vao
, vbo
, ebo
;
216 static void mesh_upload( glmesh
*mesh
,
217 mdl_vert
*verts
, u32 vert_count
,
218 u32
*indices
, u32 indice_count
)
220 //assert( mesh->loaded == 0 );
222 glGenVertexArrays( 1, &mesh
->vao
);
223 glGenBuffers( 1, &mesh
->vbo
);
224 glGenBuffers( 1, &mesh
->ebo
);
225 glBindVertexArray( mesh
->vao
);
227 size_t stride
= sizeof(mdl_vert
);
229 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
230 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
232 glBindVertexArray( mesh
->vao
);
233 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
234 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
235 indices
, GL_STATIC_DRAW
);
238 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
239 glEnableVertexAttribArray( 0 );
242 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
243 stride
, (void *)offsetof(mdl_vert
, norm
) );
244 glEnableVertexAttribArray( 1 );
247 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
248 stride
, (void *)offsetof(mdl_vert
, uv
) );
249 glEnableVertexAttribArray( 2 );
252 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
253 stride
, (void *)offsetof(mdl_vert
, colour
) );
254 glEnableVertexAttribArray( 3 );
257 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
258 stride
, (void *)offsetof(mdl_vert
, weights
) );
259 glEnableVertexAttribArray( 4 );
262 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
263 stride
, (void *)offsetof(mdl_vert
, groups
) );
264 glEnableVertexAttribArray( 5 );
268 mesh
->indice_count
= indice_count
;
272 static void mesh_bind( glmesh
*mesh
)
274 glBindVertexArray( mesh
->vao
);
277 static void mesh_drawn( u32 start
, u32 count
)
279 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
280 (void *)(start
*sizeof(u32
)) );
283 static void mesh_draw( glmesh
*mesh
)
285 mesh_drawn( 0, mesh
->indice_count
);
288 static void mesh_free( glmesh
*mesh
)
292 glDeleteVertexArrays( 1, &mesh
->vao
);
293 glDeleteBuffers( 1, &mesh
->ebo
);
294 glDeleteBuffers( 1, &mesh
->vbo
);
300 * Model implementation
303 static mdl_header
*mdl_load( const char *path
)
306 mdl_header
*header
= vg_asset_read_s( path
, &size
);
309 * Check file is valid
313 vg_error( "Could not open '%s'\n", path
);
317 if( size
< sizeof(mdl_header
) )
320 vg_error( "Invalid file '%s' (too small for header)\n", path
);
324 if( header
->file_length
!= size
)
326 vg_error( "Invalid file '%s'"
327 "(wrong .file_length, %ub != real file size %ub)\n",
328 path
, header
->file_length
, size
);
334 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
335 * and that they do not overlap.
341 u32 count
, max_count
, size
, offset
;
346 header
->vertex_count
, MDL_VERT_MAX
,
347 sizeof(mdl_vert
), header
->vertex_offset
351 header
->indice_count
, MDL_INDICE_MAX
,
352 sizeof(u32
), header
->indice_offset
356 header
->submesh_count
, MDL_SUBMESH_MAX
,
357 sizeof(mdl_submesh
), header
->submesh_offset
361 header
->material_count
, MDL_MATERIAL_MAX
,
362 sizeof(mdl_material
), header
->material_offset
366 header
->node_count
, MDL_NODE_MAX
,
367 sizeof(mdl_node
), header
->node_count
371 for( int i
=0; i
<vg_list_size(regions
); i
++ )
373 struct memregion
*ri
= ®ions
[i
];
378 if( ri
->count
> ri
->max_count
)
381 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
382 path
, ri
->desc
, ri
->count
, ri
->max_count
);
386 if( ri
->offset
>= header
->file_length
)
389 vg_error( "'%s': '%s' buffer offset is out of range\n",
394 if( ri
->offset
+ ri
->size
*ri
->count
> header
->file_length
)
397 vg_error( "'%s': '%s' buffer size is out of range\n",
402 for( int j
=0; j
<vg_list_size(regions
); j
++ )
404 struct memregion
*rj
= ®ions
[j
];
408 if( ri
->offset
>= rj
->offset
&&
409 (ri
->offset
+ri
->size
*ri
->count
< rj
->offset
+rj
->size
*rj
->count
))
412 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
413 path
, ri
->desc
, rj
->desc
);
420 * Pointer validation TODO(workshop)
424 * strings TODO(workshop)
430 static void *mdl_baseptr( mdl_header
*mdl
, u32 offset
)
432 return (void *)mdl
+ offset
;
435 static const char *mdl_pstr( mdl_header
*mdl
, u32 pstr
)
437 return (const char *)(mdl_baseptr( mdl
, mdl
->strings_offset
)) + pstr
;
440 static mdl_node
*mdl_node_from_id( mdl_header
*mdl
, u32 id
)
442 return ((mdl_node
*)mdl_baseptr( mdl
, mdl
->node_offset
)) + id
;
445 static mdl_node
*mdl_node_from_name( mdl_header
*mdl
, const char *name
)
447 for( int i
=0; i
<mdl
->node_count
; i
++ )
449 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
451 if( !strcmp( name
, mdl_pstr( mdl
, pnode
->pstr_name
)) )
458 static mdl_submesh
*mdl_submesh_from_id( mdl_header
*mdl
, u32 id
)
460 if( id
>= mdl
->submesh_count
)
463 return ((mdl_submesh
*)mdl_baseptr( mdl
, mdl
->submesh_offset
)) + id
;
466 static mdl_submesh
*mdl_node_submesh( mdl_header
*mdl
, mdl_node
*node
, u32 i
)
468 if( i
>= node
->submesh_count
)
471 return mdl_submesh_from_id( mdl
, node
->submesh_start
+i
);
474 static u32
*mdl_submesh_indices( mdl_header
*mdl
, mdl_submesh
*sm
)
476 return ((u32
*)mdl_baseptr( mdl
, mdl
->indice_offset
)) + sm
->indice_start
;
479 static mdl_vert
*mdl_submesh_vertices( mdl_header
*mdl
, mdl_submesh
*sm
)
481 return ((mdl_vert
*)mdl_baseptr(mdl
,mdl
->vertex_offset
)) + sm
->vertex_start
;
484 static mdl_material
*mdl_material_from_id( mdl_header
*mdl
, u32 id
)
486 return ((mdl_material
*)mdl_baseptr(mdl
,mdl
->material_offset
)) + id
;
489 static mdl_animation
*mdl_animation_from_id( mdl_header
*mdl
, u32 id
)
491 return ((mdl_animation
*)mdl_baseptr(mdl
,mdl
->anim_offset
)) + id
;
494 static void mdl_node_transform( mdl_node
*pnode
, m4x3f transform
)
496 q_m3x3( pnode
->q
, transform
);
497 v3_muls( transform
[0], pnode
->s
[0], transform
[0] );
498 v3_muls( transform
[1], pnode
->s
[1], transform
[1] );
499 v3_muls( transform
[2], pnode
->s
[2], transform
[2] );
500 v3_copy( pnode
->co
, transform
[3] );
503 static void mdl_unpack_submesh( mdl_header
*mdl
, glmesh
*mesh
, mdl_submesh
*sm
)
505 mesh_upload( mesh
, mdl_submesh_vertices( mdl
, sm
), sm
->vertex_count
,
506 mdl_submesh_indices( mdl
, sm
), sm
->indice_count
);
509 static void mdl_unpack_glmesh( mdl_header
*mdl
, glmesh
*mesh
)
511 u32 offset
= mdl_submesh_from_id( mdl
, 0 )->vertex_count
;
513 for( int i
=1; i
< mdl
->submesh_count
; i
++ )
515 mdl_submesh
*sm
= mdl_submesh_from_id( mdl
, i
);
516 u32
*indices
= mdl_submesh_indices( mdl
, sm
);
518 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
519 indices
[j
] += offset
;
521 offset
+= sm
->vertex_count
;
524 mdl_vert
*vertex_base
= mdl_baseptr( mdl
, mdl
->vertex_offset
);
525 u32
*indice_base
= mdl_baseptr( mdl
, mdl
->indice_offset
);
527 mesh_upload( mesh
, vertex_base
, mdl
->vertex_count
,
528 indice_base
, mdl
->indice_count
);
531 static void mdl_draw_submesh( mdl_submesh
*sm
)
533 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
536 static void *mdl_get_entdata( mdl_header
*mdl
, mdl_node
*pnode
)
538 return mdl_baseptr( mdl
, mdl
->entdata_offset
) + pnode
->offset
;
541 static mdl_keyframe
*mdl_get_animdata( mdl_header
*mdl
, mdl_animation
*anim
)
543 return mdl_baseptr( mdl
, mdl
->animdata_offset
) + anim
->offset
;
546 static void mdl_link_materials( mdl_header
*root
, mdl_header
*child
)
548 u32 lookup
[MDL_MATERIAL_MAX
];
550 for( int i
=0; i
<child
->material_count
; i
++ )
552 mdl_material
*mi
= mdl_material_from_id( child
, i
);
553 const char *si
= mdl_pstr( child
, mi
->pstr_name
);
557 for( int j
=0; j
<root
->material_count
; j
++ )
559 mdl_material
*mj
= mdl_material_from_id( root
, j
);
560 const char *sj
= mdl_pstr( root
, mj
->pstr_name
);
562 if( !strcmp( si
, sj
) )
569 if( lookup
[i
] == 0 && i
!= 0 )
571 vg_warn( "Could not link material '%s' (not present in root model)\n",
576 for( int i
=0; i
<child
->submesh_count
; i
++ )
578 mdl_submesh
*sm
= mdl_submesh_from_id( child
, i
);
579 sm
->material_id
= lookup
[sm
->material_id
];