Creates a new material with the specified type.
Parameters:
name: Material name for identification (can be NULL)type: Material type (PBR_METALLIC_ROUGHNESS, PBR_SPECULAR_GLOSSINESS, UNLIT, CUSTOM)
Returns: Pointer to new material, or NULL on failure
Example:
AP_Material *mat = AP_CreateMaterial("Steel", AP_MATERIAL_TYPE_PBR_METALLIC_ROUGHNESS);
if (!mat) AP_WARN("Failed to create material");Frees memory associated with a material.
Parameters:
material: Material to destroy (NULL-safe)
Example:
AP_DestroyMaterial(material);
material = NULL;void AP_MaterialInitPbrMetallicRoughness(AP_Material *mat, AP_Color base_color, AP_F32 metallic, AP_F32 roughness)
Initialize a PBR metallic-roughness material with base properties.
Parameters:
mat: Material to initializebase_color: Base surface color (linear RGB)metallic: Metallic factor [0, 1]roughness: Roughness factor [0, 1]
Example:
AP_MaterialInitPbrMetallicRoughness(material,
AP_C4(0.9f, 0.1f, 0.1f, 1.0f), // Red base
0.0f, // Not metallic
0.5f); // Medium roughnessInitialize an unlit material.
Parameters:
mat: Material to initializecolor: Unlit color
Example:
AP_MaterialInitUnlit(emissive_mat, AP_C4(0.0f, 1.0f, 0.0f, 1.0f)); // Green neonSet the base color/albedo texture.
Parameters:
mat: Target materialtexture: Texture handle (0 = none)
Set combined metallic-roughness texture (glTF format: R=unused, G=roughness, B=metallic).
Set normal map with intensity scale.
Parameters:
scale: Normal intensity [0, 2+]. 1.0 = default, < 1.0 = less detail, > 1.0 = more detail
Set ambient occlusion texture.
Parameters:
strength: AO strength [0, 1]
Set emissive/glow texture.
Set specular-glossiness texture (legacy workflow).
Check if material has any transparency.
Returns: true if material has transparency (BLEND or MASK alpha mode, or base_color.a < 1.0)
Check if material requires alpha blending.
Returns: true if alpha_mode == AP_ALPHA_MODE_BLEND
Check if material renders both sides.
Returns: true if double_sided flag is set
typedef struct AP_Material {
const char *name; // Material name
AP_MaterialType type; // Material type
// Base/Albedo
AP_Color base_color; // RGBA, linear
AP_UInt base_color_texture; // Texture handle
bool has_base_color_texture; // Has texture flag
// Metallic-Roughness (PBR)
AP_F32 metallic; // [0, 1]
AP_F32 roughness; // [0, 1]
AP_UInt metallic_roughness_texture; // Combined texture
bool has_metallic_roughness_texture; // Has texture flag
// Specular-Glossiness (legacy)
AP_Color specular_factor; // RGB
AP_F32 glossiness_factor; // [0, 1]
AP_UInt specular_glossiness_texture; // Texture
bool has_specular_glossiness_texture; // Has texture flag
// Normal Mapping
AP_UInt normal_texture; // Normal map
AP_F32 normal_scale; // Intensity
bool has_normal_texture; // Has texture flag
// Occlusion
AP_UInt occlusion_texture; // AO map
AP_F32 occlusion_strength; // [0, 1]
bool has_occlusion_texture; // Has texture flag
// Emissive
AP_Color emissive_factor; // RGB, can be > 1.0
AP_UInt emissive_texture; // Emissive map
bool has_emissive_texture; // Has texture flag
// Alpha/Transparency
AP_F32 alpha_cutoff; // [0, 1] for MASK mode
int alpha_mode; // OPAQUE, MASK, BLEND
bool double_sided; // Render both sides
void *user_data; // Custom data
} AP_Material;Load texture from file (PNG, JPG, TGA, etc.).
Parameters:
path: File path
Returns: Pointer to texture, or NULL on failure
Example:
AP_Texture *diffuse = AP_LoadTexture("assets/textures/wood_diffuse.png");
if (!diffuse) AP_WARN("Failed to load texture");Load texture from memory buffer.
Parameters:
data: Image data pointersize: Data size in bytes
Returns: Pointer to texture, or NULL on failure
Create texture from raw pixel data.
Parameters:
width: Texture widthheight: Texture heightpixels: Raw RGBA pixel datapitch: Bytes per row (0 = tightly packed)
Returns: Pointer to texture, or NULL on failure
Free texture memory.
Parameters:
texture: Texture to destroy (NULL-safe)
typedef struct AP_Texture {
AP_U32 id; // GPU handle
int width; // Texture width
int height; // Texture height
int channels; // Color channels (3 or 4)
} AP_Texture;AP_Mesh *AP_CreateMesh(const AP_Vertex3 *vertices, int vertex_count, const AP_U32 *indices, int index_count)
Create mesh from vertex and index data.
Parameters:
vertices: Vertex arrayvertex_count: Number of verticesindices: Index array (NULL for non-indexed)index_count: Number of indices
Returns: Pointer to mesh, or NULL on failure
Load mesh from file.
Parameters:
path: File path (glTF/glB)
Returns: Pointer to mesh, or NULL on failure
AP_Mesh *AP_CreateMeshCube(AP_F32 size);
AP_Mesh *AP_CreateMeshPlane(AP_F32 width, AP_F32 depth);
AP_Mesh *AP_CreateMeshSphere(AP_F32 radius, int slices, int stacks);Free mesh memory. Cannot destroy builtin primitives.
Check if mesh is valid.
Get number of vertices.
Get number of indices.
Get mesh's associated material (does not own).
Returns: Material pointer, or NULL
Set material for mesh (mesh does not take ownership).
Parameters:
mesh: Target meshmaterial: Material to assign
Returns: true on success
Create an empty model.
Returns: Pointer to model, or NULL on failure
Load model from glTF/glB file with materials and textures.
Parameters:
path: File path
Returns: Pointer to model, or NULL on failure
Example:
AP_Model *model = AP_LoadModel("assets/models/character.glb");
if (!AP_ModelIsValid(model)) {
AP_ERROR("Failed to load model");
return;
}Free model and all owned resources.
Parameters:
model: Model to destroy (NULL-safe)
Check if model is valid.
Get number of meshes.
Get mesh by index.
Parameters:
index: Mesh index [0, mesh_count)
Returns: Pointer to mesh, or NULL
Get number of materials.
Get material by index. Materials are owned by model.
Parameters:
index: Material index [0, material_count)
Returns: Pointer to material, or NULL
Get number of textures.
Get texture by index. Textures are owned by model.
Parameters:
index: Texture index [0, texture_count)
Returns: Pointer to texture, or NULL
Set model's world transform.
Get model's current world transform.
Set transform from position, rotation, and scale.
Set model position.
Move model by offset.
Rotate model around axis.
Scale model.
Reset to identity transform.
Set mesh's local transform.
Get mesh's local transform.
bool AP_ModelSetMeshTRS(AP_Model *model, int mesh_index, AP_Vec3 position, AP_Quat rotation, AP_Vec3 scale)
Set mesh local transform from TRS.
Draw mesh with current material and transform.
Draw mesh with override transform and tint.
bool AP_DrawMeshTRS(const AP_Mesh *mesh, AP_Vec3 position, AP_Quat rotation, AP_Vec3 scale, AP_Color tint)
Draw mesh with TRS parameters.
Draw entire model with all meshes and materials.
Draw model with override transform.
bool AP_DrawModelTRS(const AP_Model *model, AP_Vec3 position, AP_Quat rotation, AP_Vec3 scale, AP_Color tint)
Draw model with TRS parameters.
Begin 3D rendering pass.
End 3D rendering pass.
Check if currently in 3D pass.
Set current model matrix.
Get current model matrix.
Reset to identity.
Set model matrix from TRS.
Set position only.
Set rotation only.
Set scale only.
Translate by offset.
Rotate around axis.
Scale by factor.
Set texture for next draw.
Set color tint.
Set specular shininess.
Set specular strength.
Enable/disable depth testing.
Enable/disable back-face culling.
#define AP_MATERIAL_TYPE_PBR_METALLIC_ROUGHNESS 0
#define AP_MATERIAL_TYPE_PBR_SPECULAR_GLOSSINESS 1
#define AP_MATERIAL_TYPE_UNLIT 2
#define AP_MATERIAL_TYPE_CUSTOM 3#define AP_ALPHA_MODE_OPAQUE 0
#define AP_ALPHA_MODE_MASK 1
#define AP_ALPHA_MODE_BLEND 2#include <AP2/AP2.h>
int main(void) {
AP_Init(AP_INIT_ALL);
AP_Window *w = AP_CreateWindow("API Demo", 1280, 720, AP_WINDOW_RESIZABLE);
AP_SetActiveWindow(w);
AP_Camera cam = AP_CameraPerspective(
AP_V3(0, 2, 8), AP_V3(0, 0, 0), 60);
// Load model with embedded materials and textures
AP_Model *model = AP_LoadModel("model.glb");
if (!AP_ModelIsValid(model)) {
AP_WARN("Failed to load model");
return 1;
}
// Create custom material
AP_Material *mat = AP_CreateMaterial("Custom", AP_MATERIAL_TYPE_PBR_METALLIC_ROUGHNESS);
AP_MaterialInitPbrMetallicRoughness(mat,
AP_C4(1, 0, 0, 1), 0.5f, 0.4f);
AP_Mesh *sphere = AP_CreateMeshSphere(1, 32, 32);
AP_MeshSetMaterial(sphere, mat);
while (AP_IsRunning()) {
AP_ClearLights();
AP_Fill(0.1f, 0.1f, 0.1f, 1);
AP_PumpEvents();
AP_Begin3D(&cam);
AP_AddLight(AP_LightDirectional(
AP_V3(-1, -1, -1), AP_C4(1, 1, 1, 1), 1.2f));
AP_SetAmbientLight(AP_C4(0.3f, 0.3f, 0.3f, 1));
AP_DrawModel(model);
AP_Set3DPosition(AP_V3(4, 0, 0));
AP_DrawMesh(sphere);
AP_End3D();
AP_Present();
}
AP_DestroyMesh(sphere);
AP_DestroyMaterial(mat);
AP_DestroyModel(model);
AP_DestroyWindow(w);
AP_Quit();
return 0;
}