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 scene scene
;
11 typedef struct sdf_primative sdf_primative
;
12 typedef enum esdf_type esdf_type
;
26 v4f origin
; /* xyz, yaw */
72 static void mesh_upload( glmesh
*mesh
,
73 model_vert
*verts
, u32 vert_count
,
74 u32
*indices
, u32 indice_count
)
76 glGenVertexArrays( 1, &mesh
->vao
);
77 glGenBuffers( 1, &mesh
->vbo
);
78 glGenBuffers( 1, &mesh
->ebo
);
79 glBindVertexArray( mesh
->vao
);
81 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
82 glBufferData( GL_ARRAY_BUFFER
, vert_count
*sizeof(model_vert
),
83 verts
, GL_STATIC_DRAW
);
85 glBindVertexArray( mesh
->vao
);
86 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
87 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
88 indices
, GL_STATIC_DRAW
);
90 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
,
91 sizeof(model_vert
), (void*)0 );
92 glEnableVertexAttribArray( 0 );
94 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
95 sizeof(model_vert
), (void *)offsetof(model_vert
, norm
) );
96 glEnableVertexAttribArray( 1 );
98 glVertexAttribPointer( 2, 4, GL_FLOAT
, GL_FALSE
,
99 sizeof(model_vert
), (void *)offsetof(model_vert
, colour
) );
100 glEnableVertexAttribArray( 2 );
102 glVertexAttribPointer( 3, 2, GL_FLOAT
, GL_FALSE
,
103 sizeof(model_vert
), (void *)offsetof(model_vert
, uv
) );
104 glEnableVertexAttribArray( 3 );
107 mesh
->indice_count
= indice_count
;
110 static void mesh_bind( glmesh
*mesh
)
112 glBindVertexArray( mesh
->vao
);
115 static void mesh_drawn( u32 start
, u32 count
)
117 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
118 (void *)(start
*sizeof(u32
)) );
121 static void mesh_draw( glmesh
*mesh
)
123 mesh_drawn( 0, mesh
->indice_count
);
127 * Helper functions for file offsets
129 static submodel
*model_get_submodel( model
*mdl
, int id
)
131 return ((submodel
*)(mdl
+1)) + id
;
134 static model_vert
*model_vertex_base( model
*mdl
)
136 return (model_vert
*)model_get_submodel( mdl
, mdl
->layer_count
);
139 static u32
*model_indice_base( model
*mdl
)
141 return (u32
*)(model_vertex_base( mdl
) + mdl
->vertex_count
);
144 static model_vert
*submodel_vert_data( model
*mdl
, submodel
*sub
)
146 return model_vertex_base(mdl
) + sub
->vertex_start
;
149 static u32
*submodel_indice_data( model
*mdl
, submodel
*sub
)
151 return model_indice_base(mdl
) + sub
->indice_start
;
154 static submodel
*submodel_get( model
*mdl
, const char *name
)
156 for( int i
=0; i
<mdl
->layer_count
; i
++ )
158 submodel
*pmdl
=model_get_submodel(mdl
,i
);
160 if( !strcmp( pmdl
->name
, name
) )
167 static void submodel_draw( submodel
*sm
)
169 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
172 static void model_unpack( model
*model
, glmesh
*mesh
)
174 u32 offset
= model_get_submodel( model
, 0 )->vertex_count
;
176 for( int i
=1; i
<model
->layer_count
; i
++ )
178 submodel
*sm
= model_get_submodel( model
, i
);
179 u32
*indices
= submodel_indice_data( model
, sm
);
181 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
182 indices
[j
] += offset
;
184 offset
+= sm
->vertex_count
;
187 mesh_upload( mesh
, model_vertex_base( model
), model
->vertex_count
,
188 model_indice_base( model
), model
->indice_count
);