Skip to content

Commit

Permalink
Merge pull request #511 from openrails/release/1.4
Browse files Browse the repository at this point in the history
Merge pull request #509 from twpol/feature/particle-colour
  • Loading branch information
twpol authored Oct 3, 2021
2 parents 6ccf85e + 41d3f0c commit 9d1919a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
12 changes: 2 additions & 10 deletions Source/Orts.Formats.Msts/LightCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Orts.Formats.Msts
public class LightState
{
public float Duration;
public uint Color;
public Color Color;
public Vector3 Position;
public float Radius;
public Vector3 Azimuth;
Expand All @@ -44,22 +44,14 @@ public LightState(STFReader stf)
stf.MustMatch("(");
stf.ParseBlock(new[] {
new STFReader.TokenProcessor("duration", ()=>{ Duration = stf.ReadFloatBlock(STFReader.UNITS.None, null); }),
new STFReader.TokenProcessor("lightcolour", ()=>{ Color = stf.ReadHexBlock(null); }),
new STFReader.TokenProcessor("lightcolour", ()=>{ Color = stf.ReadColorBlock(null); }),
new STFReader.TokenProcessor("position", ()=>{ Position = stf.ReadVector3Block(STFReader.UNITS.None, Vector3.Zero); }),
new STFReader.TokenProcessor("radius", ()=>{ Radius = stf.ReadFloatBlock(STFReader.UNITS.Distance, null); }),
new STFReader.TokenProcessor("azimuth", ()=>{ Azimuth = stf.ReadVector3Block(STFReader.UNITS.None, Vector3.Zero); }),
new STFReader.TokenProcessor("elevation", ()=>{ Elevation = stf.ReadVector3Block(STFReader.UNITS.None, Vector3.Zero); }),
new STFReader.TokenProcessor("transition", ()=>{ Transition = 1 <= stf.ReadFloatBlock(STFReader.UNITS.None, 0); }),
new STFReader.TokenProcessor("angle", ()=>{ Angle = stf.ReadFloatBlock(STFReader.UNITS.None, null); }),
});
// Color byte order changed in XNA 4 from BGRA to RGBA
Color = new Color()
{
B = (byte)(Color),
G = (byte)(Color >> 8),
R = (byte)(Color >> 16),
A = (byte)(Color >> 24)
}.PackedValue;
}

public LightState(LightState state, bool reverse)
Expand Down
14 changes: 14 additions & 0 deletions Source/Orts.Parsers.Msts/STFReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,20 @@ public string ReadStringBlock(string defaultValue)
return defaultValue;
}

/// <summary>Read a hexadecimal encoded color from the STF format '( {color_constant} ... )'
/// </summary>
/// <param name="defaultValue">the default value if the constant is not found in the block.</param>
/// <returns>The STF block with the first {item} converted to a color constant.</returns>
public Color ReadColorBlock(Color? defaultValue)
{
var hex = this.ReadHexBlock(STFReader.SwapColorBytes(defaultValue.GetValueOrDefault(Color.Black).PackedValue));
return new Color() { PackedValue = STFReader.SwapColorBytes(hex) };
}

static uint SwapColorBytes(uint color) {
return (color & 0xFF00FF00) + (byte)(color >> 16) + (uint)((byte)color << 16);
}

/// <summary>Read an hexidecimal encoded number from the STF format '( {int_constant} ... )'
/// </summary>
/// <param name="defaultValue">the default value if the constant is not found in the block.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@ public virtual void Parse(STFReader stf)
case "maxexhaust": MaxExhaust = stf.ReadFloatBlock(STFReader.UNITS.None, 0);initLevel |= SettingsFlags.MaxExhaust; break;
case "exhaustdynamics": ExhaustAccelIncrease = stf.ReadFloatBlock(STFReader.UNITS.None, 0); initLevel |= SettingsFlags.ExhaustDynamics; break;
case "exhaustdynamicsdown": ExhaustDecelReduction = stf.ReadFloatBlock(STFReader.UNITS.None, null); initLevel |= SettingsFlags.ExhaustDynamics; break;
case "exhaustcolor": ExhaustSteadyColor.PackedValue = stf.ReadHexBlock(Color.Gray.PackedValue); initLevel |= SettingsFlags.ExhaustColor; break;
case "exhausttransientcolor": ExhaustTransientColor.PackedValue = stf.ReadHexBlock(Color.Black.PackedValue);initLevel |= SettingsFlags.ExhaustTransientColor; break;
case "exhaustcolor": ExhaustSteadyColor = stf.ReadColorBlock(Color.Gray); initLevel |= SettingsFlags.ExhaustColor; break;
case "exhausttransientcolor": ExhaustTransientColor = stf.ReadColorBlock(Color.Black);initLevel |= SettingsFlags.ExhaustTransientColor; break;
case "dieselpowertab": DieselPowerTab = new Interpolator(stf);initLevel |= SettingsFlags.DieselPowerTab; break;
case "dieselconsumptiontab": DieselConsumptionTab = new Interpolator(stf);initLevel |= SettingsFlags.DieselConsumptionTab; break;
case "throttlerpmtab":
Expand Down
6 changes: 3 additions & 3 deletions Source/RunActivity/Viewer3D/Lights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static void CalculateLightCone(LightState lightState, out Vector3 positio
angle = MathHelper.ToRadians(lightState.Angle) / 2;
radius = lightState.Radius / 2;
distance = (float)(radius / Math.Sin(angle));
color = new Color() { PackedValue = lightState.Color }.ToVector4();
color = lightState.Color.ToVector4();
}

#if DEBUG_LIGHT_STATES
Expand Down Expand Up @@ -535,11 +535,11 @@ public LightGlowPrimitive(LightViewer lightViewer, RenderProcess renderProcess,

var position1 = state1.Position; position1.Z *= -1;
var normal1 = Vector3.Transform(Vector3.Transform(-Vector3.UnitZ, Matrix.CreateRotationX(MathHelper.ToRadians(-state1.Elevation.Y))), Matrix.CreateRotationY(MathHelper.ToRadians(-state1.Azimuth.Y)));
var color1 = new Color() { PackedValue = state1.Color }.ToVector4();
var color1 = state1.Color.ToVector4();

var position2 = state2.Position; position2.Z *= -1;
var normal2 = Vector3.Transform(Vector3.Transform(-Vector3.UnitZ, Matrix.CreateRotationX(MathHelper.ToRadians(-state2.Elevation.Y))), Matrix.CreateRotationY(MathHelper.ToRadians(-state2.Azimuth.Y)));
var color2 = new Color() { PackedValue = state2.Color }.ToVector4();
var color2 = state2.Color.ToVector4();

vertexData[6 * state + 0] = new LightGlowVertex(new Vector2(1, 1), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);
vertexData[6 * state + 1] = new LightGlowVertex(new Vector2(0, 0), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);
Expand Down

0 comments on commit 9d1919a

Please sign in to comment.