OpenBve.Renderer.UpdateLighting C# (CSharp) Méthode

UpdateLighting() static private méthode

Updates the lighting model on a per frame basis
static private UpdateLighting ( ) : void
Résultat void
		internal static void UpdateLighting()
		{
			//Check that we have more than one light definition & that the array is not null
			if (DynamicLighting == false || LightDefinitions == null || LightDefinitions.Length < 2)
			{
				return;
			}
			var Time = Game.SecondsSinceMidnight;
			//Convert to absolute time of day
			//Use a while loop as it's possible to run through two days
			while (Time > 86400)
			{
				Time -= 86400;
			}
			//Run through the array
			int j = 0;
			for (int i = j; i < LightDefinitions.Length; i++)
			{
				
				if (Time < LightDefinitions[i].Time)
				{
					break;
				}
				j = i;

			}

			//We now know that our light definition is between the values defined in j and j + 1 (Or 0 if this is the end of the array)
			int k;

			if (j == 0)
			{
				//Our NEW light is to be the first entry in the array
				//This means the OLD light is the last entry, but j and k do not need reversing
				k = 0;
				j = LightDefinitions.Length - 1;
			}
			else if (j == LightDefinitions.Length - 1)
			{
				//We are wrapping around to the end of the array
				//Reverse j and k, as we have not yet passed the time for the first array entry
				j = 0;
				k = LightDefinitions.Length - 1;
			}
			else
			{
				//Somewhere in the middle, so the NEW light is simply one greater
				k = j + 1;
			}
			int t1 = LightDefinitions[j].Time, t2 = LightDefinitions[k].Time;
			
			double cb1 = LightDefinitions[j].CabBrightness, cb2 = LightDefinitions[k].Time;
			//Calculate, inverting if necessary

			//Ensure we're not about to divide by zero
			if (t2 == 0)
			{
				t2 = 1;
			}
			if (t1 == 0)
			{
				t1 = 1;
			}
			//Calculate the percentage
			double mu;
			if (k == LightDefinitions.Length - 1)
			{
				//Wrapping around
				mu = (86400 - Time + t1) / (86400 - t2 + t1);
				
			}
			else
			{
				mu = (Time - t1) / (t2 - t1);
			}
			//Calculate the final colors and positions
			OptionDiffuseColor = Color24.CosineInterpolate(LightDefinitions[j].DiffuseColor, LightDefinitions[k].DiffuseColor, mu);
			OptionAmbientColor = Color24.CosineInterpolate(LightDefinitions[j].AmbientColor, LightDefinitions[k].AmbientColor, mu);
			OptionLightPosition = Vector3.CosineInterpolate(LightDefinitions[j].LightPosition,LightDefinitions[k].LightPosition, mu);

			//Interpolate the cab brightness value
			var mu2 = (1 - System.Math.Cos(mu * System.Math.PI)) / 2;
			DynamicCabBrightness = (cb1 * (1 - mu2) + cb2 * mu2);
			//Reinitialize the lighting model with the new information
			InitializeLighting();
			//NOTE: This does not refresh the display lists
			//If we sit in place with extreme time acceleration (1000x) lighting for faces may appear a little inconsistant
		}
	}

Usage Example

Exemple #1
0
        //This renders the frame
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            if (!firstFrame)
            {
                //If the load is not complete, then we shouldn't be running the mainloop
                return;
            }
            double TimeElapsed     = RenderTimeElapsed;
            double RealTimeElapsed = RenderRealTimeElapsed;

            //Next, check if we're in paused/ in a menu
            if (Game.CurrentInterface != Game.InterfaceType.Normal)
            {
                MainLoop.UpdateControlRepeats(0.0);
                MainLoop.ProcessKeyboard();
                MainLoop.ProcessControls(0.0);
                if (Game.CurrentInterface == Game.InterfaceType.Pause)
                {
                    System.Threading.Thread.Sleep(10);
                }
                //Renderer.UpdateLighting();
                Renderer.RenderScene(TimeElapsed);
                Program.currentGameWindow.SwapBuffers();
                if (MainLoop.Quit)
                {
                    Close();
                }
                //If the menu state has not changed, don't update the rendered simulation
                return;
            }

            //Use the OpenTK framerate as this is much more accurate
            //Also avoids running a calculation
            if (TotalTimeElapsedForInfo >= 0.2)
            {
                Game.InfoFrameRate      = RenderFrequency;
                TotalTimeElapsedForInfo = 0.0;
            }


            if (Game.PreviousInterface != Game.InterfaceType.Normal)
            {
                ObjectManager.UpdateAnimatedWorldObjects(0.0, false);
                Game.PreviousInterface = Game.InterfaceType.Normal;
            }
            else
            {
                ObjectManager.UpdateAnimatedWorldObjects(TimeElapsed, false);
            }

            //We need to update the camera position in the render sequence
            //Not doing this means that the camera doesn't move
            // update in one piece
            if (World.CameraMode == World.CameraViewMode.Interior | World.CameraMode == World.CameraViewMode.InteriorLookAhead)
            {
                //Update the in-car camera based upon the current driver car (Cabview or passenger view)
                TrainManager.PlayerTrain.Cars[World.CameraCar].UpdateCamera();
            }
            else if (World.CameraMode == World.CameraViewMode.Exterior)
            {
                //Update the camera position based upon the relative car position
                TrainManager.PlayerTrain.Cars[World.CameraCar].UpdateCamera();
            }
            if (World.CameraRestriction == World.CameraRestrictionMode.NotAvailable)
            {
                World.UpdateDriverBody(TimeElapsed);
            }
            //Check if we are running at an accelerated time factor-
            //Camera motion speed should be the same whatever the game speed is
            if (TimeFactor != 1)
            {
                World.UpdateAbsoluteCamera(TimeElapsed / TimeFactor);
            }
            else
            {
                World.UpdateAbsoluteCamera(TimeElapsed);
            }
            TrainManager.UpdateTrainObjects(TimeElapsed, false);
            if (World.CameraMode == World.CameraViewMode.Interior | World.CameraMode == World.CameraViewMode.InteriorLookAhead | World.CameraMode == World.CameraViewMode.Exterior)
            {
                ObjectManager.UpdateVisibility(World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z);
                int d = TrainManager.PlayerTrain.DriverCar;
                World.CameraSpeed = TrainManager.PlayerTrain.Cars[d].Specs.CurrentSpeed;
            }
            else
            {
                World.CameraSpeed = 0.0;
            }

            World.CameraAlignmentDirection = new World.CameraAlignment();
            if (MainLoop.Quit)
            {
                Program.currentGameWindow.Exit();
            }
            Renderer.UpdateLighting();
            Renderer.RenderScene(TimeElapsed);
            Sounds.Update(TimeElapsed, Interface.CurrentOptions.SoundModel);
            Program.currentGameWindow.SwapBuffers();
            Game.UpdateBlackBox();
            // pause/menu

            // limit framerate
            if (MainLoop.LimitFramerate)
            {
                System.Threading.Thread.Sleep(10);
            }
            MainLoop.UpdateControlRepeats(RealTimeElapsed);
            MainLoop.ProcessKeyboard();
            World.UpdateMouseGrab(TimeElapsed);
            MainLoop.ProcessControls(TimeElapsed);
            for (int i = 0; i < JoystickManager.AttachedJoysticks.Length; i++)
            {
                var railDriver = JoystickManager.AttachedJoysticks[i] as JoystickManager.Raildriver;
                if (railDriver != null)
                {
                    if (Interface.CurrentOptions.RailDriverMPH)
                    {
                        railDriver.SetDisplay((int)(TrainManager.PlayerTrain.Cars[TrainManager.PlayerTrain.DriverCar].Specs
                                                    .CurrentPerceivedSpeed * 2.23694));
                    }
                    else
                    {
                        railDriver.SetDisplay((int)(TrainManager.PlayerTrain.Cars[TrainManager.PlayerTrain.DriverCar].Specs
                                                    .CurrentPerceivedSpeed * 3.6));
                    }
                }
            }
            RenderRealTimeElapsed = 0.0;
            RenderTimeElapsed     = 0.0;



#if DEBUG
            MainLoop.CheckForOpenGlError("MainLoop");
#endif
            if (Interface.CurrentOptions.UnloadUnusedTextures)
            {
                Renderer.UnloadUnusedTextures(TimeElapsed);
                Renderer.LastBoundTexture = null;
            }
            // finish
            try
            {
                Interface.SaveLogs();
            }
            catch { }
        }