LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
lg_mesh_v1.h
1 /*
2  * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2025
3  * All rights reserved
4  */
5 
6 #ifndef LG_MESH_H
7 #define LG_MESH_H
8 
9 #define LG_MESH_NAME_MAX_LEN (64 - 1)
10 #define MAT_NAME_MAX_LEN (64 - 1)
11 #define N_MAX_MAT_PER_OBJ 64 /* Or N_MAX_MAT_PER_MTL ? */
12 #define N_MAX_USEMTL_PER_OBJ 256 /* 4096 ? */
13 
14 #define LG_FLOAT_EPSILON (1e-6) /* 1e-6 or 1e-8 ? */
15 
16 #define LG_VBO_CACHE "vbo_cache"
17 #define MAX_FILE_EXT_LEN 64 /* Extension length must not exceed that */
18 #define LG_MESH_INFO_EXT ".info"
19 #define LG_MESH_VBO_EXT ".vbo"
20 #define LG_MESH_IBO_EXT ".ibo"
21 
22 #define LG_OBJ_CACHE "obj_cache" /* Only used on Android so far */
23 
24 #define LG_OBJ_FILE_MAXSIZE ((size_t)50 * (size_t)(1024 * 1024)) /* 50 MiB */
25 
26 #define CHECK_PATH(_z_) INFO_OUT("DEBUG: [%s %d]: %s() -> %s\n", basename2(__FILE__), __LINE__, __func__, _z_)
27 
28 #define LG_MESH_IS_RH 1
29 #define LG_MESH_IS_LH 2
30 
31 /* Mesh original format, ie from OBJ/FBX file or whatever */
32 typedef enum {
33  LG_MESH_OBJ,
34  LG_MESH_FBX,
35  LG_MESH_TERRAIN,
36  LG_MESH_SKYBOX,
37  LG_MESH_OTHER
38 } lg_mesh_type;
39 
40 /*
41 - Use LG_String for LG_Material ?
42  typedef struct {
43  char name[64]; -> LG_String ?
44  uint32_t indice;
45  LG_Texture *texture;
46  char tex_path[128]; -> LG_String ?
47  } LG_Material;
48 
49  sizeof(LG_Terrain) = 2.0 MiB <- big elevation array
50  sizeof(LG_Mesh) = 52.7 KiB -> 7.0 KiB - with LG_String
51  sizeof(LG_DrawElements_Cmd) = 6.0 KiB
52  sizeof(LG_SceneNode) = 2.6 KiB
53  sizeof(LG_Env) = 1.4 KiB
54  sizeof(LG_ErrorContext) = 1.1 KiB
55  sizeof(LG_Material) = 208 bytes
56  sizeof(LG_Material2) = 28 bytes
57 
58  MATS_WIH_LG_STRING
59  name ----> name->data
60  tex_path ----> tex_path->data
61 
62  is_lg_string()
63  lg_string_new()
64  lg_string_get_data()
65  lg_string_remove()
66 
67  mesh->materials[i].name)
68  mesh->materials[i].tex_path
69 
70  BUT (BIG ONE): strings will be lost when mesh is saved to file
71  Or using these funcs ?
72  zboolean lg_string_save_all_to_file(const char *)
73  zboolean lg_string_load_all_from_file(const char *)
74 */
75 
76 typedef struct /*__attribute__((packed, aligned(4)))*/ {
77  LG_String *name;
78  uint32_t indice; // Material face indice in VBO (uint32_t with GL_OES_element_index_uint extension)
79  LG_Texture *texture;
80  LG_String *tex_path; // Used by lg_mesh_reload_mats()
81 } LG_Material;
82 
83 /* Axis-aligned bounding box */
84 typedef struct {
85  float min_x;
86  float max_x;
87  float min_y;
88  float max_y;
89  float min_z;
90  float max_z;
91 } LG_BBox;
92 
93 /*
94  * === Triangle mesh ===
95  *
96  * We want the same type sizes in binary files on all suppored platforms
97  * (ie Linux and Android so far) to avoid portability issues, so we now
98  * use fixed-size types (uint32_t, int32_t, ...) a lot for vertices structs
99  * and buffer objects.
100  *
101  * In OBJ file:
102  * - 'mtllib' -> obj_file.mtl
103  * - 'usemtl' -> material name
104  *
105  * In MTL file:
106  * - 'newmtl' -> material name
107  * - 'map_K*' -> texture path
108  */
109 typedef struct {
110  int32_t type; /* lg_mesh_type */
111  char name[LG_MESH_NAME_MAX_LEN + 1]; /* OBJ/FBX basename */
112  zboolean skinned;
113  /* Generated VBO and IBO */
114  union {
115  Vertex_uv_n *vbo_data; /* VBO (with interleaved vertex data) - sizeof(Vertex_uv_n) = 24 */
116  Vertex_uvn_iw *vbo_data_iw; /* VBO (with interleaved vertex data) - sizeof(Vertex_uvn_iw) = 36 */
117  };
118  uint32_t *ibo_data; /* IBO (indices start at 0) - sizeof(uint32_t) = 4 */
119  uint32_t vbo_size; /* previously size_t */
120  uint32_t ibo_size; /* previously size_t */
121  /* Read from obj file */
122  int32_t n_v; /* Num of coords vertices */
123  int32_t n_vt; /* Num of texture coords */
124  int32_t n_vn; /* Num of normals */
125  int32_t n_f; /* Num of faces */
126  /* Materials */
127  char mtl_file[LG_MESH_NAME_MAX_LEN + 1]; /* The MTL file path */
128  int32_t n_mat; /* Num of materials (with textures) in MTL file */
129  int32_t n_usemtl; /* Num of usemtl tags in OBJ file */
130  LG_Material materials[N_MAX_USEMTL_PER_OBJ]; /* Materials referenced by usemtl tags */
131  LG_BBox bbox;
132  LG_Cuboid b_cuboid; /* Bounding cuboid */
133  Lines3D_VB b_cuboid_l3d_vb; /* Lines3D_VB from bounding box cuboid */
134  zboolean xyz_normalized;
135  double normalize_k;
136  zboolean horiz_centered; /* Horizontally centered -> x, z*/
137  zboolean vert_centered; /* Vertically centered -> y */
138  zboolean vert_bottom; /* Vertically at bottom -> y - set mesh origin at bottom, override do_vert_center */
139 } LG_Mesh;
140 
141 LG_Mesh *lg_load_vbo(const char *, zboolean, zboolean, zboolean, zboolean, zboolean, zboolean);
142 
143 LG_Mesh *lg_mesh_new_from_objfile(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
144 
145 LG_Mesh *lg_mesh_new_from_fbxfile(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
146 
147 void lg_mesh_free(LG_Mesh *);
148 
149 void lg_mesh_info(LG_Mesh *);
150 
151 int lg_obj_file_save_to_cache(void *, const char *, size_t, size_t *);
152 
153 void *lg_obj_file_open_from_cache(const char *);
154 
155 int lg_vbo_save_to_file(const char *, LG_Mesh *);
156 
157 int lg_vbo_load_from_file(const char *, LG_Mesh **);
158 
160 
161 #endif /* LG_MESH_H */
LG_Mesh
Definition: lg_mesh.h:73
LG_Cuboid
Definition: lg_3d_primitives.h:64
lg_obj_file_open_from_cache
void * lg_obj_file_open_from_cache(const char *file_name)
Definition: lg_mesh.c:349
lg_vbo_load_from_file
int lg_vbo_load_from_file(const char *path, LG_Mesh **mesh)
Definition: lg_mesh.c:420
Vertex_uv_n
Definition: lg_vertex.h:46
LG_BBox
Definition: lg_mesh.h:48
Lines3D_VB
Definition: lg_3d_primitives.h:53
lg_mesh_new_from_fbxfile
LG_Mesh * lg_mesh_new_from_fbxfile(const char *path, zboolean invert_z, zboolean do_normalize_xyz, zboolean do_horiz_center, zboolean do_vert_center, zboolean do_vert_bottom)
Definition: lg_mesh.c:203
lg_mesh_free
void lg_mesh_free(LG_Mesh *mesh)
Definition: lg_mesh.c:213
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)
Definition: lg_mesh.c:318
LG_String
Definition: lg_string.h:17
lg_mesh_new_from_objfile
LG_Mesh * lg_mesh_new_from_objfile(const char *path, zboolean invert_z, zboolean do_normalize_xyz, zboolean do_horiz_center, zboolean do_vert_center, zboolean do_vert_bottom)
Definition: lg_mesh.c:133
LG_Material
Definition: lg_mesh.h:40
lg_mesh_reload_mats
int lg_mesh_reload_mats(LG_Mesh *mesh)
Definition: lg_mesh.c:497
lg_load_vbo
LG_Mesh * lg_load_vbo(const char *path, zboolean force_reload_obj, zboolean invert_z, zboolean do_normalize_xyz, zboolean do_horiz_center, zboolean do_vert_center, zboolean do_vert_bottom)
Definition: lg_mesh.c:89
lg_mesh_info
void lg_mesh_info(LG_Mesh *mesh)
Definition: lg_mesh.c:238
LG_Texture
Definition: lg_textures.h:42
lg_vbo_save_to_file
int lg_vbo_save_to_file(const char *path, LG_Mesh *mesh)
Definition: lg_mesh.c:374
Vertex_uvn_iw
Definition: lg_vertex.h:62