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 static void mesh_upload( glmesh
*mesh
,
82 model_vert
*verts
, u32 vert_count
,
83 u32
*indices
, u32 indice_count
)
85 glGenVertexArrays( 1, &mesh
->vao
);
86 glGenBuffers( 1, &mesh
->vbo
);
87 glGenBuffers( 1, &mesh
->ebo
);
88 glBindVertexArray( mesh
->vao
);
90 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
91 glBufferData( GL_ARRAY_BUFFER
, vert_count
*sizeof(model_vert
),
92 verts
, GL_STATIC_DRAW
);
94 glBindVertexArray( mesh
->vao
);
95 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
96 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
97 indices
, GL_STATIC_DRAW
);
99 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
,
100 sizeof(model_vert
), (void*)0 );
101 glEnableVertexAttribArray( 0 );
103 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
104 sizeof(model_vert
), (void *)offsetof(model_vert
, norm
) );
105 glEnableVertexAttribArray( 1 );
107 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
108 sizeof(model_vert
), (void *)offsetof(model_vert
, colour
) );
109 glEnableVertexAttribArray( 2 );
111 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
112 sizeof(model_vert
), (void *)offsetof(model_vert
, uv
) );
113 glEnableVertexAttribArray( 3 );
116 mesh
->indice_count
= indice_count
;
119 static void mesh_bind( glmesh
*mesh
)
121 glBindVertexArray( mesh
->vao
);
124 static void mesh_drawn( u32 start
, u32 count
)
126 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
127 (void *)(start
*sizeof(u32
)) );
130 static void mesh_draw( glmesh
*mesh
)
132 mesh_drawn( 0, mesh
->indice_count
);
136 * Helper functions for file offsets
139 static submodel
*model_get_submodel( model
*mdl
, int id
)
141 return ((submodel
*)(mdl
+1)) + id
;
144 static model_marker
*model_get_marker( model
*mdl
, int id
)
146 return ((model_marker
*)model_get_submodel(mdl
,mdl
->layer_count
)) + id
;
149 static model_vert
*model_vertex_base( model
*mdl
)
151 return (model_vert
*)model_get_marker( mdl
, mdl
->marker_count
);
154 static u32
*model_indice_base( model
*mdl
)
156 return (u32
*)(model_vertex_base( mdl
) + mdl
->vertex_count
);
159 static model_vert
*submodel_vert_data( model
*mdl
, submodel
*sub
)
161 return model_vertex_base(mdl
) + sub
->vertex_start
;
164 static u32
*submodel_indice_data( model
*mdl
, submodel
*sub
)
166 return model_indice_base(mdl
) + sub
->indice_start
;
169 static submodel
*submodel_get( model
*mdl
, const char *name
)
171 for( int i
=0; i
<mdl
->layer_count
; i
++ )
173 submodel
*pmdl
=model_get_submodel(mdl
,i
);
175 if( !strcmp( pmdl
->name
, name
) )
182 static model_marker
*model_marker_get( model
*mdl
, const char *name
)
184 for( int i
=0; i
<mdl
->marker_count
; i
++ )
186 model_marker
*mk
= model_get_marker( mdl
,i
);
188 if( !strcmp( mk
->name
, name
) )
195 static void submodel_draw( submodel
*sm
)
197 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
200 static void model_unpack_submodel( model
*model
, glmesh
*mesh
, submodel
*sm
)
203 model_vertex_base( model
) + sm
->vertex_start
, sm
->vertex_count
,
204 model_indice_base( model
) + sm
->indice_start
, sm
->indice_count
);
207 static void model_unpack( model
*model
, glmesh
*mesh
)
209 u32 offset
= model_get_submodel( model
, 0 )->vertex_count
;
211 for( int i
=1; i
<model
->layer_count
; i
++ )
213 submodel
*sm
= model_get_submodel( model
, i
);
214 u32
*indices
= submodel_indice_data( model
, sm
);
216 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
217 indices
[j
] += offset
;
219 offset
+= sm
->vertex_count
;
222 mesh_upload( mesh
, model_vertex_base( model
), model
->vertex_count
,
223 model_indice_base( model
), model
->indice_count
);
226 static void mesh_free( glmesh
*mesh
)
228 glDeleteVertexArrays( 1, &mesh
->vao
);
229 glDeleteBuffers( 1, &mesh
->ebo
);
230 glDeleteBuffers( 1, &mesh
->vbo
);