Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
SkyManager.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2018 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#ifdef USE_CAELUM
23
24#include "SkyManager.h"
25
26#include "Actor.h"
27#include "AppContext.h"
28#include "CameraManager.h"
29#include "GameContext.h"
30#include "GfxScene.h"
31#include "Terrain.h"
33
34#include <Caelum.h>
35
36using namespace RoR;
37
38SkyManager::SkyManager() : m_caelum_system(nullptr), m_last_clock(0.0)
39{
40 // Initialise CaelumSystem.
41 m_caelum_system = new Caelum::CaelumSystem(
42 RoR::App::GetAppContext()->GetOgreRoot(),
43 App::GetGfxScene()->GetSceneManager(),
44 Caelum::CaelumSystem::CAELUM_COMPONENTS_DEFAULT
45 );
46
47 m_caelum_system->attachViewport(RoR::App::GetAppContext()->GetViewport());
48
49 // Register caelum as a listener.
50 RoR::App::GetAppContext()->GetRenderWindow()->addListener(m_caelum_system);
51 RoR::App::GetAppContext()->GetOgreRoot()->addFrameListener(m_caelum_system);
52}
53
54SkyManager::~SkyManager()
55{
56 RoR::App::GetAppContext()->GetRenderWindow()->removeListener(m_caelum_system);
57 m_caelum_system->shutdown(false);
58 m_caelum_system = nullptr;
59}
60
61void SkyManager::NotifySkyCameraChanged(Ogre::Camera* cam)
62{
63 if (m_caelum_system)
64 m_caelum_system->notifyCameraChanged(cam);
65}
66
67void SkyManager::DetectSkyUpdate()
68{
69 if (!m_caelum_system || !App::GetGameContext()->GetTerrain())
70 {
71 return;
72 }
73
74 Caelum::LongReal c = m_caelum_system->getUniversalClock()->getJulianDay();
75
76 if (c - m_last_clock > 0.001f)
77 {
78 TerrainGeometryManager* gm = App::GetGameContext()->GetTerrain()->getGeometryManager();
79 if (gm)
80 gm->updateLightMap();
81 }
82
83 m_last_clock = c;
84}
85
86void SkyManager::LoadCaelumScript(std::string script, int fogStart, int fogEnd)
87{
88 // load the caelum config
89 try
90 {
91 Caelum::CaelumPlugin::getSingleton().loadCaelumSystemFromScript(m_caelum_system, script);
92
93 // overwrite some settings
94#ifdef CAELUM_VERSION_SEC
95 // important: overwrite fog settings if not using infinite farclip
96 if (fogStart != -1 && fogEnd != -1 && fogStart < fogEnd)
97 {
98 // setting farclip (hacky)
99 App::GetCameraManager()->GetCamera()->setFarClipDistance(fogEnd / 0.8);
100 // custom boundaries
101 m_caelum_system->setManageSceneFog(Ogre::FOG_LINEAR);
102 m_caelum_system->setManageSceneFogStart(fogStart);
103 m_caelum_system->setManageSceneFogEnd(fogEnd);
104 }
105 else if (App::GetCameraManager()->GetCamera()->getFarClipDistance() > 0)
106 {
107 if (fogStart != -1 && fogEnd != -1)
108 {
109 LOG("CaelumFogStart must be smaller then CaelumFogEnd. Ignoring boundaries.");
110 }
111 else if (fogStart != -1 || fogEnd != -1)
112 {
113 LOG("You always need to define both boundaries (CaelumFogStart AND CaelumFogEnd). Ignoring boundaries.");
114 }
115 // non infinite farclip
116 float farclip = App::GetCameraManager()->GetCamera()->getFarClipDistance();
117 m_caelum_system->setManageSceneFog(Ogre::FOG_LINEAR);
118 m_caelum_system->setManageSceneFogStart(farclip * 0.7);
119 m_caelum_system->setManageSceneFogEnd(farclip * 0.9);
120 }
121 else
122 {
123 // no fog in infinite farclip
124 m_caelum_system->setManageSceneFog(Ogre::FOG_NONE);
125 }
126#else
127#error please use a recent Caelum version, see http://www.rigsofrods.org/wiki/pages/Compiling_3rd_party_libraries#Caelum
128#endif // CAELUM_VERSION
129 // now optimize the moon a bit
130 if (m_caelum_system->getMoon())
131 {
132 m_caelum_system->getMoon()->setAutoDisable(true);
133 //m_caelum_system->getMoon()->setAutoDisableThreshold(1);
134 m_caelum_system->getMoon()->setForceDisable(true);
135 m_caelum_system->getMoon()->getMainLight()->setCastShadows(false);
136 }
137
138 m_caelum_system->setEnsureSingleShadowSource(true);
139 m_caelum_system->setEnsureSingleLightSource(true);
140
141 // enforcing update, so shadows are set correctly before creating the terrain
142 m_caelum_system->updateSubcomponents(0.1);
143 }
144 catch (Ogre::Exception& e)
145 {
146 RoR::LogFormat("[RoR] Exception while loading sky script: %s", e.getFullDescription().c_str());
147 }
148 Ogre::Vector3 lightsrc = m_caelum_system->getSun()->getMainLight()->getDirection();
149 m_caelum_system->getSun()->getMainLight()->setDirection(lightsrc.normalisedCopy());
150}
151
152void SkyManager::SetSkyTimeFactor(float factor)
153{
154 m_caelum_system->getUniversalClock()->setTimeScale(factor);
155}
156
157Ogre::Light* SkyManager::GetSkyMainLight()
158{
159 if (m_caelum_system && m_caelum_system->getSun())
160 {
161 return m_caelum_system->getSun()->getMainLight();
162 }
163 return nullptr;
164}
165
166float SkyManager::GetSkyTimeFactor()
167{
168 return m_caelum_system->getUniversalClock()->getTimeScale();
169}
170
171std::string SkyManager::GetPrettyTime()
172{
173 int ignore;
174 int hour;
175 int minute;
176 Caelum::LongReal second;
177 Caelum::Astronomy::getGregorianDateTimeFromJulianDay(m_caelum_system->getJulianDay()
178 , ignore, ignore, ignore, hour, minute, second);
179
180 char buf[100];
181 snprintf(buf, 100, "%02d:%02d:%02d", hour, minute, static_cast<int>(second));
182 return buf;
183}
184
185#endif //USE_CAELUM
System integration layer; inspired by OgreBites::ApplicationContext.
void LOG(const char *msg)
Legacy alias - formerly a macro.
Game state manager and message-queue provider.
Ogre::Root * GetOgreRoot()
Definition AppContext.h:65
Ogre::RenderWindow * GetRenderWindow()
Definition AppContext.h:67
this class handles all interactions with the Ogre Terrain system
AppContext * GetAppContext()
void LogFormat(const char *format,...)
Improved logging utility. Uses fixed 2Kb buffer.