LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
lg_mesh.c File Reference

Functions

LG_Meshlg_load_vbo_or_mesh (const char *mesh_file, LG_LoadMesh_Flags flags)
 
LG_Meshlg_load_vbo (const char *path, zboolean force_reload, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
 
LG_Meshlg_mesh_new_from_objfile (const char *path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
 
LG_Meshlg_mesh_new_from_fbxfile (const char *path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
 
void lg_mesh_free (LG_Mesh *mesh)
 
void lg_mesh_info (LG_Mesh *mesh)
 
int lg_obj_file_save_to_cache (void *asset_buf, const char *file_name, size_t size, size_t *written_size)
 
void * lg_obj_file_open_from_cache (const char *file_name)
 
int lg_vbo_save_to_file (const char *path, LG_Mesh *mesh)
 
int lg_vbo_save_to_file_in_cache (const char *path, LG_Mesh *mesh)
 
int lg_vbo_load_from_file (const char *path, LG_Mesh **mesh)
 
int lg_vbo_load_from_file_in_cache (const char *path, LG_Mesh **mesh)
 
int lg_mesh_reload_mats (LG_Mesh *mesh)
 
LG_LoadMesh_Flags lg_loadmesh_flags (zboolean force_reload, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
 

Detailed Description

This module deals with meshes, ie data that will feed VBOs and IBOs for fast and efficient drawing.

=== MESHES MUST BE PURE-TRIANGULAR ===

 Using MeshLab filters:
    remeshing, simplification and reconstruction
            turn into a pure-triangle mesh
 Also:
    normals, curvatures and orientation
            transform: translate, center, set origin
                    -> center on layer bbox
    normals, curvatures and orientation
            transform: scale, normalize
                    -> scale to unit bbox

At the moment, read only a limited set of data from WAVEFRONT OBJ files: vertex/index values for v (coords), vt (texture coords), vn (normals), f (triangular faces), mtllib, usemtl (materials/textures stuff), plus newmtl, and map_K* (in MTL files).

Will create a Vertex_uv_n VBO (see lg_vertex.h).

You can/should only use this func to load meshes:

 LG_Mesh *lg_load_vbo_or_mesh(const char *mesh_file, LG_LoadMesh_Flags flags)

 // mesh_file = path to VBO or OBJ file

 // Mesh loading flags
 typedef struct {
    zboolean        force_reload;
    zboolean        invert_z;
    zboolean        normalize_xyz;
    zboolean        horiz_center;
    zboolean        vert_center;
    zboolean        vert_bottom;
 } LG_LoadMesh_Flags;

and you can/should only use this func when done with a mesh:

 void       lg_mesh_free(LG_Mesh *)

and just ignore the details.

(Use everything here only if you know what you're doing).

Function Documentation

◆ lg_load_vbo_or_mesh()

LG_Mesh* lg_load_vbo_or_mesh ( const char *  mesh_file,
LG_LoadMesh_Flags  flags 
)

Load VBO &co

=== THIS IS THE NEW ENTRY POINT FOR LOADING MESHES (OBJ, VBO, IBO) ===

New helper func which will call sequentially:

  • (if force_reload is set to FALSE)
  • (else)

THIS IS NOW THE ONLY FUNC YOU NEED TO CALL

Parameters
mesh_filePath to VBO or OBJ file
flagsApply to mesh: force_reload, invert_z, normalize_xyz, horiz_center, vert_center, vert_bottom
Returns
Pointer to LG_Mesh if OK, NULL on error

◆ lg_load_vbo()

LG_Mesh* lg_load_vbo ( const char *  path,
zboolean  force_reload,
zboolean  invert_z,
zboolean  normalize_xyz,
zboolean  horiz_center,
zboolean  vert_center,
zboolean  vert_bottom 
)

Load VBO &co


THIS WHOLE MODULE WORKS GREAT BUT NEED TO FIND LESS CONFUSING FUNCS NAMES AND IMPROVE DOC

We use a cache mechanism:

  • Try first to load the mesh data (VBO, IBO, and info) from cache, unless force_reload is set
  • If it doesn't exist, load and parse the obj file, then save it to cache

All mesh data is saved

On Android, obj files are cached as well

-> Should use:

Refactoring the I/O API -> assets dir and writable dir ? lg_io.c/h ? No, probably just need to better doc everything

Parameters
pathPath to OBJ file
force_reloadForce (re)loading and (re)parsing of the OBJ file
invert_zInvert z spatial coord - should be set to TRUE if mesh uses RH coords sys, FALSE if mesh uses LH coords sys
normalize_xyz
horiz_center
vert_centerCenter mesh vertically, if vert_bottom is not set
vert_bottomSet mesh origin vertically at bottom, override vert_center
Returns
A LG_Mesh instance if OK, NULL on error

◆ lg_mesh_new_from_objfile()

LG_Mesh* lg_mesh_new_from_objfile ( const char *  path,
zboolean  invert_z,
zboolean  normalize_xyz,
zboolean  horiz_center,
zboolean  vert_center,
zboolean  vert_bottom 
)

Create mesh from obj file

Parameters
pathOBJ file name
invert_zInvert z spatial coord - should be set to TRUE if mesh uses RH coords sys, FALSE if mesh uses LH coords sys
normalize_xyzNormalize spatial coords
horiz_centerCenter mesh horizontally
vert_centerCenter mesh vertically, if vert_bottom is not set
vert_bottomSet mesh origin vertically at bottom, override vert_center
Returns
A new LG_Mesh instance if OK, NULL on error

◆ lg_mesh_new_from_fbxfile()

LG_Mesh* lg_mesh_new_from_fbxfile ( const char *  path,
zboolean  invert_z,
zboolean  normalize_xyz,
zboolean  horiz_center,
zboolean  vert_center,
zboolean  vert_bottom 
)

Create mesh from fbx file

Parameters
pathFBX file name
invert_zInvert z spatial coord - should be set to TRUE if mesh uses RH coords sys, FALSE if mesh uses LH coords sys
normalize_xyzNormalize spatial coords
horiz_centerCenter mesh horizontally
vert_centerCenter mesh vertically, if vert_bottom is not set
vert_bottomSet mesh origin vertically at bottom, override vert_center
Returns
A new LG_Mesh instance if OK, NULL on error

◆ lg_mesh_free()

void lg_mesh_free ( LG_Mesh mesh)

Free mesh

Parameters
mesh

◆ lg_mesh_info()

void lg_mesh_info ( LG_Mesh mesh)

Print out LG_Mesh info

Parameters
mesh

◆ lg_obj_file_save_to_cache()

int lg_obj_file_save_to_cache ( void *  asset_buf,
const char *  file_name,
size_t  size,
size_t *  written_size 
)

Save obj file to cache

Parameters
asset_buf?
file_name?
size?
written_size?
Returns
LG_OK if OK, error code otherwise

◆ lg_obj_file_open_from_cache()

void* lg_obj_file_open_from_cache ( const char *  file_name)

Load obj file from cache

Parameters
file_nameThe OBJ file name
Returns
Opened stream if OK, NULL on error

◆ lg_vbo_save_to_file()

int lg_vbo_save_to_file ( const char *  path,
LG_Mesh mesh 
)

Save mesh VBO and IBO in binary format, plus LG_Mesh struct instance as INFO -> file_name.vbo, file_name.ibo, and file_name.info. Don't add suffix LG_MESH_INFO_EXT, LG_MESH_VBO_EXT, LG_MESH_IBO_EXT to path, as it's done automagically. Only the basename of path is used. All binary files are saved in the assets vbo folder.

Parameters
pathPath to OBJ file - only BASENAME is required
meshPointer to a LG_Mesh
Returns
LG_OK if OK, error code otherwise

◆ lg_vbo_save_to_file_in_cache()

int lg_vbo_save_to_file_in_cache ( const char *  path,
LG_Mesh mesh 
)

Save mesh VBO and IBO in binary format, plus LG_Mesh struct instance as INFO -> file_name.vbo, file_name.ibo, and file_name.info. Don't add suffix LG_MESH_INFO_EXT, LG_MESH_VBO_EXT, LG_MESH_IBO_EXT to path, as it's done automagically. Only the basename of path is used. All binary files are saved in the vbo_cache folder.

Parameters
pathPath to OBJ file - only BASENAME is required
meshPointer to a LG_Mesh
Returns
LG_OK if OK, error code otherwise

◆ lg_vbo_load_from_file()

int lg_vbo_load_from_file ( const char *  path,
LG_Mesh **  mesh 
)

Same as above but for reading binary files in the assets vbo folder

Parameters
pathPath to OBJ file - only BASENAME is required
meshPointer to an array of pointers to LG_Mesh
Returns
LG_OK if OK, error code otherwise

◆ lg_vbo_load_from_file_in_cache()

int lg_vbo_load_from_file_in_cache ( const char *  path,
LG_Mesh **  mesh 
)

Same as above but for reading binary files in the vbo_cache folder

Parameters
pathPath to OBJ file - only BASENAME is required
meshPointer to an array of pointers to LG_Mesh
Returns
LG_OK if OK, error code otherwise

◆ lg_mesh_reload_mats()

int lg_mesh_reload_mats ( LG_Mesh mesh)

Reload materials textures correctly, when mesh is loaded from cache

Parameters
meshPointer to a LG_Mesh
Returns
LG_OK if OK, error code otherwise

◆ lg_loadmesh_flags()

LG_LoadMesh_Flags lg_loadmesh_flags ( zboolean  force_reload,
zboolean  invert_z,
zboolean  normalize_xyz,
zboolean  horiz_center,
zboolean  vert_center,
zboolean  vert_bottom 
)

Mesh loading flags

See lg_load_vbo()

You can use these helper, which are all set to TRUE:

  • FORCE_RELOAD
  • INVERT_Z
  • NORMALIZE_XYZ
  • VERT_CENTER
  • VERT_BOTTOM
Parameters
force_reloadForce (re)loading and (re)parsing of the OBJ file
invert_zInvert z spatial coord - should be set to TRUE if mesh uses RH coords sys, FALSE if mesh uses LH coords sys
normalize_xyz
horiz_center
vert_centerCenter mesh vertically, if vert_bottom is not set
vert_bottomSet mesh origin vertically at bottom, override vert_center
Returns
A LG_LoadMesh_Flags