X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=model.h;h=aade46aa21ddd00c341c552d71669bc20f1fbd00;hb=refs%2Fheads%2Fmaster;hp=2aaf48358517283fa72d6c40a1e0b1d6194e28a7;hpb=54ca09093b8d5f752b4a0897bd9703f3b2ad6ed1;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/model.h b/model.h deleted file mode 100644 index 2aaf483..0000000 --- a/model.h +++ /dev/null @@ -1,185 +0,0 @@ -#include "vg/vg.h" - -typedef struct model model; -typedef struct glmesh glmesh; -typedef struct submodel submodel; -typedef struct model_vert model_vert; -typedef struct scene scene; -typedef struct sdf_primative sdf_primative; -typedef enum esdf_type esdf_type; - -#pragma pack(push,1) -struct model -{ - u32 identifier; - - u32 vertex_count, - indice_count, - layer_count; -}; - -struct sdf_primative -{ - v4f origin; /* xyz, yaw */ - /* Cone: - x base scale - y height - */ - v4f info; -}; - -struct submodel -{ - u32 indice_start, - indice_count, - vertex_start, - vertex_count; - - boxf bbx; - sdf_primative sdf; - - enum esdf_type - { - k_sdf_none = 0, - k_sdf_cone, - k_sdf_sphere, - k_sdf_box - } - sdf_type; - - char name[32]; -}; - -struct model_vert -{ - v3f co, - norm; - v4f colour; - v2f uv; -}; -#pragma pack(pop) - -struct glmesh -{ - GLuint vao, vbo, ebo; - u32 indice_count; -}; - -static void mesh_upload( glmesh *mesh, - model_vert *verts, u32 vert_count, - u32 *indices, u32 indice_count ) -{ - glGenVertexArrays( 1, &mesh->vao ); - glGenBuffers( 1, &mesh->vbo ); - glGenBuffers( 1, &mesh->ebo ); - glBindVertexArray( mesh->vao ); - - glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo ); - glBufferData( GL_ARRAY_BUFFER, vert_count*sizeof(model_vert), - verts, GL_STATIC_DRAW ); - - glBindVertexArray( mesh->vao ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo ); - glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32), - indices, GL_STATIC_DRAW ); - - glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void*)0 ); - glEnableVertexAttribArray( 0 ); - - glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void *)offsetof(model_vert, norm) ); - glEnableVertexAttribArray( 1 ); - - glVertexAttribPointer( 2, 4, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void *)offsetof(model_vert, colour) ); - glEnableVertexAttribArray( 2 ); - - glVertexAttribPointer( 3, 2, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void *)offsetof(model_vert, uv) ); - glEnableVertexAttribArray( 3 ); - - VG_CHECK_GL(); - mesh->indice_count = indice_count; -} - -static void mesh_bind( glmesh *mesh ) -{ - glBindVertexArray( mesh->vao ); -} - -static void mesh_drawn( u32 start, u32 count ) -{ - glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT, - (void *)(start*sizeof(u32)) ); -} - -static void mesh_draw( glmesh *mesh ) -{ - mesh_drawn( 0, mesh->indice_count ); -} - -/* - * Helper functions for file offsets - */ -static submodel *model_get_submodel( model *mdl, int id ) -{ - return ((submodel*)(mdl+1)) + id; -} - -static model_vert *model_vertex_base( model *mdl ) -{ - return (model_vert *)model_get_submodel( mdl, mdl->layer_count ); -} - -static u32 *model_indice_base( model *mdl ) -{ - return (u32 *)(model_vertex_base( mdl ) + mdl->vertex_count); -} - -static model_vert *submodel_vert_data( model *mdl, submodel *sub ) -{ - return model_vertex_base(mdl) + sub->vertex_start; -} - -static u32 *submodel_indice_data( model *mdl, submodel *sub ) -{ - return model_indice_base(mdl) + sub->indice_start; -} - -static submodel *submodel_get( model *mdl, const char *name ) -{ - for( int i=0; ilayer_count; i++ ) - { - submodel *pmdl =model_get_submodel(mdl,i); - - if( !strcmp( pmdl->name, name ) ) - return pmdl; - } - - return NULL; -} - -static void submodel_draw( submodel *sm ) -{ - mesh_drawn( sm->indice_start, sm->indice_count ); -} - -static void model_unpack( model *model, glmesh *mesh ) -{ - u32 offset = model_get_submodel( model, 0 )->vertex_count; - - for( int i=1; ilayer_count; i++ ) - { - submodel *sm = model_get_submodel( model, i ); - u32 *indices = submodel_indice_data( model, sm ); - - for( u32 j=0; jindice_count; j++ ) - indices[j] += offset; - - offset += sm->vertex_count; - } - - mesh_upload( mesh, model_vertex_base( model ), model->vertex_count, - model_indice_base( model ), model->indice_count ); -}