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? */
69 static void mesh_upload( glmesh
*mesh
,
70 model_vert
*verts
, u32 vert_count
,
71 u32
*indices
, u32 indice_count
)
73 glGenVertexArrays( 1, &mesh
->vao
);
74 glGenBuffers( 1, &mesh
->vbo
);
75 glGenBuffers( 1, &mesh
->ebo
);
76 glBindVertexArray( mesh
->vao
);
78 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
79 glBufferData( GL_ARRAY_BUFFER
, vert_count
*sizeof(model_vert
),
80 verts
, GL_STATIC_DRAW
);
82 glBindVertexArray( mesh
->vao
);
83 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
84 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
85 indices
, GL_STATIC_DRAW
);
87 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
,
88 sizeof(model_vert
), (void*)0 );
89 glEnableVertexAttribArray( 0 );
91 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
92 sizeof(model_vert
), (void *)offsetof(model_vert
, norm
) );
93 glEnableVertexAttribArray( 1 );
95 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
96 sizeof(model_vert
), (void *)offsetof(model_vert
, colour
) );
97 glEnableVertexAttribArray( 2 );
99 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
100 sizeof(model_vert
), (void *)offsetof(model_vert
, uv
) );
101 glEnableVertexAttribArray( 3 );
104 mesh
->indice_count
= indice_count
;
107 static void mesh_bind( glmesh
*mesh
)
109 glBindVertexArray( mesh
->vao
);
112 static void mesh_drawn( u32 start
, u32 count
)
114 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
115 (void *)(start
*sizeof(u32
)) );
118 static void mesh_draw( glmesh
*mesh
)
120 mesh_drawn( 0, mesh
->indice_count
);
124 * Helper functions for file offsets
127 static submodel
*model_get_submodel( model
*mdl
, int id
)
129 return ((submodel
*)(mdl
+1)) + id
;
132 static model_marker
*model_get_marker( model
*mdl
, int id
)
134 return ((model_marker
*)model_get_submodel(mdl
,mdl
->layer_count
)) + id
;
137 static model_vert
*model_vertex_base( model
*mdl
)
139 return (model_vert
*)model_get_marker( mdl
, mdl
->marker_count
);
142 static u32
*model_indice_base( model
*mdl
)
144 return (u32
*)(model_vertex_base( mdl
) + mdl
->vertex_count
);
147 static model_vert
*submodel_vert_data( model
*mdl
, submodel
*sub
)
149 return model_vertex_base(mdl
) + sub
->vertex_start
;
152 static u32
*submodel_indice_data( model
*mdl
, submodel
*sub
)
154 return model_indice_base(mdl
) + sub
->indice_start
;
157 static void *get_entdata_raw( model
*mdl
, model_marker
*marker
)
159 return ((void *)(model_indice_base(mdl
) + mdl
->indice_count
)) +
163 static submodel
*submodel_get( model
*mdl
, const char *name
)
165 for( int i
=0; i
<mdl
->layer_count
; i
++ )
167 submodel
*pmdl
=model_get_submodel(mdl
,i
);
169 if( !strcmp( pmdl
->name
, name
) )
176 static model_marker
*model_marker_get( model
*mdl
, const char *name
)
178 for( int i
=0; i
<mdl
->marker_count
; i
++ )
180 model_marker
*mk
= model_get_marker( mdl
,i
);
182 if( !strcmp( mk
->name
, name
) )
189 static void submodel_draw( submodel
*sm
)
191 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
194 static void model_unpack_submodel( model
*model
, glmesh
*mesh
, submodel
*sm
)
197 model_vertex_base( model
) + sm
->vertex_start
, sm
->vertex_count
,
198 model_indice_base( model
) + sm
->indice_start
, sm
->indice_count
);
201 static void model_unpack( model
*model
, glmesh
*mesh
)
203 u32 offset
= model_get_submodel( model
, 0 )->vertex_count
;
205 for( int i
=1; i
<model
->layer_count
; i
++ )
207 submodel
*sm
= model_get_submodel( model
, i
);
208 u32
*indices
= submodel_indice_data( model
, sm
);
210 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
211 indices
[j
] += offset
;
213 offset
+= sm
->vertex_count
;
216 mesh_upload( mesh
, model_vertex_base( model
), model
->vertex_count
,
217 model_indice_base( model
), model
->indice_count
);
220 static void mesh_free( glmesh
*mesh
)
222 glDeleteVertexArrays( 1, &mesh
->vao
);
223 glDeleteBuffers( 1, &mesh
->ebo
);
224 glDeleteBuffers( 1, &mesh
->vbo
);