Added blas-tlas;

Added Multi-Scattering GGX;
This commit is contained in:
2025-12-30 15:25:16 +09:00
parent bfd06bdd11
commit 5f5404268c
24 changed files with 1478 additions and 183 deletions

View File

@@ -4,6 +4,7 @@
#include "assimp/cimport.h"
#include "assimp/scene.h"
#include "assimp/postprocess.h"
#include "cglm/struct/mat4.h"
static texture_handle_t load_material_texture(const struct aiMaterial* material, enum aiTextureType type, const char* filename, scene_t* scene)
{
@@ -27,6 +28,8 @@ static texture_handle_t load_material_texture(const struct aiMaterial* material,
mesh_handle_t mesh_load(const char* filename, scene_t* scene)
{
mesh_handle_t entity = {0};
entity.model_id = UINT32_MAX;
entity.instance_id = UINT32_MAX;
const struct aiScene* mesh_scene = aiImportFile(filename, aiProcessPreset_TargetRealtime_Quality);
if (mesh_scene == NULL)
@@ -35,9 +38,29 @@ mesh_handle_t mesh_load(const char* filename, scene_t* scene)
return entity;
}
entity.triangle_id = scene->triangles.count;
entity.material_id = scene->materials.count;
// Reserve a model sized for the imported geometry.
uint64_t triangle_reserve = 0;
for (uint32_t i = 0; i < mesh_scene->mNumMeshes; i++)
{
const struct aiMesh* mesh = mesh_scene->mMeshes[i];
if (mesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE)
{
continue;
}
// Each face is expected to be a triangle; we still validate per face below.
triangle_reserve += (uint64_t)mesh->mNumFaces;
}
entity.model_id = scene_add_mesh_model(scene, triangle_reserve);
mesh_model_t* model = NULL;
if (entity.model_id != UINT32_MAX && entity.model_id < scene->mesh_models.capacity)
{
model = &scene->mesh_models.buffer[entity.model_id];
}
for (uint32_t i = 0; i < mesh_scene->mNumMaterials; i++)
{
const struct aiMaterial* src = mesh_scene->mMaterials[i];
@@ -108,11 +131,31 @@ mesh_handle_t mesh_load(const char* filename, scene_t* scene)
}
}
triangle_create(vertices[0], vertices[1], vertices[2], entity.material_id + mesh->mMaterialIndex, &scene->triangles);
if (model != NULL)
{
triangle_create(vertices[0], vertices[1], vertices[2], (uint8_t)(entity.material_id + mesh->mMaterialIndex), &model->triangles);
}
entity.triangle_count++;
}
}
if (model != NULL && model->triangles.count > 0)
{
bvh_tree_free(&model->blas);
if (bvh_tree_init(&model->blas, &model->triangles))
{
(void)bvh_tree_build(&model->blas);
if (model->blas.nodes != NULL && model->blas.node_count > 0)
{
model->local_bounds = model->blas.nodes[0].bounds;
}
}
mat4s identity = glms_mat4_identity();
entity.local_to_world = identity;
entity.instance_id = scene_add_mesh_instance(scene, entity.model_id, identity);
}
aiReleaseImport(mesh_scene);
return entity;
}