LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2026 ETMSoftware
Loading...
Searching...
No Matches
lg_mesh.h
1/*
2 * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2026
3 * All rights reserved
4 */
5
6#ifndef LG_MESH_H
7#define LG_MESH_H
8
9#define LG_MESH_NAME_MAX_LEN (512 - 1)
10#define LG_MESH_FULLPATH_MAX_LEN (512 - 1)
11#define MAT_NAME_MAX_LEN (64 - 1)
12#define TEX_FILENAME_MAX_LEN (512 - 1) /* = LG_TEX_PATH_MAX_LEN */
13
14#define N_MAT_MAX 64
15#define N_USEMTL_MAX 4096
16
17#define BINARY_MESH_EXT "bmesh"
18#define LG_BMESH_HEADER_SIZE 65536 /* 64 KiB - make sure it's < sizeof(LG_Mesh) */
19
20#define LG_FLOAT_EPSILON (1e-6) /* 1e-6 or 1e-8 ? */
21#define MAX_U32BIT_VALUE 4294967296
22
23#define CHECK_PATH(_z_) INFO_OUT("DEBUG: [%s %d]: %s() -> %s\n", basename2(__FILE__), __LINE__, __func__, _z_)
24
25#define LG_MESH_IS_RH 1
26#define LG_MESH_IS_LH 2
27
28/*
29 * If GL ES version < 3.0, need GL_OES_element_index_uint extension for UNSIGNED_INT (uint32_t) IBO indices
30 */
31
32/* Mesh original format, ie from OBJ/FBX file or whatever */
33typedef enum {
34 LG_MESH_OBJ,
35 LG_MESH_FBX,
36 LG_MESH_TERRAIN,
37 LG_MESH_OTHER
38} lg_mesh_type;
39
40/* Actually name and texture - misleading */
41typedef struct {
42 char name[MAT_NAME_MAX_LEN + 1];
43 LG_Texture *texture;
44 char tex_full_path[TEX_FILENAME_MAX_LEN + 1]; /* Full path to image file */
46
47/* Axis-aligned bounding box */
48typedef union {
49 struct {
50 float min_x;
51 float max_x;
52 float min_y;
53 float max_y;
54 float min_z;
55 float max_z;
56 };
57 #if 0
58 /* LG_BBox_v2 ? */
59 struct {
60 vec3_t center;
61 float extents;
62 };
63 /* LG_BBox_v3 ? */
64 struct {
65 vec3_t min;
66 vec3_t max;
67 };
68 #endif
69} LG_BBox;
70
71/*
72 * === Triangle mesh ===
73 *
74 * We want the same type sizes in binary files on all suppored platforms
75 * (ie Linux and Android so far) to avoid portability issues, so we now
76 * use fixed-size types (uint32_t, int32_t, ...) a lot for vertices structs
77 * and buffer objects.
78 *
79 * In OBJ file:
80 * - 'mtllib' -> obj_file.mtl
81 * - 'usemtl' -> material name
82 *
83 * In MTL file:
84 * - 'newmtl' -> material name
85 * - 'map_K*' -> texture path
86 */
87typedef struct {
88 int32_t type; /* lg_mesh_type = mesh original format, ie from OBJ/FBX file or whatever */
89 char full_path[LG_MESH_FULLPATH_MAX_LEN + 1]; /* Full path of MESH file (OBJ/FBX/BMESH) */
90 zboolean skinned;
91 /* Generated VBO and IBO */
92 union {
93 Vertex_uv_n *vbo_data; /* VBO (with interleaved vertex data) - sizeof(Vertex_uv_n) = 24 */
94 Vertex_uvn_iw *vbo_data_iw; /* VBO (with interleaved vertex data) - sizeof(Vertex_uvn_iw) = 36 */
95 //Vertex_rgba_n *vbo_data_rgba_n; /* -> TO ASSIGN COLORS TO ELEVATIONS - sizeof(Vertex_rgba_n) = 24 */
96 };
97 uint32_t *ibo_data; /* IBO (indices start at 0) - sizeof(uint32_t) = 4 */
98 uint32_t vbo_size; /* previously size_t */
99 uint32_t ibo_size; /* previously size_t */
100 /* Read from OBJ file */
101 int32_t n_v; /* Num of coords vertices */
102 int32_t n_vt; /* Num of texture coords */
103 int32_t n_vn; /* Num of normals */
104 int32_t n_f; /* Num of faces */
105 char mtl_file[LG_MESH_FULLPATH_MAX_LEN + 1]; /* The MTL file path */
106 int32_t n_usemtl; /* Num of 'usemtl' tags in OBJ file */
107 /* Read from MTL file */
108 int32_t n_mat; /* Num of 'newmtl'/'map_K' tags (materials with referenced textures) in MTL file */
109 LG_Material materials[N_MAT_MAX]; /* Material's 'lib' (LG_Material = name and texture) */
110 uint32_t i_mat_in_vbo[N_USEMTL_MAX]; /* Material face indice in VBO, referenced by 'usemtl' tags */
111 uint32_t i_mat_in_lib[N_MAT_MAX]; /* Texture indice in materials[N_MAT_MAX] */
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 */
123typedef struct {
124 zboolean save_to_assets; /* Save to assets as BMESH, otherwise to cache - on Android, should always be set to FALSE as you can't write to assets */
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 SAVE_TO_ASSETS 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/*
141 * BINARY MESH (.bmesh) file format
142 * ================================
143 * - Header = Serializable_LG_Mesh
144 * -> header_size = lg_align_up_to_next_4_bytes_boundary((void *)sizeof(Serializable_LG_Mesh))
145 * - VBO data block - aligned to 4 bytes boundaries
146 * -> starting at header_size
147 * - IBO data block - aligned to 4 bytes boundaries
148 * -> starting at header_size + lg_align_up_to_next_4_bytes_boundary((void *)(uint64_t)mesh->vbo_size)
149 */
150
151/*
152 * === Structs for serialization ===
153 * We convert pointers to two uint32_t values:
154 * Vertex_uv_n *vbo_data
155 * ->
156 * uint32_t vbo_data_big;
157 * uint32_t vbo_data_little;
158 *
159 */
160
161typedef struct {
162 char name[MAT_NAME_MAX_LEN + 1];
163 uint32_t texture_big; /* Pointer value as 2 uint32_t */
164 uint32_t texture_little;
165 char tex_full_path[TEX_FILENAME_MAX_LEN + 1];
167
168typedef struct {
169 int32_t type;
170 char full_path[LG_MESH_FULLPATH_MAX_LEN + 1];
171 zboolean skinned;
172 uint32_t vbo_data_big; /* Pointer value as 2 uint32_t */
173 uint32_t vbo_data_little;
174 uint32_t ibo_data_big; /* Pointer value as 2 uint32_t */
175 uint32_t ibo_data_little;
176 uint32_t vbo_size;
177 uint32_t ibo_size;
178 int32_t n_v;
179 int32_t n_vt;
180 int32_t n_vn;
181 int32_t n_f;
182 char mtl_file[LG_MESH_FULLPATH_MAX_LEN + 1];
183 int32_t n_usemtl;
184 int32_t n_mat;
185 Serializable_LG_Material materials[N_MAT_MAX];
186 uint32_t i_mat_in_vbo[N_USEMTL_MAX];
187 uint32_t i_mat_in_lib[N_MAT_MAX];
188 LG_BBox bbox;
189 LG_Cuboid b_cuboid;
190 Lines3D_VB b_cuboid_l3d_vb;
191 zboolean xyz_normalized;
192 double normalize_k;
193 zboolean horiz_centered;
194 zboolean vert_centered;
195 zboolean vert_bottom;
197
199
200LG_Mesh *lg_mesh_new_from_obj(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
201
202LG_Mesh *lg_mesh_new_from_obj_relpath(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
203
204LG_Mesh *lg_mesh_new_from_fbx(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
205
206LG_Mesh *lg_mesh_new_from_fbx_relpath(const char *, zboolean, zboolean, zboolean, zboolean, zboolean);
207
208void lg_mesh_free(LG_Mesh *);
209
210void lg_mesh_free_v2(LG_Mesh **);
211
212int lg_bmesh_save_to_file(const char *, LG_Mesh *);
213
214int lg_bmesh_load_from_file(const char *, LG_Mesh **);
215
217
219
221
223
225
226LG_LoadMesh_Flags lg_loadmesh_flags(zboolean, zboolean, zboolean, zboolean, zboolean, zboolean);
227
228void lg_mesh_info(LG_Mesh *, int);
229
230char *lg_replace_file_extension(const char *, const char *);
231
232#endif /* LG_MESH_H */
void lg_mesh_info(LG_Mesh *mesh, int show_first_n)
Definition lg_mesh.c:646
LG_Mesh * lg_mesh_new_from_fbx(const char *full_path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:234
int lg_mesh_reload_mats(LG_Mesh *mesh)
Definition lg_mesh.c:464
void lg_mesh_free_v2(LG_Mesh **mesh)
Definition lg_mesh.c:291
LG_Mesh * lg_mesh_new_from_obj_relpath(const char *relative_path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:208
LG_Mesh * lg_mesh_new_from_fbx_relpath(const char *relative_path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:250
LG_LoadMesh_Flags lg_loadmesh_flags(zboolean save_to_assets, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:629
int lg_bmesh_load_from_file(const char *full_path, LG_Mesh **mesh)
Definition lg_mesh.c:375
void lg_serialize_LG_Material_64_to_32(Serializable_LG_Material *s_mat, LG_Material *mat)
Definition lg_mesh.c:486
void lg_unserialize_LG_Material_32_to_64(LG_Material *mat, Serializable_LG_Material *s_mat)
Definition lg_mesh.c:500
void lg_unserialize_LG_Mesh_32_to_64(LG_Mesh *mesh, Serializable_LG_Mesh *s_mesh)
Definition lg_mesh.c:565
void lg_serialize_LG_Mesh_64_to_32(Serializable_LG_Mesh *s_mesh, LG_Mesh *mesh)
Definition lg_mesh.c:513
LG_Mesh * lg_mesh_new_from_obj(const char *full_path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:182
int lg_bmesh_save_to_file(const char *full_path, LG_Mesh *mesh)
Definition lg_mesh.c:316
void lg_mesh_free(LG_Mesh *mesh)
Definition lg_mesh.c:260
char * lg_replace_file_extension(const char *path, const char *new_ext)
Definition lg_mesh.c:751
LG_Mesh * lg_mesh_load(const char *full_path, LG_LoadMesh_Flags flags)
Definition lg_mesh.c:104
Definition lg_3d_primitives.h:67
Definition lg_mesh.h:123
Definition lg_mesh.h:41
Definition lg_mesh.h:87
Definition lg_textures.h:45
Definition lg_3d_primitives.h:53
Definition lg_mesh.h:161
Definition lg_mesh.h:168
Definition lg_vertex.h:46
Definition lg_vertex.h:62
Definition math_3d.h:123
Definition lg_mesh.h:48