6 typedef struct glmesh glmesh
;
8 typedef struct mdl_vert mdl_vert
;
9 typedef struct mdl_submesh mdl_submesh
;
10 typedef struct mdl_material mdl_material
;
11 typedef struct mdl_node mdl_node
;
12 typedef struct mdl_header mdl_header
;
13 typedef struct mdl_animation mdl_animation
;
14 typedef struct mdl_keyframe mdl_keyframe
;
16 #define MDL_SIZE_MAX 0x1000000
17 #define MDL_VERT_MAX 1000000
18 #define MDL_INDICE_MAX 1000000
19 #define MDL_MATERIAL_MAX 32
20 #define MDL_NODE_MAX 4000
21 #define MDL_SUBMESH_MAX 8000
22 #define MDL_STRING_LENGTH_MAX 64
58 union{ u32 submesh_start
, sub_uid
; };
87 u32 identifier
, version
, file_length
;
89 u32 vertex_count
, vertex_offset
,
90 indice_count
, indice_offset
,
91 submesh_count
, submesh_offset
,
92 material_count
, material_offset
,
93 node_count
, node_offset
,
94 anim_count
, anim_offset
,
95 strings_offset
, entdata_offset
, animdata_offset
;
99 * Entity data structures
102 struct classtype_block
107 struct classtype_gate
113 struct classtype_spawn
118 struct classtype_water
123 struct classtype_car_path
128 struct classtype_instance
133 struct classtype_capsule
135 float height
, radius
;
138 struct classtype_route_node
143 struct classtype_route
150 struct classtype_bone
162 struct classtype_skeleton
171 struct classtype_skin
179 * Simple mesh interface for OpenGL
184 GLuint vao
, vbo
, ebo
;
188 static void mesh_upload( glmesh
*mesh
,
189 mdl_vert
*verts
, u32 vert_count
,
190 u32
*indices
, u32 indice_count
)
192 glGenVertexArrays( 1, &mesh
->vao
);
193 glGenBuffers( 1, &mesh
->vbo
);
194 glGenBuffers( 1, &mesh
->ebo
);
195 glBindVertexArray( mesh
->vao
);
197 size_t stride
= sizeof(mdl_vert
);
199 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
200 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
202 glBindVertexArray( mesh
->vao
);
203 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
204 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
205 indices
, GL_STATIC_DRAW
);
208 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
209 glEnableVertexAttribArray( 0 );
212 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
213 stride
, (void *)offsetof(mdl_vert
, norm
) );
214 glEnableVertexAttribArray( 1 );
217 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
218 stride
, (void *)offsetof(mdl_vert
, uv
) );
219 glEnableVertexAttribArray( 2 );
222 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
223 stride
, (void *)offsetof(mdl_vert
, colour
) );
224 glEnableVertexAttribArray( 3 );
227 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
228 stride
, (void *)offsetof(mdl_vert
, weights
) );
229 glEnableVertexAttribArray( 4 );
232 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
233 stride
, (void *)offsetof(mdl_vert
, groups
) );
234 glEnableVertexAttribArray( 5 );
238 mesh
->indice_count
= indice_count
;
241 static void mesh_bind( glmesh
*mesh
)
243 glBindVertexArray( mesh
->vao
);
246 static void mesh_drawn( u32 start
, u32 count
)
248 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
249 (void *)(start
*sizeof(u32
)) );
252 static void mesh_draw( glmesh
*mesh
)
254 mesh_drawn( 0, mesh
->indice_count
);
257 static void mesh_free( glmesh
*mesh
)
259 glDeleteVertexArrays( 1, &mesh
->vao
);
260 glDeleteBuffers( 1, &mesh
->ebo
);
261 glDeleteBuffers( 1, &mesh
->vbo
);
266 * Model implementation
269 static mdl_header
*mdl_load( const char *path
)
272 mdl_header
*header
= vg_asset_read_s( path
, &size
);
275 * Check file is valid
279 vg_error( "Could not open '%s'\n", path
);
283 if( size
< sizeof(mdl_header
) )
286 vg_error( "Invalid file '%s' (too small for header)\n", path
);
290 if( header
->file_length
!= size
)
292 vg_error( "Invalid file '%s'"
293 "(wrong .file_length, %ub != real file size %ub)\n",
294 path
, header
->file_length
, size
);
300 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
301 * and that they do not overlap.
307 u32 count
, max_count
, size
, offset
;
312 header
->vertex_count
, MDL_VERT_MAX
,
313 sizeof(mdl_vert
), header
->vertex_offset
317 header
->indice_count
, MDL_INDICE_MAX
,
318 sizeof(u32
), header
->indice_offset
322 header
->submesh_count
, MDL_SUBMESH_MAX
,
323 sizeof(mdl_submesh
), header
->submesh_offset
327 header
->material_count
, MDL_MATERIAL_MAX
,
328 sizeof(mdl_material
), header
->material_offset
332 header
->node_count
, MDL_NODE_MAX
,
333 sizeof(mdl_node
), header
->node_count
337 for( int i
=0; i
<vg_list_size(regions
); i
++ )
339 struct memregion
*ri
= ®ions
[i
];
344 if( ri
->count
> ri
->max_count
)
347 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
348 path
, ri
->desc
, ri
->count
, ri
->max_count
);
352 if( ri
->offset
>= header
->file_length
)
355 vg_error( "'%s': '%s' buffer offset is out of range\n",
360 if( ri
->offset
+ ri
->size
*ri
->count
> header
->file_length
)
363 vg_error( "'%s': '%s' buffer size is out of range\n",
368 for( int j
=0; j
<vg_list_size(regions
); j
++ )
370 struct memregion
*rj
= ®ions
[j
];
374 if( ri
->offset
>= rj
->offset
&&
375 (ri
->offset
+ri
->size
*ri
->count
< rj
->offset
+rj
->size
*rj
->count
))
378 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
379 path
, ri
->desc
, rj
->desc
);
386 * Pointer validation TODO(workshop)
390 * strings TODO(workshop)
396 static void *mdl_baseptr( mdl_header
*mdl
, u32 offset
)
398 return (void *)mdl
+ offset
;
401 static const char *mdl_pstr( mdl_header
*mdl
, u32 pstr
)
403 return (const char *)(mdl_baseptr( mdl
, mdl
->strings_offset
)) + pstr
;
406 static mdl_node
*mdl_node_from_id( mdl_header
*mdl
, u32 id
)
408 return ((mdl_node
*)mdl_baseptr( mdl
, mdl
->node_offset
)) + id
;
411 static mdl_node
*mdl_node_from_name( mdl_header
*mdl
, const char *name
)
413 for( int i
=0; i
<mdl
->node_count
; i
++ )
415 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
417 if( !strcmp( name
, mdl_pstr( mdl
, pnode
->pstr_name
)) )
424 static mdl_submesh
*mdl_submesh_from_id( mdl_header
*mdl
, u32 id
)
426 if( id
>= mdl
->submesh_count
)
429 return ((mdl_submesh
*)mdl_baseptr( mdl
, mdl
->submesh_offset
)) + id
;
432 static mdl_submesh
*mdl_node_submesh( mdl_header
*mdl
, mdl_node
*node
, u32 i
)
434 if( i
>= node
->submesh_count
)
437 return mdl_submesh_from_id( mdl
, node
->submesh_start
+i
);
440 static u32
*mdl_submesh_indices( mdl_header
*mdl
, mdl_submesh
*sm
)
442 return ((u32
*)mdl_baseptr( mdl
, mdl
->indice_offset
)) + sm
->indice_start
;
445 static mdl_vert
*mdl_submesh_vertices( mdl_header
*mdl
, mdl_submesh
*sm
)
447 return ((mdl_vert
*)mdl_baseptr(mdl
,mdl
->vertex_offset
)) + sm
->vertex_start
;
450 static mdl_material
*mdl_material_from_id( mdl_header
*mdl
, u32 id
)
452 return ((mdl_material
*)mdl_baseptr(mdl
,mdl
->material_offset
)) + id
;
455 static mdl_animation
*mdl_animation_from_id( mdl_header
*mdl
, u32 id
)
457 return ((mdl_animation
*)mdl_baseptr(mdl
,mdl
->anim_offset
)) + id
;
460 static void mdl_node_transform( mdl_node
*pnode
, m4x3f transform
)
462 q_m3x3( pnode
->q
, transform
);
463 v3_muls( transform
[0], pnode
->s
[0], transform
[0] );
464 v3_muls( transform
[1], pnode
->s
[1], transform
[1] );
465 v3_muls( transform
[2], pnode
->s
[2], transform
[2] );
466 v3_copy( pnode
->co
, transform
[3] );
469 static void mdl_unpack_submesh( mdl_header
*mdl
, glmesh
*mesh
, mdl_submesh
*sm
)
471 mesh_upload( mesh
, mdl_submesh_vertices( mdl
, sm
), sm
->vertex_count
,
472 mdl_submesh_indices( mdl
, sm
), sm
->indice_count
);
475 static void mdl_unpack_glmesh( mdl_header
*mdl
, glmesh
*mesh
)
477 u32 offset
= mdl_submesh_from_id( mdl
, 0 )->vertex_count
;
479 for( int i
=1; i
< mdl
->submesh_count
; i
++ )
481 mdl_submesh
*sm
= mdl_submesh_from_id( mdl
, i
);
482 u32
*indices
= mdl_submesh_indices( mdl
, sm
);
484 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
485 indices
[j
] += offset
;
487 offset
+= sm
->vertex_count
;
490 mdl_vert
*vertex_base
= mdl_baseptr( mdl
, mdl
->vertex_offset
);
491 u32
*indice_base
= mdl_baseptr( mdl
, mdl
->indice_offset
);
493 mesh_upload( mesh
, vertex_base
, mdl
->vertex_count
,
494 indice_base
, mdl
->indice_count
);
497 static void mdl_draw_submesh( mdl_submesh
*sm
)
499 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
502 static void *mdl_get_entdata( mdl_header
*mdl
, mdl_node
*pnode
)
504 return mdl_baseptr( mdl
, mdl
->entdata_offset
) + pnode
->offset
;
507 static mdl_keyframe
*mdl_get_animdata( mdl_header
*mdl
, mdl_animation
*anim
)
509 return mdl_baseptr( mdl
, mdl
->animdata_offset
) + anim
->offset
;
512 static void mdl_link_materials( mdl_header
*root
, mdl_header
*child
)
514 u32 lookup
[MDL_MATERIAL_MAX
];
516 for( int i
=0; i
<child
->material_count
; i
++ )
518 mdl_material
*mi
= mdl_material_from_id( child
, i
);
519 const char *si
= mdl_pstr( child
, mi
->pstr_name
);
523 for( int j
=0; j
<root
->material_count
; j
++ )
525 mdl_material
*mj
= mdl_material_from_id( root
, j
);
526 const char *sj
= mdl_pstr( root
, mj
->pstr_name
);
528 if( !strcmp( si
, sj
) )
535 if( lookup
[i
] == 0 && i
!= 0 )
537 vg_warn( "Could not link material '%s' (not present in root model)\n",
542 for( int i
=0; i
<child
->submesh_count
; i
++ )
544 mdl_submesh
*sm
= mdl_submesh_from_id( child
, i
);
545 sm
->material_id
= lookup
[sm
->material_id
];