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

Functions

LG_Meshlg_load_vbo (const char *path, zboolean force_reload_obj, zboolean do_normalize_xyz)
 
LG_Meshlg_mesh_new_from_objfile (const char *path, zboolean do_normalize_xyz)
 
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_load_from_file (const char *path, LG_Mesh **mesh)
 

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(const char *path, zboolean force_reload_obj, zboolean do_normalize_xyz)

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()

LG_Mesh* lg_load_vbo ( const char *  path,
zboolean  force_reload_obj,
zboolean  do_normalize_xyz 
)

Load VBO &co

=== This is the entry point for loading meshes (obj, vbo, ibo) ===

THIS WHOLE MODULE WORKS GREAT BUT NEED TO FIND A MORE DESCRIPTIVE AND ACCURATE NAME FOR THIS FUNC (AND OTHERS AS WELL ?)

We use a cache mechanism:

  • Try first to load the mesh data (VBO, IBO, and info) from cache
  • 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

Parameters
pathPath to obj file
force_reload_obj
do_normalize_xyz
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  do_normalize_xyz 
)

Create mesh from obj file

Parameters
path
do_normalize_xyz
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

◆ lg_obj_file_open_from_cache()

void* lg_obj_file_open_from_cache ( const char *  file_name)

Load obj file from cache

◆ lg_vbo_save_to_file()

int lg_vbo_save_to_file ( const char *  path,
LG_Mesh mesh 
)

Actually save mesh with VBO and IBO in binary form, so should find a more accurate name. Don't add suffix LG_MESH_INFO_EXT, LG_MESH_VBO_EXT, LG_MESH_IBO_EXT to path, it's done automagically. Only the basename of path is used. All binary files are saved in the cache.

◆ 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