69static inline char *gm3u_skip_spaces(
char *s) {
70 while (*s && (*s ==
' ' || *s ==
'\t'))
87 for (
size_t i = 0; i < mtllib->
n_textures; i++)
101 memset(tex->
path, 0,
sizeof(tex->
path));
102 memcpy(tex->
path, path, strlen(path));
119 mtl_lib->
name[0] =
'\0';
123 FILE *f = fopen(path,
"r");
131 while (fgets(line,
sizeof(line), f)) {
132 char *p = gm3u_skip_spaces(line);
133 if (*p ==
'\0' || *p ==
'#')
136 if (strncmp(p,
"newmtl ", 7) == 0) {
144 current->
alpha = 1.0;
145 current->
diffuse = gm_rgb(200, 200, 200);
146 sscanf(p + 7,
"%63s", current->
name);
147 }
else if (current) {
148 if (strncmp(p,
"Kd ", 3) == 0) {
150 sscanf(p + 3,
"%f %f %f", &r, &g, &b);
152 gm_rgb((
int)(r * 255), (
int)(g * 255), (
int)(b * 255));
153 }
else if (strncmp(p,
"Ks ", 3) == 0) {
155 sscanf(p + 3,
"%f %f %f", &r, &g, &b);
157 gm_rgb((
int)(r * 255), (
int)(g * 255), (
int)(b * 255));
158 }
else if (strncmp(p,
"Ns ", 3) == 0) {
160 }
else if (p[0] ==
'd' && isspace(p[1])) {
161 current->
alpha = atof(p + 2);
162 }
else if (strncmp(p,
"Tr ", 3) == 0) {
163 current->
alpha = 1.0 - atof(p + 3);
164 }
else if (strncmp(p,
"map_Kd", 6) == 0) {
165 char tex_path[256] = {0};
166 gm3u_str_copy_eol(tex_path, gm3u_skip_spaces(p + 6),
sizeof(tex_path));
168 snprintf(full_path,
sizeof(full_path),
"%s/%s", dir, tex_path);
175 }
else if (strncmp(p,
"map_Ks", 6) == 0) {
176 char tex_path[256] = {0};
177 gm3u_str_copy_eol(tex_path, gm3u_skip_spaces(p + 6),
sizeof(tex_path));
179 snprintf(full_path,
sizeof(full_path),
"%s/%s", dir, tex_path);
186 }
else if (strncmp(p,
"map_Ke", 6) == 0) {
187 char tex_path[256] = {0};
188 gm3u_str_copy_eol(tex_path, gm3u_skip_spaces(p + 6),
sizeof(tex_path));
190 snprintf(full_path,
sizeof(full_path),
"%s/%s", dir, tex_path);
197 }
else if (strncmp(p,
"map_d", 5) == 0) {
198 char tex_path[256] = {0};
199 gm3u_str_copy_eol(tex_path, gm3u_skip_spaces(p + 5),
sizeof(tex_path));
201 snprintf(full_path,
sizeof(full_path),
"%s/%s", dir, tex_path);
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
int32_t gm_image_data_load(gmImageData *data, const char *path)
Loads image file from disk into a gmImageData struct.
Definition image.h:29
void * realloc(void *ptr, size_t size)
Custom implementation of realloc for memory allocated by malloc (this custom version).
Definition malloc.h:236
void free(void *ptr)
Custom implementation of free for memory allocated by malloc (this custom version).
Definition malloc.h:189
gm3Material * gm3_mtl_find_mat(gm3MtlLib *file, const char *name, int *index)
Finds a material by name within a loaded MTL file.
Definition mtl.h:219
int gm3_mtl_load(gm3MtlLib *mtl_lib, const char *path, const char *dir)
Loads materials and textures from an OBJ-style .mtl file.
Definition mtl.h:118
void gm3_mtl_free(gm3MtlLib *file)
Definition mtl.h:238
long gm3_mtl_add_texture(gm3MtlLib *mtllib, const char *path)
Adds a texture to the material library, loading it if not already present.
Definition mtl.h:86
Defines a dynamic string structure and provides utility functions for its manipulation.
Represents a single 3D material with various rendering properties.
Definition mtl.h:26
long tex_diffuse
Definition mtl.h:34
long tex_emissive
Definition mtl.h:37
double alpha
Definition mtl.h:31
gmColor diffuse
Definition mtl.h:28
long tex_alpha
Definition mtl.h:36
gmColor emissive
Definition mtl.h:32
char name[64]
Definition mtl.h:27
gmColor specular
Definition mtl.h:29
double shininess
Definition mtl.h:30
long tex_specular
Definition mtl.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 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
A container for raw, CPU-side image pixel data.
Definition image.h:18
Provides general utility functions for file handling and string manipulation.
void gmu_get_filename_base(const char *path, char *out_base, size_t out_size)
Extracts the base filename (filename with extension) from a full path.
Definition utils.h:24