Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Dec 19, 2024
1 parent 02e7ac4 commit 4c97cd7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1589,9 +1589,9 @@ pl_camera_update(plCameraComponent* ptCamera)
const float fInvtanHalfFovy = 1.0f / tanf(ptCamera->fFieldOfView / 2.0f);
ptCamera->tProjMat.col[0].x = fInvtanHalfFovy / ptCamera->fAspectRatio;
ptCamera->tProjMat.col[1].y = fInvtanHalfFovy;
ptCamera->tProjMat.col[2].z = ptCamera->fNearZ / (ptCamera->fNearZ - ptCamera->fFarZ);
ptCamera->tProjMat.col[2].z = ptCamera->fFarZ / (ptCamera->fFarZ - ptCamera->fNearZ);
ptCamera->tProjMat.col[2].w = 1.0f;
ptCamera->tProjMat.col[3].z = -ptCamera->fNearZ * ptCamera->fFarZ / (ptCamera->fNearZ - ptCamera->fFarZ);
ptCamera->tProjMat.col[3].z = -ptCamera->fNearZ * ptCamera->fFarZ / (ptCamera->fFarZ - ptCamera->fNearZ);
ptCamera->tProjMat.col[3].w = 0.0f;
}
else if(ptCamera->tType == PL_CAMERA_TYPE_ORTHOGRAPHIC)
Expand Down
6 changes: 3 additions & 3 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -2425,7 +2425,7 @@ pl_refr_reload_scene_shaders(uint32_t uSceneHandle)
.pTempConstantData = aiConstantData0,
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
.ulDepthMode = PL_COMPARE_MODE_GREATER_OR_EQUAL,
.ulCullMode = PL_CULL_MODE_NONE,
.ulWireframe = 0,
.ulStencilMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -2645,7 +2645,7 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)
.pTempConstantData = aiConstantData0,
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
.ulDepthMode = PL_COMPARE_MODE_GREATER_OR_EQUAL,
.ulCullMode = PL_CULL_MODE_NONE,
.ulWireframe = 0,
.ulStencilMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -2930,7 +2930,7 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)
.tStencilStoreOp = PL_STORE_OP_DONT_CARE,
.tCurrentUsage = PL_TEXTURE_USAGE_SAMPLED,
.tNextUsage = PL_TEXTURE_USAGE_SAMPLED,
.fClearZ = 1.0f
.fClearZ = 0.0f
},
.tDimensions = {.x = gptData->tShadowAtlasResolution.x, .y = gptData->tShadowAtlasResolution.y}
};
Expand Down
42 changes: 33 additions & 9 deletions extensions/pl_renderer_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,20 @@ pl__sat_visibility_test(plCameraComponent* ptCamera, const plAABB* ptAABB)
{
const float fTanFov = tanf(0.5f * ptCamera->fFieldOfView);

const float fZNear = ptCamera->fNearZ;
const float fZFar = ptCamera->fFarZ;
// const float fZNear = ptCamera->fNearZ;
// const float fZFar = ptCamera->fFarZ;

// reverse z
const float fZFar = ptCamera->fNearZ;
const float fZNear = ptCamera->fFarZ;

// half width, half height
const float fXNear = ptCamera->fAspectRatio * ptCamera->fNearZ * fTanFov;
const float fYNear = ptCamera->fNearZ * fTanFov;
// const float fXNear = ptCamera->fAspectRatio * ptCamera->fNearZ * fTanFov;
// const float fYNear = ptCamera->fNearZ * fTanFov;

// reverse z
const float fXNear = ptCamera->fAspectRatio * ptCamera->fFarZ * fTanFov;
const float fYNear = ptCamera->fFarZ * fTanFov;

// consider four adjacent corners of the AABB
plVec3 atCorners[] = {
Expand Down Expand Up @@ -760,13 +768,23 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer
plCameraComponent* ptSceneCamera = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_CAMERA, tCamera);

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

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

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


// const float fMinZ = fFarClip;
// const float fMaxZ = fFarClip + fClipRange;

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

Expand Down Expand Up @@ -831,9 +849,12 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer
{ -1.0f, -1.0f, 0.0f },
{ 1.0f, -1.0f, 0.0f },
{ 1.0f, 1.0f, 0.0f },
}; // x is reversed for reverse z here currently
};
// plMat4 tCameraInversion = pl_mul_mat4(&tCameraProjMat, &ptSceneCamera->tViewMat);
plMat4 tCameraInversion = pl_mul_mat4(&ptSceneCamera->tProjMat, &ptSceneCamera->tViewMat);
tCameraInversion = pl_mat4_invert(&tCameraInversion);

// convert to world space
for(uint32_t i = 0; i < 8; i++)
{
plVec4 tInvCorner = pl_mul_mat4_vec4(&tCameraInversion, (plVec4){.xyz = atCameraCorners[i], .w = 1.0f});
Expand Down Expand Up @@ -880,7 +901,9 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer
tShadowCamera.fNearZ = 0.01f;
fLastSplitDist = fSplitDist;

// tShadowCamera.tProjMat.col[2].z *= -1.0f;
atCamViewProjs[uCascade] = pl_mul_mat4(&tShadowCamera.tProjMat, &tShadowCamera.tViewMat);

ptShadowData->cascadeViewProjMat[uCascade] = atCamViewProjs[uCascade];
}

Expand Down Expand Up @@ -956,7 +979,8 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer

gptGfx->reset_draw_stream(ptStream, uVisibleOpaqueDrawCount + uVisibleTransparentDrawCount);

gptGfx->set_depth_bias(ptEncoder, 0.005f, 0.0f, 1.0f);
gptGfx->set_depth_bias(ptEncoder, 0.005f, 0.0f, -1.0f);
// gptGfx->set_depth_bias(ptEncoder, 0.0f, 0.0f, 0.0f);

for(uint32_t i = 0; i < uVisibleOpaqueDrawCount; i++)
{
Expand Down Expand Up @@ -1350,7 +1374,7 @@ pl_refr_create_global_shaders(void)
.tVertexShader = gptShader->load_glsl("../shaders/shadow.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
.ulDepthMode = PL_COMPARE_MODE_GREATER_OR_EQUAL,
.ulCullMode = PL_CULL_MODE_NONE,
.ulWireframe = 0,
.ulStencilMode = PL_COMPARE_MODE_ALWAYS,
Expand Down
6 changes: 3 additions & 3 deletions sandbox/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)

// create main camera
plCameraComponent* ptMainCamera = NULL;
ptEditorData->tMainCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "main camera", (plVec3){-9.6f, 2.096f, 0.86f}, PL_PI_3, ptIO->tMainViewportSize.x / ptIO->tMainViewportSize.y, 0.1f, 48.0f, &ptMainCamera);
ptEditorData->tMainCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "main camera", (plVec3){-9.6f, 2.096f, 0.86f}, PL_PI_3, ptIO->tMainViewportSize.x / ptIO->tMainViewportSize.y, 48.0f, 0.1f, &ptMainCamera);
gptCamera->set_pitch_yaw(ptMainCamera, -0.245f, 1.816f);
gptCamera->update(ptMainCamera);
gptEcs->attach_script(ptMainComponentLibrary, "pl_script_camera", PL_SCRIPT_FLAG_PLAYING, ptEditorData->tMainCamera, NULL);

// create cull camera
plCameraComponent* ptCullCamera = NULL;
ptEditorData->tCullCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "cull camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->tMainViewportSize.x / ptIO->tMainViewportSize.y, 0.1f, 25.0f, &ptCullCamera);
ptEditorData->tCullCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "cull camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->tMainViewportSize.x / ptIO->tMainViewportSize.y, 25.0f, 0.1f, &ptCullCamera);
gptCamera->set_pitch_yaw(ptCullCamera, 0.0f, PL_PI);
gptCamera->update(ptCullCamera);

Expand All @@ -200,7 +200,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;

plLightComponent* ptLight2 = NULL;
gptEcs->create_directional_light(ptMainComponentLibrary, "sunlight2", (plVec3){0.0f, -1.0f, -1.0f}, &ptLight2);
gptEcs->create_directional_light(ptMainComponentLibrary, "sunlight2", (plVec3){0.375f, -1.0f, -0.085f}, &ptLight2);
ptLight2->uCascadeCount = 4;
ptLight2->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;

Expand Down
17 changes: 5 additions & 12 deletions shaders/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,14 @@ const mat4 biasMat = mat4(
0.5, 0.5, 0.0, 1.0
);

const vec2 atCascadeOffsets[4] = {
vec2(0),
vec2(0, 0.5),
vec2(0.5, 0),
vec2(0.5, 0.5),
};

float textureProj(vec4 shadowCoord, vec2 offset, int textureIndex)
{
float shadow = 1.0;
vec2 comp2 = shadowCoord.st + offset;

if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
{
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
float dist = 1.0 - texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
if (shadowCoord.w > 0 && dist < shadowCoord.z)
{
shadow = 0.01; // ambient
Expand Down Expand Up @@ -368,11 +361,11 @@ void main()
abiasMat[1][1] *= tShadowData.fFactor;
abiasMat[3][0] *= tShadowData.fFactor;
abiasMat[3][1] *= tShadowData.fFactor;
vec4 shadowCoord = (abiasMat * tShadowData.cascadeViewProjMat[cascadeIndex]) * vec4(tWorldPosition.xyz, 1.0);
vec4 shadowCoord = (abiasMat * tShadowData.cascadeViewProjMat[cascadeIndex]) * vec4(tWorldPosition.xyz, 1.0);
// shadowCoord.z = -shadowCoord.z;
shadow = 0;
// shadow = textureProj(shadowCoord / shadowCoord.w, atCascadeOffsets[cascadeIndex], tShadowData.iShadowMapTexIdx);
// shadow = filterPCF(shadowCoord / shadowCoord.w, atCascadeOffsets[cascadeIndex], tShadowData.iShadowMapTexIdx);
shadow = filterPCF(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * 0.25, 0), tShadowData.iShadowMapTexIdx);
// shadow = textureProj(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
shadow = filterPCF(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
}

if(tLightData.iType != PL_LIGHT_TYPE_DIRECTIONAL)
Expand Down
1 change: 1 addition & 0 deletions shaders/shadow.vert
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void main()

vec4 pos = tObjectInfo.tModel * inPosition;
gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.iIndex] * pos;
gl_Position.z = 1.0 - gl_Position.z;
tShaderIn.tUV[0] = inTexCoord0;
tShaderIn.tUV[1] = inTexCoord1;
tShaderIn.tUV[2] = inTexCoord2;
Expand Down

0 comments on commit 4c97cd7

Please sign in to comment.