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 MAT_NAME_MAX_LEN (64 - 1)
10
11#define N_NEWMTL_USEMTL_TAGS_MAX 4096
12
13#define BINARY_MESH_EXT "bmesh"
14
15#define LG_FLOAT_EPSILON (1e-6) /* 1e-6 or 1e-8 ? */
16#define MAX_U32BIT_VALUE 4294967296
17
18#define LG_MESH_IS_RH 1
19#define LG_MESH_IS_LH 2
20
21/*
22 * If GL ES version < 3.0, need GL_OES_element_index_uint extension for UNSIGNED_INT (uint32_t) IBO indices
23 */
24
25/* Mesh original format, ie from OBJ/FBX file or whatever */
26typedef enum {
27 LG_MESH_OBJ,
28 LG_MESH_FBX,
29 LG_MESH_TERRAIN,
30 LG_MESH_OTHER
31} lg_mesh_type;
32
33/* Actually name and texture - misleading */
34typedef struct {
35 char name[MAT_NAME_MAX_LEN + 1];
36 LG_Texture *texture;
37 LG_Path tex_path;
39
40/* Axis-aligned bounding box */
41typedef union {
42 struct {
43 float min_x;
44 float max_x;
45 float min_y;
46 float max_y;
47 float min_z;
48 float max_z;
49 };
50 #if 0
51 /* LG_BBox_v2 ? */
52 struct {
53 vec3_t center;
54 float extents;
55 };
56 /* LG_BBox_v3 ? */
57 struct {
58 vec3_t min;
59 vec3_t max;
60 };
61 #endif
62} LG_BBox;
63
64/*
65 * === Triangle mesh ===
66 *
67 * We want the same type sizes in binary files on all suppored platforms
68 * (ie Linux and Android so far) to avoid portability issues, so we now
69 * use fixed-size types (uint32_t, int32_t, ...) a lot for vertices structs
70 * and buffer objects.
71 *
72 * In OBJ file:
73 * - 'mtllib' -> obj_file.mtl
74 * - 'usemtl' -> material name
75 *
76 * In MTL file:
77 * - 'newmtl' -> material name
78 * - 'map_K*' -> texture path
79 */
80typedef struct {
81 int32_t type; /* lg_mesh_type = mesh original format, ie from OBJ/FBX file or whatever */
82 LG_Path path; /* Path to MESH file (OBJ/FBX/BMESH) */
83 zboolean16 __unused__; /* Kept for backward binary compatibility and enventual futur use */
84 zboolean16 skinned;
85 /* Generated VBO and IBO */
86 union {
87 Vertex_uv_n *vbo_data; /* VBO (with interleaved vertex data) - sizeof(Vertex_uv_n) = 24 */
88 Vertex_uvn_iw *vbo_data_iw; /* VBO (with interleaved vertex data) - sizeof(Vertex_uvn_iw) = 36 */
89 //Vertex_rgba_n *vbo_data_rgba_n; /* -> TO ASSIGN COLORS TO ELEVATIONS - sizeof(Vertex_rgba_n) = 24 */
90 };
91 uint32_t *ibo_data; /* IBO (indices start at 0) - sizeof(uint32_t) = 4 */
92 uint32_t vbo_size; /* previously size_t */
93 uint32_t ibo_size; /* previously size_t */
94 /* Read from OBJ file */
95 int32_t n_v; /* Num of coords vertices */
96 int32_t n_vt; /* Num of texture coords */
97 int32_t n_vn; /* Num of normals */
98 int32_t n_f; /* Num of faces */
99 char mtl_filename[LG_PATH_MAX_LEN + 1]; /* MTL file name - this file is expected to be in the same dir as the obj file */
100 int32_t n_usemtl; /* Num of 'usemtl' tags in OBJ file */
101 /* Read from MTL file */
102 int32_t n_mat; /* Num of 'newmtl'/'map_K' tags (materials with referenced textures) in MTL file */
103 LG_Material materials[N_NEWMTL_USEMTL_TAGS_MAX]; /* Material's 'lib' (LG_Material = name and texture) */
104 uint32_t i_mat_in_vbo[N_NEWMTL_USEMTL_TAGS_MAX]; /* Material face indice in VBO, referenced by 'usemtl' tags */
105 uint32_t i_mat_in_lib[N_NEWMTL_USEMTL_TAGS_MAX]; /* Texture indice in materials[N_NEWMTL_USEMTL_TAGS_MAX] */
106 LG_BBox bbox;
107 LG_Cuboid b_cuboid; /* Bounding cuboid */
108 Lines3D_VB b_cuboid_l3d_vb; /* Lines3D_VB from bounding cuboid */
109 zboolean xyz_normalized;
110 double normalize_k;
111 zboolean horiz_centered; /* Horizontally centered -> x, z*/
112 zboolean vert_centered; /* Vertically centered -> y */
113 zboolean vert_bottom; /* Vertically at bottom -> y - set mesh origin at bottom, override do_vert_center */
114} LG_Mesh;
115
116/* Mesh loading flags */
117typedef struct {
118 zboolean save_as_bmesh_linux; /* If OBJ or FBX loaded from assets, save as a BMESH asset, only on Linux */
119 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 */
120 zboolean normalize_xyz;
121 zboolean horiz_center;
122 zboolean vert_center; /* Center mesh vertically, if vert_bottom is not set */
123 zboolean vert_bottom; /* Set mesh origin vertically at bottom, override vert_center */
125
126/* Helper #define's */
127#define SAVE_AS_BMESH_LINUX TRUE
128#define INVERT_Z TRUE
129#define NORMALIZE_XYZ TRUE
130#define HORIZ_CENTER TRUE
131#define VERT_CENTER TRUE
132#define VERT_BOTTOM TRUE
133
134/*
135 * BINARY MESH (.bmesh) file format
136 * ================================
137 * - Header = Serializable_LG_Mesh
138 * -> header_size = lg_align_up_to_next_4_bytes_boundary((void *)sizeof(Serializable_LG_Mesh))
139 * - VBO data block - aligned to 4 bytes boundaries
140 * -> starting at header_size
141 * - IBO data block - aligned to 4 bytes boundaries
142 * -> starting at header_size + lg_align_up_to_next_4_bytes_boundary((void *)(uint64_t)mesh->vbo_size)
143 */
144
145/*
146 * === Structs for serialization ===
147 * We convert pointers to two uint32_t values:
148 * Vertex_uv_n *vbo_data
149 * ->
150 * uint32_t vbo_data_big;
151 * uint32_t vbo_data_little;
152 *
153 */
154
155typedef struct {
156 char name[MAT_NAME_MAX_LEN + 1];
157 uint32_t texture_big; /* Pointer value as 2 uint32_t */
158 uint32_t texture_little;
159 LG_Path tex_path;
161
162typedef struct {
163 int32_t type;
164 LG_Path path;
165 zboolean16 __unused__; /* Kept for backward binary compatibility and enventual futur use */
166 zboolean16 skinned;
167 uint32_t vbo_data_big; /* Pointer value as 2 uint32_t */
168 uint32_t vbo_data_little;
169 uint32_t ibo_data_big; /* Pointer value as 2 uint32_t */
170 uint32_t ibo_data_little;
171 uint32_t vbo_size;
172 uint32_t ibo_size;
173 int32_t n_v;
174 int32_t n_vt;
175 int32_t n_vn;
176 int32_t n_f;
177 char mtl_filename[LG_PATH_MAX_LEN + 1];
178 int32_t n_usemtl;
179 int32_t n_mat;
180 Serializable_LG_Material materials[N_NEWMTL_USEMTL_TAGS_MAX];
181 uint32_t i_mat_in_vbo[N_NEWMTL_USEMTL_TAGS_MAX];
182 uint32_t i_mat_in_lib[N_NEWMTL_USEMTL_TAGS_MAX];
183 LG_BBox bbox;
184 LG_Cuboid b_cuboid;
185 Lines3D_VB b_cuboid_l3d_vb;
186 zboolean xyz_normalized;
187 double normalize_k;
188 zboolean horiz_centered;
189 zboolean vert_centered;
190 zboolean vert_bottom;
192
194
195LG_Mesh *lg_mesh_new_from_obj(LG_Path, zboolean, zboolean, zboolean, zboolean, zboolean);
196
197LG_Mesh *lg_mesh_new_from_fbx(LG_Path, zboolean, zboolean, zboolean, zboolean, zboolean);
198
199void lg_mesh_free(LG_Mesh *);
200
201void lg_mesh_free_v2(LG_Mesh **);
202
204
206
208
210
212
214
216
217LG_LoadMesh_Flags lg_loadmesh_flags(zboolean, zboolean, zboolean, zboolean, zboolean, zboolean);
218
220
221void lg_mesh_info(LG_Mesh *, int);
222
223char *lg_mesh_info_to_string(LG_Mesh *, int);
224
225char *lg_replace_file_extension(const char *, const char *);
226
227#endif /* LG_MESH_H */
LG_Mesh * lg_mesh_new_from_obj(LG_Path path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:177
void lg_mesh_info(LG_Mesh *mesh, int show_first_n)
Definition lg_mesh.c:622
int lg_mesh_reload_mats(LG_Mesh *mesh)
Definition lg_mesh.c:425
void lg_mesh_free_v2(LG_Mesh **mesh)
Definition lg_mesh.c:244
LG_Mesh * lg_mesh_new_from_fbx(LG_Path path, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:203
LG_Mesh * lg_bmesh_load_from_file(LG_Path path)
Definition lg_mesh.c:333
void lg_serialize_LG_Material_64_to_32(Serializable_LG_Material *s_mat, LG_Material *mat)
Definition lg_mesh.c:447
LG_Mesh * lg_mesh_load(LG_Path path, LG_LoadMesh_Flags flags)
Definition lg_mesh.c:97
void lg_unserialize_LG_Material_32_to_64(LG_Material *mat, Serializable_LG_Material *s_mat)
Definition lg_mesh.c:462
void lg_mesh_override_bounding_cuboid_color(LG_Mesh *mesh, LG_Color_u color)
Definition lg_mesh.c:608
void lg_unserialize_LG_Mesh_32_to_64(LG_Mesh *mesh, Serializable_LG_Mesh *s_mesh)
Definition lg_mesh.c:525
void lg_serialize_LG_Mesh_64_to_32(Serializable_LG_Mesh *s_mesh, LG_Mesh *mesh)
Definition lg_mesh.c:476
char * lg_mesh_info_to_string(LG_Mesh *mesh, int show_first_n)
Definition lg_mesh.c:721
LG_LoadMesh_Flags lg_loadmesh_flags(zboolean save_as_bmesh_linux, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition lg_mesh.c:586
void lg_mesh_free(LG_Mesh *mesh)
Definition lg_mesh.c:213
int lg_bmesh_save_to_file(LG_Path path, LG_Mesh *mesh)
Definition lg_mesh.c:269
char * lg_replace_file_extension(const char *path, const char *new_ext)
Definition lg_mesh.c:879
Definition lg_vertex.h:111
Definition lg_3d_primitives.h:67
Definition lg_mesh.h:117
Definition lg_mesh.h:34
Definition lg_mesh.h:80
Definition lg_goodies.h:20
Definition lg_textures.h:48
Definition lg_3d_primitives.h:53
Definition lg_mesh.h:155
Definition lg_mesh.h:162
Definition lg_vertex.h:46
Definition lg_vertex.h:62
Definition math_3d.c:135
Definition lg_mesh.h:41