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
;
14 #define MDL_SIZE_MAX 0x1000000
15 #define MDL_VERT_MAX 1000000
16 #define MDL_INDICE_MAX 1000000
17 #define MDL_MATERIAL_MAX 32
18 #define MDL_NODE_MAX 4000
19 #define MDL_SUBMESH_MAX 8000
20 #define MDL_STRING_LENGTH_MAX 64
56 union{ u32 submesh_start
, sub_uid
; };
68 u32 identifier
, version
, file_length
;
70 u32 vertex_count
, vertex_offset
,
71 indice_count
, indice_offset
,
72 submesh_count
, submesh_offset
,
73 material_count
, material_offset
,
74 node_count
, node_offset
,
75 strings_offset
, entdata_offset
,
76 anim_count
, anim_offset
;
80 * Entity data structures
83 struct classtype_block
94 struct classtype_spawn
99 struct classtype_water
104 struct classtype_car_path
109 struct classtype_instance
114 struct classtype_capsule
116 float height
, radius
;
119 struct classtype_route_node
124 struct classtype_route
131 struct classtype_bone
136 struct classtype_skeleton
142 struct classtype_skin
150 * Simple mesh interface for OpenGL
155 GLuint vao
, vbo
, ebo
;
159 static void mesh_upload( glmesh
*mesh
,
160 mdl_vert
*verts
, u32 vert_count
,
161 u32
*indices
, u32 indice_count
)
163 glGenVertexArrays( 1, &mesh
->vao
);
164 glGenBuffers( 1, &mesh
->vbo
);
165 glGenBuffers( 1, &mesh
->ebo
);
166 glBindVertexArray( mesh
->vao
);
168 size_t stride
= sizeof(mdl_vert
);
170 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
171 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
173 glBindVertexArray( mesh
->vao
);
174 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
175 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
176 indices
, GL_STATIC_DRAW
);
179 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
180 glEnableVertexAttribArray( 0 );
183 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
184 stride
, (void *)offsetof(mdl_vert
, norm
) );
185 glEnableVertexAttribArray( 1 );
188 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
189 stride
, (void *)offsetof(mdl_vert
, uv
) );
190 glEnableVertexAttribArray( 2 );
193 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
194 stride
, (void *)offsetof(mdl_vert
, colour
) );
195 glEnableVertexAttribArray( 3 );
198 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
199 stride
, (void *)offsetof(mdl_vert
, weights
) );
200 glEnableVertexAttribArray( 4 );
203 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
204 stride
, (void *)offsetof(mdl_vert
, groups
) );
205 glEnableVertexAttribArray( 5 );
209 mesh
->indice_count
= indice_count
;
212 static void mesh_bind( glmesh
*mesh
)
214 glBindVertexArray( mesh
->vao
);
217 static void mesh_drawn( u32 start
, u32 count
)
219 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
220 (void *)(start
*sizeof(u32
)) );
223 static void mesh_draw( glmesh
*mesh
)
225 mesh_drawn( 0, mesh
->indice_count
);
228 static void mesh_free( glmesh
*mesh
)
230 glDeleteVertexArrays( 1, &mesh
->vao
);
231 glDeleteBuffers( 1, &mesh
->ebo
);
232 glDeleteBuffers( 1, &mesh
->vbo
);
237 * Model implementation
240 static mdl_header
*mdl_load( const char *path
)
243 mdl_header
*header
= vg_asset_read_s( path
, &size
);
246 * Check file is valid
250 vg_error( "Could not open '%s'\n", path
);
254 if( size
< sizeof(mdl_header
) )
257 vg_error( "Invalid file '%s' (too small for header)\n", path
);
261 if( header
->file_length
!= size
)
263 vg_error( "Invalid file '%s'"
264 "(wrong .file_length, %ub != real file size %ub)\n",
265 path
, header
->file_length
, size
);
271 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
272 * and that they do not overlap.
278 u32 count
, max_count
, size
, offset
;
283 header
->vertex_count
, MDL_VERT_MAX
,
284 sizeof(mdl_vert
), header
->vertex_offset
288 header
->indice_count
, MDL_INDICE_MAX
,
289 sizeof(u32
), header
->indice_offset
293 header
->submesh_count
, MDL_SUBMESH_MAX
,
294 sizeof(mdl_submesh
), header
->submesh_offset
298 header
->material_count
, MDL_MATERIAL_MAX
,
299 sizeof(mdl_material
), header
->material_offset
303 header
->node_count
, MDL_NODE_MAX
,
304 sizeof(mdl_node
), header
->node_count
308 for( int i
=0; i
<vg_list_size(regions
); i
++ )
310 struct memregion
*ri
= ®ions
[i
];
315 if( ri
->count
> ri
->max_count
)
318 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
319 path
, ri
->desc
, ri
->count
, ri
->max_count
);
323 if( ri
->offset
>= header
->file_length
)
326 vg_error( "'%s': '%s' buffer offset is out of range\n",
331 if( ri
->offset
+ ri
->size
*ri
->count
> header
->file_length
)
334 vg_error( "'%s': '%s' buffer size is out of range\n",
339 for( int j
=0; j
<vg_list_size(regions
); j
++ )
341 struct memregion
*rj
= ®ions
[j
];
345 if( ri
->offset
>= rj
->offset
&&
346 (ri
->offset
+ri
->size
*ri
->count
< rj
->offset
+rj
->size
*rj
->count
))
349 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
350 path
, ri
->desc
, rj
->desc
);
357 * Pointer validation TODO(workshop)
361 * strings TODO(workshop)
367 static void *mdl_baseptr( mdl_header
*mdl
, u32 offset
)
369 return (void *)mdl
+ offset
;
372 static const char *mdl_pstr( mdl_header
*mdl
, u32 pstr
)
374 return (const char *)(mdl_baseptr( mdl
, mdl
->strings_offset
)) + pstr
;
377 static mdl_node
*mdl_node_from_id( mdl_header
*mdl
, u32 id
)
379 return ((mdl_node
*)mdl_baseptr( mdl
, mdl
->node_offset
)) + id
;
382 static mdl_node
*mdl_node_from_name( mdl_header
*mdl
, const char *name
)
384 for( int i
=0; i
<mdl
->node_count
; i
++ )
386 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
388 if( !strcmp( name
, mdl_pstr( mdl
, pnode
->pstr_name
)) )
395 static mdl_submesh
*mdl_submesh_from_id( mdl_header
*mdl
, u32 id
)
397 if( id
>= mdl
->submesh_count
)
400 return ((mdl_submesh
*)mdl_baseptr( mdl
, mdl
->submesh_offset
)) + id
;
403 static mdl_submesh
*mdl_node_submesh( mdl_header
*mdl
, mdl_node
*node
, u32 i
)
405 if( i
>= node
->submesh_count
)
408 return mdl_submesh_from_id( mdl
, node
->submesh_start
+i
);
411 static u32
*mdl_submesh_indices( mdl_header
*mdl
, mdl_submesh
*sm
)
413 return ((u32
*)mdl_baseptr( mdl
, mdl
->indice_offset
)) + sm
->indice_start
;
416 static mdl_vert
*mdl_submesh_vertices( mdl_header
*mdl
, mdl_submesh
*sm
)
418 return ((mdl_vert
*)mdl_baseptr(mdl
,mdl
->vertex_offset
)) + sm
->vertex_start
;
421 static mdl_material
*mdl_material_from_id( mdl_header
*mdl
, u32 id
)
423 return ((mdl_material
*)mdl_baseptr(mdl
,mdl
->material_offset
)) + id
;
426 static void mdl_node_transform( mdl_node
*pnode
, m4x3f transform
)
428 q_m3x3( pnode
->q
, transform
);
429 v3_muls( transform
[0], pnode
->s
[0], transform
[0] );
430 v3_muls( transform
[1], pnode
->s
[1], transform
[1] );
431 v3_muls( transform
[2], pnode
->s
[2], transform
[2] );
432 v3_copy( pnode
->co
, transform
[3] );
435 static void mdl_unpack_submesh( mdl_header
*mdl
, glmesh
*mesh
, mdl_submesh
*sm
)
437 mesh_upload( mesh
, mdl_submesh_vertices( mdl
, sm
), sm
->vertex_count
,
438 mdl_submesh_indices( mdl
, sm
), sm
->indice_count
);
441 static void mdl_unpack_glmesh( mdl_header
*mdl
, glmesh
*mesh
)
443 u32 offset
= mdl_submesh_from_id( mdl
, 0 )->vertex_count
;
445 for( int i
=1; i
< mdl
->submesh_count
; i
++ )
447 mdl_submesh
*sm
= mdl_submesh_from_id( mdl
, i
);
448 u32
*indices
= mdl_submesh_indices( mdl
, sm
);
450 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
451 indices
[j
] += offset
;
453 offset
+= sm
->vertex_count
;
456 mdl_vert
*vertex_base
= mdl_baseptr( mdl
, mdl
->vertex_offset
);
457 u32
*indice_base
= mdl_baseptr( mdl
, mdl
->indice_offset
);
459 mesh_upload( mesh
, vertex_base
, mdl
->vertex_count
,
460 indice_base
, mdl
->indice_count
);
463 static void mdl_draw_submesh( mdl_submesh
*sm
)
465 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
468 static void *mdl_get_entdata( mdl_header
*mdl
, mdl_node
*pnode
)
470 return mdl_baseptr( mdl
, mdl
->entdata_offset
) + pnode
->offset
;
473 static void mdl_link_materials( mdl_header
*root
, mdl_header
*child
)
475 u32 lookup
[MDL_MATERIAL_MAX
];
477 for( int i
=0; i
<child
->material_count
; i
++ )
479 mdl_material
*mi
= mdl_material_from_id( child
, i
);
480 const char *si
= mdl_pstr( child
, mi
->pstr_name
);
484 for( int j
=0; j
<root
->material_count
; j
++ )
486 mdl_material
*mj
= mdl_material_from_id( root
, j
);
487 const char *sj
= mdl_pstr( root
, mj
->pstr_name
);
489 if( !strcmp( si
, sj
) )
496 if( lookup
[i
] == 0 && i
!= 0 )
498 vg_warn( "Could not link material '%s' (not present in root model)\n",
503 for( int i
=0; i
<child
->submesh_count
; i
++ )
505 mdl_submesh
*sm
= mdl_submesh_from_id( child
, i
);
506 sm
->material_id
= lookup
[sm
->material_id
];