LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
lg_mesh.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_APP_CACHE "cache"
17 #define LG_VBO_CACHE LG_APP_CACHE "/vbo"
18 #define LG_OBJ_CACHE LG_APP_CACHE "/obj" /* Only used on Android so far */
19 
20 #define MAX_FILE_EXT_LEN 64 /* Extension length must not exceed that */
21 #define LG_MESH_INFO_EXT ".info"
22 #define LG_MESH_VBO_EXT ".vbo"
23 #define LG_MESH_IBO_EXT ".ibo"
24 
25 #define LG_OBJ_FILE_MAXSIZE ((size_t)50 * (size_t)(1024 * 1024)) /* 50 MiB */
26 
27 #define CHECK_PATH(_z_) INFO_OUT("DEBUG: [%s %d]: %s() -> %s\n", basename2(__FILE__), __LINE__, __func__, _z_)
28 
29 #define LG_MESH_IS_RH 1
30 #define LG_MESH_IS_LH 2
31 
32 /* Mesh original format, ie from OBJ/FBX file or whatever */
33 typedef enum {
34  LG_MESH_OBJ,
35  LG_MESH_FBX,
36  //LG_MESH_VBO, // With associated IBO and INFO files
37  LG_MESH_TERRAIN,
38  LG_MESH_SKYBOX,
39  LG_MESH_OTHER
40 } lg_mesh_type;
41 
42 typedef struct {
43  char name[MAT_NAME_MAX_LEN + 1];
44  uint32_t indice; /* Material face indice in VBO (uint32_t with GL_OES_element_index_uint extension) */
45  LG_Texture *texture;
46  char tex_path[LG_TEX_PATH_MAX_LEN + 1]; /* Used by lg_mesh_reload_mats() */
47 } LG_Material;
48 
49 /* Axis-aligned bounding box */
50 typedef union {
51  struct {
52  float min_x;
53  float max_x;
54  float min_y;
55  float max_y;
56  float min_z;
57  float max_z;
58  };
59  #if 0
60  /* LG_BBox_v2 ? */
61  struct {
62  vec3_t center;
63  float extents;
64  };
65  /* LG_BBox_v3 ? */
66  struct {
67  vec3_t min;
68  vec3_t max;
69  };
70  #endif
71 } LG_BBox;
72 
73 /*
74  * === Triangle mesh ===
75  *
76  * We want the same type sizes in binary files on all suppored platforms
77  * (ie Linux and Android so far) to avoid portability issues, so we now
78  * use fixed-size types (uint32_t, int32_t, ...) a lot for vertices structs
79  * and buffer objects.
80  *
81  * In OBJ file:
82  * - 'mtllib' -> obj_file.mtl
83  * - 'usemtl' -> material name
84  *
85  * In MTL file:
86  * - 'newmtl' -> material name
87  * - 'map_K*' -> texture path
88  */
89 typedef struct {
90  int32_t type; /* lg_mesh_type */
91  char name[LG_MESH_NAME_MAX_LEN + 1]; /* OBJ/FBX basename */
92  zboolean skinned;
93  /* Generated VBO and IBO */
94  union {
95  Vertex_uv_n *vbo_data; /* VBO (with interleaved vertex data) - sizeof(Vertex_uv_n) = 24 */
96  Vertex_uvn_iw *vbo_data_iw; /* VBO (with interleaved vertex data) - sizeof(Vertex_uvn_iw) = 36 */
97  //Vertex_rgba_n *vbo_data_rgba_n; // -> TO ASSIGN COLORS TO ELEVATIONS
98  };
99  uint32_t *ibo_data; /* IBO (indices start at 0) - sizeof(uint32_t) = 4 */
100  uint32_t vbo_size; /* previously size_t */
101  uint32_t ibo_size; /* previously size_t */
102  /* Read from obj file */
103  int32_t n_v; /* Num of coords vertices */
104  int32_t n_vt; /* Num of texture coords */
105  int32_t n_vn; /* Num of normals */
106  int32_t n_f; /* Num of faces */
107  /* Materials */
108  char mtl_file[LG_MESH_NAME_MAX_LEN + 1]; /* The MTL file path */
109  int32_t n_mat; /* Num of materials (with textures) in MTL file */
110  int32_t n_usemtl; /* Num of usemtl tags in OBJ file */
111  LG_Material materials[N_MAX_USEMTL_PER_OBJ]; /* Materials referenced by usemtl tags */
112  LG_BBox bbox;
113  LG_Cuboid b_cuboid; /* Bounding cuboid */
114  Lines3D_VB b_cuboid_l3d_vb; /* Lines3D_VB from bounding box cuboid */
115  zboolean xyz_normalized;
116  double normalize_k;
117  zboolean horiz_centered; /* Horizontally centered -> x, z*/
118  zboolean vert_centered; /* Vertically centered -> y */
119  zboolean vert_bottom; /* Vertically at bottom -> y - set mesh origin at bottom, override do_vert_center */
120 } LG_Mesh;
121 
122 /* Mesh loading flags */
123 typedef struct {
124  zboolean force_reload; /* Force (re)loading and (re)parsing of the OBJ file */
125  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 */
126  zboolean normalize_xyz;
127  zboolean horiz_center;
128  zboolean vert_center; /* Center mesh vertically, if vert_bottom is not set */
129  zboolean vert_bottom; /* Set mesh origin vertically at bottom, override vert_center */
131 
132 /* Helper #define's */
133 #define FORCE_RELOAD TRUE
134 #define INVERT_Z TRUE
135 #define NORMALIZE_XYZ TRUE
136 #define HORIZ_CENTER TRUE
137 #define VERT_CENTER TRUE
138 #define VERT_BOTTOM TRUE
139 
140 LG_Mesh *lg_mesh_load(const char *, LG_LoadMesh_Flags);
141 
142 LG_Mesh *lg_mesh_new_from_objfile(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
143 
144 LG_Mesh *lg_mesh_new_from_fbxfile(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
145 
146 void lg_mesh_free(LG_Mesh *);
147 
148 void lg_mesh_info(LG_Mesh *);
149 
150 int lg_obj_file_save_to_cache(void *, const char *, size_t, size_t *);
151 
152 void *lg_obj_file_open_from_cache(const char *);
153 
154 int lg_vbo_save_to_file(const char *, LG_Mesh *);
155 
156 int lg_vbo_save_to_file_in_cache(const char *, LG_Mesh *);
157 
158 int lg_vbo_load_from_file(const char *, LG_Mesh **);
159 
160 int lg_vbo_load_from_file_in_cache(const char *, LG_Mesh **);
161 
163 
164 LG_LoadMesh_Flags lg_loadmesh_flags(zboolean, zboolean, zboolean, zboolean, zboolean, zboolean);
165 
166 #endif /* LG_MESH_H */
LG_Mesh
Definition: lg_mesh.h:89
lg_vbo_load_from_file
int lg_vbo_load_from_file(const char *file_name, LG_Mesh **mesh)
Definition: lg_mesh.c:531
LG_LoadMesh_Flags
Definition: lg_mesh.h:123
LG_Cuboid
Definition: lg_3d_primitives.h:64
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)
Definition: lg_mesh.c:136
lg_obj_file_open_from_cache
void * lg_obj_file_open_from_cache(const char *file_name)
Definition: lg_mesh.c:354
lg_vbo_load_from_file_in_cache
int lg_vbo_load_from_file_in_cache(const char *file_name, LG_Mesh **mesh)
Definition: lg_mesh.c:543
Vertex_uv_n
Definition: lg_vertex.h:46
Lines3D_VB
Definition: lg_3d_primitives.h:53
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
vec3_t
Definition: math_3d.h:123
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:319
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)
Definition: lg_mesh.c:596
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)
Definition: lg_mesh.c:204
LG_Material
Definition: lg_mesh.h:42
lg_vbo_save_to_file
int lg_vbo_save_to_file(const char *file_name, LG_Mesh *mesh)
Definition: lg_mesh.c:425
lg_mesh_reload_mats
int lg_mesh_reload_mats(LG_Mesh *mesh)
Definition: lg_mesh.c:561
lg_vbo_save_to_file_in_cache
int lg_vbo_save_to_file_in_cache(const char *file_name, LG_Mesh *mesh)
Definition: lg_mesh.c:440
lg_mesh_info
void lg_mesh_info(LG_Mesh *mesh)
Definition: lg_mesh.c:239
LG_Texture
Definition: lg_textures.h:45
Vertex_uvn_iw
Definition: lg_vertex.h:62
LG_BBox
Definition: lg_mesh.h:50