Example #1
0
        protected override void UpdateStates(int stateIndex1, int stateIndex2)
        {
            var state1 = Light.States[stateIndex1];
            var state2 = Light.States[stateIndex2];

            LightViewer.CalculateLightCone(state1, out Position1, out Direction1, out Angle1, out Radius1, out Distance1, out Color1);
            LightViewer.CalculateLightCone(state2, out Position2, out Direction2, out Angle2, out Radius2, out Distance2, out Color2);
        }
Example #2
0
        public LightGlowPrimitive(LightViewer lightViewer, RenderProcess renderProcess, Light light)
            : base(light)
        {
            Debug.Assert(light.Type == LightType.Glow, "LightGlowPrimitive is only for LightType.Glow lights.");

            if (VertexDeclaration == null)
            {
                VertexDeclaration = new VertexDeclaration(renderProcess.GraphicsDevice, LightGlowVertex.VertexElements);
            }
            if (VertexBuffer == null)
            {
                var vertexData = new LightGlowVertex[6 * StateCount];
                SetUpTransitions((state, stateIndex1, stateIndex2) =>
                {
                    var state1 = Light.States[stateIndex1];
                    var state2 = Light.States[stateIndex2];

#if DEBUG_LIGHT_TRANSITIONS
                    Console.WriteLine("    Transition {0} is from state {1} to state {2} over {3:F1}s", state, stateIndex1, stateIndex2, state1.Duration);
#endif

                    // FIXME: Is conversion of "azimuth" to a normal right?

                    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 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();

                    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);
                    vertexData[6 * state + 2] = new LightGlowVertex(new Vector2(1, 0), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);
                    vertexData[6 * state + 3] = new LightGlowVertex(new Vector2(1, 1), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);
                    vertexData[6 * state + 4] = new LightGlowVertex(new Vector2(0, 1), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);
                    vertexData[6 * state + 5] = new LightGlowVertex(new Vector2(0, 0), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);
                });
                VertexBuffer = new VertexBuffer(renderProcess.GraphicsDevice, typeof(LightGlowVertex), vertexData.Length, BufferUsage.WriteOnly);
                VertexBuffer.SetData(vertexData);
            }
            if (IndexBuffer == null)
            {
                var indexData = new short[] {
                    0, 1, 2, 3, 4, 5
                };
                IndexBuffer = new IndexBuffer(renderProcess.GraphicsDevice, typeof(short), indexData.Length, BufferUsage.WriteOnly);
                IndexBuffer.SetData(indexData);
            }

            UpdateState(lightViewer);
        }
Example #3
0
        public LightConePrimitive(LightViewer lightViewer, RenderProcess renderProcess, Light light)
            : base(light)
        {
            Debug.Assert(light.Type == LightType.Cone, "LightConePrimitive is only for LightType.Cone lights.");

            if (VertexDeclaration == null)
            {
                VertexDeclaration = new VertexDeclaration(renderProcess.GraphicsDevice, LightConeVertex.VertexElements);
            }
            if (VertexBuffer == null)
            {
                var vertexData = new LightConeVertex[(CircleSegments + 2) * StateCount];
                SetUpTransitions((state, stateIndex1, stateIndex2) =>
                {
                    var state1 = Light.States[stateIndex1];
                    var state2 = Light.States[stateIndex2];

#if DEBUG_LIGHT_TRANSITIONS
                    Console.WriteLine("    Transition {0} is from state {1} to state {2} over {3:F1}s", state, stateIndex1, stateIndex2, state1.Duration);
#endif

                    Vector3 position1, position2, direction1, direction2;
                    float angle1, angle2, radius1, radius2, distance1, distance2;
                    Vector4 color1, color2;
                    LightViewer.CalculateLightCone(state1, out position1, out direction1, out angle1, out radius1, out distance1, out color1);
                    LightViewer.CalculateLightCone(state2, out position2, out direction2, out angle2, out radius2, out distance2, out color2);
                    var direction1Right = Vector3.Cross(direction1, Vector3.UnitY);
                    var direction1Up    = Vector3.Cross(direction1Right, direction1);
                    var direction2Right = Vector3.Cross(direction2, Vector3.UnitY);
                    var direction2Up    = Vector3.Cross(direction2Right, direction2);

                    for (var i = 0; i < CircleSegments; i++)
                    {
                        var a1 = MathHelper.TwoPi * i / CircleSegments;
                        var a2 = MathHelper.TwoPi * (i + 1) / CircleSegments;
                        var v1 = position1 + direction1 * distance1 + direction1Right * (float)(radius1 * Math.Cos(a1)) + direction1Up * (float)(radius1 * Math.Sin(a1));
                        var v2 = position2 + direction2 * distance2 + direction2Right * (float)(radius2 * Math.Cos(a2)) + direction2Up * (float)(radius2 * Math.Sin(a2));
                        vertexData[(CircleSegments + 2) * state + i] = new LightConeVertex(v1, v2, color1, color2);
                    }
                    vertexData[(CircleSegments + 2) * state + CircleSegments + 0] = new LightConeVertex(position1, position2, color1, color2);
                    vertexData[(CircleSegments + 2) * state + CircleSegments + 1] = new LightConeVertex(new Vector3(position1.X, position1.Y, position1.Z - distance1), new Vector3(position2.X, position2.Y, position2.Z - distance2), color1, color2);
                });
                VertexBuffer = new VertexBuffer(renderProcess.GraphicsDevice, typeof(LightConeVertex), vertexData.Length, BufferUsage.WriteOnly);
                VertexBuffer.SetData(vertexData);
            }
            if (IndexBuffer == null)
            {
                var indexData = new short[6 * CircleSegments];
                for (var i = 0; i < CircleSegments; i++)
                {
                    var i2 = (i + 1) % CircleSegments;
                    indexData[6 * i + 0] = (short)(CircleSegments + 0);
                    indexData[6 * i + 1] = (short)i2;
                    indexData[6 * i + 2] = (short)i;
                    indexData[6 * i + 3] = (short)i;
                    indexData[6 * i + 4] = (short)i2;
                    indexData[6 * i + 5] = (short)(CircleSegments + 1);
                }
                IndexBuffer = new IndexBuffer(renderProcess.GraphicsDevice, typeof(short), indexData.Length, BufferUsage.WriteOnly);
                IndexBuffer.SetData(indexData);
            }

            UpdateState(lightViewer);
        }
Example #4
0
        internal void UpdateState(LightViewer lightViewer)
        {
            var oldEnabled = Enabled;

            Enabled = true;
            if (Light.Headlight != LightHeadlightCondition.Ignore)
            {
                if (Light.Headlight == LightHeadlightCondition.Off)
                {
                    Enabled &= lightViewer.TrainHeadlight == 0;
                }
                else if (Light.Headlight == LightHeadlightCondition.Dim)
                {
                    Enabled &= lightViewer.TrainHeadlight == 1;
                }
                else if (Light.Headlight == LightHeadlightCondition.Bright)
                {
                    Enabled &= lightViewer.TrainHeadlight == 2;
                }
                else if (Light.Headlight == LightHeadlightCondition.DimBright)
                {
                    Enabled &= lightViewer.TrainHeadlight >= 1;
                }
                else if (Light.Headlight == LightHeadlightCondition.OffDim)
                {
                    Enabled &= lightViewer.TrainHeadlight <= 1;
                }
                else if (Light.Headlight == LightHeadlightCondition.OffBright)
                {
                    Enabled &= lightViewer.TrainHeadlight != 1;
                }
                else
                {
                    Enabled &= false;
                }
            }
            if (Light.Unit != LightUnitCondition.Ignore)
            {
                if (Light.Unit == LightUnitCondition.Middle)
                {
                    Enabled &= !lightViewer.CarIsFirst && !lightViewer.CarIsLast;
                }
                else if (Light.Unit == LightUnitCondition.First)
                {
                    Enabled &= lightViewer.CarIsFirst && !lightViewer.CarIsReversed;
                }
                else if (Light.Unit == LightUnitCondition.Last)
                {
                    Enabled &= lightViewer.CarIsLast && !lightViewer.CarIsReversed;
                }
                else if (Light.Unit == LightUnitCondition.LastRev)
                {
                    Enabled &= lightViewer.CarIsLast && lightViewer.CarIsReversed;
                }
                else if (Light.Unit == LightUnitCondition.FirstRev)
                {
                    Enabled &= lightViewer.CarIsFirst && lightViewer.CarIsReversed;
                }
                else
                {
                    Enabled &= false;
                }
            }
            if (Light.Penalty != LightPenaltyCondition.Ignore)
            {
                if (Light.Penalty == LightPenaltyCondition.No)
                {
                    Enabled &= !lightViewer.Penalty;
                }
                else if (Light.Penalty == LightPenaltyCondition.Yes)
                {
                    Enabled &= lightViewer.Penalty;
                }
                else
                {
                    Enabled &= false;
                }
            }
            if (Light.Control != LightControlCondition.Ignore)
            {
                if (Light.Control == LightControlCondition.AI)
                {
                    Enabled &= !lightViewer.CarIsPlayer;
                }
                else if (Light.Control == LightControlCondition.Player)
                {
                    Enabled &= lightViewer.CarIsPlayer;
                }
                else
                {
                    Enabled &= false;
                }
            }
            if (Light.Service != LightServiceCondition.Ignore)
            {
                if (Light.Service == LightServiceCondition.No)
                {
                    Enabled &= !lightViewer.CarInService;
                }
                else if (Light.Service == LightServiceCondition.Yes)
                {
                    Enabled &= lightViewer.CarInService;
                }
                else
                {
                    Enabled &= false;
                }
            }
            if (Light.TimeOfDay != LightTimeOfDayCondition.Ignore)
            {
                if (Light.TimeOfDay == LightTimeOfDayCondition.Day)
                {
                    Enabled &= lightViewer.IsDay;
                }
                else if (Light.TimeOfDay == LightTimeOfDayCondition.Night)
                {
                    Enabled &= !lightViewer.IsDay;
                }
                else
                {
                    Enabled &= false;
                }
            }
            if (Light.Weather != LightWeatherCondition.Ignore)
            {
                if (Light.Weather == LightWeatherCondition.Clear)
                {
                    Enabled &= lightViewer.Weather == WeatherType.Clear;
                }
                else if (Light.Weather == LightWeatherCondition.Rain)
                {
                    Enabled &= lightViewer.Weather == WeatherType.Rain;
                }
                else if (Light.Weather == LightWeatherCondition.Snow)
                {
                    Enabled &= lightViewer.Weather == WeatherType.Snow;
                }
                else
                {
                    Enabled &= false;
                }
            }
            if (Light.Coupling != LightCouplingCondition.Ignore)
            {
                if (Light.Coupling == LightCouplingCondition.Front)
                {
                    Enabled &= lightViewer.CarCoupledFront && !lightViewer.CarCoupledRear;
                }
                else if (Light.Coupling == LightCouplingCondition.Rear)
                {
                    Enabled &= !lightViewer.CarCoupledFront && lightViewer.CarCoupledRear;
                }
                else if (Light.Coupling == LightCouplingCondition.Both)
                {
                    Enabled &= lightViewer.CarCoupledFront && lightViewer.CarCoupledRear;
                }
                else
                {
                    Enabled &= false;
                }
            }

            if (oldEnabled != Enabled)
            {
                FadeIn   = Enabled;
                FadeOut  = !Enabled;
                FadeTime = 0;
            }

#if DEBUG_LIGHT_STATES
            Console.WriteLine(LightViewer.PrimitiveStateFormat, Light.Index, Enabled, Light.Type, Light.Headlight, Light.Unit, Light.Penalty, Light.Control, Light.Service, Light.TimeOfDay, Light.Weather, Light.Coupling);
#endif
        }