Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Source.Common/Client/IEngineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector3> boxColors = default);
void ComputeLighting(in Vector3 pt, Vector3? normal, bool clamp, out Vector3 color, Span<Vector3> boxColors = default);

// Activates/deactivates an occluder...
void ActivateOccluder(int nOccluderIndex, bool bActive);
Expand Down
1 change: 1 addition & 0 deletions Source.Common/Engine/IModelRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public interface IModelRender
int DrawStaticPropArrayFast(Span<StaticPropRenderInfo> 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<Matrix3x4> boneToWorldArray);
bool DrawModelSetup(ref ModelRenderInfo info, ref DrawModelState state, Span<Matrix3x4> customBoneToWorld, out Span<Matrix3x4> boneToWorldArray);
ref Matrix4x4 SetupModelState(IClientRenderable renderable);
Expand Down
19 changes: 6 additions & 13 deletions Source.Engine/CdllEngineInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> Key_LookupBinding(ReadOnlySpan<char> binding) => Key.NameForBinding(binding);
public void GetMainMenuBackgroundName(Span<char> dest) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -292,11 +293,7 @@ public void LinearToGamma(Span<float> linear, Span<float> 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();
Expand Down Expand Up @@ -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<Vector3> boxColors = default) {
throw new NotImplementedException();
}
public void ComputeLighting(in Vector3 pt, Vector3? normal, bool clamp, out Vector3 color, Span<Vector3> boxColors = default) => Render.ComputeLighting(pt, normal, clamp, out color, boxColors);

public void ActivateOccluder(int nOccluderIndex, bool bActive) {
throw new NotImplementedException();
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 51 additions & 1 deletion Source.Engine/ImplStudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LightDesc> 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<Vector4> 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;
Expand Down Expand Up @@ -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;
Expand Down
117 changes: 114 additions & 3 deletions Source.Engine/Lightcache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LightDesc> 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<Vector3> boxColors) {
throw new NotImplementedException();
public void ComputeLighting(in Vector3 pt, Vector3? normal, bool clamp, out Vector3 color, Span<Vector3> 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<LightDesc> 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<Vector3> 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
Expand Down
Loading