diff --git a/src/Engine.Graphics.Raylib/RaylibRenderer.cs b/src/Engine.Graphics.Raylib/RaylibRenderer.cs index d485dcc..d148bb1 100644 --- a/src/Engine.Graphics.Raylib/RaylibRenderer.cs +++ b/src/Engine.Graphics.Raylib/RaylibRenderer.cs @@ -133,10 +133,10 @@ public sealed class RaylibRenderer : IRenderer Raylib.BeginMode3D(shadowCamera); // Grab light view/proj matrices AFTER BeginMode3D (like official example) - // Raylib matrices are row-major, need to transpose for OpenGL (column-major) - var lv = Rlgl.GetMatrixModelview(); - var lp = Rlgl.GetMatrixProjection(); - lightViewProj = Matrix4x4.Transpose(lv * lp); + // SetShaderValueMatrix passes Matrix4x4 as-is to glUniformMatrix4fv(transpose=false) + // which interprets row-major System.Numerics as column-major = effectively transposed. + // So we just multiply directly (no manual transpose needed). + lightViewProj = Rlgl.GetMatrixModelview() * Rlgl.GetMatrixProjection(); // Draw all shadow casters with the simple shadow shader world.Each((Entity e, ref EngineMesh mesh, ref EngineTransform transform) => @@ -198,22 +198,29 @@ public sealed class RaylibRenderer : IRenderer var (axis, angle) = QuaternionToAxisAngle(rotation); SetMaterialUniforms(material, model); - // Bind shadow map on Emission slot (texture unit 1) for each entity + // Bind shadow map on Emission slot (texture unit 1) if (hasDirectionalLight && _shadowMapLoc >= 0) { unsafe { Raylib.SetMaterialTexture(ref model.Materials[0], MaterialMapIndex.Emission, shadowTex); } } - Raylib.DrawModelEx(model, position, axis, angle * 180.0f / MathF.PI, scale, Color.White); - - // Re-bind shadow map on unit 1 after DrawModelEx (it may have reset texture state) + // Use BeginShaderMode to lock our shader, then bind shadow map on unit 1 + // BEFORE DrawModelEx so it's active during the draw if (hasDirectionalLight && _shadowMapLoc >= 0) { + Raylib.BeginShaderMode(_shader); Rlgl.ActiveTextureSlot(1); Rlgl.EnableTexture(_shadowDepthTex); Raylib.SetShaderValue(_shader, _shadowMapLoc, 1, ShaderUniformDataType.Int); Rlgl.ActiveTextureSlot(0); } + + Raylib.DrawModelEx(model, position, axis, angle * 180.0f / MathF.PI, scale, Color.White); + + if (hasDirectionalLight && _shadowMapLoc >= 0) + { + Raylib.EndShaderMode(); + } } });