Gama C Library
Gama C API Documentation
mtl.h File Reference

Defines structures for 3D materials and material libraries, and functions for loading MTL files. More...

#include "../color.h"
#include "../image.h"
#include "../str.h"
#include "../utils.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  gm3Material
 Represents a single 3D material with various rendering properties. More...
struct  gm3Texture
 Represents a 3D texture, including its raw image data and file path. More...
struct  gm3MtlLib
 Represents a material library, typically loaded from an .mtl file. More...

Functions

long gm3_mtl_add_texture (gm3MtlLib *mtllib, const char *path)
 Adds a texture to the material library, loading it if not already present.
int gm3_mtl_load (gm3MtlLib *mtl_lib, const char *path, const char *dir)
 Loads materials and textures from an OBJ-style .mtl file.
gm3Materialgm3_mtl_find_mat (gm3MtlLib *file, const char *name, int *index)
 Finds a material by name within a loaded MTL file.
void gm3_mtl_free (gm3MtlLib *file)

Detailed Description

Defines structures for 3D materials and material libraries, and functions for loading MTL files.

This file handles parsing and representing material data typically found in .mtl files accompanying .obj models. It includes material properties like colors, shininess, alpha, and references to texture maps.

Function Documentation

◆ gm3_mtl_add_texture()

long gm3_mtl_add_texture ( gm3MtlLib * mtllib,
const char * path )

Adds a texture to the material library, loading it if not already present.

This function checks if a texture with the given path already exists in the mtllib. If so, it returns its index. Otherwise, it loads the image data, adds it to the mtllib's texture array, and returns the new index.

Parameters
mtllibA pointer to the gm3MtlLib to add the texture to.
pathThe file path of the texture to add.
Returns
The index of the texture in the mtllib->textures array on success, -1 on failure.

◆ gm3_mtl_find_mat()

gm3Material * gm3_mtl_find_mat ( gm3MtlLib * file,
const char * name,
int * index )

Finds a material by name within a loaded MTL file.

◆ gm3_mtl_free()

void gm3_mtl_free ( gm3MtlLib * file)

◆ gm3_mtl_load()

int gm3_mtl_load ( gm3MtlLib * mtl_lib,
const char * path,
const char * dir )

Loads materials and textures from an OBJ-style .mtl file.

This function parses the specified .mtl file, extracting material properties and loading any referenced textures.

Parameters
mtl_libA pointer to the gm3MtlLib struct to populate with loaded data.
pathThe file path to the .mtl file.
dirThe base directory for resolving relative texture paths.
Returns
0 on success, -1 on memory allocation failure, -2 if the file cannot be opened, or a negative value from gm3_mtl_add_texture on texture loading failure.