Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7772988
feat: add TextureUnits enum for texture management
AhmedAmrNabil Apr 13, 2026
46262a1
feat: add light component with deserialization
AhmedAmrNabil Apr 13, 2026
41023d2
feat: add LitMaterial class with lighting support and texture management
AhmedAmrNabil Apr 13, 2026
5341f54
feat: add deserialization support for Light assets in AssetLoader
AhmedAmrNabil Apr 13, 2026
360595c
fix: add light component source file to the project
AhmedAmrNabil Apr 13, 2026
622cc53
feat: make light a component instead of asset
AhmedAmrNabil Apr 13, 2026
97388a3
feat: setup lights for shaders in forward-renderer
AhmedAmrNabil Apr 13, 2026
ca4254a
fix: fix lit material deserialize
AhmedAmrNabil Apr 13, 2026
cc42984
feat: add tangent attribute to Vertex and update Mesh for normal mapping
AhmedAmrNabil Apr 13, 2026
af6e999
feat: initialize texture pointers and alphaThreshold in LitMaterial
AhmedAmrNabil Apr 13, 2026
49bb544
fix: pass camera pos as vec3 in forward renderer
AhmedAmrNabil Apr 13, 2026
ff03f0f
feat(flake): add gdb for debugging
AhmedAmrNabil Apr 13, 2026
5238282
fix: correct spotAngles assignment in Light deserialization
AhmedAmrNabil Apr 13, 2026
389d110
fix: correct light direction calculation
AhmedAmrNabil Apr 13, 2026
184cd2f
feat: add initial lighting shader
AhmedAmrNabil Apr 13, 2026
4e8e55b
feat: add lighting test scene
AhmedAmrNabil Apr 13, 2026
d77e7f9
feat: add assimp library to vendor
AhmedAmrNabil Apr 13, 2026
d2419ac
feat: add model class with loading and drawing
AhmedAmrNabil Apr 14, 2026
765e2f3
feat: add ModelRendererComponent with deserialization
AhmedAmrNabil Apr 14, 2026
2deb711
feat: add model deserialization to AssetLoader
AhmedAmrNabil Apr 14, 2026
e63c291
feat: add bone weight and id to vertex
AhmedAmrNabil Apr 14, 2026
28a8549
feat: add model rendering support to ForwardRenderer
AhmedAmrNabil Apr 14, 2026
dad97ee
refactor: precompute if material have textures or not and add metalli…
AhmedAmrNabil Apr 14, 2026
877b424
feat(litShader): add textureMetalRoughness handling
AhmedAmrNabil Apr 14, 2026
2304b7a
fix(cmake): add model-renderer component to the project executable
AhmedAmrNabil Apr 14, 2026
6007f9e
feat: drone model to test
AhmedAmrNabil Apr 14, 2026
666dd9b
fix: remove old debug prints
AhmedAmrNabil Apr 14, 2026
a8d8b7d
feat(model): enhance texture loading with wrapping and filtering para…
AhmedAmrNabil Apr 15, 2026
35dd04f
feat(model): update model loading to support scene-based texture hand…
AhmedAmrNabil Apr 15, 2026
34c1a2b
fix: fix light leak behind objects
AhmedAmrNabil Apr 15, 2026
e383f40
refactor: use assetloader cache for model loading
AhmedAmrNabil Apr 15, 2026
a854eb6
chore: change drone model from gltf to glb
AhmedAmrNabil Apr 15, 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
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
path = vendor/glfw
url = https://github.com/glfw/glfw.git
shallow = true

[submodule "vendor/bullet3"]
path = vendor/bullet3
url = https://github.com/bulletphysics/bullet3.git
shallow = true

[submodule "vendor/assimp"]
path = vendor/assimp
url = https://github.com/assimp/assimp.git
shallow = true
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ set(BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE)
set(BUILD_BULLET3 ON CACHE BOOL "" FORCE)
add_subdirectory(vendor/bullet3)

# ---- Assimp ----
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_SAMPLES OFF CACHE BOOL "" FORCE)
set(ASSIMP_INSTALL OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_ASSIMP_TOOLS OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_MINIZIP ON CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_ZLIB ON CACHE BOOL "" FORCE)
add_subdirectory(vendor/assimp)

# ---- GLM (Header-only) ----
add_library(glm INTERFACE)
target_include_directories(glm
Expand Down Expand Up @@ -129,6 +139,8 @@ add_executable(${PROJECT_NAME}

src/common/mesh/mesh-utils.cpp

src/common/model/model.cpp

src/common/texture/sampler.cpp
src/common/texture/texture-utils.cpp
src/common/texture/screenshot.cpp
Expand All @@ -142,8 +154,10 @@ add_executable(${PROJECT_NAME}

src/common/components/camera.cpp
src/common/components/mesh-renderer.cpp
src/common/components/model-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 Expand Up @@ -173,6 +187,7 @@ target_link_libraries(${PROJECT_NAME}
BulletCollision
BulletSoftBody
LinearMath
assimp::assimp
)

set_target_properties(${PROJECT_NAME} PROPERTIES
Expand Down
Binary file added assets/gltf/mech_drone/mech_drone.glb
Binary file not shown.
190 changes: 190 additions & 0 deletions assets/shaders/lit.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#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;

sampler2D textureMetalnessRoughness;
bool hasMetalnessRoughness;
};
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) {
if(material.hasMetalnessRoughness)
return texture(material.textureMetalnessRoughness, uv).b * material.metallic; // B channel
if(material.hasTextureMetallic)
return texture(material.textureMetallic, uv).r * material.metallic;
return material.metallic;
}

float sampleRoughness(vec2 uv) {
if(material.hasMetalnessRoughness)
return texture(material.textureMetalnessRoughness, uv).g * material.roughness; // G channel
if(material.hasTextureRoughness)
return texture(material.textureRoughness, uv).r * material.roughness;
return material.roughness;
}

float sampleAO(vec2 uv) {
return material.hasTextureAmbientOcclusion ? texture(material.textureAmbientOcclusion, uv).r * material.ambientOcclusion : material.ambientOcclusion;
}

vec3 sampleEmission(vec2 uv) {
if(material.hasTextureEmissive)
return texture(material.textureEmissive, uv).rgb; // texture owns the color
return material.emission; // factor only when no texture
}

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);
if(dot(toLight, N) <= 0.0) {
return vec3(0.0);
}

// 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 inner = light.spotAngles.x;
float outer = light.spotAngles.y;
float epsilon = inner - outer;
float spot = clamp((theta - outer) / epsilon, 0.0, 1.0);
spot = pow(spot, 64.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);
for(int i = 0; i < numLights; 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);
}
39 changes: 39 additions & 0 deletions assets/shaders/lit.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#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;
layout(location = 5) in ivec4 bone_ids;
layout(location = 6) in vec4 weights;

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);
}
Loading