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
28 k_classtype_block
= 2,
29 k_classtype_spawn
= 3,
30 k_classtype_water
= 4,
31 k_classtype_car_path
= 5,
32 k_classtype_instance
= 6,
33 k_classtype_capsule
= 7,
34 k_classtype_route_node
= 8,
35 k_classtype_route
= 9,
36 k_classtype_bone
= 10,
37 k_classtype_skeleton
= 11,
76 union{ u32 submesh_start
, sub_uid
; };
105 u32 identifier
, version
, file_length
;
107 u32 vertex_count
, vertex_offset
,
108 indice_count
, indice_offset
,
109 submesh_count
, submesh_offset
,
110 material_count
, material_offset
,
111 node_count
, node_offset
,
112 anim_count
, anim_offset
,
113 strings_offset
, entdata_offset
, animdata_offset
;
117 * Entity data structures
120 struct classtype_block
125 struct classtype_gate
131 struct classtype_spawn
136 struct classtype_water
141 struct classtype_car_path
146 struct classtype_instance
151 struct classtype_capsule
153 float height
, radius
;
156 struct classtype_route_node
161 struct classtype_route
167 struct classtype_bone
179 struct classtype_skeleton
188 struct classtype_skin
196 * Simple mesh interface for OpenGL
201 GLuint vao
, vbo
, ebo
;
206 static void mesh_upload( glmesh
*mesh
,
207 mdl_vert
*verts
, u32 vert_count
,
208 u32
*indices
, u32 indice_count
)
210 //assert( mesh->loaded == 0 );
212 glGenVertexArrays( 1, &mesh
->vao
);
213 glGenBuffers( 1, &mesh
->vbo
);
214 glGenBuffers( 1, &mesh
->ebo
);
215 glBindVertexArray( mesh
->vao
);
217 size_t stride
= sizeof(mdl_vert
);
219 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
220 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
222 glBindVertexArray( mesh
->vao
);
223 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
224 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
225 indices
, GL_STATIC_DRAW
);
228 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
229 glEnableVertexAttribArray( 0 );
232 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
233 stride
, (void *)offsetof(mdl_vert
, norm
) );
234 glEnableVertexAttribArray( 1 );
237 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
238 stride
, (void *)offsetof(mdl_vert
, uv
) );
239 glEnableVertexAttribArray( 2 );
242 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
243 stride
, (void *)offsetof(mdl_vert
, colour
) );
244 glEnableVertexAttribArray( 3 );
247 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
248 stride
, (void *)offsetof(mdl_vert
, weights
) );
249 glEnableVertexAttribArray( 4 );
252 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
253 stride
, (void *)offsetof(mdl_vert
, groups
) );
254 glEnableVertexAttribArray( 5 );
258 mesh
->indice_count
= indice_count
;
262 static void mesh_bind( glmesh
*mesh
)
264 glBindVertexArray( mesh
->vao
);
267 static void mesh_drawn( u32 start
, u32 count
)
269 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
270 (void *)(start
*sizeof(u32
)) );
273 static void mesh_draw( glmesh
*mesh
)
275 mesh_drawn( 0, mesh
->indice_count
);
278 static void mesh_free( glmesh
*mesh
)
282 glDeleteVertexArrays( 1, &mesh
->vao
);
283 glDeleteBuffers( 1, &mesh
->ebo
);
284 glDeleteBuffers( 1, &mesh
->vbo
);
290 * Model implementation
293 static mdl_header
*mdl_load( const char *path
)
296 mdl_header
*header
= vg_asset_read_s( path
, &size
);
299 * Check file is valid
303 vg_error( "Could not open '%s'\n", path
);
307 if( size
< sizeof(mdl_header
) )
310 vg_error( "Invalid file '%s' (too small for header)\n", path
);
314 if( header
->file_length
!= size
)
316 vg_error( "Invalid file '%s'"
317 "(wrong .file_length, %ub != real file size %ub)\n",
318 path
, header
->file_length
, size
);
324 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
325 * and that they do not overlap.
331 u32 count
, max_count
, size
, offset
;
336 header
->vertex_count
, MDL_VERT_MAX
,
337 sizeof(mdl_vert
), header
->vertex_offset
341 header
->indice_count
, MDL_INDICE_MAX
,
342 sizeof(u32
), header
->indice_offset
346 header
->submesh_count
, MDL_SUBMESH_MAX
,
347 sizeof(mdl_submesh
), header
->submesh_offset
351 header
->material_count
, MDL_MATERIAL_MAX
,
352 sizeof(mdl_material
), header
->material_offset
356 header
->node_count
, MDL_NODE_MAX
,
357 sizeof(mdl_node
), header
->node_count
361 for( int i
=0; i
<vg_list_size(regions
); i
++ )
363 struct memregion
*ri
= ®ions
[i
];
368 if( ri
->count
> ri
->max_count
)
371 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
372 path
, ri
->desc
, ri
->count
, ri
->max_count
);
376 if( ri
->offset
>= header
->file_length
)
379 vg_error( "'%s': '%s' buffer offset is out of range\n",
384 if( ri
->offset
+ ri
->size
*ri
->count
> header
->file_length
)
387 vg_error( "'%s': '%s' buffer size is out of range\n",
392 for( int j
=0; j
<vg_list_size(regions
); j
++ )
394 struct memregion
*rj
= ®ions
[j
];
398 if( ri
->offset
>= rj
->offset
&&
399 (ri
->offset
+ri
->size
*ri
->count
< rj
->offset
+rj
->size
*rj
->count
))
402 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
403 path
, ri
->desc
, rj
->desc
);
410 * Pointer validation TODO(workshop)
414 * strings TODO(workshop)
420 static void *mdl_baseptr( mdl_header
*mdl
, u32 offset
)
422 return (void *)mdl
+ offset
;
425 static const char *mdl_pstr( mdl_header
*mdl
, u32 pstr
)
427 return (const char *)(mdl_baseptr( mdl
, mdl
->strings_offset
)) + pstr
;
430 static mdl_node
*mdl_node_from_id( mdl_header
*mdl
, u32 id
)
432 return ((mdl_node
*)mdl_baseptr( mdl
, mdl
->node_offset
)) + id
;
435 static mdl_node
*mdl_node_from_name( mdl_header
*mdl
, const char *name
)
437 for( int i
=0; i
<mdl
->node_count
; i
++ )
439 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
441 if( !strcmp( name
, mdl_pstr( mdl
, pnode
->pstr_name
)) )
448 static mdl_submesh
*mdl_submesh_from_id( mdl_header
*mdl
, u32 id
)
450 if( id
>= mdl
->submesh_count
)
453 return ((mdl_submesh
*)mdl_baseptr( mdl
, mdl
->submesh_offset
)) + id
;
456 static mdl_submesh
*mdl_node_submesh( mdl_header
*mdl
, mdl_node
*node
, u32 i
)
458 if( i
>= node
->submesh_count
)
461 return mdl_submesh_from_id( mdl
, node
->submesh_start
+i
);
464 static u32
*mdl_submesh_indices( mdl_header
*mdl
, mdl_submesh
*sm
)
466 return ((u32
*)mdl_baseptr( mdl
, mdl
->indice_offset
)) + sm
->indice_start
;
469 static mdl_vert
*mdl_submesh_vertices( mdl_header
*mdl
, mdl_submesh
*sm
)
471 return ((mdl_vert
*)mdl_baseptr(mdl
,mdl
->vertex_offset
)) + sm
->vertex_start
;
474 static mdl_material
*mdl_material_from_id( mdl_header
*mdl
, u32 id
)
476 return ((mdl_material
*)mdl_baseptr(mdl
,mdl
->material_offset
)) + id
;
479 static mdl_animation
*mdl_animation_from_id( mdl_header
*mdl
, u32 id
)
481 return ((mdl_animation
*)mdl_baseptr(mdl
,mdl
->anim_offset
)) + id
;
484 static void mdl_node_transform( mdl_node
*pnode
, m4x3f transform
)
486 q_m3x3( pnode
->q
, transform
);
487 v3_muls( transform
[0], pnode
->s
[0], transform
[0] );
488 v3_muls( transform
[1], pnode
->s
[1], transform
[1] );
489 v3_muls( transform
[2], pnode
->s
[2], transform
[2] );
490 v3_copy( pnode
->co
, transform
[3] );
493 static void mdl_unpack_submesh( mdl_header
*mdl
, glmesh
*mesh
, mdl_submesh
*sm
)
495 mesh_upload( mesh
, mdl_submesh_vertices( mdl
, sm
), sm
->vertex_count
,
496 mdl_submesh_indices( mdl
, sm
), sm
->indice_count
);
499 static void mdl_unpack_glmesh( mdl_header
*mdl
, glmesh
*mesh
)
501 u32 offset
= mdl_submesh_from_id( mdl
, 0 )->vertex_count
;
503 for( int i
=1; i
< mdl
->submesh_count
; i
++ )
505 mdl_submesh
*sm
= mdl_submesh_from_id( mdl
, i
);
506 u32
*indices
= mdl_submesh_indices( mdl
, sm
);
508 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
509 indices
[j
] += offset
;
511 offset
+= sm
->vertex_count
;
514 mdl_vert
*vertex_base
= mdl_baseptr( mdl
, mdl
->vertex_offset
);
515 u32
*indice_base
= mdl_baseptr( mdl
, mdl
->indice_offset
);
517 mesh_upload( mesh
, vertex_base
, mdl
->vertex_count
,
518 indice_base
, mdl
->indice_count
);
521 static void mdl_draw_submesh( mdl_submesh
*sm
)
523 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
526 static void *mdl_get_entdata( mdl_header
*mdl
, mdl_node
*pnode
)
528 return mdl_baseptr( mdl
, mdl
->entdata_offset
) + pnode
->offset
;
531 static mdl_keyframe
*mdl_get_animdata( mdl_header
*mdl
, mdl_animation
*anim
)
533 return mdl_baseptr( mdl
, mdl
->animdata_offset
) + anim
->offset
;
536 static void mdl_link_materials( mdl_header
*root
, mdl_header
*child
)
538 u32 lookup
[MDL_MATERIAL_MAX
];
540 for( int i
=0; i
<child
->material_count
; i
++ )
542 mdl_material
*mi
= mdl_material_from_id( child
, i
);
543 const char *si
= mdl_pstr( child
, mi
->pstr_name
);
547 for( int j
=0; j
<root
->material_count
; j
++ )
549 mdl_material
*mj
= mdl_material_from_id( root
, j
);
550 const char *sj
= mdl_pstr( root
, mj
->pstr_name
);
552 if( !strcmp( si
, sj
) )
559 if( lookup
[i
] == 0 && i
!= 0 )
561 vg_warn( "Could not link material '%s' (not present in root model)\n",
566 for( int i
=0; i
<child
->submesh_count
; i
++ )
568 mdl_submesh
*sm
= mdl_submesh_from_id( child
, i
);
569 sm
->material_id
= lookup
[sm
->material_id
];