Skip to content
Merged
Show file tree
Hide file tree
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 Apr 13, 2026
a630647
feat: add light component with deserialization
AhmedAmrNabil Apr 13, 2026
0def554
feat: add LitMaterial class with lighting support and texture management
AhmedAmrNabil Apr 13, 2026
725afc8
feat: add deserialization support for Light assets in AssetLoader
AhmedAmrNabil Apr 13, 2026
139990e
fix: add light component source file to the project
AhmedAmrNabil Apr 13, 2026
611617a
feat: make light a component instead of asset
AhmedAmrNabil Apr 13, 2026
0a1e718
feat: setup lights for shaders in forward-renderer
AhmedAmrNabil Apr 13, 2026
8b79826
fix: fix lit material deserialize
AhmedAmrNabil Apr 13, 2026
c2ba863
feat: add tangent attribute to Vertex and update Mesh for normal mapping
AhmedAmrNabil Apr 13, 2026
168a016
feat: initialize texture pointers and alphaThreshold in LitMaterial
AhmedAmrNabil Apr 13, 2026
3404e75
fix: pass camera pos as vec3 in forward renderer
AhmedAmrNabil Apr 13, 2026
edd9030
feat(flake): add gdb for debugging
AhmedAmrNabil Apr 13, 2026
2aa4ba4
fix: correct spotAngles assignment in Light deserialization
AhmedAmrNabil Apr 13, 2026
7d76f0b
fix: correct light direction calculation
AhmedAmrNabil Apr 13, 2026
3480dbe
feat: add initial lighting shader
AhmedAmrNabil Apr 13, 2026
9953a3b
feat: add lighting test scene
AhmedAmrNabil Apr 13, 2026
42e5dc5
fix: constrain max lights to 8 in cpp code
AhmedAmrNabil Apr 15, 2026
e6ca1ed
fix: correctly calculate spotlight
AhmedAmrNabil Apr 15, 2026
86ebe32
feat: add default sampler for light material
AhmedAmrNabil Apr 15, 2026
11fabbd
fix: update light uniform setup in lit materials
AhmedAmrNabil Apr 15, 2026
a3446fd
fix: fix light direction and spotlight
AhmedAmrNabil Apr 15, 2026
f38f6f4
chore: remove legacy clear asset
AhmedAmrNabil Apr 15, 2026
55f57ae
chore: update default sampler name in asset loader and material deser…
AhmedAmrNabil Apr 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ add_executable(${PROJECT_NAME}
src/common/components/mesh-renderer.cpp
src/common/components/free-camera-controller.cpp
src/common/components/movement.cpp
src/common/components/light.cpp

src/game/components/collider.cpp
src/game/components/enemy.cpp
Expand Down
174 changes: 174 additions & 0 deletions assets/shaders/lit.frag
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
Comment thread
AhmedSobhy01 marked this conversation as resolved.

#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);
}
37 changes: 37 additions & 0 deletions assets/shaders/lit.vert
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);
}
40 changes: 34 additions & 6 deletions config/app.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"textured": {
"vs": "assets/shaders/textured.vert",
"fs": "assets/shaders/textured.frag"
},
"lit": {
"vs": "assets/shaders/lit.vert",
"fs": "assets/shaders/lit.frag"
}
},
"textures": {
Expand Down Expand Up @@ -144,8 +148,8 @@
"sampler": "default"
},
"monkey": {
"type": "textured",
"shader": "textured",
"type": "lit",
"shader": "lit",
"pipelineState": {
"faceCulling": {
"enabled": false
Expand All @@ -154,8 +158,8 @@
"enabled": true
}
},
"tint": [1.0, 1.0, 1.0, 1.0],
"texture": "monkey",
"tint": [1, 1, 1, 1],
"textureAlbedo": "monkey",
"sampler": "default"
},
"moon": {
Expand All @@ -178,8 +182,14 @@
"game": {
"enemySpawner": {
"spawnPoints": [
[30.0, 0.0, 30.0], [-30.0, 0.0, 30.0], [30.0, 0.0, -30.0], [-30.0, 0.0, -30.0],
[42.0, 0.0, 0.0], [-42.0, 0.0, 0.0], [0.0, 0.0, 42.0], [0.0, 0.0, -42.0]
[30.0, 0.0, 30.0],
[-30.0, 0.0, 30.0],
[30.0, 0.0, -30.0],
[-30.0, 0.0, -30.0],
[42.0, 0.0, 0.0],
[-42.0, 0.0, 0.0],
[0.0, 0.0, 42.0],
[0.0, 0.0, -42.0]
],
"spawnInterval": 2.0,
"initialSpawnCount": 4,
Expand Down Expand Up @@ -283,6 +293,13 @@
"layer": "player",
"radius": 0.45,
"height": 1.8
},
{
"type": "light",
"lightType": "spot",
"color": [1, 1, 1],
"attenuation": [1.0, 0.22, 0.2],
"spotAngles": [2, 5]
}
]
},
Expand All @@ -298,6 +315,17 @@
"material": "ground"
}
]
},
{
"position": [2, 1, -2],
"scale": [5, 5, 5],
"components": [
{
"type": "Mesh Renderer",
"mesh": "monkey",
"material": "monkey"
}
]
}
]
}
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
just-lsp
ccache
clang-tools
gdb
];
};

Expand Down
17 changes: 17 additions & 0 deletions src/common/asset-loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ namespace our {
}
}

template <>
void AssetLoader<our::Light>::deserialize(const nlohmann::json& data) {
if (data.is_object()) {
for (auto& [name, desc] : data.items()) {
our::Light* light = new our::Light();
light->deserialize(desc);
assets[name] = light;
}
}
};

void deserializeAllAssets(const nlohmann::json& assetData) {
if (!assetData.is_object()) return;
if (assetData.contains("shaders")) AssetLoader<ShaderProgram>::deserialize(assetData["shaders"]);
Expand All @@ -123,6 +134,12 @@ namespace our {
if (assetData.contains("meshes")) AssetLoader<Mesh>::deserialize(assetData["meshes"]);
if (assetData.contains("materials")) AssetLoader<Material>::deserialize(assetData["materials"]);
if (assetData.contains("sounds")) AssetLoader<AudioBuffer>::deserialize(assetData["sounds"]);
if (assetData.contains("lights")) AssetLoader<our::Light>::deserialize(assetData["lights"]);
// setting some default assets if something is missing
if (!AssetLoader<Sampler>::get("default")) {
Sampler* defaultSampler = new Sampler();
AssetLoader<Sampler>::add("default", defaultSampler);
}
}

void clearAllAssets() {
Expand Down
4 changes: 4 additions & 0 deletions src/common/asset-loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ namespace our {
}
return nullptr;
};

static void add(const std::string& name, T* asset) {
assets[name] = asset;
}
// This function deletes all the assets held by this class and clear the assets map
static void clear() {
for (auto& [name, asset] : assets) {
Expand Down
1 change: 1 addition & 0 deletions src/common/components/component-deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace our {
ComponentRegistry::registerType<MovementComponent>();
ComponentRegistry::registerType<MeshRendererComponent>();
ComponentRegistry::registerType<AudioSourceComponent>();
ComponentRegistry::registerType<Light>();

Comment thread
AhmedAmrNabil marked this conversation as resolved.
initialized = true;
}
Expand Down
25 changes: 25 additions & 0 deletions src/common/components/light.cpp
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;
Comment thread
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
Loading