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

Functions

LG_Meshlg_mesh_load (const char *file_name, LG_LoadMesh_Flags flags)
 
LG_Meshlg_mesh_new_from_objfile (const char *file_name, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
 
LG_Meshlg_mesh_new_from_fbxfile (const char *file_name, 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 *file_name, LG_Mesh *mesh)
 
int lg_vbo_save_to_file_in_cache (const char *file_name, LG_Mesh *mesh)
 
int lg_vbo_load_from_file (const char *file_name, LG_Mesh **mesh)
 
int lg_vbo_load_from_file_in_cache (const char *file_name, 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.

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

LG_Mesh *lg_mesh_load(const char *file_name, LG_LoadMesh_Flags flags)
// LG_Mesh struct is defined in lg_mesh.h
// file_name = OBJ or VBO &co files (= VBO, IBO, and INFO binaries) - only BASENAME is required
// Mesh loading flags
typedef struct {
zboolean force_reload; // Force (re)loading and (re)parsing of the OBJ file
zboolean invert_z; // Invert z spatial coord - should be set to TRUE if mesh uses RH coords sys, FALSE if mesh uses LH coords sys
zboolean normalize_xyz;
zboolean horiz_center;
zboolean vert_center; // Center mesh vertically, if vert_bottom is not set
zboolean vert_bottom; // Set mesh origin vertically at bottom, override vert_center

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

and just ignore the details.

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

Function Documentation

◆ lg_mesh_load()

LG_Mesh* lg_mesh_load ( const char *  file_name,
LG_LoadMesh_Flags  flags 
)

Load OBJ or VBO &co.

=== THIS IS THE NEW ENTRY POINT FOR LOADING MESHES (OBJ, FBX, VBO, IBO, INFO) AND THE ONLY FUNC YOU NEED TO CALL ===

We use the following sequence:

  • Try first to load the mesh data (VBO, IBO, and INFO) in the assets/meshes/vbo folder, unless force_reload is set
  • If not found, look in the cache/vbo folder
  • If not found, load and parse the OBJ file in the assets/meshes/obj folder, then save it to cache

On Android, OBJ files are cached as well

TO AVOID ANY POSSIBLE CONFUSION, ALWAYS FOLLOW THIS:
- PATHS ARE FULL (IE ABSOLUTE) AND INCLUDE THE FILE NAME
- FILE_NAMES ARE BASENAMES (WITH EXTENSION)
Parameters
file_nameName of OBJ or VBO &co files (= VBO, IBO, and INFO binaries) - only BASENAME is required
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_mesh_new_from_objfile()

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

Create mesh from OBJ file.

Read 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).

Other data is ignored so far.

=== WARNING: MESHES MUST BE PURE-TRIANGULAR ===

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

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
Parameters
file_nameOBJ file name - only BASENAME is required
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 *  file_name,
zboolean  invert_z,
zboolean  normalize_xyz,
zboolean  horiz_center,
zboolean  vert_center,
zboolean  vert_bottom 
)

Create mesh from FBX file.

Parameters
file_nameFBX file name - only BASENAME is required
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_bufMem buffer
file_nameOBJ file name - only BASENAME is required
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)

Open OBJ file from cache.

NOTE: this func is meant to be used with lg_obj2vbo().

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 *  file_name,
LG_Mesh mesh 
)

Save mesh VBO and IBO in binary format, plus LG_Mesh struct instance as INFO -> file_name.obj.vbo, file_name.obj.ibo, and file_name.obj.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
file_nameOBJ file name - 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 *  file_name,
LG_Mesh mesh 
)

Save mesh VBO and IBO in binary format, plus LG_Mesh struct instance as INFO -> file_name.obj.vbo, file_name.obj.ibo, and file_name.obj.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 cache/vbo folder.

Parameters
file_nameOBJ file name - 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 *  file_name,
LG_Mesh **  mesh 
)

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

Parameters
file_nameOBJ file name - 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 *  file_name,
LG_Mesh **  mesh 
)

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

Parameters
file_nameOBJ file name - 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
LG_Mesh
Definition: lg_mesh.h:87
LG_LoadMesh_Flags
Definition: lg_mesh.h:121
lg_mesh_load
LG_Mesh * lg_mesh_load(const char *file_name, LG_LoadMesh_Flags flags)
Definition: lg_mesh.c:71
lg_mesh_free
void lg_mesh_free(LG_Mesh *mesh)
Definition: lg_mesh.c:214