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,
80 union{ u32 submesh_start
, sub_uid
; };
109 u32 identifier
, version
, file_length
;
111 u32 vertex_count
, vertex_offset
,
112 indice_count
, indice_offset
,
113 submesh_count
, submesh_offset
,
114 material_count
, material_offset
,
115 node_count
, node_offset
,
116 anim_count
, anim_offset
,
117 strings_offset
, entdata_offset
, animdata_offset
;
121 * Entity data structures
124 struct classtype_block
129 struct classtype_gate
135 struct classtype_spawn
140 struct classtype_water
145 struct classtype_car_path
150 struct classtype_instance
155 struct classtype_capsule
157 float height
, radius
;
160 struct classtype_route_node
165 struct classtype_route
171 struct classtype_bone
183 struct classtype_skeleton
192 struct classtype_skin
200 * Simple mesh interface for OpenGL
205 GLuint vao
, vbo
, ebo
;
210 static void mesh_upload( glmesh
*mesh
,
211 mdl_vert
*verts
, u32 vert_count
,
212 u32
*indices
, u32 indice_count
)
214 //assert( mesh->loaded == 0 );
216 glGenVertexArrays( 1, &mesh
->vao
);
217 glGenBuffers( 1, &mesh
->vbo
);
218 glGenBuffers( 1, &mesh
->ebo
);
219 glBindVertexArray( mesh
->vao
);
221 size_t stride
= sizeof(mdl_vert
);
223 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
224 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
226 glBindVertexArray( mesh
->vao
);
227 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
228 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
229 indices
, GL_STATIC_DRAW
);
232 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
233 glEnableVertexAttribArray( 0 );
236 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
237 stride
, (void *)offsetof(mdl_vert
, norm
) );
238 glEnableVertexAttribArray( 1 );
241 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
242 stride
, (void *)offsetof(mdl_vert
, uv
) );
243 glEnableVertexAttribArray( 2 );
246 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
247 stride
, (void *)offsetof(mdl_vert
, colour
) );
248 glEnableVertexAttribArray( 3 );
251 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
252 stride
, (void *)offsetof(mdl_vert
, weights
) );
253 glEnableVertexAttribArray( 4 );
256 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
257 stride
, (void *)offsetof(mdl_vert
, groups
) );
258 glEnableVertexAttribArray( 5 );
262 mesh
->indice_count
= indice_count
;
266 static void mesh_bind( glmesh
*mesh
)
268 glBindVertexArray( mesh
->vao
);
271 static void mesh_drawn( u32 start
, u32 count
)
273 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
274 (void *)(start
*sizeof(u32
)) );
277 static void mesh_draw( glmesh
*mesh
)
279 mesh_drawn( 0, mesh
->indice_count
);
282 static void mesh_free( glmesh
*mesh
)
286 glDeleteVertexArrays( 1, &mesh
->vao
);
287 glDeleteBuffers( 1, &mesh
->ebo
);
288 glDeleteBuffers( 1, &mesh
->vbo
);
294 * Model implementation
297 static mdl_header
*mdl_load( const char *path
)
300 mdl_header
*header
= vg_asset_read_s( path
, &size
);
303 * Check file is valid
307 vg_error( "Could not open '%s'\n", path
);
311 if( size
< sizeof(mdl_header
) )
314 vg_error( "Invalid file '%s' (too small for header)\n", path
);
318 if( header
->file_length
!= size
)
320 vg_error( "Invalid file '%s'"
321 "(wrong .file_length, %ub != real file size %ub)\n",
322 path
, header
->file_length
, size
);
328 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
329 * and that they do not overlap.
335 u32 count
, max_count
, size
, offset
;
340 header
->vertex_count
, MDL_VERT_MAX
,
341 sizeof(mdl_vert
), header
->vertex_offset
345 header
->indice_count
, MDL_INDICE_MAX
,
346 sizeof(u32
), header
->indice_offset
350 header
->submesh_count
, MDL_SUBMESH_MAX
,
351 sizeof(mdl_submesh
), header
->submesh_offset
355 header
->material_count
, MDL_MATERIAL_MAX
,
356 sizeof(mdl_material
), header
->material_offset
360 header
->node_count
, MDL_NODE_MAX
,
361 sizeof(mdl_node
), header
->node_count
365 for( int i
=0; i
<vg_list_size(regions
); i
++ )
367 struct memregion
*ri
= ®ions
[i
];
372 if( ri
->count
> ri
->max_count
)
375 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
376 path
, ri
->desc
, ri
->count
, ri
->max_count
);
380 if( ri
->offset
>= header
->file_length
)
383 vg_error( "'%s': '%s' buffer offset is out of range\n",
388 if( ri
->offset
+ ri
->size
*ri
->count
> header
->file_length
)
391 vg_error( "'%s': '%s' buffer size is out of range\n",
396 for( int j
=0; j
<vg_list_size(regions
); j
++ )
398 struct memregion
*rj
= ®ions
[j
];
402 if( ri
->offset
>= rj
->offset
&&
403 (ri
->offset
+ri
->size
*ri
->count
< rj
->offset
+rj
->size
*rj
->count
))
406 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
407 path
, ri
->desc
, rj
->desc
);
414 * Pointer validation TODO(workshop)
418 * strings TODO(workshop)
424 static void *mdl_baseptr( mdl_header
*mdl
, u32 offset
)
426 return (void *)mdl
+ offset
;
429 static const char *mdl_pstr( mdl_header
*mdl
, u32 pstr
)
431 return (const char *)(mdl_baseptr( mdl
, mdl
->strings_offset
)) + pstr
;
434 static mdl_node
*mdl_node_from_id( mdl_header
*mdl
, u32 id
)
436 return ((mdl_node
*)mdl_baseptr( mdl
, mdl
->node_offset
)) + id
;
439 static mdl_node
*mdl_node_from_name( mdl_header
*mdl
, const char *name
)
441 for( int i
=0; i
<mdl
->node_count
; i
++ )
443 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
445 if( !strcmp( name
, mdl_pstr( mdl
, pnode
->pstr_name
)) )
452 static mdl_submesh
*mdl_submesh_from_id( mdl_header
*mdl
, u32 id
)
454 if( id
>= mdl
->submesh_count
)
457 return ((mdl_submesh
*)mdl_baseptr( mdl
, mdl
->submesh_offset
)) + id
;
460 static mdl_submesh
*mdl_node_submesh( mdl_header
*mdl
, mdl_node
*node
, u32 i
)
462 if( i
>= node
->submesh_count
)
465 return mdl_submesh_from_id( mdl
, node
->submesh_start
+i
);
468 static u32
*mdl_submesh_indices( mdl_header
*mdl
, mdl_submesh
*sm
)
470 return ((u32
*)mdl_baseptr( mdl
, mdl
->indice_offset
)) + sm
->indice_start
;
473 static mdl_vert
*mdl_submesh_vertices( mdl_header
*mdl
, mdl_submesh
*sm
)
475 return ((mdl_vert
*)mdl_baseptr(mdl
,mdl
->vertex_offset
)) + sm
->vertex_start
;
478 static mdl_material
*mdl_material_from_id( mdl_header
*mdl
, u32 id
)
480 return ((mdl_material
*)mdl_baseptr(mdl
,mdl
->material_offset
)) + id
;
483 static mdl_animation
*mdl_animation_from_id( mdl_header
*mdl
, u32 id
)
485 return ((mdl_animation
*)mdl_baseptr(mdl
,mdl
->anim_offset
)) + id
;
488 static void mdl_node_transform( mdl_node
*pnode
, m4x3f transform
)
490 q_m3x3( pnode
->q
, transform
);
491 v3_muls( transform
[0], pnode
->s
[0], transform
[0] );
492 v3_muls( transform
[1], pnode
->s
[1], transform
[1] );
493 v3_muls( transform
[2], pnode
->s
[2], transform
[2] );
494 v3_copy( pnode
->co
, transform
[3] );
497 static void mdl_unpack_submesh( mdl_header
*mdl
, glmesh
*mesh
, mdl_submesh
*sm
)
499 mesh_upload( mesh
, mdl_submesh_vertices( mdl
, sm
), sm
->vertex_count
,
500 mdl_submesh_indices( mdl
, sm
), sm
->indice_count
);
503 static void mdl_unpack_glmesh( mdl_header
*mdl
, glmesh
*mesh
)
505 u32 offset
= mdl_submesh_from_id( mdl
, 0 )->vertex_count
;
507 for( int i
=1; i
< mdl
->submesh_count
; i
++ )
509 mdl_submesh
*sm
= mdl_submesh_from_id( mdl
, i
);
510 u32
*indices
= mdl_submesh_indices( mdl
, sm
);
512 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
513 indices
[j
] += offset
;
515 offset
+= sm
->vertex_count
;
518 mdl_vert
*vertex_base
= mdl_baseptr( mdl
, mdl
->vertex_offset
);
519 u32
*indice_base
= mdl_baseptr( mdl
, mdl
->indice_offset
);
521 mesh_upload( mesh
, vertex_base
, mdl
->vertex_count
,
522 indice_base
, mdl
->indice_count
);
525 static void mdl_draw_submesh( mdl_submesh
*sm
)
527 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
530 static void *mdl_get_entdata( mdl_header
*mdl
, mdl_node
*pnode
)
532 return mdl_baseptr( mdl
, mdl
->entdata_offset
) + pnode
->offset
;
535 static mdl_keyframe
*mdl_get_animdata( mdl_header
*mdl
, mdl_animation
*anim
)
537 return mdl_baseptr( mdl
, mdl
->animdata_offset
) + anim
->offset
;
540 static void mdl_link_materials( mdl_header
*root
, mdl_header
*child
)
542 u32 lookup
[MDL_MATERIAL_MAX
];
544 for( int i
=0; i
<child
->material_count
; i
++ )
546 mdl_material
*mi
= mdl_material_from_id( child
, i
);
547 const char *si
= mdl_pstr( child
, mi
->pstr_name
);
551 for( int j
=0; j
<root
->material_count
; j
++ )
553 mdl_material
*mj
= mdl_material_from_id( root
, j
);
554 const char *sj
= mdl_pstr( root
, mj
->pstr_name
);
556 if( !strcmp( si
, sj
) )
563 if( lookup
[i
] == 0 && i
!= 0 )
565 vg_warn( "Could not link material '%s' (not present in root model)\n",
570 for( int i
=0; i
<child
->submesh_count
; i
++ )
572 mdl_submesh
*sm
= mdl_submesh_from_id( child
, i
);
573 sm
->material_id
= lookup
[sm
->material_id
];