Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan hoffstadt committed May 7, 2024
1 parent 4c7f78b commit 47db63d
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 118 deletions.
19 changes: 11 additions & 8 deletions apps/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)

ptAppData->uSceneHandle0 = gptRenderer->create_scene();

pl_begin_profile_sample("load environments");
gptRenderer->load_skybox_from_panorama(ptAppData->uSceneHandle0, "../data/glTF-Sample-Environments-main/field.jpg", 1024);
pl_end_profile_sample();
// pl_begin_profile_sample("load environments");
// gptRenderer->load_skybox_from_panorama(ptAppData->uSceneHandle0, "../data/glTF-Sample-Environments-main/field.jpg", 1024);
// pl_end_profile_sample();

pl_begin_profile_sample("create scene views");
ptAppData->uViewHandle0 = gptRenderer->create_view(ptAppData->uSceneHandle0, (plVec2){ptIO->afMainViewportSize[0] , ptIO->afMainViewportSize[1]});
Expand All @@ -250,12 +250,15 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
plModelLoaderData tLoaderData0 = {0};

pl_begin_profile_sample("load models 0");
plMat4 tTransform0 = pl_mat4_translate_xyz(2, 2, 0);
// plMat4 tTransform0 = pl_mat4_translate_xyz(1, 0, 1);
// plMat4 tTransform1 = pl_mat4_translate_xyz(2, 0, 2);
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/lunar_terrain/lunar_terrain.gltf", NULL, &tLoaderData0);
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/modern_spacesuit_rigged/modern_spacesuit_rigged.gltf", &tTransform0, &tLoaderData0);
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/FlightHelmet/glTF/FlightHelmet.gltf", NULL, &tLoaderData0);
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/CesiumMan/glTF/CesiumMan.gltf", NULL, &tLoaderData0);
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/CesiumMan/glTF/CesiumMan.gltf", &tTransform1, &tLoaderData0);
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/DamagedHelmet/glTF/DamagedHelmet.gltf", NULL, &tLoaderData0);
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/Sponza/glTF/Sponza.gltf", NULL, &tLoaderData0);
gptModelLoader->load_stl(ptMainComponentLibrary, "../data/pilotlight-assets-master/meshes/monkey.stl", (plVec4){1.0f, 1.0f, 0.0f, 0.8f}, &tTransform0, &tLoaderData0);
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/Sponza/glTF/Sponza.gltf", NULL, &tLoaderData0);
// gptModelLoader->load_stl(ptMainComponentLibrary, "../data/pilotlight-assets-master/meshes/monkey.stl", (plVec4){1.0f, 1.0f, 0.0f, 0.8f}, &tTransform0, &tLoaderData0);
gptRenderer->add_drawable_objects_to_scene(ptAppData->uSceneHandle0, tLoaderData0.uOpaqueCount, tLoaderData0.atOpaqueObjects, tLoaderData0.uTransparentCount, tLoaderData0.atTransparentObjects);
gptModelLoader->free_data(&tLoaderData0);
pl_end_profile_sample();
Expand Down Expand Up @@ -373,7 +376,7 @@ pl_app_update(plAppData* ptAppData)
plCameraComponent* ptCamera = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera);
plCameraComponent* ptCullCamera = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera);

static const float fCameraTravelSpeed = 4.0f;
static const float fCameraTravelSpeed = 1.0f;
static const float fCameraRotationSpeed = 0.005f;

// camera space
Expand Down
2 changes: 1 addition & 1 deletion dependencies/pilotlight-libs
36 changes: 27 additions & 9 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ pl_ecs_cleanup_component_library(plComponentLibrary* ptLibrary)
pl_sb_free(sbtMeshes[i].sbtVertexJoints[1]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[0]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[1]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[2]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[3]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[4]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[5]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[6]);
pl_sb_free(sbtMeshes[i].sbtVertexTextureCoordinates[7]);
pl_sb_free(sbtMeshes[i].sbuIndices);
}

Expand Down Expand Up @@ -540,7 +546,7 @@ pl_ecs_add_component(plComponentLibrary* ptLibrary, plComponentType tType, plEnt
if(bAddSlot)
pl_sb_add(sbComponents);
ptManager->pComponents = sbComponents;
sbComponents[uComponentIndex] = (plObjectComponent){0};
sbComponents[uComponentIndex] = (plObjectComponent){.tMesh= {UINT32_MAX, UINT32_MAX}, .tTransform ={UINT32_MAX, UINT32_MAX}};
return &sbComponents[uComponentIndex];
}

Expand Down Expand Up @@ -870,16 +876,28 @@ pl__object_update_job(uint32_t uJobIndex, void* pData)
plObjectComponent* ptObject = &sbtComponents[uJobIndex];
plTransformComponent* ptTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform);
plMeshComponent* ptMesh = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_MESH, ptObject->tMesh);
plSkinComponent* ptSkinComponent = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_SKIN, ptMesh->tSkinComponent);

plMat4 tTransform = ptTransform->tWorld;

if(ptSkinComponent)
{
plEntity tJointEntity = ptSkinComponent->sbtJoints[0];
plTransformComponent* ptJointComponent = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, tJointEntity);

const plMat4* ptIBM = &ptSkinComponent->sbtInverseBindMatrices[0];
tTransform = pl_mul_mat4(&ptJointComponent->tWorld, ptIBM);
}

const plVec3 tVerticies[] = {
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMax.z }),
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMax.z }),
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMax.z }),
pl_mul_mat4_vec3(&ptTransform->tWorld, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMax.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMin.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMax.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMin.y, ptMesh->tAABB.tMax.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMax.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMax.z }),
pl_mul_mat4_vec3(&tTransform, (plVec3){ ptMesh->tAABB.tMin.x, ptMesh->tAABB.tMax.y, ptMesh->tAABB.tMax.z }),
};

// calculate AABB
Expand Down
20 changes: 13 additions & 7 deletions extensions/pl_ecs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,18 @@ enum _plMeshFormatFlags
PL_MESH_FORMAT_FLAG_HAS_TANGENT = 1 << 2,
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_0 = 1 << 3,
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_1 = 1 << 4,
PL_MESH_FORMAT_FLAG_HAS_COLOR_0 = 1 << 5,
PL_MESH_FORMAT_FLAG_HAS_COLOR_1 = 1 << 6,
PL_MESH_FORMAT_FLAG_HAS_JOINTS_0 = 1 << 7,
PL_MESH_FORMAT_FLAG_HAS_JOINTS_1 = 1 << 8,
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_0 = 1 << 9,
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_1 = 1 << 10
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_2 = 1 << 5,
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_3 = 1 << 6,
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_4 = 1 << 7,
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_5 = 1 << 8,
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_6 = 1 << 9,
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_7 = 1 << 10,
PL_MESH_FORMAT_FLAG_HAS_COLOR_0 = 1 << 11,
PL_MESH_FORMAT_FLAG_HAS_COLOR_1 = 1 << 12,
PL_MESH_FORMAT_FLAG_HAS_JOINTS_0 = 1 << 13,
PL_MESH_FORMAT_FLAG_HAS_JOINTS_1 = 1 << 14,
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_0 = 1 << 15,
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_1 = 1 << 16
};

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -421,7 +427,7 @@ typedef struct _plMeshComponent
plVec4* sbtVertexColors[2];
plVec4* sbtVertexWeights[2];
plVec4* sbtVertexJoints[2];
plVec2* sbtVertexTextureCoordinates[2];
plVec2* sbtVertexTextureCoordinates[8];
uint32_t* sbuIndices;
plAABB tAABB;
plAABB tAABBFinal;
Expand Down
2 changes: 1 addition & 1 deletion extensions/pl_graphics_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Index of this file:
#endif

#ifndef PL_MAX_DYNAMIC_DATA_SIZE
#define PL_MAX_DYNAMIC_DATA_SIZE 512
#define PL_MAX_DYNAMIC_DATA_SIZE 256
#endif

#ifndef PL_MAX_BUFFERS_PER_BIND_GROUP
Expand Down
52 changes: 36 additions & 16 deletions extensions/pl_model_loader_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ pl__load_gltf(plComponentLibrary* ptLibrary, const char* pcPath, const plMat4* p
for(size_t j = 0; j < ptGScene->nodes_count; j++)
{
const cgltf_node* ptNode = ptGScene->nodes[j];
pl__refr_load_gltf_object(ptDataOut, &tLoadingData, acDirectory, (plEntity){UINT32_MAX, UINT32_MAX}, ptNode);
plEntity tRoot = {UINT32_MAX, UINT32_MAX};
if(ptTransform)
{
const plEntity tNodeEntity = {.ulData = pl_hm_lookup(&tLoadingData.tNodeHashMap, (uint64_t)ptNode)};
plTransformComponent* ptTransformComponent = gptECS->get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, tNodeEntity);
ptTransformComponent->tWorld = pl_mul_mat4(ptTransform, &ptTransformComponent->tWorld);
tRoot = gptECS->create_transform(ptLibrary, "load transform");
plTransformComponent* ptTransformComponent = gptECS->get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, tRoot);
ptTransformComponent->tWorld = *ptTransform;
pl_decompose_matrix(&ptTransformComponent->tWorld, &ptTransformComponent->tScale, &ptTransformComponent->tRotation, &ptTransformComponent->tTranslation);
}

pl__refr_load_gltf_object(ptDataOut, &tLoadingData, acDirectory, tRoot, ptNode);
}
}

Expand Down Expand Up @@ -291,7 +291,7 @@ static void
pl__refr_load_material(const char* pcDirectory, plMaterialComponent* ptMaterial, const cgltf_material* ptGltfMaterial)
{
ptMaterial->tShaderType = PL_SHADER_TYPE_PBR;
ptMaterial->tFlags = ptGltfMaterial->double_sided ? PL_MATERIAL_FLAG_DOUBLE_SIDED : PL_MATERIAL_FLAG_NONE;
ptMaterial->tFlags |= ptGltfMaterial->double_sided ? PL_MATERIAL_FLAG_DOUBLE_SIDED : PL_MATERIAL_FLAG_NONE;
ptMaterial->fAlphaCutoff = ptGltfMaterial->alpha_cutoff;

// blend mode
Expand Down Expand Up @@ -416,21 +416,35 @@ pl__refr_load_attributes(plMeshComponent* ptMesh, const cgltf_primitive* ptPrimi
case cgltf_attribute_type_normal:
{
pl_sb_resize(ptMesh->sbtVertexNormals, (uint32_t)szVertexCount);
for(size_t i = 0; i < szVertexCount; i++)
if(ptAttribute->data->component_type == cgltf_component_type_r_32f && ptAttribute->data->type == cgltf_type_vec3)
{
plVec3* ptRawData = (plVec3*)&pucBufferStart[i * szStride];
ptMesh->sbtVertexNormals[i] = *ptRawData;
for(size_t i = 0; i < szVertexCount; i++)
{
plVec3* ptRawData = (plVec3*)&pucBufferStart[i * szStride];
ptMesh->sbtVertexNormals[i] = *ptRawData;
}
}
else
{
PL_ASSERT(false);
}
break;
}

case cgltf_attribute_type_tangent:
{
pl_sb_resize(ptMesh->sbtVertexTangents, (uint32_t)szVertexCount);
for(size_t i = 0; i < szVertexCount; i++)
if(ptAttribute->data->component_type == cgltf_component_type_r_32f && ptAttribute->data->type == cgltf_type_vec4)
{
plVec4* ptRawData = (plVec4*)&pucBufferStart[i * szStride];
ptMesh->sbtVertexTangents[i] = *ptRawData;
for(size_t i = 0; i < szVertexCount; i++)
{
plVec4* ptRawData = (plVec4*)&pucBufferStart[i * szStride];
ptMesh->sbtVertexTangents[i] = *ptRawData;
}
}
else
{
PL_ASSERT(false);
}
break;
}
Expand Down Expand Up @@ -804,11 +818,12 @@ pl__refr_load_gltf_object(plModelLoaderData* ptData, plGltfLoadingData* ptSceneD
for(size_t szPrimitiveIndex = 0; szPrimitiveIndex < ptNode->mesh->primitives_count; szPrimitiveIndex++)
{
// add mesh to our node
plEntity tNewObject = gptECS->create_object(ptLibrary, ptNode->mesh->name);
plObjectComponent* ptObject = gptECS->get_component(ptLibrary, PL_COMPONENT_TYPE_OBJECT, tNewObject);
plMeshComponent* ptMesh = gptECS->get_component(ptLibrary, PL_COMPONENT_TYPE_MESH, tNewObject);
plEntity tNewObject = gptECS->create_tag(ptLibrary, ptNode->mesh->name);
plObjectComponent* ptObject = gptECS->add_component(ptLibrary, PL_COMPONENT_TYPE_OBJECT, tNewObject);
plMeshComponent* ptMesh = gptECS->add_component(ptLibrary, PL_COMPONENT_TYPE_MESH, tNewObject);
ptMesh->tSkinComponent = tSkinEntity;


ptObject->tMesh = tNewObject;
ptObject->tTransform = tNewEntity; // TODO: delete unused entities (old transform)

const cgltf_primitive* ptPrimitive = &ptNode->mesh->primitives[szPrimitiveIndex];
Expand Down Expand Up @@ -851,6 +866,9 @@ pl__refr_load_gltf_object(plModelLoaderData* ptData, plGltfLoadingData* ptSceneD
if(ptMaterial->tBlendMode != PL_BLEND_MODE_OPAQUE)
bOpaque = false;

if(ptMaterial->tFlags & PL_MATERIAL_FLAG_DOUBLE_SIDED)
bOpaque = false;

if(bOpaque)
pl_sb_push(ptData->atOpaqueObjects, tNewObject);
else
Expand All @@ -861,7 +879,9 @@ pl__refr_load_gltf_object(plModelLoaderData* ptData, plGltfLoadingData* ptSceneD

// recurse through children
for(size_t i = 0; i < ptNode->children_count; i++)
{
pl__refr_load_gltf_object(ptData, ptSceneData, pcDirectory, tNewEntity, ptNode->children[i]);
}
}

//-----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 47db63d

Please sign in to comment.