61 for (
size_t i = 0; i < m->
n_mtllibs; ++i) {
105 gm3_pos_center(¢er, &
max, &
min);
108 gm3_pos_substract(&m->
vertices[v], ¢er);
152#define GM3_BAKED_MESH_MAGIC 0x474D334D
156 size_t total_textures_size = 0;
157 for (
size_t i = 0; i < mesh->
n_mtllibs; i++) {
159 total_textures_size +=
sizeof(size_t);
160 for (
size_t j = 0; j < lib->
n_textures; j++) {
163 total_textures_size +=
sizeof(int32_t) * 2;
164 total_textures_size +=
sizeof(size_t);
165 total_textures_size += data_size;
166 total_textures_size += 256;
171 size_t mtllibs_size = 0;
172 for (
size_t i = 0; i < mesh->
n_mtllibs; i++) {
174 mtllibs_size +=
sizeof(size_t);
175 mtllibs_size +=
sizeof(size_t);
182 mtllibs_size + total_textures_size;
188 char *p = (
char *)*data;
199 memcpy(p, &header,
sizeof(header));
221 for (
size_t i = 0; i < mesh->
n_mtllibs; i++) {
224 memcpy(p, lib->
name, 256);
238 for (
size_t j = 0; j < lib->
n_textures; j++) {
241 memcpy(p, tex->
path, 256);
243 memcpy(p, &tex->
data.
width,
sizeof(int32_t));
244 p +=
sizeof(int32_t);
245 memcpy(p, &tex->
data.
height,
sizeof(int32_t));
246 p +=
sizeof(int32_t);
247 memcpy(p, &data_size,
sizeof(
size_t));
249 memcpy(p, tex->
data.
data, data_size);
258 memset(mesh, 0,
sizeof(
gm3Mesh));
259 const char *p = (
const char *)data;
301 for (
size_t i = 0; i < mesh->
n_mtllibs; i++) {
303 memcpy(lib->
name, p, 256);
318 for (
size_t j = 0; j < lib->
n_textures; j++) {
320 size_t data_size = 0;
321 memcpy(tex->
path, p, 256);
323 memcpy(&tex->
data.
width, p,
sizeof(int32_t));
324 p +=
sizeof(int32_t);
325 memcpy(&tex->
data.
height, p,
sizeof(int32_t));
326 p +=
sizeof(int32_t);
327 memcpy(&data_size, p,
sizeof(
size_t));
330 memcpy(tex->
data.
data, p, data_size);
338 if ((
size_t)(p - (
const char *)data) > size) {
void * malloc(size_t size)
Custom implementation of malloc using a static memory pool.
Definition malloc.h:144
void * calloc(size_t count, size_t size)
Custom implementation of calloc using a static memory pool.
Definition malloc.h:215
void free(void *ptr)
Custom implementation of free for memory allocated by malloc (this custom version).
Definition malloc.h:189
double fmin(double a, double b)
Returns the smaller of two double values.
Definition math.h:413
double fmax(double a, double b)
Returns the larger of two double values.
Definition math.h:428
int min(int a, int b)
Returns the smaller of two integer values.
Definition math.h:406
int max(int a, int b)
Returns the larger of two integer values.
Definition math.h:421
int gm3_mesh_center(gm3Mesh *m)
Centers the mesh geometry around the origin (0,0,0).
Definition mesh.h:86
void gm3_mesh_free(gm3Mesh *m)
Frees all dynamically allocated memory associated with a gm3Mesh struct.
Definition mesh.h:51
int gm3_mesh_deserialize(gm3Mesh *mesh, const void *data, size_t size)
Deserializes a gm3Mesh from a binary buffer.
Definition mesh.h:257
int gm3_mesh_serialize(const gm3Mesh *mesh, void **data, size_t *size)
Serializes a gm3Mesh into a binary buffer.
Definition mesh.h:154
#define GM3_BAKED_MESH_MAGIC
Magic number for the baked mesh binary format: "GM3M".
Definition mesh.h:152
Defines structures for 3D materials and material libraries, and functions for loading MTL files.
Represents a single 3D material with various rendering properties.
Definition mtl.h:26
Represents a single face (triangle) in a 3D mesh.
Definition mesh.h:10
int material
Definition mesh.h:13
int material_file
Definition mesh.h:14
size_t vertices[3]
Definition mesh.h:11
long uvs[3]
Definition mesh.h:12
gm3Pos normal
Definition mesh.h:15
Represents a 3D mesh composed of vertices, faces, normals, and texture coordinates.
Definition mesh.h:30
size_t n_mtllibs
Definition mesh.h:44
size_t n_normals
Definition mesh.h:38
gm3Tex * texs
Definition mesh.h:40
size_t n_texs
Definition mesh.h:41
gm3MeshFace * faces
Definition mesh.h:34
size_t n_vertices
Definition mesh.h:32
gm3Pos * vertices
Definition mesh.h:31
gm3MtlLib * mtllibs
Definition mesh.h:43
gm3Pos * normals
Definition mesh.h:37
size_t n_faces
Definition mesh.h:35
Represents a material library, typically loaded from an .mtl file.
Definition mtl.h:53
size_t n_textures
Definition mtl.h:60
gm3Material * materials
Definition mtl.h:56
char name[256]
Definition mtl.h:54
size_t n_materials
Definition mtl.h:57
gm3Texture * textures
Definition mtl.h:59
Represents a 3D position or vector.
Definition position.h:11
double y
Definition position.h:12
double z
Definition position.h:12
double x
Definition position.h:12
Represents a 2D texture coordinate.
Definition mesh.h:21
double v
Definition mesh.h:22
double u
Definition mesh.h:22
Represents a 3D texture, including its raw image data and file path.
Definition mtl.h:43
gmImageData data
Definition mtl.h:44
char path[256]
Definition mtl.h:45
int32_t width
Definition image.h:19
int32_t height
Definition image.h:19
unsigned char * data
Definition image.h:20