LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_mesh.h
1 /*
2  * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2024
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 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 /* NEW - not really used so far */
29 typedef enum {
30  LG_MESH_OBJ,
31  LG_MESH_TERRAIN,
32  LG_MESH_SKYBOX,
33  LG_MESH_OTHER
34 } lg_mesh_type;
35 
36 typedef struct {
37  char name[MAT_NAME_MAX_LEN + 1];
38  uint32_t indice; /* Material face indice in VBO (uint32_t with GL_OES_element_index_uint extension) */
39  LG_Texture *texture;
40 } LG_Material;
41 
42 /* Axis-aligned bounding box */
43 typedef struct {
44  float min_x;
45  float max_x;
46  float min_y;
47  float max_y;
48  float min_z;
49  float max_z;
50 } LG_BBox;
51 
52 /*
53  * === Triangle mesh ===
54  *
55  * We want the same type sizes in binary files on all suppored platforms
56  * (ie Linux and Android so far) to avoid portability issues, so we now
57  * use fixed-size types (uint32_t, int32_t, ...) a lot for vertices structs
58  * and buffer objects.
59  *
60  * In OBJ file:
61  * - 'mtllib' -> obj_file.mtl
62  * - 'usemtl' -> material name
63  *
64  * In MTL file:
65  * - 'newmtl' -> material name
66  * - 'map_K*' -> texture path
67  */
68 typedef struct {
69  lg_mesh_type type; /* NEW - not really used so far */
70  char name[LG_MESH_NAME_MAX_LEN + 1]; /* Originally the OBJ file pathname */
71  /* Generated VBO and IBO */
72  Vertex_uv_n *vbo_data; /* VBO (with interleaved vertex data) - sizeof(Vertex_uv_n) = 24 */
73  uint32_t *ibo_data; /* IBO (indices start at 0) - sizeof(uint32_t) = 4 */
74  uint32_t vbo_size; /* previously size_t */
75  uint32_t ibo_size; /* previously size_t */
76  /* Read from obj file */
77  int32_t n_v; /* Num of coords vertices */
78  int32_t n_vt; /* Num of texture coords */
79  int32_t n_vn; /* Num of normals */
80  int32_t n_f; /* Num of faces */
81  /* Materials */
82  char mtl_file[LG_MESH_NAME_MAX_LEN + 1]; /* The MTL file path */
83  int32_t n_mat; /* Num of materials (with textures) in MTL file */
84  int32_t n_usemtl; /* Num of usemtl tags in OBJ file */
85  LG_Material materials[N_MAX_USEMTL_PER_OBJ]; /* Materials referenced by usemtl tags */
86  LG_BBox bbox;
87  LG_Cuboid b_cuboid; /* Bounding cuboid */
88  Lines3D_VB b_cuboid_l3d_vb; /* Lines3D_VB from bounding box cuboid */
89  zboolean xyz_normalized;
90  double normalize_k;
91 } LG_Mesh;
92 
93 LG_Mesh *lg_load_vbo(const char *, zboolean, zboolean);
94 
95 LG_Mesh *lg_mesh_new_from_objfile(const char *, zboolean);
96 
97 void lg_mesh_free(LG_Mesh *);
98 
99 void lg_mesh_info(LG_Mesh *);
100 
101 int lg_obj_file_save_to_cache(void *, const char *, size_t, size_t *);
102 
103 void *lg_obj_file_open_from_cache(const char *);
104 
105 int lg_vbo_save_to_file(const char *, LG_Mesh *);
106 
107 int lg_vbo_load_from_file(const char *, LG_Mesh **);
108 
109 #if 0
110 int lg_rebuild_vbo_and_ibo(LG_Mesh *);
111 #endif
112 
113 #endif /* LG_MESH_H */
LG_Mesh
Definition: lg_mesh.h:68
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:298
lg_vbo_load_from_file
int lg_vbo_load_from_file(const char *path, LG_Mesh **mesh)
Definition: lg_mesh.c:361
Vertex_uv_n
Definition: lg_vertex.h:44
LG_BBox
Definition: lg_mesh.h:43
Lines3D_VB
Definition: lg_3d_primitives.h:53
lg_mesh_new_from_objfile
LG_Mesh * lg_mesh_new_from_objfile(const char *path, zboolean do_normalize_xyz)
Definition: lg_mesh.c:113
lg_mesh_free
void lg_mesh_free(LG_Mesh *mesh)
Definition: lg_mesh.c:179
lg_load_vbo
LG_Mesh * lg_load_vbo(const char *path, zboolean force_reload_obj, zboolean do_normalize_xyz)
Definition: lg_mesh.c:73
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:270
LG_Material
Definition: lg_mesh.h:36
lg_mesh_info
void lg_mesh_info(LG_Mesh *mesh)
Definition: lg_mesh.c:204
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:319