diff --git a/Source.Common/Client/IEngineClient.cs b/Source.Common/Client/IEngineClient.cs index 5d37a1ee..27f61839 100644 --- a/Source.Common/Client/IEngineClient.cs +++ b/Source.Common/Client/IEngineClient.cs @@ -321,7 +321,7 @@ public interface IEngineClient // Computes light due to dynamic lighting at a point // If the normal isn't specified, then it'll return the maximum lighting // If pBoxColors is specified (it's an array of 6), then it'll copy the light contribution at each box side. - void ComputeLighting(in Vector3 pt, in Vector3 normal, bool clamp, out Vector3 color, Span boxColors = default); + void ComputeLighting(in Vector3 pt, Vector3? normal, bool clamp, out Vector3 color, Span boxColors = default); // Activates/deactivates an occluder... void ActivateOccluder(int nOccluderIndex, bool bActive); diff --git a/Source.Common/Engine/IModelRender.cs b/Source.Common/Engine/IModelRender.cs index d660efcd..22aa000f 100644 --- a/Source.Common/Engine/IModelRender.cs +++ b/Source.Common/Engine/IModelRender.cs @@ -70,6 +70,7 @@ public interface IModelRender int DrawStaticPropArrayFast(Span props, int count, bool shadowDepth); void SetStaticLighting(ModelInstanceHandle_t handle, LightCacheHandle_t? cache); void SetViewTarget(StudioHdr studioHdr, int bodyIndex, in Vector3 target); + void SetupLighting(in Vector3 center); void DrawModelExecute(ref DrawModelState state, ref ModelRenderInfo info, Span boneToWorldArray); bool DrawModelSetup(ref ModelRenderInfo info, ref DrawModelState state, Span customBoneToWorld, out Span boneToWorldArray); ref Matrix4x4 SetupModelState(IClientRenderable renderable); diff --git a/Source.Engine/CdllEngineInterface.cs b/Source.Engine/CdllEngineInterface.cs index fb9f2c0f..1d22fe15 100644 --- a/Source.Engine/CdllEngineInterface.cs +++ b/Source.Engine/CdllEngineInterface.cs @@ -27,7 +27,7 @@ namespace Source.Engine; #if !SWDS public class EngineClient(Cbuf Cbuf, Scr Scr, Con Con, Key Key, IGame game, Host Host, MaterialSystem_Config MaterialSystemConfig, - MatSysInterface MatSys, ModelLoader modelloader, CL CL, Cmd Cmd) : IEngineClient + MatSysInterface MatSys, ModelLoader modelloader, CL CL, Cmd Cmd, Render Render) : IEngineClient { public ReadOnlySpan Key_LookupBinding(ReadOnlySpan binding) => Key.NameForBinding(binding); public void GetMainMenuBackgroundName(Span dest) { @@ -200,7 +200,8 @@ public int GetIntersectingSurfaces(Model? model, in Vector3 center, float radius } public Vector3 GetLightForPoint(in Vector3 pos, bool clamp) { - throw new NotImplementedException(); + ComputeLighting(pos, null, clamp, out Vector3 color); + return color; } public IMaterial? TraceLineMaterialAndLighting(in Vector3 start, in Vector3 end, out Vector3 diffuseLightColor, out Vector3 baseColor) { @@ -292,11 +293,7 @@ public void LinearToGamma(Span linear, Span gamma) { } public float LightStyleValue(int style) => Render.LightStyleValue((byte)style); - public void ComputeDynamicLighting(in Vector3 pt, in Vector3 normal, out Vector3 color) { - // throw new NotImplementedException(); - // TODO - color = default; - } + public void ComputeDynamicLighting(in Vector3 pt, in Vector3 normal, out Vector3 color) => Render.ComputeDynamicLighting(pt, normal, out color); public void GetAmbientLightColor(out Vector3 color) { BSPDWorldLight? worldLight = ((Render)g_EngineRenderer).FindAmbientLight(); @@ -383,9 +380,7 @@ public float SentenceLength(int sentenceIndex) { return g_AudioSystem.SentenceLength(sentenceIndex); } - public void ComputeLighting(in Vector3 pt, in Vector3 normal, bool clamp, out Vector3 color, Span boxColors = default) { - throw new NotImplementedException(); - } + public void ComputeLighting(in Vector3 pt, Vector3? normal, bool clamp, out Vector3 color, Span boxColors = default) => Render.ComputeLighting(pt, normal, clamp, out color, boxColors); public void ActivateOccluder(int nOccluderIndex, bool bActive) { throw new NotImplementedException(); @@ -546,9 +541,7 @@ public int GetAppID() { throw new NotImplementedException(); } - public Vector3 GetLightForPointFast(in Vector3 pos, bool bClamp) { - throw new NotImplementedException(); - } + public Vector3 GetLightForPointFast(in Vector3 pos, bool bClamp) => Render.GetLightForPointFast(pos, bClamp); public void SetRestrictServerCommands(bool restrict) => cl.RestrictServerCommands = restrict; public void SetRestrictClientCommands(bool restrict) => cl.RestrictClientCommands = restrict; diff --git a/Source.Engine/ImplStudio.cs b/Source.Engine/ImplStudio.cs index e6e5a72e..0678299b 100644 --- a/Source.Engine/ImplStudio.cs +++ b/Source.Engine/ImplStudio.cs @@ -451,6 +451,56 @@ public void SetViewTarget(StudioHdr studioHdr, int bodyIndex, in Vector3 target) StudioRender.SetEyeViewTarget(studioHdr.GetRenderHdr(), bodyIndex, in target); } + public void SetupLighting(in Vector3 center) { + LightingState state = default; + LightcacheGetDynamic_Stats stats = default; + ITexture? envCubemapTexture = Render.LightcacheGetDynamic(center, ref state, ref stats); + + Span lightDescs = stackalloc LightDesc[2]; + int lightCount = 0; + for (int i = 0; i < state.NumLights; i++) { + if (!WorldLightToMaterialLight(ref state.LocalLight[i].Dereference(), out LightDesc lightDesc)) + continue; + lightDesc.Color *= Render.LightStyleValue((byte)state.LocalLight[i].Dereference().Style); + lightDescs[lightCount] = lightDesc; + if (++lightCount >= lightDescs.Length) + break; + } + int maxLights = HardwareConfig.MaxNumLights(); + if (lightCount > maxLights) + lightCount = maxLights; + + Span ambientCube = stackalloc Vector4[6]; + for (int i = 0; i < 6; i++) + ambientCube[i] = new(state.BoxColor[i], 1.0f); + + using MatRenderContextPtr renderContext = new(materials); + renderContext.SetAmbientLightCube(ambientCube); + + if (envCubemapTexture != null) + renderContext.BindLocalCubemap(envCubemapTexture); + + int light = 0; + for (; light < lightCount; light++) { + ref LightDesc desc = ref lightDescs[light]; + LightTypeOptimizationFlags flags = 0; + if (desc.Attenuation0 != 0.0f) + flags |= LightTypeOptimizationFlags.HasAttenuation0; + if (desc.Attenuation1 != 0.0f) + flags |= LightTypeOptimizationFlags.HasAttenuation1; + if (desc.Attenuation2 != 0.0f) + flags |= LightTypeOptimizationFlags.HasAttenuation2; + desc.Flags = (uint)flags; + renderContext.SetLight(light, in desc); + } + + for (; light < maxLights; light++) { + LightDesc disableDesc = default; + disableDesc.Type = LightType.Disable; + renderContext.SetLight(light, in disableDesc); + } + } + readonly IMDLCache MDLCache; readonly IStudioRender StudioRender; readonly IMaterialSystem materials; @@ -1784,7 +1834,7 @@ private void SnapCurrentLightingState(ModelInstance inst, ref LightingState ligh const float MIN_LIGHT_VALUE = 0.03f; - private static bool WorldLightToMaterialLight(ref BSPDWorldLight worldLight, out LightDesc light) { + internal static bool WorldLightToMaterialLight(ref BSPDWorldLight worldLight, out LightDesc light) { light = default; light.Attenuation0 = 0.0f; light.Attenuation1 = 0.0f; diff --git a/Source.Engine/Lightcache.cs b/Source.Engine/Lightcache.cs index c7861523..b5855eff 100644 --- a/Source.Engine/Lightcache.cs +++ b/Source.Engine/Lightcache.cs @@ -1428,12 +1428,123 @@ public void InvalidateStaticLightingCache() { // todo } + static readonly Vector3[] ZeroAmbientCube = new Vector3[6]; + public void ComputeDynamicLighting(in Vector3 pt, in Vector3 normal, out Vector3 color) { - throw new NotImplementedException(); + if (!CL.ActiveDlights && !CL.ActiveElights) { + color = Vector3.Zero; + return; + } + + Span lightDescs = stackalloc LightDesc[Constants.MAX_DLIGHTS + Constants.MAX_ELIGHTS]; + int lightCount = 0; + + if (CL.ActiveDlights) { + for (int i = 0; i < Constants.MAX_DLIGHTS; i++) { + DLight dl = CL.DLights[i]; + if ((Render.DLightActive & (1 << i)) == 0 || (dl.Flags & (DLightFlags.NoModelIllumination | DLightFlags.DisplacementMask)) != 0) + continue; + + BSPDWorldLight worldLight = default; + WorldLightFromDynamicLight(dl, ref worldLight); + ModelRender.WorldLightToMaterialLight(ref worldLight, out lightDescs[lightCount++]); + } + } + + if (CL.ActiveElights) { + for (int i = 0; i < Constants.MAX_ELIGHTS; i++) { + DLight dl = CL.ELights[i]; + if (!dl.IsRadiusGreaterThanZero() || (dl.Flags & (DLightFlags.NoModelIllumination | DLightFlags.DisplacementMask)) != 0) + continue; + + BSPDWorldLight worldLight = default; + WorldLightFromDynamicLight(dl, ref worldLight); + ModelRender.WorldLightToMaterialLight(ref worldLight, out lightDescs[lightCount++]); + } + } + + if (lightCount == 0) { + color = Vector3.Zero; + return; + } + +#if !SWDS + studiorender.ComputeLighting(ZeroAmbientCube, lightCount, lightDescs, pt, normal, out color); +#else + color = Vector3.Zero; +#endif } - public void ComputeLighting(in Vector3 pt, in Vector3 normal, bool clamp, out Vector3 color, Span boxColors) { - throw new NotImplementedException(); + public void ComputeLighting(in Vector3 pt, Vector3? normal, bool clamp, out Vector3 color, Span boxColors) { + LightingState lightingState = default; + LightcacheGetDynamic_Stats stats = default; + LightcacheGetDynamic(pt, ref lightingState, ref stats, LightCacheFlags.Static | LightCacheFlags.Dynamic | LightCacheFlags.LightStyle | LightCacheFlags.AllowFast); + + if (normal is Vector3 surfaceNormal) { + Span lightDescs = stackalloc LightDesc[MAXLOCALLIGHTS]; + for (int i = 0; i < lightingState.NumLights; i++) + ModelRender.WorldLightToMaterialLight(ref lightingState.LocalLight[i].Dereference(), out lightDescs[i]); + +#if !SWDS + studiorender.ComputeLighting(lightingState.BoxColor, lightingState.NumLights, lightDescs, pt, surfaceNormal, out color); +#else + color = Vector3.Zero; +#endif + } + else { + for (int i = 0; i < lightingState.NumLights; i++) { + BSPDWorldLightPtr light = lightingState.LocalLight[i]; + if (light.Lights == s_pDynamicLight) + continue; + + ref BSPDWorldLight worldLight = ref light.Dereference(); + float ratio = LightIntensityAndDirectionAtPoint(ref worldLight, null, pt, LightIntensityFlags.NoOcclusionCheck, null, out Vector3 direction); + float angle = Engine_WorldLightAngle(worldLight, worldLight.Normal, direction, direction); + AddWorldLightToLightCube(worldLight, lightingState.BoxColor, direction, angle * ratio); + } + + ref AmbientCube box = ref lightingState.BoxColor; + color = new( + (box[0].X + box[1].X + box[2].X + box[3].X + box[4].X + box[5].X) * 0.16666667f, + (box[0].Y + box[1].Y + box[2].Y + box[3].Y + box[4].Y + box[5].Y) * 0.16666667f, + (box[0].Z + box[1].Z + box[2].Z + box[3].Z + box[4].Z + box[5].Z) * 0.16666667f); + } + + if (!boxColors.IsEmpty) { + for (int i = 0; i < 6; i++) + boxColors[i] = lightingState.BoxColor[i]; + } + + if (clamp) { + if (color.X > 1.0f) + color.X = 1.0f; + if (color.Y > 1.0f) + color.Y = 1.0f; + if (color.Z > 1.0f) + color.Z = 1.0f; + } + } + + public Vector3 GetLightForPointFast(in Vector3 pos, bool clamp) { + Vector3 ret = Vector3.Zero; + int leafIndex = CM.PointLeafnum(pos); + Span cube = stackalloc Vector3[6]; + ModelLoader.Mod_LeafAmbientColorAtPos(cube, pos, leafIndex); + for (int i = 0; i < 6; i++) { + ret.X = MathF.Max(ret.X, cube[i].X); + ret.Y = MathF.Max(ret.Y, cube[i].Y); + ret.Z = MathF.Max(ret.Z, cube[i].Z); + } + + if (clamp) { + if (ret.X > 1.0f) + ret.X = 1.0f; + if (ret.Y > 1.0f) + ret.Y = 1.0f; + if (ret.Z > 1.0f) + ret.Z = 1.0f; + } + return ret; } // Finds ambient lights