Skip to content

feat: material-based embedded texture loading for assimp models#32

Open
gimploo wants to merge 2 commits into
devfrom
feat/embedded-textures-test
Open

feat: material-based embedded texture loading for assimp models#32
gimploo wants to merge 2 commits into
devfrom
feat/embedded-textures-test

Conversation

@gimploo

@gimploo gimploo commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Summary

Extends glmodel_t to load textures through Assimp materials using aiGetMaterialTexture, supporting both external file paths and embedded texture references (*N syntax) from scene->mTextures.

Previously, only scene-level embedded textures (scene->mTextures) were loaded, but material textures (the primary source for formats like glTF/GLB) were completely ignored.

Changes

1. gfx/gl/texture_types.h — Extended gltexturetype enum

Added PBR texture type variants (values 1–17 match aiTextureType for direct casting):
DIFFUSE, SPECULAR, AMBIENT, EMISSIVE, HEIGHT, NORMALS, SHININESS, OPACITY, DISPLACEMENT, LIGHTMAP, REFLECTION, BASE_COLOR, NORMAL_CAMERA, EMISSION_COLOR, METALNESS, DIFFUSE_ROUGHNESS, AMBIENT_OCCLUSION

2. gfx/model/assimp.h — Core model loading rewrite (based on dev)

  • New model_texture_t struct pairs gltexture2d_t with aiTextureType
  • New assimp__internal_load_texture_from_path helper resolves texture paths:
    • *N → embedded reference → gltexture2d_embedded_init from scene->mTextures[N]
    • Regular path → gltexture2d_init with absolute path from model directory
  • Rewrote processMaterial to iterate all aiTextureType values, call aiGetMaterialTexture, load textures with type deduplication
  • Material deduplication in processScene using bitmask (multiple meshes sharing same material processed once)
  • Removed load_alltextures — material-based loading supersedes scene-level scanning
  • Updated glmodel_get_texuturelist to map aiTextureTypegltexturetype via direct cast
  • Updated glmodel_destroy for new struct layout

3. gfx/glrenderer3d.h — Renderer binding

All PBR texture types fall through to 2D texture bind path (same as GL_TEXTURE_TYPE_NORMAL)

Testing

  • Build compiles successfully
  • Tested with res/towermodel.glb (GLB with embedded PNG baseColorTexture): model loads without errors

Extend glmodel_t to load textures through Assimp materials using
aiGetMaterialTexture, supporting both external file paths and embedded
texture references (*N syntax) from scene->mTextures.

Changes:
- Extend gltexturetype enum with PBR texture types (DIFFUSE, SPECULAR,
  NORMALS, BASE_COLOR, METALNESS, etc.) mapped to aiTextureType values
- Add model_texture_t struct pairing gltexture2d_t with aiTextureType
- Replace scene-level load_alltextures with material-driven loading
  via aiGetMaterialTexture in processMaterial
- Resolve embedded texture references (*N) back to scene->mTextures[N]
- Deduplicate materials processed across multiple meshes in processScene
- Update renderer to bind all PBR texture types as 2D textures
Comment thread gfx/model/assimp.h
Comment on lines +373 to +380
if (!(processed_materials & ((u64)1 << mesh->mMaterialIndex))) {
processed_materials |= (u64)1 << mesh->mMaterialIndex;
assimp__internal_glmesh_processMaterial(
self,
scene,
mesh->mMaterialIndex
);
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain the bitmask logic and the reason to bring in a comment

Comment thread gfx/model/assimp.h Outdated
str_t absolute_texture_path = str_join(self->arena, &self->directory_path, aitexture->mFilename.data);
texture = gltexture2d_init(absolute_texture_path.data);
gltexture2d_t assimp__internal_load_texture_from_path(glmodel_t *self, const struct aiScene *scene, const char *path) {
if (path[0] == '*') {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace the condition with a variable describing the comparison

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant