Functions for loading and managing 3D models from OBJ files. More...
Classes | |
struct | Face |
Structure representing a face of a 3D model. More... | |
struct | LoadingModelBuffer |
Structure to hold the data of a 3D model being loaded. More... | |
Macros | |
#define | MAX_VERTEX_PER_FACE 10 |
Maximum number of vertices per face. | |
Functions | |
int | malloc_obj (ModelObjectData *obj, LoadingModelBuffer *buffer, u32 vim, u32 fim, u32 vnim, u32 vtim) |
Allocates memory for an ModelObjectData's vertex, face, normal, and texture data. | |
void | close_realloc_obj (ModelObjectData *obj, LoadingModelBuffer *buffer, u32 vi, u32 fi, u32 vni, u32 vti) |
Cleans up and reallocates the memory for the vertex data and faces of a 3D object, and generates the Vertex Array Object (VAO) for rendering. | |
int | load_obj_model (const char *path, ModelData *modelPtr) |
Loads a 3D model from an OBJ file, including its associated materials, vertices, normals, and texture coordinates. | |
Variables | |
Vertex * | LoadingModelBuffer::vertex |
Normal * | LoadingModelBuffer::normals |
TextureVertex * | LoadingModelBuffer::textureVertex |
Face * | LoadingModelBuffer::faces |
Functions for loading and managing 3D models from OBJ files.
#define MAX_VERTEX_PER_FACE 10 |
Maximum number of vertices per face.
int malloc_obj | ( | ModelObjectData * | obj, |
LoadingModelBuffer * | buffer, | ||
u32 | vim, | ||
u32 | fim, | ||
u32 | vnim, | ||
u32 | vtim | ||
) |
Allocates memory for an ModelObjectData's vertex, face, normal, and texture data.
obj | {ModelObjectData*} A pointer to the ModelObjectData structure to be initialized. |
vim | {u32} The initial number of vertices to allocate memory for. |
fim | {u32} The initial number of faces to allocate memory for. |
vnim | {u32} The initial number of normals to allocate memory for. |
vtim | {u32} The initial number of texture coordinates to allocate memory for. |
This function allocates the necessary memory for various data fields in the specified ModelObjectData, including vertices, faces, normals, and texture coordinates. It initializes the materials and materialsLength to NULL and sets the materialsCount to zero.
Important Notes:
obj
is a valid pointer to an ModelObjectData structure.Example Usage: malloc_obj(&object, initialVertexCount, initialFaceCount, initialNormalCount, initialTextureVertexCount);
void close_realloc_obj | ( | ModelObjectData * | obj, |
LoadingModelBuffer * | buffer, | ||
u32 | vi, | ||
u32 | fi, | ||
u32 | vni, | ||
u32 | vti | ||
) |
Cleans up and reallocates the memory for the vertex data and faces of a 3D object, and generates the Vertex Array Object (VAO) for rendering.
obj | {ModelObjectData*} A pointer to the ModelObjectData structure to be cleaned up and reallocated. |
vi | {u32} The current number of vertices loaded for the object. |
fi | {u32} The current number of faces loaded for the object. |
vni | {u32} The current number of normals loaded for the object. |
vti | {u32} The current number of texture coordinates loaded for the object. |
This function frees the previously allocated memory for normals, texture vertices, and vertex data associated with the given ModelObjectData structure. It then reallocates the facesVertex array to fit the actual number of faces and generates the corresponding Vertex Array Object (VAO) for rendering.
Important Notes:
Example Usage: close_realloc_obj(&object, vi, fi, vni, vti);
int load_obj_model | ( | const char * | path, |
ModelData * | modelPtr | ||
) |
Loads a 3D model from an OBJ file, including its associated materials, vertices, normals, and texture coordinates.
path | {char*} The directory path where the OBJ file is located. |
filename | {char*} The name of the OBJ file to be loaded. |
model | {ModelData*} A pointer to a Model structure that will be populated with the loaded data. |
This function reads the specified OBJ file and extracts geometric data such as vertices, texture coordinates, normals, and faces. It also handles the associated materials through a corresponding MTL file, updating the Model structure accordingly. The model is dynamically allocated and resized as necessary to accommodate the loaded data.
Important Notes:
load_mtl
, find_material
, malloc_obj
, and close_realloc_obj
for loading materials and managing dynamic memory.Example Usage: ModelData model; if (load_obj_model("assets/models/", "example.obj", &model) == 0) { // Successfully loaded the model } else { // Handle error }
LoadingModelBuffer::vertex |
Array of vertices
Array of vertices that define the geometric points of the model.
LoadingModelBuffer::normals |
Array of normals
Array of normals that define the direction perpendicular to the surface of the model's faces.
LoadingModelBuffer::textureVertex |
Array of texture vertices
Array of texture vertices that map the 2D texture coordinates to the 3D model.
LoadingModelBuffer::faces |
Array of faces
Array of faces that define the polygons of the model, typically triangles or quads.