Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed May 11, 2024
1 parent db1a1d4 commit 946a96e
Show file tree
Hide file tree
Showing 13 changed files with 803 additions and 190 deletions.
80 changes: 48 additions & 32 deletions apps/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ typedef struct plAppData_t
plEntity tMainCamera;
plEntity tSunlight;

// CSM TESTING
plEntity tTestCamera;

// views
uint32_t uSceneHandle0;
uint32_t uViewHandle0;
Expand Down Expand Up @@ -244,42 +241,47 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)


// create main camera
ptAppData->tMainCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "main camera", (plVec3){0, 2.0f, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.01f, 400.0f);
ptAppData->tMainCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "main camera", (plVec3){0, 2.0f, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.01f, 48.0f);
gptCamera->set_pitch_yaw(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera), 0.0f, PL_PI);
gptCamera->update(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera));

// create cull camera
ptAppData->tCullCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "cull camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.1f, 106.0f);
ptAppData->tCullCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "cull camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.1f, 50.0f);
gptCamera->set_pitch_yaw(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera), 0.0f, PL_PI);
gptCamera->update(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera));

ptAppData->tTestCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "test camera", (plVec3){0.0f, 2.0f, 10.0f}, PL_PI_3, 1.0f, 0.01f, 25.0f);
gptCamera->set_pitch_yaw(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tTestCamera), 0.0f, PL_PI);
gptCamera->update(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tTestCamera));


// create lights
for(uint32_t i = 0; i < 25; i++)
{
plEntity tLight = gptEcs->create_point_light(ptMainComponentLibrary, "sunlight", (plVec3){(float)i-12.0f, 1.0f, -1.0f});
plLightComponent* ptLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, tLight);
ptLight->tColor.g = (float)i / 256.0f;
}
ptAppData->tSunlight = gptEcs->create_directional_light(ptMainComponentLibrary, "sunlight", (plVec3){-1.0f, -1.0f, 0.0f});
plLightComponent* ptLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, ptAppData->tSunlight);
gptEcs->set_light_shadows(ptMainComponentLibrary, ptAppData->tSunlight, ptAppData->tTestCamera, PL_MAX_SHADOW_CASCADES);
gptEcs->set_light_shadows(ptMainComponentLibrary, ptAppData->tSunlight, 3);

// load models

plModelLoaderData tLoaderData0 = {0};

pl_begin_profile_sample("load models 0");
// const plMat4 tTransform0 = pl_mat4_translate_xyz(2.0f, 1.0f, 0.0f);
const plMat4 tTransform0 = pl_mat4_translate_xyz(2.0f, 0.0f, 0.0f);
// const plMat4 tTransform0 = pl_mat4_scale_xyz(2.0f, 2.0f, 2.0f);

const plMat4 atTransforms[] = {
pl_mat4_translate_xyz(0.0f, 0.0f, 0.0f),
pl_mat4_translate_xyz(0.0f, 0.0f, 2.0f),
pl_mat4_translate_xyz(0.0f, 0.0f, 4.0f),
};
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/terrain_gridlines.gltf", NULL, &tLoaderData0);
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/town.gltf", &tTransform0, &tLoaderData0);
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/Sponza/glTF/Sponza.gltf", NULL, &tLoaderData0);
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/CesiumMan/glTF/CesiumMan.gltf", &tTransform0, &tLoaderData0);
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/BrainStem/glTF/BrainStem.gltf", NULL, &tLoaderData0);

for(uint32_t i = 0; i < 1; i++)
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/oaktree.gltf", &atTransforms[i], &tLoaderData0);
// for(uint32_t i = 0; i < 3; i++)
// gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/oaktree.gltf", &atTransforms[i], &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 @@ -394,7 +396,6 @@ 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);
plCameraComponent* ptTestCamera = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tTestCamera);

static const float fCameraTravelSpeed = 4.0f;
static const float fCameraRotationSpeed = 0.005f;
Expand Down Expand Up @@ -423,7 +424,6 @@ pl_app_update(plAppData* ptAppData)

gptCamera->update(ptCamera);
gptCamera->update(ptCullCamera);
gptCamera->update(ptTestCamera);

// run ecs system
gptRenderer->run_ecs(ptAppData->uSceneHandle0);
Expand All @@ -436,7 +436,8 @@ pl_app_update(plAppData* ptAppData)
uint64_t ulValue2 = ulValue0 + 2;
uint64_t ulValue3 = ulValue0 + 3;
uint64_t ulValue4 = ulValue0 + 4;
ptAppData->aulNextTimelineValue[ptGraphics->uCurrentFrameIndex] = ulValue4;
uint64_t ulValue5 = ulValue0 + 5;
ptAppData->aulNextTimelineValue[ptGraphics->uCurrentFrameIndex] = ulValue5;


// first set of work
Expand Down Expand Up @@ -476,6 +477,27 @@ pl_app_update(plAppData* ptAppData)
};
gptGfx->submit_command_buffer(ptGraphics, &tCommandBuffer, &tSubmitInfo00);



const plBeginCommandInfo tBeginInfo11 = {
.uWaitSemaphoreCount = 1,
.atWaitSempahores = {ptAppData->atSempahore[ptGraphics->uCurrentFrameIndex]},
.auWaitSemaphoreValues = {ulValue2}
};
tCommandBuffer = gptGfx->begin_command_recording(ptGraphics, &tBeginInfo11);

gptRenderer->do_shadows(tCommandBuffer, ptAppData->uSceneHandle0, ptAppData->uViewHandle0, ptAppData->tMainCamera, ptAppData->tSunlight);


gptGfx->end_command_recording(ptGraphics, &tCommandBuffer);

const plSubmitInfo tSubmitInfo11 = {
.uSignalSemaphoreCount = 1,
.atSignalSempahores = {ptAppData->atSempahore[ptGraphics->uCurrentFrameIndex]},
.auSignalSemaphoreValues = {ulValue3}
};
gptGfx->submit_command_buffer(ptGraphics, &tCommandBuffer, &tSubmitInfo11);

plViewOptions tViewOptions = {
.bShowAllBoundingBoxes = ptAppData->bDrawAllBoundingBoxes,
.bShowVisibleBoundingBoxes = ptAppData->bDrawVisibleBoundingBoxes,
Expand All @@ -488,13 +510,12 @@ pl_app_update(plAppData* ptAppData)
if(ptAppData->bFrustumCulling && ptAppData->bFreezeCullCamera)
tViewOptions.ptCullCamera = ptCullCamera;


// second set of work

const plBeginCommandInfo tBeginInfo1 = {
.uWaitSemaphoreCount = 1,
.atWaitSempahores = {ptAppData->atSempahore[ptGraphics->uCurrentFrameIndex]},
.auWaitSemaphoreValues = {ulValue2}
.auWaitSemaphoreValues = {ulValue3}
};
tCommandBuffer = gptGfx->begin_command_recording(ptGraphics, &tBeginInfo1);
gptRenderer->render_scene(tCommandBuffer, ptAppData->uSceneHandle0, ptAppData->uViewHandle0, tViewOptions);
Expand All @@ -504,7 +525,7 @@ pl_app_update(plAppData* ptAppData)
const plSubmitInfo tSubmitInfo1 = {
.uSignalSemaphoreCount = 1,
.atSignalSempahores = {ptAppData->atSempahore[ptGraphics->uCurrentFrameIndex]},
.auSignalSemaphoreValues = {ulValue3}
.auSignalSemaphoreValues = {ulValue4}
};
gptGfx->submit_command_buffer(ptGraphics, &tCommandBuffer, &tSubmitInfo1);

Expand All @@ -513,7 +534,7 @@ pl_app_update(plAppData* ptAppData)
const plBeginCommandInfo tBeginInfo2 = {
.uWaitSemaphoreCount = 1,
.atWaitSempahores = {ptAppData->atSempahore[ptGraphics->uCurrentFrameIndex]},
.auWaitSemaphoreValues = {ulValue3},
.auWaitSemaphoreValues = {ulValue4},
};
tCommandBuffer = gptGfx->begin_command_recording(ptGraphics, &tBeginInfo2);

Expand Down Expand Up @@ -570,16 +591,11 @@ pl_app_update(plAppData* ptAppData)
ptAppData->bResize = true;
pl_checkbox("Always Resize", &ptAppData->bAlwaysResize);


pl_slider_float("Light X Dir", &ptLight->tDirection.x, -1.0f, 1.0f);
pl_slider_float("Light Y Dir", &ptLight->tDirection.y, -1.0f, 1.0f);
pl_slider_float("Light Z Dir", &ptLight->tDirection.z, -1.0f, 1.0f);

pl_slider_float("Camera Pos X", &ptTestCamera->tPos.x, -100.0f, 100.0f);
pl_slider_float("Camera Pos Y", &ptTestCamera->tPos.y, -100.0f, 100.0f);
pl_slider_float("Camera Pos Z", &ptTestCamera->tPos.z, -100.0f, 100.0f);
pl_slider_float("Camera Yaw", &ptTestCamera->fYaw, -PL_PI, PL_PI);
pl_slider_float("Camera Pitch", &ptTestCamera->fPitch, -PL_PI_2, PL_PI_2);
plLightComponent* ptLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, ptAppData->tSunlight);
pl_slider_float("split", &ptLight->fCascadeSplitLambda, 0.1f, 1.0f);
pl_slider_float("x", &ptLight->tDirection.x, -1.0f, 1.0f);
pl_slider_float("y", &ptLight->tDirection.y, -1.0f, 1.0f);
pl_slider_float("z", &ptLight->tDirection.z, -1.0f, 1.0f);

pl_end_collapsing_header();
}
Expand Down Expand Up @@ -635,7 +651,7 @@ pl_app_update(plAppData* ptAppData)
const plSubmitInfo tSubmitInfo2 = {
.uSignalSemaphoreCount = 1,
.atSignalSempahores = {ptAppData->atSempahore[ptGraphics->uCurrentFrameIndex]},
.auSignalSemaphoreValues = {ulValue4},
.auSignalSemaphoreValues = {ulValue5},
};
gptGfx->end_command_recording(ptGraphics, &tCommandBuffer);
if(!gptGfx->present(ptGraphics, &tCommandBuffer, &tSubmitInfo2))
Expand Down
123 changes: 5 additions & 118 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static void pl_ecs_attach_component (plComponentLibrary* ptLibrary, plEntity tEn
static void pl_ecs_deattach_component(plComponentLibrary* ptLibrary, plEntity tEntity);

// lights
static void pl_ecs_set_light_shadows(plComponentLibrary* ptLibrary, plEntity tLight, plEntity tSceneCamera, uint32_t uCascadeCount);
static void pl_ecs_set_light_shadows(plComponentLibrary* ptLibrary, plEntity tLight, uint32_t uCascadeCount);

// update systems
static void pl_run_object_update_system (plComponentLibrary* ptLibrary);
Expand All @@ -91,7 +91,6 @@ static void pl_run_skin_update_system (plComponentLibrary* ptLibrar
static void pl_run_hierarchy_update_system (plComponentLibrary* ptLibrary);
static void pl_run_animation_update_system (plComponentLibrary* ptLibrary, float fDeltaTime);
static void pl_run_inverse_kinematics_update_system(plComponentLibrary* ptLibrary);
static void pl_run_light_update_system (plComponentLibrary* ptLibrary);

// misc.
static void pl_calculate_normals (plMeshComponent* atMeshes, uint32_t uComponentCount);
Expand Down Expand Up @@ -164,7 +163,6 @@ pl_load_ecs_api(void)
.run_hierarchy_update_system = pl_run_hierarchy_update_system,
.run_skin_update_system = pl_run_skin_update_system,
.run_animation_update_system = pl_run_animation_update_system,
.run_light_update_system = pl_run_light_update_system,
.run_inverse_kinematics_update_system = pl_run_inverse_kinematics_update_system
};
return &tApi;
Expand Down Expand Up @@ -686,13 +684,13 @@ pl_ecs_add_component(plComponentLibrary* ptLibrary, plComponentType tType, plEnt
.fIntensity = 1.0f,
.fRange = 10.0f,
.tType = PL_LIGHT_TYPE_DIRECTIONAL,
.fCascadeSplitLambda = 0.95f,
.fCascadeSplitLambda = 0.8f,
.uCascadeCount = 0,
.tFlags = 0,
.tSceneCamera = {UINT32_MAX, UINT32_MAX}
.afCascadeSplits = {0}
};
for(uint32_t i = 0; i < PL_MAX_SHADOW_CASCADES; i++)
sbComponents[uComponentIndex].atShadowCameras[i] = (plEntity){UINT32_MAX, UINT32_MAX};
sbComponents[uComponentIndex].afCascadeSplits[i] = 8 * powf(10.0f, (float)i);
return &sbComponents[uComponentIndex];
}

Expand Down Expand Up @@ -752,17 +750,11 @@ pl_ecs_create_point_light(plComponentLibrary* ptLibrary, const char* pcName, plV
}

static void
pl_ecs_set_light_shadows(plComponentLibrary* ptLibrary, plEntity tLight, plEntity tSceneCamera, uint32_t uCascadeCount)
pl_ecs_set_light_shadows(plComponentLibrary* ptLibrary, plEntity tLight, uint32_t uCascadeCount)
{
plLightComponent* ptLight = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_LIGHT, tLight);
ptLight->uCascadeCount = uCascadeCount;
ptLight->tSceneCamera = tSceneCamera;
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;
for(uint32_t i = 0; i < uCascadeCount; i++)
{
ptLight->atShadowCameras[i] = pl_ecs_create_orthographic_camera(ptLibrary,
"shadow camera", (plVec3){0.0f, 0.0f, 0.0f},1.0f, 1.0f, 0.0f, 1.0f);
}
}

static plEntity
Expand Down Expand Up @@ -1238,111 +1230,6 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime)
pl_end_profile_sample();
}

static void
pl_run_light_update_system(plComponentLibrary* ptLibrary)
{
pl_begin_profile_sample(__FUNCTION__);

plLightComponent* sbtComponents = ptLibrary->tLightComponentManager.pComponents;
const uint32_t uComponentCount = pl_sb_size(sbtComponents);
for(uint32_t uComponentIndex = 0; uComponentIndex < uComponentCount; uComponentIndex++)
{
plLightComponent* ptLight = &sbtComponents[uComponentIndex];

if(ptLight->uCascadeCount == 0)
continue;

plCameraComponent* ptSceneCamera = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_CAMERA, ptLight->tSceneCamera);

// float fCascadeSplitLambda;
// uint32_t uCascadeCount;
// plEntity tSceneCamera;
// plEntity atShadowCameras[PL_MAX_SHADOW_CASCADES];

float afCascadeSplits[PL_MAX_SHADOW_CASCADES] = {0};

const float fNearClip = ptSceneCamera->fNearZ;
const float fFarClip = ptSceneCamera->fFarZ;
const float fClipRange = fFarClip - fNearClip;

const float fMinZ = fNearClip;
const float fMaxZ = fNearClip + fClipRange;

const float fRange = fMaxZ - fMinZ;
const float fRatio = fMaxZ / fMinZ;

for(uint32_t i = 0; i < ptLight->uCascadeCount; i++)
{
const float p = (i + 1) / (float)ptLight->uCascadeCount;
const float fLog = fMinZ * powf(fRatio, p);
const float fUniform = fMinZ + fRange * p;
const float fD = ptLight->fCascadeSplitLambda * (fLog - fUniform) + fUniform;
afCascadeSplits[i] = (fD - fNearClip) / fClipRange;
}

float fLastSplitDist = 0.0;
for(uint32_t j = 0; j < ptLight->uCascadeCount; j++)
{
const float fSplitDist = afCascadeSplits[j];

plCameraComponent* ptShadowCamera = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_CAMERA, ptLight->atShadowCameras[j]);

plVec3 atCameraCorners[] = {
{ -1.0f, 1.0f, 0.0f },
{ -1.0f, -1.0f, 0.0f },
{ 1.0f, -1.0f, 0.0f },
{ 1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f },
{ -1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f },
};
plMat4 tCameraInversion = pl_mul_mat4(&ptSceneCamera->tProjMat, &ptSceneCamera->tViewMat);
tCameraInversion = pl_mat4_invert(&tCameraInversion);
for(uint32_t i = 0; i < 8; i++)
{
plVec4 tInvCorner = pl_mul_mat4_vec4(&tCameraInversion, (plVec4){.xyz = atCameraCorners[i], .w = 1.0f});
atCameraCorners[i] = pl_div_vec3_scalarf(tInvCorner.xyz, tInvCorner.w);
}

for(uint32_t i = 0; i < 4; i++)
{
const plVec3 tDist = pl_sub_vec3(atCameraCorners[i + 4], atCameraCorners[i]);
atCameraCorners[i + 4] = pl_add_vec3(atCameraCorners[i], pl_mul_vec3_scalarf(tDist, fSplitDist));
atCameraCorners[i] = pl_add_vec3(atCameraCorners[i], pl_mul_vec3_scalarf(tDist, fLastSplitDist));
}

// get frustum center
plVec3 tFrustumCenter = {0};
for(uint32_t i = 0; i < 8; i++)
tFrustumCenter = pl_add_vec3(tFrustumCenter, atCameraCorners[i]);
tFrustumCenter = pl_div_vec3_scalarf(tFrustumCenter, 8.0f);

float fRadius = 0.0f;
for (uint32_t i = 0; i < 8; i++)
{
float fDistance = pl_length_vec3(pl_sub_vec3(atCameraCorners[i], tFrustumCenter));
fRadius = pl_max(fRadius, fDistance);
}
fRadius = ceilf(fRadius * 16.0f) / 16.0f;

plVec3 tDirection = ptLight->tDirection;

tDirection = pl_norm_vec3(tDirection);
plVec3 tEye = pl_sub_vec3(tFrustumCenter, pl_mul_vec3_scalarf(tDirection, fRadius));

pl_camera_look_at(ptShadowCamera, tEye, tFrustumCenter);
ptShadowCamera->fWidth = fRadius * 2.0f;
ptShadowCamera->fHeight = fRadius * 2.0f;
ptShadowCamera->fNearZ = 0;
ptShadowCamera->fFarZ = fRadius * 2.0f;
pl_camera_update(ptShadowCamera);
fLastSplitDist = afCascadeSplits[j];
}
}
pl_end_profile_sample();
}

static void
pl_run_inverse_kinematics_update_system(plComponentLibrary* ptLibrary)
{
Expand Down
12 changes: 4 additions & 8 deletions extensions/pl_ecs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ typedef struct _plEcsI
void (*calculate_tangents)(plMeshComponent*, uint32_t uMeshCount);

// lights
void (*set_light_shadows)(plComponentLibrary*, plEntity tLight, plEntity tSceneCamera, uint32_t uCascadeCount);
void (*set_light_shadows)(plComponentLibrary*, plEntity tLight, uint32_t uCascadeCount);

// systems
void (*run_object_update_system) (plComponentLibrary*);
Expand All @@ -163,7 +163,6 @@ typedef struct _plEcsI
void (*run_hierarchy_update_system) (plComponentLibrary*);
void (*run_animation_update_system) (plComponentLibrary*, float fDeltaTime);
void (*run_inverse_kinematics_update_system)(plComponentLibrary*);
void (*run_light_update_system) (plComponentLibrary*);
} plEcsI;

typedef struct _plCameraI
Expand Down Expand Up @@ -392,12 +391,9 @@ typedef struct _plLightComponent
float fRange;
plVec3 tPosition;
plVec3 tDirection;

// shadows
float fCascadeSplitLambda;
uint32_t uCascadeCount;
plEntity tSceneCamera;
plEntity atShadowCameras[PL_MAX_SHADOW_CASCADES];
float fCascadeSplitLambda;
float afCascadeSplits[PL_MAX_SHADOW_CASCADES];
uint32_t uCascadeCount;
} plLightComponent;

typedef struct _plObjectComponent
Expand Down
Loading

0 comments on commit 946a96e

Please sign in to comment.