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
;
33 v3f pivot
; /* same as co? */
62 static void mesh_upload( glmesh
*mesh
,
63 model_vert
*verts
, u32 vert_count
,
64 u32
*indices
, u32 indice_count
)
66 glGenVertexArrays( 1, &mesh
->vao
);
67 glGenBuffers( 1, &mesh
->vbo
);
68 glGenBuffers( 1, &mesh
->ebo
);
69 glBindVertexArray( mesh
->vao
);
71 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
72 glBufferData( GL_ARRAY_BUFFER
, vert_count
*sizeof(model_vert
),
73 verts
, GL_STATIC_DRAW
);
75 glBindVertexArray( mesh
->vao
);
76 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
77 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
78 indices
, GL_STATIC_DRAW
);
80 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
,
81 sizeof(model_vert
), (void*)0 );
82 glEnableVertexAttribArray( 0 );
84 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
85 sizeof(model_vert
), (void *)offsetof(model_vert
, norm
) );
86 glEnableVertexAttribArray( 1 );
88 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
89 sizeof(model_vert
), (void *)offsetof(model_vert
, colour
) );
90 glEnableVertexAttribArray( 2 );
92 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
93 sizeof(model_vert
), (void *)offsetof(model_vert
, uv
) );
94 glEnableVertexAttribArray( 3 );
97 mesh
->indice_count
= indice_count
;
100 static void mesh_bind( glmesh
*mesh
)
102 glBindVertexArray( mesh
->vao
);
105 static void mesh_drawn( u32 start
, u32 count
)
107 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
108 (void *)(start
*sizeof(u32
)) );
111 static void mesh_draw( glmesh
*mesh
)
113 mesh_drawn( 0, mesh
->indice_count
);
117 * Helper functions for file offsets
120 static submodel
*model_get_submodel( model
*mdl
, int id
)
122 return ((submodel
*)(mdl
+1)) + id
;
125 static model_marker
*model_get_marker( model
*mdl
, int id
)
127 return ((model_marker
*)model_get_submodel(mdl
,mdl
->layer_count
)) + id
;
130 static model_vert
*model_vertex_base( model
*mdl
)
132 return (model_vert
*)model_get_marker( mdl
, mdl
->marker_count
);
135 static u32
*model_indice_base( model
*mdl
)
137 return (u32
*)(model_vertex_base( mdl
) + mdl
->vertex_count
);
140 static model_vert
*submodel_vert_data( model
*mdl
, submodel
*sub
)
142 return model_vertex_base(mdl
) + sub
->vertex_start
;
145 static u32
*submodel_indice_data( model
*mdl
, submodel
*sub
)
147 return model_indice_base(mdl
) + sub
->indice_start
;
150 static submodel
*submodel_get( model
*mdl
, const char *name
)
152 for( int i
=0; i
<mdl
->layer_count
; i
++ )
154 submodel
*pmdl
=model_get_submodel(mdl
,i
);
156 if( !strcmp( pmdl
->name
, name
) )
163 static model_marker
*model_marker_get( model
*mdl
, const char *name
)
165 for( int i
=0; i
<mdl
->marker_count
; i
++ )
167 model_marker
*mk
= model_get_marker( mdl
,i
);
169 if( !strcmp( mk
->name
, name
) )
176 static void submodel_draw( submodel
*sm
)
178 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
181 static void model_unpack_submodel( model
*model
, glmesh
*mesh
, submodel
*sm
)
184 model_vertex_base( model
) + sm
->vertex_start
, sm
->vertex_count
,
185 model_indice_base( model
) + sm
->indice_start
, sm
->indice_count
);
188 static void model_unpack( model
*model
, glmesh
*mesh
)
190 u32 offset
= model_get_submodel( model
, 0 )->vertex_count
;
192 for( int i
=1; i
<model
->layer_count
; i
++ )
194 submodel
*sm
= model_get_submodel( model
, i
);
195 u32
*indices
= submodel_indice_data( model
, sm
);
197 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
198 indices
[j
] += offset
;
200 offset
+= sm
->vertex_count
;
203 mesh_upload( mesh
, model_vertex_base( model
), model
->vertex_count
,
204 model_indice_base( model
), model
->indice_count
);
207 static void mesh_free( glmesh
*mesh
)
209 glDeleteVertexArrays( 1, &mesh
->vao
);
210 glDeleteBuffers( 1, &mesh
->ebo
);
211 glDeleteBuffers( 1, &mesh
->vbo
);