-
Notifications
You must be signed in to change notification settings - Fork 3
✨ feat: lighting #25
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
✨ feat: lighting #25
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0b99262
feat: add TextureUnits enum for texture management
AhmedAmrNabil a630647
feat: add light component with deserialization
AhmedAmrNabil 0def554
feat: add LitMaterial class with lighting support and texture management
AhmedAmrNabil 725afc8
feat: add deserialization support for Light assets in AssetLoader
AhmedAmrNabil 139990e
fix: add light component source file to the project
AhmedAmrNabil 611617a
feat: make light a component instead of asset
AhmedAmrNabil 0a1e718
feat: setup lights for shaders in forward-renderer
AhmedAmrNabil 8b79826
fix: fix lit material deserialize
AhmedAmrNabil c2ba863
feat: add tangent attribute to Vertex and update Mesh for normal mapping
AhmedAmrNabil 168a016
feat: initialize texture pointers and alphaThreshold in LitMaterial
AhmedAmrNabil 3404e75
fix: pass camera pos as vec3 in forward renderer
AhmedAmrNabil edd9030
feat(flake): add gdb for debugging
AhmedAmrNabil 2aa4ba4
fix: correct spotAngles assignment in Light deserialization
AhmedAmrNabil 7d76f0b
fix: correct light direction calculation
AhmedAmrNabil 3480dbe
feat: add initial lighting shader
AhmedAmrNabil 9953a3b
feat: add lighting test scene
AhmedAmrNabil 42e5dc5
fix: constrain max lights to 8 in cpp code
AhmedAmrNabil e6ca1ed
fix: correctly calculate spotlight
AhmedAmrNabil 86ebe32
feat: add default sampler for light material
AhmedAmrNabil 11fabbd
fix: update light uniform setup in lit materials
AhmedAmrNabil a3446fd
fix: fix light direction and spotlight
AhmedAmrNabil f38f6f4
chore: remove legacy clear asset
AhmedAmrNabil 55f57ae
chore: update default sampler name in asset loader and material deser…
AhmedAmrNabil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| #version 330 core | ||
|
|
||
| in Varyings { | ||
| vec4 color; | ||
| vec2 tex_coord; | ||
| vec3 worldPos; | ||
| vec3 worldNormal; | ||
| mat3 TBN; | ||
| } fs_in; | ||
|
|
||
| out vec4 frag_color; | ||
|
|
||
| // ── Material ───────────────────────────────────────────────────────── | ||
| struct Material { | ||
| // factors (used when no texture) | ||
| vec3 albedo; | ||
| float metallic; | ||
| float roughness; | ||
| float ambientOcclusion; | ||
| vec3 emission; | ||
|
|
||
| // textures + flags | ||
| sampler2D textureAlbedo; | ||
| bool hasTextureAlbedo; | ||
|
|
||
| sampler2D textureMetallic; | ||
| bool hasTextureMetallic; | ||
|
|
||
| sampler2D textureRoughness; | ||
| bool hasTextureRoughness; | ||
|
|
||
| sampler2D textureNormal; | ||
| bool hasTextureNormal; | ||
|
|
||
| sampler2D textureAmbientOcclusion; | ||
| bool hasTextureAmbientOcclusion; | ||
|
|
||
| sampler2D textureEmissive; | ||
| bool hasTextureEmissive; | ||
| }; | ||
| uniform Material material; | ||
| uniform float alphaThreshold; | ||
| uniform vec4 tint; | ||
|
|
||
| // ── Lights ─────────────────────────────────────────────────────────── | ||
| #define MAX_LIGHTS 8 | ||
|
|
||
| #define LIGHT_DIRECTIONAL 0 | ||
| #define LIGHT_POINT 1 | ||
| #define LIGHT_SPOT 2 | ||
|
|
||
| struct Light { | ||
| int type; | ||
| vec3 color; | ||
| vec3 position; | ||
| vec3 direction; | ||
| vec3 attenuation; // (constant, linear, quadratic) | ||
| vec2 spotAngles; // (inner, outer) in radians | ||
| }; | ||
| uniform Light lights[MAX_LIGHTS]; | ||
| uniform int numLights; | ||
|
|
||
| uniform vec3 cameraPos; | ||
|
|
||
| // ── Helpers ─────────────────────────────────────────────────────────── | ||
| vec3 sampleAlbedo(vec2 uv) { | ||
| vec3 base = material.hasTextureAlbedo ? texture(material.textureAlbedo, uv).rgb : vec3(1.0); | ||
| return base * material.albedo; | ||
| } | ||
|
|
||
| float sampleMetallic(vec2 uv) { | ||
| return material.hasTextureMetallic ? texture(material.textureMetallic, uv).r * material.metallic : material.metallic; | ||
| } | ||
|
|
||
| float sampleRoughness(vec2 uv) { | ||
| return material.hasTextureRoughness ? texture(material.textureRoughness, uv).r * material.roughness : material.roughness; | ||
| } | ||
|
|
||
| float sampleAO(vec2 uv) { | ||
| return material.hasTextureAmbientOcclusion ? texture(material.textureAmbientOcclusion, uv).r * material.ambientOcclusion : material.ambientOcclusion; | ||
| } | ||
|
|
||
| vec3 sampleEmission(vec2 uv) { | ||
| return material.hasTextureEmissive ? texture(material.textureEmissive, uv).rgb * material.emission : material.emission; | ||
| } | ||
|
|
||
| vec3 sampleNormal(vec2 uv) { | ||
| if(!material.hasTextureNormal) | ||
| return normalize(fs_in.worldNormal); | ||
| vec3 n = texture(material.textureNormal, uv).rgb * 2.0 - 1.0; | ||
| return normalize(fs_in.TBN * n); | ||
| } | ||
|
|
||
| // ── Blinn-Phong per light ───────────────────────────────────────────── | ||
| vec3 calcLight( | ||
| Light light, | ||
| vec3 N, | ||
| vec3 V, | ||
| vec3 albedo, | ||
| float roughness, | ||
| float metallic | ||
| ) { | ||
|
|
||
| vec3 L; | ||
| float attenuation = 1.0; | ||
|
|
||
| if(light.type == LIGHT_DIRECTIONAL) { | ||
| L = normalize(-light.direction); | ||
|
|
||
| } else { | ||
| vec3 toLight = light.position - fs_in.worldPos; | ||
| float dist = length(toLight); | ||
| L = normalize(toLight); | ||
|
|
||
| // quadratic attenuation: 1 / (c + l*d + q*d^2) | ||
| attenuation = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist); | ||
|
|
||
| if(light.type == LIGHT_SPOT) { | ||
| float theta = dot(L, normalize(-light.direction)); | ||
| float innerCos = cos(light.spotAngles.x); | ||
| float outerCos = cos(light.spotAngles.y); | ||
| float epsilon = innerCos - outerCos; | ||
| float spot = clamp((theta - outerCos) / epsilon, 0.0, 1.0); | ||
| attenuation *= spot; | ||
| } | ||
| } | ||
|
|
||
| // diffuse | ||
| float diff = max(dot(N, L), 0.0); | ||
| vec3 diffuse = diff * albedo * light.color; | ||
|
|
||
| // specular (Blinn-Phong) | ||
| // roughness → shininess: rough=1 → shininess=2, rough=0 → shininess=256 | ||
| float shininess = mix(256.0, 2.0, roughness); | ||
| vec3 H = normalize(L + V); | ||
| float spec = pow(max(dot(N, H), 0.0), shininess); | ||
| // metallic surfaces use albedo color for specular | ||
| vec3 specColor = mix(vec3(0.04), albedo, metallic); | ||
| vec3 specular = spec * specColor * light.color; | ||
|
|
||
| return (diffuse + specular) * attenuation; | ||
| } | ||
|
|
||
| // ── Main ────────────────────────────────────────────────────────────── | ||
| void main() { | ||
| vec2 uv = fs_in.tex_coord; | ||
| vec3 albedo = sampleAlbedo(uv) * fs_in.color.rgb * tint.rgb; | ||
| float metallic = sampleMetallic(uv); | ||
| float roughness = sampleRoughness(uv); | ||
| float ao = sampleAO(uv); | ||
| vec3 emission = sampleEmission(uv); | ||
| vec3 N = sampleNormal(uv); | ||
| vec3 V = normalize(cameraPos - fs_in.worldPos); | ||
|
|
||
| // ambient | ||
| vec3 ambient = vec3(0.03) * albedo * ao; | ||
|
|
||
| // accumulate all lights | ||
| vec3 lighting = vec3(0.0); | ||
| int lightCount = min(numLights, MAX_LIGHTS); | ||
| for(int i = 0; i < lightCount; i++) { | ||
| lighting += calcLight(lights[i], N, V, albedo, roughness, metallic); | ||
| } | ||
|
|
||
| vec3 result = ambient + lighting + emission; | ||
|
|
||
| // alpha from albedo texture or tint | ||
| float alpha = material.hasTextureAlbedo ? texture(material.textureAlbedo, uv).a * tint.a * fs_in.color.a : tint.a * fs_in.color.a; | ||
|
|
||
| if(alpha < alphaThreshold) | ||
| discard; | ||
|
|
||
| frag_color = vec4(result, alpha); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #version 330 core | ||
|
|
||
| layout(location = 0) in vec3 position; | ||
| layout(location = 1) in vec4 color; | ||
| layout(location = 2) in vec2 tex_coord; | ||
| layout(location = 3) in vec3 normal; | ||
| layout(location = 4) in vec3 tangent; | ||
|
|
||
| out Varyings { | ||
| vec4 color; | ||
| vec2 tex_coord; | ||
| vec3 worldPos; | ||
| vec3 worldNormal; | ||
| mat3 TBN; | ||
| } vs_out; | ||
|
|
||
| uniform mat4 transform; | ||
| uniform mat4 model; | ||
|
|
||
| void main() { | ||
| gl_Position = transform * vec4(position, 1.0); | ||
|
|
||
| vs_out.color = color; | ||
| vs_out.tex_coord = tex_coord; | ||
| vs_out.worldPos = vec3(model * vec4(position, 1.0)); | ||
|
|
||
| // normal matrix — handles non-uniform scaling correctly | ||
| mat3 normalMatrix = transpose(inverse(mat3(model))); | ||
| vec3 N = normalize(normalMatrix * normal); | ||
| vs_out.worldNormal = N; | ||
|
|
||
| // TBN matrix for normal mapping | ||
| vec3 T = normalize(normalMatrix * tangent); | ||
| T = normalize(T - dot(T, N) * N); // re-orthogonalize | ||
| vec3 B = cross(N, T); | ||
| vs_out.TBN = mat3(T, B, N); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ | |
| just-lsp | ||
| ccache | ||
| clang-tools | ||
| gdb | ||
| ]; | ||
| }; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #include "light.hpp" | ||
|
|
||
| #include <deserialize-utils.hpp> | ||
| #include <glm/vec2.hpp> | ||
| #include <glm/vec3.hpp> | ||
| #include <json/json.hpp> | ||
|
|
||
| namespace our { | ||
|
|
||
| void Light::deserialize(const nlohmann::json& data) { | ||
| if (!data.is_object()) return; | ||
| std::string typeStr = data.value("lightType", "point"); | ||
| if (typeStr == "directional") { | ||
| type = LightType::DIRECTIONAL; | ||
| } else if (typeStr == "point") { | ||
| type = LightType::POINT; | ||
| } else if (typeStr == "spot") { | ||
| type = LightType::SPOT; | ||
|
AhmedAmrNabil marked this conversation as resolved.
|
||
| } | ||
| color = data.value("color", glm::vec3(1.0f, 1.0f, 1.0f)); | ||
| attenuation = data.value("attenuation", glm::vec3(1.0f, 0.0f, 0.0f)); | ||
| glm::vec2 spotAnglesDegrees = data.value("spotAngles", glm::vec2(15.0f, 30.0f)); | ||
| spotAngles = {glm::radians(spotAnglesDegrees.x), glm::radians(spotAnglesDegrees.y)}; | ||
| } | ||
| } // namespace our | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.