Skip to content

Added shadow mapping for directional lights #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Feb 7, 2025

Conversation

adriengivry
Copy link
Member

@adriengivry adriengivry commented Nov 15, 2024

Description

Implementation of shadow mapping!

  • Shadow casting AND receiving is enabled on the Default.ovmat material by default.
  • When a new material is created, these 2 settings are OFF by default.
  • Directional lights (components) have a "Shadow casting" option that is OFF by default.
  • Includes 2 implementations for shadows: hard-shadows, and soft-shadows/PCF (default).
  • Introduces a TextureHandle class, which wraps an OpenGL texture ID, and can be passed as a uniform. This TextureHandle class is used by the framebuffer to expose its rendered image.
  • Texture now inherits from TextureHandle.
  • ShadowRenderPass added: handles the rendering of shadow casters in the shadow map
  • ShadowRenderFeature added: ensures that the shadow map is properly bound to each entity set to receive shadows.
  • Shadow area size is exposed through the CDirectionaLight component. Changing this setting will change the radius of the shadow (bigger radius = less precision)
  • Shadow area size is exposed through the CDirectionaLight component.
  • Shadow follow camera is a setting exposed through the CDirectionaLight component allowing the directional light to use the position of the camera as a source for its light space matrix (for shadow map rendering), making shadows "follow" the camera around, instead of being static.
  • Added a singleUse parameter to Material::Set, for properties that shouldn't be stored after drawing (i.e. shadow maps texture handles)

Notes

This PR also updates OvEditor and OvGame premake files to disable fastuptodate, which forces both project to trigger their post-build command before any run (resulting in resources being properly updated between runs, even if no source file changed).

Limitations

  • Only one directional light can cast shadows per scene, if more are found, warnings will be logged in the console and the subsequent lights will be ignored.
  • Shadow casting passes use a custom Shadow.ovfx shader, which doesn't vertex transformations outside of the regular modelMatrix * vec4(geo_Pos, 1.0);, which could result in shaders using instancing or vertex displacement to not cast shadows properly. One way to mitigate this would be to add a way to specify a shader override for certain passes (like shadows), through the material. However this change should be made in a separate PR.
  • Shadow map resolution isn't exposed at the moment, as changing it could introduce shadow acne (the shadow bias being hard coded, not depending on the shadow map resolution).

To-Do

  • Move shadow implementation outside of the editor (Rendering/Core)
  • Cleanup shader code
  • Make it use scene directional lights instead of the manually set one
  • Take ambient light into account
  • Add options to enable/disable shadow casting/receiving
  • Make sure the ShadowRenderFeature only runs on materials set to receive shadow
  • Cleanup frame buffer
  • Investigate better architecture for Texture/TextureHandle
  • Increase directional light radius
  • Make it work with other lighting models (PBR...)
  • Fix material exception on edit for the 2nd time (related to std::any usage)
  • Make shadow area size changeable
  • Validate frustum behaviour (this feature is broken anyway, so I guess we'll see later)
  • Add prefix to exclude some uniforms from being exposed to the material editor
  • Expose new methods & settings to lua
  • Add new methods & settings to the wiki

Related Issues

Fixes #250
Fixes #313

Screenshots

image
overload-shadow-on-off

Resources

Mostly followed:
https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping

@adriengivry adriengivry self-assigned this Nov 15, 2024
@adriengivry adriengivry changed the title Dirty implementation (WIP) Shadow Mapping (WIP) Nov 15, 2024
@adriengivry adriengivry force-pushed the feature/shadow_mapping branch from 3be07a2 to 61200e6 Compare November 16, 2024 00:56
@adriengivry adriengivry changed the title Shadow Mapping (WIP) Added shadow mapping for directional lights Nov 21, 2024
@adriengivry adriengivry marked this pull request as ready for review November 21, 2024 15:44
Copy link
Member Author

@adriengivry adriengivry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting some weird logs in the console with the shadow mapping. I need to investigate!

[ERROR] 2024-11-21_12-34-55 OpenGL Debug Message:
Debug message (1282): GL_INVALID_OPERATION error generated. Texture name does not refer to a texture object generated by OpenGL.
Source: API
Type: Error
Severity: High

Edit: Fixed! Introduced singleUse parameter in Material::Set

…ny temporary TextureHandle (such as a ShadowMap) passed to a material will be released after use (to avoid accessing deallocated memory)
@adriengivry adriengivry mentioned this pull request Nov 22, 2024
10 tasks
@adriengivry adriengivry added Feature Graphics Graphical feature labels Feb 7, 2025
@adriengivry adriengivry merged commit c4262a1 into develop Feb 7, 2025
1 check passed
@adriengivry adriengivry deleted the feature/shadow_mapping branch February 7, 2025 03:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Graphics Graphical feature
Development

Successfully merging this pull request may close these issues.

Add missing types to serializer, material, and shader uniforms Shadow casting
2 participants