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
88 struct classtype_spawn
93 struct classtype_water
98 struct classtype_car_path
103 struct classtype_instance
111 * Simple mesh interface for OpenGL
116 GLuint vao
, vbo
, ebo
;
120 static void mesh_upload( glmesh
*mesh
,
121 mdl_vert
*verts
, u32 vert_count
,
122 u32
*indices
, u32 indice_count
)
124 glGenVertexArrays( 1, &mesh
->vao
);
125 glGenBuffers( 1, &mesh
->vbo
);
126 glGenBuffers( 1, &mesh
->ebo
);
127 glBindVertexArray( mesh
->vao
);
129 size_t stride
= sizeof(mdl_vert
);
131 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
132 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
134 glBindVertexArray( mesh
->vao
);
135 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
136 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
137 indices
, GL_STATIC_DRAW
);
139 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
140 glEnableVertexAttribArray( 0 );
142 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
143 stride
, (void *)offsetof(mdl_vert
, norm
) );
144 glEnableVertexAttribArray( 1 );
146 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
147 stride
, (void *)offsetof(mdl_vert
, colour
) );
148 glEnableVertexAttribArray( 2 );
150 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
151 stride
, (void *)offsetof(mdl_vert
, uv
) );
152 glEnableVertexAttribArray( 3 );
155 mesh
->indice_count
= indice_count
;
158 static void mesh_bind( glmesh
*mesh
)
160 glBindVertexArray( mesh
->vao
);
163 static void mesh_drawn( u32 start
, u32 count
)
165 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
166 (void *)(start
*sizeof(u32
)) );
169 static void mesh_draw( glmesh
*mesh
)
171 mesh_drawn( 0, mesh
->indice_count
);
174 static void mesh_free( glmesh
*mesh
)
176 glDeleteVertexArrays( 1, &mesh
->vao
);
177 glDeleteBuffers( 1, &mesh
->ebo
);
178 glDeleteBuffers( 1, &mesh
->vbo
);
183 * Model implementation
186 static mdl_header
*mdl_load( const char *path
)
189 mdl_header
*header
= vg_asset_read_s( path
, &size
);
192 * Check file is valid
196 vg_error( "Could not open '%s'\n", path
);
200 if( size
< sizeof(mdl_header
) )
203 vg_error( "Invalid file '%s' (too small for header)\n", path
);
207 if( header
->file_length
!= size
)
209 vg_error( "Invalid file '%s'"
210 "(wrong .file_length, %ub != real file size %ub)\n",
211 path
, header
->file_length
, size
);
217 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
218 * and that they do not overlap.
224 u32 count
, max_count
, size
, offset
;
229 header
->vertex_count
, MDL_VERT_MAX
,
230 sizeof(mdl_vert
), header
->vertex_offset
234 header
->indice_count
, MDL_INDICE_MAX
,
235 sizeof(u32
), header
->indice_offset
239 header
->submesh_count
, MDL_SUBMESH_MAX
,
240 sizeof(mdl_submesh
), header
->submesh_offset
244 header
->material_count
, MDL_MATERIAL_MAX
,
245 sizeof(mdl_material
), header
->material_offset
249 header
->node_count
, MDL_NODE_MAX
,
250 sizeof(mdl_node
), header
->node_count
254 for( int i
=0; i
<vg_list_size(regions
); i
++ )
256 struct memregion
*ri
= ®ions
[i
];
261 if( ri
->count
> ri
->max_count
)
264 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
265 path
, ri
->desc
, ri
->count
, ri
->max_count
);
269 if( ri
->offset
>= header
->file_length
)
272 vg_error( "'%s': '%s' buffer offset is out of range\n",
277 if( ri
->offset
+ ri
->size
*ri
->count
> header
->file_length
)
280 vg_error( "'%s': '%s' buffer size is out of range\n",
285 for( int j
=0; j
<vg_list_size(regions
); j
++ )
287 struct memregion
*rj
= ®ions
[j
];
291 if( ri
->offset
>= rj
->offset
&&
292 (ri
->offset
+ri
->size
*ri
->count
< rj
->offset
+rj
->size
*rj
->count
))
295 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
296 path
, ri
->desc
, rj
->desc
);
303 * Pointer validation TODO(workshop)
307 * strings TODO(workshop)
313 static void *mdl_baseptr( mdl_header
*mdl
, u32 offset
)
315 return (void *)mdl
+ offset
;
318 static const char *mdl_pstr( mdl_header
*mdl
, u32 pstr
)
320 return (const char *)(mdl_baseptr( mdl
, mdl
->strings_offset
)) + pstr
;
323 static mdl_node
*mdl_node_from_id( mdl_header
*mdl
, u32 id
)
325 return ((mdl_node
*)mdl_baseptr( mdl
, mdl
->node_offset
)) + id
;
328 static mdl_node
*mdl_node_from_name( mdl_header
*mdl
, const char *name
)
330 for( int i
=0; i
<mdl
->node_count
; i
++ )
332 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
334 if( !strcmp( name
, mdl_pstr( mdl
, pnode
->pstr_name
)) )
341 static mdl_submesh
*mdl_submesh_from_id( mdl_header
*mdl
, u32 id
)
343 if( id
>= mdl
->submesh_count
)
346 return ((mdl_submesh
*)mdl_baseptr( mdl
, mdl
->submesh_offset
)) + id
;
349 static mdl_submesh
*mdl_node_submesh( mdl_header
*mdl
, mdl_node
*node
, u32 i
)
351 if( i
>= node
->submesh_count
)
354 return mdl_submesh_from_id( mdl
, node
->submesh_start
+i
);
357 static u32
*mdl_submesh_indices( mdl_header
*mdl
, mdl_submesh
*sm
)
359 return ((u32
*)mdl_baseptr( mdl
, mdl
->indice_offset
)) + sm
->indice_start
;
362 static mdl_vert
*mdl_submesh_vertices( mdl_header
*mdl
, mdl_submesh
*sm
)
364 return ((mdl_vert
*)mdl_baseptr(mdl
,mdl
->vertex_offset
)) + sm
->vertex_start
;
367 static mdl_material
*mdl_material_from_id( mdl_header
*mdl
, u32 id
)
369 return ((mdl_material
*)mdl_baseptr(mdl
,mdl
->material_offset
)) + id
;
372 static void mdl_node_transform( mdl_node
*pnode
, m4x3f transform
)
374 q_m3x3( pnode
->q
, transform
);
375 v3_muls( transform
[0], pnode
->s
[0], transform
[0] );
376 v3_muls( transform
[1], pnode
->s
[1], transform
[1] );
377 v3_muls( transform
[2], pnode
->s
[2], transform
[2] );
378 v3_copy( pnode
->co
, transform
[3] );
381 static void mdl_unpack_submesh( mdl_header
*mdl
, glmesh
*mesh
, mdl_submesh
*sm
)
383 mesh_upload( mesh
, mdl_submesh_vertices( mdl
, sm
), sm
->vertex_count
,
384 mdl_submesh_indices( mdl
, sm
), sm
->indice_count
);
387 static void mdl_unpack_glmesh( mdl_header
*mdl
, glmesh
*mesh
)
389 u32 offset
= mdl_submesh_from_id( mdl
, 0 )->vertex_count
;
391 for( int i
=1; i
< mdl
->submesh_count
; i
++ )
393 mdl_submesh
*sm
= mdl_submesh_from_id( mdl
, i
);
394 u32
*indices
= mdl_submesh_indices( mdl
, sm
);
396 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
397 indices
[j
] += offset
;
399 offset
+= sm
->vertex_count
;
402 mdl_vert
*vertex_base
= mdl_baseptr( mdl
, mdl
->vertex_offset
);
403 u32
*indice_base
= mdl_baseptr( mdl
, mdl
->indice_offset
);
405 mesh_upload( mesh
, vertex_base
, mdl
->vertex_count
,
406 indice_base
, mdl
->indice_count
);
409 static void mdl_draw_submesh( mdl_submesh
*sm
)
411 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
414 static void *mdl_get_entdata( mdl_header
*mdl
, mdl_node
*pnode
)
416 return mdl_baseptr( mdl
, mdl
->entdata_offset
) + pnode
->offset
;
419 static void mdl_link_materials( mdl_header
*root
, mdl_header
*child
)
421 u32 lookup
[MDL_MATERIAL_MAX
];
423 for( int i
=0; i
<child
->material_count
; i
++ )
425 mdl_material
*mi
= mdl_material_from_id( child
, i
);
426 const char *si
= mdl_pstr( child
, mi
->pstr_name
);
430 for( int j
=0; j
<root
->material_count
; j
++ )
432 mdl_material
*mj
= mdl_material_from_id( root
, j
);
433 const char *sj
= mdl_pstr( root
, mj
->pstr_name
);
435 if( !strcmp( si
, sj
) )
442 if( lookup
[i
] == 0 && i
!= 0 )
444 vg_warn( "Could not link material '%s' (not present in root model)\n",
449 for( int i
=0; i
<child
->submesh_count
; i
++ )
451 mdl_submesh
*sm
= mdl_submesh_from_id( child
, i
);
452 sm
->material_id
= lookup
[sm
->material_id
];