6 typedef struct model model
;
7 typedef struct glmesh glmesh
;
8 typedef struct submodel submodel
;
9 typedef struct model_vert model_vert
;
10 typedef struct model_marker model_marker
;
11 typedef struct sdf_primative sdf_primative
;
12 typedef enum esdf_type esdf_type
;
27 v4f origin
; /* xyz, yaw */
81 #define VERTEX_STANDARD_ATTRIBUTES \
82 "layout (location=0) in vec3 a_co;" \
83 "layout (location=1) in vec3 a_norm;" \
84 "layout (location=2) in vec4 a_colour;" \
85 "layout (location=3) in vec2 a_uv;"
87 static void mesh_upload( glmesh
*mesh
,
88 model_vert
*verts
, u32 vert_count
,
89 u32
*indices
, u32 indice_count
)
91 glGenVertexArrays( 1, &mesh
->vao
);
92 glGenBuffers( 1, &mesh
->vbo
);
93 glGenBuffers( 1, &mesh
->ebo
);
94 glBindVertexArray( mesh
->vao
);
96 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
97 glBufferData( GL_ARRAY_BUFFER
, vert_count
*sizeof(model_vert
),
98 verts
, GL_STATIC_DRAW
);
100 glBindVertexArray( mesh
->vao
);
101 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
102 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
103 indices
, GL_STATIC_DRAW
);
105 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
,
106 sizeof(model_vert
), (void*)0 );
107 glEnableVertexAttribArray( 0 );
109 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
110 sizeof(model_vert
), (void *)offsetof(model_vert
, norm
) );
111 glEnableVertexAttribArray( 1 );
113 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
114 sizeof(model_vert
), (void *)offsetof(model_vert
, colour
) );
115 glEnableVertexAttribArray( 2 );
117 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
118 sizeof(model_vert
), (void *)offsetof(model_vert
, uv
) );
119 glEnableVertexAttribArray( 3 );
122 mesh
->indice_count
= indice_count
;
125 static void mesh_bind( glmesh
*mesh
)
127 glBindVertexArray( mesh
->vao
);
130 static void mesh_drawn( u32 start
, u32 count
)
132 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
133 (void *)(start
*sizeof(u32
)) );
136 static void mesh_draw( glmesh
*mesh
)
138 mesh_drawn( 0, mesh
->indice_count
);
142 * Helper functions for file offsets
145 static submodel
*model_get_submodel( model
*mdl
, int id
)
147 return ((submodel
*)(mdl
+1)) + id
;
150 static model_marker
*model_get_marker( model
*mdl
, int id
)
152 return ((model_marker
*)model_get_submodel(mdl
,mdl
->layer_count
)) + id
;
155 static model_vert
*model_vertex_base( model
*mdl
)
157 return (model_vert
*)model_get_marker( mdl
, mdl
->marker_count
);
160 static u32
*model_indice_base( model
*mdl
)
162 return (u32
*)(model_vertex_base( mdl
) + mdl
->vertex_count
);
165 static model_vert
*submodel_vert_data( model
*mdl
, submodel
*sub
)
167 return model_vertex_base(mdl
) + sub
->vertex_start
;
170 static u32
*submodel_indice_data( model
*mdl
, submodel
*sub
)
172 return model_indice_base(mdl
) + sub
->indice_start
;
175 static submodel
*submodel_get( model
*mdl
, const char *name
)
177 for( int i
=0; i
<mdl
->layer_count
; i
++ )
179 submodel
*pmdl
=model_get_submodel(mdl
,i
);
181 if( !strcmp( pmdl
->name
, name
) )
188 static model_marker
*model_marker_get( model
*mdl
, const char *name
)
190 for( int i
=0; i
<mdl
->marker_count
; i
++ )
192 model_marker
*mk
= model_get_marker( mdl
,i
);
194 if( !strcmp( mk
->name
, name
) )
201 static void submodel_draw( submodel
*sm
)
203 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
206 static void model_unpack( model
*model
, glmesh
*mesh
)
208 u32 offset
= model_get_submodel( model
, 0 )->vertex_count
;
210 for( int i
=1; i
<model
->layer_count
; i
++ )
212 submodel
*sm
= model_get_submodel( model
, i
);
213 u32
*indices
= submodel_indice_data( model
, sm
);
215 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
216 indices
[j
] += offset
;
218 offset
+= sm
->vertex_count
;
221 mesh_upload( mesh
, model_vertex_base( model
), model
->vertex_count
,
222 model_indice_base( model
), model
->indice_count
);
225 static void mesh_free( glmesh
*mesh
)
227 glDeleteVertexArrays( 1, &mesh
->vao
);
228 glDeleteBuffers( 1, &mesh
->ebo
);
229 glDeleteBuffers( 1, &mesh
->vbo
);