fix: shadows

This commit is contained in:
2025-10-11 18:55:37 +02:00
parent ff9e23255c
commit 884696feaa
3 changed files with 25 additions and 9 deletions

View File

@ -42,7 +42,7 @@ public:
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f)); m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
m_registry.emplace<camera>(cameraEntity); m_registry.emplace<camera>(cameraEntity);
Object* targetObj = Object::LoadFile("./assets/cube.obj"); Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
const auto targetEntity = m_registry.create(); const auto targetEntity = m_registry.create();
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.5f, 0.f)); m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.5f, 0.f));
m_registry.emplace<mesh>(targetEntity, std::unique_ptr<Object>(targetObj)); m_registry.emplace<mesh>(targetEntity, std::unique_ptr<Object>(targetObj));

View File

@ -158,23 +158,39 @@ void Renderer::Render(entt::registry& registry) {
auto shadowLight = registry.view<light, transform>().back(); auto shadowLight = registry.view<light, transform>().back();
auto &comp = registry.get<transform>(shadowLight); auto &comp = registry.get<transform>(shadowLight);
float near_plane = -10.0f, far_plane = 20.0f; float near_plane = 0.1f, far_plane = 50.0f; // pick bounds that cover your scene
glm::mat4 lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane); glm::vec3 lightPos = comp.position;
glm::vec3 target = glm::vec3(0.0f, 0.5f, 0.0f);
glm::mat4 lightView = glm::lookAt(comp.position, glm::mat4 lightView = glm::lookAt(lightPos, target, glm::vec3(0.0f, 1.0f, 0.0f));
glm::vec3( 0.0f, 0.0f, 0.0f), glm::mat4 lightProjection = glm::ortho(-6.0f, 6.0f, -6.0f, 6.0f, 1.0f, 20.0f);
glm::vec3( 0.0f, 1.0f, 0.0f));
glm::mat4 lightSpaceMatrix = lightProjection * lightView; glm::mat4 lightSpaceMatrix = lightProjection * lightView;
// lightView = glm::lookAt(/*eye*/ -lightDir * distance, /*center*/ vec3(0), up)
// glm::mat4 lightSpaceMatrix = lightProjection * lightView;
SwitchShader(&m_depthShader); SwitchShader(&m_depthShader);
m_currentShader->setMat4("u_lightSpace", lightSpaceMatrix); m_currentShader->setMat4("u_lightSpace", lightSpaceMatrix);
// enable culling and render front faces to the shadow map
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT); // only for the depth pass
// or use polygon offset:
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(2.0f, 4.0f);
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, m_depth_fbo); glBindFramebuffer(GL_FRAMEBUFFER, m_depth_fbo);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
RenderScene(registry); RenderScene(registry);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
// enable culling and render front faces to the shadow map
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); // only for the depth pass
// or use polygon offset:
glDisable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(0.f, 1.f);
glViewport(0, 0, Window::GetWidth(), Window::GetHeight()); glViewport(0, 0, Window::GetWidth(), Window::GetHeight());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@ -60,7 +60,7 @@ float ShadowCalculation(vec4 fragPosLightSpace, vec3 N, vec3 L)
float currentDepth = projCoords.z; float currentDepth = projCoords.z;
// bias to prevent self-shadowing (depend on slope) // bias to prevent self-shadowing (depend on slope)
float bias = max(0.05 * (1.0 - dot(N, L)), 0.005); float bias = max(0.001 * (1.0 - dot(N, L)), 0.0005);
// PCF (3x3) // PCF (3x3)
float shadow = 0.0; float shadow = 0.0;