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
54 union{ u32 submesh_start
, sub_uid
; };
64 u32 identifier
, version
, file_length
;
66 u32 vertex_count
, vertex_offset
,
67 indice_count
, indice_offset
,
68 submesh_count
, submesh_offset
,
69 material_count
, material_offset
,
70 node_count
, node_offset
,
71 strings_offset
, entdata_offset
;
75 * Entity data structures
78 struct classtype_block
89 struct classtype_spawn
94 struct classtype_water
99 struct classtype_car_path
104 struct classtype_instance
109 struct classtype_capsule
111 float height
, radius
;
114 struct classtype_route_node
119 struct classtype_route
128 * Simple mesh interface for OpenGL
133 GLuint vao
, vbo
, ebo
;
137 static void mesh_upload( glmesh
*mesh
,
138 mdl_vert
*verts
, u32 vert_count
,
139 u32
*indices
, u32 indice_count
)
141 glGenVertexArrays( 1, &mesh
->vao
);
142 glGenBuffers( 1, &mesh
->vbo
);
143 glGenBuffers( 1, &mesh
->ebo
);
144 glBindVertexArray( mesh
->vao
);
146 size_t stride
= sizeof(mdl_vert
);
148 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
149 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
151 glBindVertexArray( mesh
->vao
);
152 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
153 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
154 indices
, GL_STATIC_DRAW
);
156 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
157 glEnableVertexAttribArray( 0 );
159 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
160 stride
, (void *)offsetof(mdl_vert
, norm
) );
161 glEnableVertexAttribArray( 1 );
163 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
164 stride
, (void *)offsetof(mdl_vert
, colour
) );
165 glEnableVertexAttribArray( 2 );
167 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
168 stride
, (void *)offsetof(mdl_vert
, uv
) );
169 glEnableVertexAttribArray( 3 );
172 mesh
->indice_count
= indice_count
;
175 static void mesh_bind( glmesh
*mesh
)
177 glBindVertexArray( mesh
->vao
);
180 static void mesh_drawn( u32 start
, u32 count
)
182 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
183 (void *)(start
*sizeof(u32
)) );
186 static void mesh_draw( glmesh
*mesh
)
188 mesh_drawn( 0, mesh
->indice_count
);
191 static void mesh_free( glmesh
*mesh
)
193 glDeleteVertexArrays( 1, &mesh
->vao
);
194 glDeleteBuffers( 1, &mesh
->ebo
);
195 glDeleteBuffers( 1, &mesh
->vbo
);
200 * Model implementation
203 static mdl_header
*mdl_load( const char *path
)
206 mdl_header
*header
= vg_asset_read_s( path
, &size
);
209 * Check file is valid
213 vg_error( "Could not open '%s'\n", path
);
217 if( size
< sizeof(mdl_header
) )
220 vg_error( "Invalid file '%s' (too small for header)\n", path
);
224 if( header
->file_length
!= size
)
226 vg_error( "Invalid file '%s'"
227 "(wrong .file_length, %ub != real file size %ub)\n",
228 path
, header
->file_length
, size
);
234 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
235 * and that they do not overlap.
241 u32 count
, max_count
, size
, offset
;
246 header
->vertex_count
, MDL_VERT_MAX
,
247 sizeof(mdl_vert
), header
->vertex_offset
251 header
->indice_count
, MDL_INDICE_MAX
,
252 sizeof(u32
), header
->indice_offset
256 header
->submesh_count
, MDL_SUBMESH_MAX
,
257 sizeof(mdl_submesh
), header
->submesh_offset
261 header
->material_count
, MDL_MATERIAL_MAX
,
262 sizeof(mdl_material
), header
->material_offset
266 header
->node_count
, MDL_NODE_MAX
,
267 sizeof(mdl_node
), header
->node_count
271 for( int i
=0; i
<vg_list_size(regions
); i
++ )
273 struct memregion
*ri
= ®ions
[i
];
278 if( ri
->count
> ri
->max_count
)
281 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
282 path
, ri
->desc
, ri
->count
, ri
->max_count
);
286 if( ri
->offset
>= header
->file_length
)
289 vg_error( "'%s': '%s' buffer offset is out of range\n",
294 if( ri
->offset
+ ri
->size
*ri
->count
> header
->file_length
)
297 vg_error( "'%s': '%s' buffer size is out of range\n",
302 for( int j
=0; j
<vg_list_size(regions
); j
++ )
304 struct memregion
*rj
= ®ions
[j
];
308 if( ri
->offset
>= rj
->offset
&&
309 (ri
->offset
+ri
->size
*ri
->count
< rj
->offset
+rj
->size
*rj
->count
))
312 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
313 path
, ri
->desc
, rj
->desc
);
320 * Pointer validation TODO(workshop)
324 * strings TODO(workshop)
330 static void *mdl_baseptr( mdl_header
*mdl
, u32 offset
)
332 return (void *)mdl
+ offset
;
335 static const char *mdl_pstr( mdl_header
*mdl
, u32 pstr
)
337 return (const char *)(mdl_baseptr( mdl
, mdl
->strings_offset
)) + pstr
;
340 static mdl_node
*mdl_node_from_id( mdl_header
*mdl
, u32 id
)
342 return ((mdl_node
*)mdl_baseptr( mdl
, mdl
->node_offset
)) + id
;
345 static mdl_node
*mdl_node_from_name( mdl_header
*mdl
, const char *name
)
347 for( int i
=0; i
<mdl
->node_count
; i
++ )
349 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
351 if( !strcmp( name
, mdl_pstr( mdl
, pnode
->pstr_name
)) )
358 static mdl_submesh
*mdl_submesh_from_id( mdl_header
*mdl
, u32 id
)
360 if( id
>= mdl
->submesh_count
)
363 return ((mdl_submesh
*)mdl_baseptr( mdl
, mdl
->submesh_offset
)) + id
;
366 static mdl_submesh
*mdl_node_submesh( mdl_header
*mdl
, mdl_node
*node
, u32 i
)
368 if( i
>= node
->submesh_count
)
371 return mdl_submesh_from_id( mdl
, node
->submesh_start
+i
);
374 static u32
*mdl_submesh_indices( mdl_header
*mdl
, mdl_submesh
*sm
)
376 return ((u32
*)mdl_baseptr( mdl
, mdl
->indice_offset
)) + sm
->indice_start
;
379 static mdl_vert
*mdl_submesh_vertices( mdl_header
*mdl
, mdl_submesh
*sm
)
381 return ((mdl_vert
*)mdl_baseptr(mdl
,mdl
->vertex_offset
)) + sm
->vertex_start
;
384 static mdl_material
*mdl_material_from_id( mdl_header
*mdl
, u32 id
)
386 return ((mdl_material
*)mdl_baseptr(mdl
,mdl
->material_offset
)) + id
;
389 static void mdl_node_transform( mdl_node
*pnode
, m4x3f transform
)
391 q_m3x3( pnode
->q
, transform
);
392 v3_muls( transform
[0], pnode
->s
[0], transform
[0] );
393 v3_muls( transform
[1], pnode
->s
[1], transform
[1] );
394 v3_muls( transform
[2], pnode
->s
[2], transform
[2] );
395 v3_copy( pnode
->co
, transform
[3] );
398 static void mdl_unpack_submesh( mdl_header
*mdl
, glmesh
*mesh
, mdl_submesh
*sm
)
400 mesh_upload( mesh
, mdl_submesh_vertices( mdl
, sm
), sm
->vertex_count
,
401 mdl_submesh_indices( mdl
, sm
), sm
->indice_count
);
404 static void mdl_unpack_glmesh( mdl_header
*mdl
, glmesh
*mesh
)
406 u32 offset
= mdl_submesh_from_id( mdl
, 0 )->vertex_count
;
408 for( int i
=1; i
< mdl
->submesh_count
; i
++ )
410 mdl_submesh
*sm
= mdl_submesh_from_id( mdl
, i
);
411 u32
*indices
= mdl_submesh_indices( mdl
, sm
);
413 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
414 indices
[j
] += offset
;
416 offset
+= sm
->vertex_count
;
419 mdl_vert
*vertex_base
= mdl_baseptr( mdl
, mdl
->vertex_offset
);
420 u32
*indice_base
= mdl_baseptr( mdl
, mdl
->indice_offset
);
422 mesh_upload( mesh
, vertex_base
, mdl
->vertex_count
,
423 indice_base
, mdl
->indice_count
);
426 static void mdl_draw_submesh( mdl_submesh
*sm
)
428 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
431 static void *mdl_get_entdata( mdl_header
*mdl
, mdl_node
*pnode
)
433 return mdl_baseptr( mdl
, mdl
->entdata_offset
) + pnode
->offset
;
436 static void mdl_link_materials( mdl_header
*root
, mdl_header
*child
)
438 u32 lookup
[MDL_MATERIAL_MAX
];
440 for( int i
=0; i
<child
->material_count
; i
++ )
442 mdl_material
*mi
= mdl_material_from_id( child
, i
);
443 const char *si
= mdl_pstr( child
, mi
->pstr_name
);
447 for( int j
=0; j
<root
->material_count
; j
++ )
449 mdl_material
*mj
= mdl_material_from_id( root
, j
);
450 const char *sj
= mdl_pstr( root
, mj
->pstr_name
);
452 if( !strcmp( si
, sj
) )
459 if( lookup
[i
] == 0 && i
!= 0 )
461 vg_warn( "Could not link material '%s' (not present in root model)\n",
466 for( int i
=0; i
<child
->submesh_count
; i
++ )
468 mdl_submesh
*sm
= mdl_submesh_from_id( child
, i
);
469 sm
->material_id
= lookup
[sm
->material_id
];