RigsofRods
Soft-body Physics Simulation
SkyX.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of SkyX.
4 Visit http://www.paradise-studios.net/products/skyx/
5 
6 Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt.
21 --------------------------------------------------------------------------------
22 */
23 
24 #pragma warning(disable:4355)
25 
26 #include "SkyX.h"
27 
28 namespace SkyX
29 {
30  SkyX::SkyX(Ogre::SceneManager* sm, Controller* c)
31  : Ogre::FrameListener()
32  , Ogre::RenderTargetListener()
33  , mSceneManager(sm)
34  , mController(c)
35  , mCamera(0)
36  , mMeshManager(new MeshManager(this))
37  , mAtmosphereManager(new AtmosphereManager(this))
38  , mGPUManager(new GPUManager(this))
39  , mMoonManager(new MoonManager(this))
40  , mCloudsManager(new CloudsManager(this))
41  , mRenderQueueGroups(RenderQueueGroups(Ogre::RENDER_QUEUE_SKIES_EARLY, Ogre::RENDER_QUEUE_3, Ogre::RENDER_QUEUE_4, Ogre::RENDER_QUEUE_2))
42  , mCreated(false)
43  , mLastCameraPosition(Ogre::Vector3(0,0,0))
44  , mLastCameraFarClipDistance(-1)
45  , mInfiniteCameraFarClipDistance(100000)
46  , mVisible(true)
47  , mLightingMode(LM_LDR)
48  , mStarfield(true)
49  , mTimeMultiplier(0.1f)
50  , mTimeOffset(0.0f)
51  {
52  // Need to be instanced here, when SkyX::mSceneManager is valid
53  mVCloudsManager = new VCloudsManager(this);
54  }
55 
56  SkyX::~SkyX()
57  {
58  remove();
59 
60  delete mMeshManager;
61  delete mAtmosphereManager;
62  delete mGPUManager;
63  delete mMoonManager;
64  delete mCloudsManager;
65  delete mVCloudsManager;
66 
67  if (mCfgFileManager)
68  delete mCfgFileManager;
69 
70  if (mController->getDeleteBySkyX())
71  {
72  delete mController;
73  }
74  }
75 
76  void SkyX::create()
77  {
78  if (mCreated)
79  {
80  return;
81  }
82 
83  mGPUManager->_notifySkydomeMaterialChanged();
84 
85  mMeshManager->create();
86  mMeshManager->setMaterialName(mGPUManager->getSkydomeMaterialName());
87 
88  mAtmosphereManager->_update(mAtmosphereManager->getOptions(), true);
89 
90  if (mStarfield)
91  {
92  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_FRAGMENT, "uTime", mTimeOffset*0.5f, false);
93  }
94 
95  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_VERTEX, "uLightDir", mController->getSunDirection());
96  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_FRAGMENT, "uLightDir", mController->getSunDirection());
97 
98  mMoonManager->create();
99 
100  setVisible(mVisible);
101 
102  mLastCameraPosition = Ogre::Vector3(0,0,0);
103  mLastCameraFarClipDistance = -1;
104 
105  mCreated = true;
106  }
107 
108  void SkyX::remove()
109  {
110  if (!mCreated)
111  {
112  return;
113  }
114 
115  mCloudsManager->removeAll();
116  mMeshManager->remove();
117  mMoonManager->remove();
118  mVCloudsManager->remove();
119 
120  mCamera = 0;
121 
122  mCreated = false;
123  }
124 
125  void SkyX::update(const Ogre::Real& timeSinceLastFrame)
126  {
127  if (!mCreated)
128  {
129  return;
130  }
131 
132  if (mTimeMultiplier != 0)
133  {
134  float timemultiplied = timeSinceLastFrame * mTimeMultiplier;
135 
136  mTimeOffset += timemultiplied;
137 
138  mController->update(timemultiplied);
139 
140  if (mStarfield)
141  {
142  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_FRAGMENT, "uTime", mTimeOffset*0.5f, false);
143  }
144  }
145 
146  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_VERTEX, "uLightDir", mController->getSunDirection());
147  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_FRAGMENT, "uLightDir", mController->getSunDirection());
148 
149  mMoonManager->updateMoonPhase(mController->getMoonPhase());
150  mCloudsManager->update();
151  mVCloudsManager->update(timeSinceLastFrame);
152  }
153 
154  void SkyX::notifyCameraRender(Ogre::Camera* c)
155  {
156  if (!mCreated)
157  {
158  return;
159  }
160 
161  mCamera = c;
162 
163  if (mLastCameraPosition != c->getDerivedPosition())
164  {
165  mMeshManager->getSceneNode()->setPosition(mCamera->getDerivedPosition());
166 
167  mLastCameraPosition = mCamera->getDerivedPosition();
168  }
169 
170  if (mLastCameraFarClipDistance != c->getFarClipDistance())
171  {
172  mMeshManager->updateGeometry(c);
173 
174  mLastCameraFarClipDistance = mCamera->getFarClipDistance();
175  }
176 
177  mMoonManager->updateGeometry(c);
178 
179  mVCloudsManager->notifyCameraRender(c);
180  }
181 
182  void SkyX::setVisible(const bool& visible)
183  {
184  mVisible = visible;
185 
186  if (!mCreated)
187  {
188  return;
189  }
190 
191  mMeshManager->getSceneNode()->setVisible(mVisible);
192  mMoonManager->getMoonSceneNode()->setVisible(mVisible);
193 
194  if (mVCloudsManager->isCreated())
195  {
196  mVCloudsManager->getVClouds()->setVisible(mVisible);
197  }
198  }
199 
200  void SkyX::setRenderQueueGroups(const RenderQueueGroups& rqg)
201  {
202  mRenderQueueGroups = rqg;
203 
204  mVCloudsManager->getVClouds()->setRenderQueueGroups(
205  VClouds::VClouds::RenderQueueGroups(mRenderQueueGroups.vclouds, mRenderQueueGroups.vcloudsLightningsUnder, mRenderQueueGroups.vcloudsLightningsOver));
206 
207  if (!mCreated)
208  {
209  return;
210  }
211 
212  mMeshManager->getEntity()->setRenderQueueGroup(mRenderQueueGroups.skydome);
213  mMoonManager->getMoonBillboard()->setRenderQueueGroup(mRenderQueueGroups.skydome+1);
214  }
215 
216  void SkyX::setLightingMode(const LightingMode& lm)
217  {
218  mLightingMode = lm;
219 
220  if (!mCreated)
221  {
222  return;
223  }
224 
225  mGPUManager->_notifySkydomeMaterialChanged();
226 
227  // Update skydome material
228  mMeshManager->setMaterialName(mGPUManager->getSkydomeMaterialName());
229  // Update layered clouds material
230  mCloudsManager->registerAll();
231  // Update ground passes materials and update textures gamma correction
232  mGPUManager->_updateFP();
233 
234  // Update parameters
235  mAtmosphereManager->_update(mAtmosphereManager->getOptions(), true);
236  }
237 
238  void SkyX::setStarfieldEnabled(const bool& Enabled)
239  {
240  mStarfield = Enabled;
241 
242  if (!mCreated)
243  {
244  return;
245  }
246 
247  mGPUManager->_notifySkydomeMaterialChanged();
248 
249  // Update skydome material
250  mMeshManager->setMaterialName(mGPUManager->getSkydomeMaterialName());
251 
252  // Update parameters
253  mAtmosphereManager->_update(mAtmosphereManager->getOptions(), true);
254 
255  if (mStarfield)
256  {
257  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_FRAGMENT, "uTime", mTimeOffset*0.5f, false);
258  }
259 
260  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_VERTEX, "uLightDir", mController->getSunDirection());
261  mGPUManager->setGpuProgramParameter(GPUManager::GPUP_FRAGMENT, "uLightDir", mController->getSunDirection());
262  }
263 
264  bool SkyX::frameStarted(const Ogre::FrameEvent& e)
265  {
266  update(e.timeSinceLastFrame);
267  return true;
268  }
269 
270  void SkyX::preViewportUpdate(const Ogre::RenderTargetViewportEvent& evt)
271  {
272  notifyCameraRender(evt.source->getCamera());
273  }
274 }
SkyX::SkyX::LightingMode
LightingMode
Lighting mode enumeration SkyX is designed for true HDR rendering, but there is a big number of appli...
Definition: SkyX.h:96
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::VClouds::VClouds::RenderQueueGroups
Render queue groups.
Definition: VClouds.h:40
SkyX::AtmosphereManager
Definition: AtmosphereManager.h:33
SkyX::MoonManager
Definition: MoonManager.h:33
SkyX::CloudsManager
Definition: CloudsManager.h:224
SkyX::VCloudsManager
Definition: VCloudsManager.h:36
SkyX::SkyX::RenderQueueGroups
Render queue groups.
Definition: SkyX.h:66
SkyX.h
SkyX::GPUManager::GPUP_VERTEX
@ GPUP_VERTEX
Definition: GPUManager.h:41
SkyX::GPUManager::GPUP_FRAGMENT
@ GPUP_FRAGMENT
Definition: GPUManager.h:43
SkyX::Controller
Controller base class.
Definition: Controller.h:33
Ogre
Definition: ExtinguishableFireAffector.cpp:35
SkyX::MeshManager
Definition: MeshManager.h:33
SkyX::SkyX
SkyX class Create simple and beautiful skies!
Definition: SkyX.h:61
SkyX::GPUManager
Definition: GPUManager.h:33