RigsofRods
Soft-body Physics Simulation
SkyXManager.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-2014 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.com/
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 #include "SkyXManager.h"
23 
24 #include "Actor.h"
25 #include "AppContext.h"
26 #include "CameraManager.h"
27 #include "GameContext.h"
28 #include "GfxScene.h"
29 #include "HydraxWater.h"
30 #include "Terrain.h"
31 #include "TerrainGeometryManager.h"
32 
33 using namespace Ogre;
34 using namespace RoR;
35 
36 SkyXManager::SkyXManager(Ogre::String configFile)
37 {
38  InitLight();
39 
40  //Ogre::ResourceGroupManager::getSingleton().addResourceLocation("..\\resource\\SkyX\\","FileSystem", "SkyX",true); //Temp
41 
42  mBasicController = new SkyX::BasicController();
43  mSkyX = new SkyX::SkyX(App::GetGfxScene()->GetSceneManager(), mBasicController);
44 
45  mCfgFileManager = new SkyX::CfgFileManager(mSkyX, mBasicController, App::GetCameraManager()->GetCamera());
46  mCfgFileManager->load(configFile);
47 
48  mSkyX->create();
49 
50  RoR::App::GetAppContext()->GetOgreRoot()->addFrameListener(mSkyX);
51  RoR::App::GetAppContext()->GetRenderWindow()->addListener(mSkyX);
52 }
53 
54 SkyXManager::~SkyXManager()
55 {
56  RoR::App::GetAppContext()->GetRenderWindow()->removeListener(mSkyX);
57  mSkyX->remove();
58 
59  mSkyX = nullptr;
60 
61  delete mBasicController;
62  mBasicController = nullptr;
63 }
64 
65 Vector3 SkyXManager::getMainLightDirection()
66 {
67  if (mBasicController != nullptr)
68  return mBasicController->getSunDirection();
69  return Ogre::Vector3(0.0,0.0,0.0);
70 }
71 
72 Light *SkyXManager::getMainLight()
73 {
74  return mLight1;
75 }
76 
77 bool SkyXManager::update(float dt)
78 {
79  UpdateSkyLight();
80  mSkyX->update(dt);
81  return true;
82 }
83 
84 
85 bool SkyXManager::UpdateSkyLight()
86 {
87  Ogre::Vector3 lightDir = -getMainLightDirection();
88  const Ogre::Vector3 camPos = App::GetCameraManager()->GetCameraNode()->_getDerivedPosition();
89  Ogre::Vector3 sunPos = camPos - lightDir*mSkyX->getMeshManager()->getSkydomeRadius(App::GetCameraManager()->GetCamera());
90 
91  // Calculate current color gradients point
92  float point = (-lightDir.y + 1.0f) / 2.0f;
93 
95  {
96  App::GetGameContext()->GetTerrain()->getHydraxManager ()->GetHydrax ()->setWaterColor (mWaterGradient.getColor (point));
98  }
99 
100 
101  mLight0 = App::GetGfxScene()->GetSceneManager()->getLight("Light0");
102  mLight1 = App::GetGfxScene()->GetSceneManager()->getLight("Light1");
103 
104  mLight0->setPosition(sunPos*0.02);
105  mLight1->setDirection(lightDir);
106  if (App::GetGameContext()->GetTerrain()->getWater())
107  {
109  }
110 
111  //setFadeColour was removed with https://github.com/RigsOfRods/rigs-of-rods/pull/1459
112 /* Ogre::Vector3 sunCol = mSunGradient.getColor(point);
113  mLight0->setSpecularColour(sunCol.x, sunCol.y, sunCol.z);
114  if (App::GetGameContext()->GetTerrain()->getWater()) App::GetGameContext()->GetTerrain()->getWater()->setFadeColour(Ogre::ColourValue(sunCol.x, sunCol.y, sunCol.z));
115  */
116  Ogre::Vector3 ambientCol = mAmbientGradient.getColor(point);
117  mLight1->setDiffuseColour(ambientCol.x, ambientCol.y, ambientCol.z);
118  mLight1->setPosition(100,100,100);
119 
120  if (mBasicController->getTime().x > 12)
121  {
122  if (mBasicController->getTime().x > mBasicController->getTime().z)
123  mLight0->setVisible(false);
124  else
125  mLight0->setVisible(true);
126  }
127  else
128  {
129  if (mBasicController->getTime ().x < mBasicController->getTime ().z)
130  mLight0->setVisible (false);
131  else
132  mLight0->setVisible (true);
133  }
134 
135  if (round (mBasicController->getTime ().x) != mLastHour)
136  {
138  if (gm)
139  gm->updateLightMap ();
140 
141  mLastHour = round (mBasicController->getTime ().x);
142  }
143 
144  return true;
145 }
146 
147 bool SkyXManager::InitLight()
148 {
149  // Water
150  mWaterGradient = SkyX::ColorGradient();
151  mWaterGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.058209,0.535822,0.779105)*0.4, 1));
152  mWaterGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.058209,0.535822,0.729105)*0.3, 0.8));
153  mWaterGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.058209,0.535822,0.679105)*0.25, 0.6));
154  mWaterGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.058209,0.535822,0.679105)*0.2, 0.5));
155  mWaterGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.058209,0.535822,0.679105)*0.1, 0.45));
156  mWaterGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.058209,0.535822,0.679105)*0.025, 0));
157  // Sun
158  mSunGradient = SkyX::ColorGradient();
159  mSunGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.8,0.75,0.55)*1.5, 1.0f));
160  mSunGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.8,0.75,0.55)*1.4, 0.75f));
161  mSunGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.8,0.75,0.55)*1.3, 0.5625f));
162  mSunGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.5,0.2)*1.5, 0.5f));
163  mSunGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.5,0.5)*0.25, 0.45f));
164  mSunGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.5,0.5)*0.01, 0.0f));
165  // Ambient
166  mAmbientGradient = SkyX::ColorGradient();
167  mAmbientGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*1, 1.0f));
168  mAmbientGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*1, 0.6f));
169  mAmbientGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.6, 0.5f));
170  mAmbientGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.3, 0.45f));
171  mAmbientGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.1, 0.35f));
172  mAmbientGradient.addCFrame(SkyX::ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.05, 0.0f));
173 
174  App::GetGfxScene()->GetSceneManager()->setAmbientLight(ColourValue(0.35,0.35,0.35)); //Not needed because terrn2 has ambientlight settings
175 
176  // Light
177  mLight0 = App::GetGfxScene()->GetSceneManager()->createLight("Light0");
178  mLight0->setDiffuseColour(1, 1, 1);
179  mLight0->setCastShadows(false);
180 
181  mLight1 = App::GetGfxScene()->GetSceneManager()->createLight("Light1");
182  mLight1->setType(Ogre::Light::LT_DIRECTIONAL);
183 
184  return true;
185 }
186 
187 size_t SkyXManager::getMemoryUsage()
188 {
189  //TODO
190  return 0;
191 }
192 
193 void SkyXManager::freeResources()
194 {
195  //TODO
196 }
GameContext.h
Game state manager and message-queue provider.
SkyX::ColorGradient
Definition: ColorGradient.h:31
SkyXManager.h
SkyX::BasicController
Basic controller class
Definition: BasicController.h:35
RoR::App::GetCameraManager
CameraManager * GetCameraManager()
Definition: Application.cpp:275
RoR::AppContext::GetRenderWindow
Ogre::RenderWindow * GetRenderWindow()
Definition: AppContext.h:67
RoR::App::GetAppContext
AppContext * GetAppContext()
Definition: Application.cpp:266
Hydrax::Hydrax::setSunPosition
void setSunPosition(const Ogre::Vector3 &SunPosition)
Set sun position.
Definition: Hydrax.cpp:883
TerrainGeometryManager.h
CameraManager.h
RoR::HydraxWater::GetHydrax
Hydrax::Hydrax * GetHydrax()
Definition: HydraxWater.h:52
AppContext.h
System integration layer; inspired by OgreBites::ApplicationContext.
RoR::CameraManager::GetCameraNode
Ogre::SceneNode * GetCameraNode()
Definition: CameraManager.h:63
RoR::TerrainGeometryManager::updateLightMap
void updateLightMap()
Definition: TerrainGeometryManager.cpp:388
Actor.h
RoR::GfxScene::GetSceneManager
Ogre::SceneManager * GetSceneManager()
Definition: GfxScene.h:64
RoR::Terrain::getGeometryManager
TerrainGeometryManager * getGeometryManager()
Definition: Terrain.h:75
GfxScene.h
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:280
RoR::Terrain::getHydraxManager
HydraxWater * getHydraxManager()
Definition: Terrain.h:78
SkyX::ColorGradient::ColorFrame
std::pair< Ogre::Vector3, Ogre::Real > ColorFrame
Color frame type definition ColorFrame.first: Colour value ColorFrame.second: Position in the gradien...
Definition: ColorGradient.h:38
RoR::IWater::WaterSetSunPosition
virtual void WaterSetSunPosition(Ogre::Vector3)
Definition: IWater.h:50
RoR::AppContext::GetOgreRoot
Ogre::Root * GetOgreRoot()
Definition: AppContext.h:65
Terrain.h
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::TerrainGeometryManager
this class handles all interactions with the Ogre Terrain system
Definition: TerrainGeometryManager.h:38
SkyX::CfgFileManager
Config file manager.
Definition: SCfgFileManager.h:40
RoR
Definition: AppContext.h:36
SkyX::SkyX
SkyX class Create simple and beautiful skies!
Definition: SkyX.h:61
RoR::App::GetGfxScene
GfxScene * GetGfxScene()
Definition: Application.cpp:276
RoR::Terrain::getWater
IWater * getWater()
Definition: Terrain.h:84
HydraxWater.h
Hydrax::Hydrax::setWaterColor
void setWaterColor(const Ogre::Vector3 &WaterColor)
Definition: Hydrax.cpp:814
RoR::GameContext::GetTerrain
const TerrainPtr & GetTerrain()
Definition: GameContext.h:117