3 typedef struct model model
;
4 typedef struct glmesh glmesh
;
5 typedef struct submodel submodel
;
6 typedef struct model_vert model_vert
;
7 typedef struct scene scene
;
8 typedef struct sdf_primative sdf_primative
;
9 typedef enum esdf_type esdf_type
;
23 v4f origin
; /* xyz, yaw */
68 static void mesh_upload( glmesh
*mesh
,
69 model_vert
*verts
, u32 vert_count
,
70 u32
*indices
, u32 indice_count
)
72 glGenVertexArrays( 1, &mesh
->vao
);
73 glGenBuffers( 1, &mesh
->vbo
);
74 glGenBuffers( 1, &mesh
->ebo
);
75 glBindVertexArray( mesh
->vao
);
77 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
78 glBufferData( GL_ARRAY_BUFFER
, vert_count
*sizeof(model_vert
),
79 verts
, GL_STATIC_DRAW
);
81 glBindVertexArray( mesh
->vao
);
82 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
83 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
84 indices
, GL_STATIC_DRAW
);
86 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
,
87 sizeof(model_vert
), (void*)0 );
88 glEnableVertexAttribArray( 0 );
90 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
91 sizeof(model_vert
), (void *)offsetof(model_vert
, norm
) );
92 glEnableVertexAttribArray( 1 );
94 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
95 sizeof(model_vert
), (void *)offsetof(model_vert
, colour
) );
96 glEnableVertexAttribArray( 2 );
98 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
99 sizeof(model_vert
), (void *)offsetof(model_vert
, uv
) );
100 glEnableVertexAttribArray( 3 );
103 mesh
->indice_count
= indice_count
;
106 static void mesh_bind( glmesh
*mesh
)
108 glBindVertexArray( mesh
->vao
);
111 static void mesh_drawn( u32 start
, u32 count
)
113 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
114 (void *)(start
*sizeof(u32
)) );
117 static void mesh_draw( glmesh
*mesh
)
119 mesh_drawn( 0, mesh
->indice_count
);
123 * Helper functions for file offsets
125 static submodel
*model_get_submodel( model
*mdl
, int id
)
127 return ((submodel
*)(mdl
+1)) + id
;
130 static model_vert
*model_vertex_base( model
*mdl
)
132 return (model_vert
*)model_get_submodel( mdl
, mdl
->layer_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 void submodel_draw( submodel
*sm
)
165 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
168 static void model_unpack( model
*model
, glmesh
*mesh
)
170 u32 offset
= model_get_submodel( model
, 0 )->vertex_count
;
172 for( int i
=1; i
<model
->layer_count
; i
++ )
174 submodel
*sm
= model_get_submodel( model
, i
);
175 u32
*indices
= submodel_indice_data( model
, sm
);
177 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
178 indices
[j
] += offset
;
180 offset
+= sm
->vertex_count
;
183 mesh_upload( mesh
, model_vertex_base( model
), model
->vertex_count
,
184 model_indice_base( model
), model
->indice_count
);