RigsofRods
Soft-body Physics Simulation
VClouds.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 #include "VClouds.h"
25 
26 #include "gfx/skyx/SkyX.h"
27 
28 namespace SkyX { namespace VClouds
29 {
30  VClouds::VClouds(Ogre::SceneManager *sm)
31  : mSceneManager(sm)
32  , mCamera(0)
33  , mCreated(false)
34  , mGeometrySettings(GeometrySettings())
35  , mDistanceFallingParams(Ogre::Vector2(1,-1))
36  , mRenderQueueGroups(RenderQueueGroups(Ogre::RENDER_QUEUE_MAIN, Ogre::RENDER_QUEUE_9, Ogre::RENDER_QUEUE_4))
37  , mWindDirection(Ogre::Degree(0))
38  , mWindSpeed(80.0f)
39  , mWheater(Ogre::Vector2(0.5f, 1.0f))
40  , mDelayedResponse(false)
41  , mSunDirection(Ogre::Vector3(0,-1,0))
42  , mSunColor(Ogre::Vector3(1,1,1))
43  , mAmbientColor(Ogre::Vector3(0.63f,0.63f,0.7f))
44  , mLightResponse(Ogre::Vector4(0.25f,0.2f,1.0f,0.1f))
45  , mAmbientFactors(Ogre::Vector4(0.45f,0.3f,0.6f,1))
46  , mGlobalOpacity(1.0f)
47  , mCloudFieldScale(1.0f)
48  , mNoiseScale(4.2f)
49  , mVisible(true)
50  , mDataManager(new DataManager(this))
51  , mGeometryManager(new GeometryManager(this))
52  , mLightningManager(new LightningManager(this))
53  , mCamerasData(std::vector<CameraData>())
54  , mVolCloudsMaterial(Ogre::MaterialPtr())
55  , mVolCloudsLightningMaterial(Ogre::MaterialPtr())
56  {
57  }
58 
59  VClouds::~VClouds()
60  {
61  remove();
62 
63  delete mDataManager;
64  delete mGeometryManager;
65  delete mLightningManager;
66  }
67 
68  void VClouds::create()
69  {
70  remove();
71 
72  mVolCloudsMaterial = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().getByName("SkyX_VolClouds"));
73  mVolCloudsLightningMaterial = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().getByName("SkyX_VolClouds_Lightning"));
74 
75  if (mVolCloudsMaterial.isNull() || mVolCloudsLightningMaterial.isNull())
76  {
77  SkyXLOG("Error while creating SkyX::VClouds::VClouds, materials are not found");
78  return;
79  }
80 
81  // Data manager
82  mDataManager->create(128,128,20);
83 
84  // Geometry manager
87 
88  mGeometryManager->getSceneNode()->setVisible(mVisible);
89 
91  ->getTechnique(0)->getPass(0)->getVertexProgramParameters()->setNamedConstant("uRadius", mGeometrySettings.Radius);
93  getTechnique(0)->getPass(0)->getVertexProgramParameters()->setNamedConstant("uRadius", mGeometrySettings.Radius);
94 
95  // Lightning manager
97 
98  mCreated = true;
99 
100  // Update material parameters
105 
106  // Set current wheater
108  }
109 
110  void VClouds::create(const GeometrySettings& gs)
111  {
112  // Update geometry settings
113  mGeometrySettings = gs;
114 
115  create();
116  }
117 
118  void VClouds::create(const Ogre::Vector2& Height, const float& Radius)
119  {
120  // Update geometry params
121  mGeometrySettings.Height = Height;
122  mGeometrySettings.Radius = Radius;
123 
124  create();
125  }
126 
127  void VClouds::remove()
128  {
129  if (!mCreated)
130  {
131  return;
132  }
133 
134  mDataManager->remove();
137 
138  mCamera = 0;
139  mCamerasData.clear();
140 
141  mVolCloudsMaterial.setNull();
142  mVolCloudsLightningMaterial.setNull();
143 
144  mCreated = false;
145  }
146 
147  void VClouds::update(const Ogre::Real& timeSinceLastFrame)
148  {
149  if (!mCreated)
150  {
151  return;
152  }
153 
154  mDataManager->update(timeSinceLastFrame);
155  mGeometryManager->update(timeSinceLastFrame);
156  mLightningManager->update(timeSinceLastFrame);
157 
159  {
161  getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uInterpolation", mDataManager->_getInterpolation());
163  getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uSunDirection", -mSunDirection);
164  }
165  else
166  {
168  getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uInterpolation", mDataManager->_getInterpolation());
170  getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uSunDirection", -mSunDirection);
171  }
172  }
173 
174  void VClouds::notifyCameraRender(Ogre::Camera* c, const Ogre::Real& timeSinceLastCameraFrame)
175  {
176  if (!mCreated)
177  {
178  return;
179  }
180 
181  mCamera = c;
182 
183  // Check if the camera is registered
184  bool isRegistered = false;
185  for (Ogre::uint32 k = 0; k < mCamerasData.size(); k++)
186  {
187  if (mCamerasData.at(k).camera == c)
188  {
189  isRegistered = true;
190  break;
191  }
192  }
193 
194  if (!isRegistered)
195  {
196  mCamerasData.push_back(CameraData(c));
197  SkyXLOG("VClouds warning: unregistered camera registered, manual unregistering is needed before camera destruction");
198  }
199 
200  mGeometryManager->updateGeometry(c, timeSinceLastCameraFrame);
202 
205  }
206 
207  void VClouds::registerCamera(Ogre::Camera* c)
208  {
209  for (Ogre::uint32 k = 0; k < mCamerasData.size(); k++)
210  {
211  if (mCamerasData.at(k).camera == c)
212  {
213  return;
214  }
215  }
216 
217  mCamerasData.push_back(CameraData(c));
218  }
219 
220  void VClouds::unregisterCamera(Ogre::Camera* c)
221  {
222  for (std::vector<CameraData>::iterator it = mCamerasData.begin(); it != mCamerasData.end(); it++)
223  {
224  if ((*it).camera == c)
225  {
226  mCamerasData.erase(it);
227  return;
228  }
229  }
230  }
231 
232  void VClouds::setVisible(const bool& visible)
233  {
234  mVisible = visible;
235 
236  if (!mCreated)
237  {
238  return;
239  }
240 
241  mGeometryManager->getSceneNode()->setVisible(mVisible);
243  }
244 
245  void VClouds::setRenderQueueGroups(const RenderQueueGroups& rqg)
246  {
247  mRenderQueueGroups = rqg;
248 
249  if (!mCreated)
250  {
251  return;
252  }
253 
255  }
256 
257  void VClouds::setSunColor(const Ogre::Vector3& SunColor)
258  {
259  mSunColor = SunColor;
260 
261  if (!mCreated)
262  {
263  return;
264  }
265 
266  mVolCloudsMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
267  ->setNamedConstant("uSunColor", mSunColor);
268  mVolCloudsLightningMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
269  ->setNamedConstant("uSunColor", mSunColor);
270  }
271 
272  void VClouds::setAmbientColor(const Ogre::Vector3& AmbientColor)
273  {
274  mAmbientColor = AmbientColor;
275 
276  if (!mCreated)
277  {
278  return;
279  }
280 
281  mVolCloudsMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
282  ->setNamedConstant("uAmbientColor", mAmbientColor);
283  mVolCloudsLightningMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
284  ->setNamedConstant("uAmbientColor", mAmbientColor);
285  }
286 
287  void VClouds::setLightResponse(const Ogre::Vector4& LightResponse)
288  {
289  mLightResponse = LightResponse;
290 
291  if (!mCreated)
292  {
293  return;
294  }
295 
296  mVolCloudsMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
297  ->setNamedConstant("uLightResponse", mLightResponse);
298  mVolCloudsLightningMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
299  ->setNamedConstant("uLightResponse", mLightResponse);
300  }
301 
302  void VClouds::setAmbientFactors(const Ogre::Vector4& AmbientFactors)
303  {
304  mAmbientFactors = AmbientFactors;
305 
306  if (!mCreated)
307  {
308  return;
309  }
310 
311  mVolCloudsMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
312  ->setNamedConstant("uAmbientFactors", mAmbientFactors);
313  mVolCloudsLightningMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()
314  ->setNamedConstant("uAmbientFactors", mAmbientFactors);
315  }
316 
317  void VClouds::setWheater(const float& Humidity, const float& AverageCloudsSize, const bool& DelayedResponse)
318  {
319  mWheater = Ogre::Vector2(Humidity, AverageCloudsSize);
320  mDelayedResponse = DelayedResponse;
321 
322  if (!mCreated)
323  {
324  return;
325  }
326 
328  }
329 
330 }}
SkyX::VClouds::VClouds::GeometrySettings::Height
Ogre::Vector2 Height
Height: x = Altitude over the camera, y: Field height (both in world coordinates)
Definition: VClouds.h:65
VClouds.h
SkyX::VClouds::DataManager
Definition: DataManager.h:36
SkyX::VClouds::DataManager::create
void create(const int &nx, const int &ny, const int &nz)
Create.
Definition: DataManager.cpp:158
SkyX::VClouds::VClouds::mAmbientColor
Ogre::Vector3 mAmbientColor
Ambient color.
Definition: VClouds.h:534
SkyX::VClouds::VClouds::mCamerasData
std::vector< CameraData > mCamerasData
Cameras data.
Definition: VClouds.h:577
SkyX::VClouds::VClouds::GeometrySettings::NumberOfBlocks
int NumberOfBlocks
Number of blocks.
Definition: VClouds.h:71
SkyX::VClouds::GeometryManager::_updateRenderQueueGroup
void _updateRenderQueueGroup(const Ogre::uint8 &rqg)
Update render queue group.
Definition: GeometryManager.cpp:125
SkyX::VClouds::VClouds::RenderQueueGroups::vclouds
Ogre::uint8 vclouds
VClouds render queue group.
Definition: VClouds.h:53
SkyX::VClouds::GeometryManager::_getCurrentDistance
const Ogre::Vector3 _getCurrentDistance() const
Get current camera to cloud field distance.
Definition: GeometryManager.h:116
SkyX::VClouds::VClouds::GeometrySettings::Alpha
Ogre::Radian Alpha
Angles.
Definition: VClouds.h:67
SkyX::VClouds::VClouds::mGeometrySettings
GeometrySettings mGeometrySettings
Geometry settings.
Definition: VClouds.h:510
SkyX::VClouds::DataManager::remove
void remove()
Remove.
Definition: DataManager.cpp:55
SkyX::VClouds::VClouds::setLightResponse
void setLightResponse(const Ogre::Vector4 &LightResponse)
Set light response.
Definition: VClouds.cpp:287
SkyX::VClouds::VClouds::mVolCloudsLightningMaterial
Ogre::MaterialPtr mVolCloudsLightningMaterial
Vol. clouds + lightning material.
Definition: VClouds.h:574
SkyX::VClouds::GeometryManager::getHeight
const Ogre::Vector2 getHeight() const
Get height (x = Altitude over the camera, y: Field height (both in world coordinates))
Definition: GeometryManager.h:95
SkyX::VClouds::VClouds::GeometrySettings::Radius
float Radius
Radius.
Definition: VClouds.h:69
SkyX::VClouds::GeometryManager
Definition: GeometryManager.h:35
SkyX::VClouds::VClouds::create
void create()
Create.
Definition: VClouds.cpp:68
SkyX::VClouds::VClouds::mWheater
Ogre::Vector2 mWheater
Wheater parameters: x = Humidity, y = Average clouds size, both un [0,1] range.
Definition: VClouds.h:524
SkyX::VClouds::LightningManager::isEnabled
const bool & isEnabled() const
Get whether the lightning system is enabled or not.
Definition: LightningManager.h:127
SkyX::VClouds::VClouds::GeometrySettings::Beta
Ogre::Radian Beta
Definition: VClouds.h:67
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::VClouds::DataManager::_getInterpolation
const Ogre::Real _getInterpolation() const
Get current interpolation factor.
Definition: DataManager.h:117
SkyX::VClouds::VClouds::RenderQueueGroups
Render queue groups.
Definition: VClouds.h:40
SkyX::VClouds::GeometryManager::create
void create(const Ogre::Vector2 &Height, const float &Radius, const Ogre::Radian &Alpha, const Ogre::Radian &Beta, const int &NumberOfBlocks, const int &Na, const int &Nb, const int &Nc)
Create.
Definition: GeometryManager.cpp:52
SkyX::VClouds::GeometryManager::getSceneNode
Ogre::SceneNode * getSceneNode()
Get scene node.
Definition: GeometryManager.h:87
SkyX::VClouds::VClouds::mCreated
bool mCreated
Has been create(...) already called?
Definition: VClouds.h:507
SkyX::VClouds::VClouds::CameraData
Camera data struct.
Definition: VClouds.h:110
SkyX::VClouds::VClouds::mDelayedResponse
bool mDelayedResponse
Delayed response (This param is stored to allow the user call setWheater(...) before create() )
Definition: VClouds.h:526
SkyX::VClouds::VClouds::mRenderQueueGroups
RenderQueueGroups mRenderQueueGroups
Render queue groups.
Definition: VClouds.h:516
SkyX::VClouds::VClouds::mGeometryManager
GeometryManager * mGeometryManager
Geometry manager.
Definition: VClouds.h:562
SkyX::VClouds::LightningManager::update
void update(const Ogre::Real &timeSinceLastFrame)
Update, to be invoked per frame.
Definition: LightningManager.cpp:105
SkyX.h
SkyX::VClouds::LightningManager::_updateRenderQueueGroup
void _updateRenderQueueGroup(const Ogre::uint8 &rqg)
Update render queue group.
Definition: LightningManager.cpp:298
SkyX::VClouds::VClouds::GeometrySettings
Geometry settings.
Definition: VClouds.h:62
SkyX::VClouds::LightningManager
Definition: LightningManager.h:35
SkyX::VClouds::VClouds::mAmbientFactors
Ogre::Vector4 mAmbientFactors
Ambient factors x - constant, y - linear, z - cuadratic, w - cubic.
Definition: VClouds.h:546
SkyX::VClouds::GeometryManager::update
void update(const Ogre::Real &timeSinceLastFrame)
Update, to be invoked per frame.
Definition: GeometryManager.cpp:94
SkyX::VClouds::VClouds::mSunColor
Ogre::Vector3 mSunColor
Sun color.
Definition: VClouds.h:532
SkyX::VClouds::VClouds::mLightningManager
LightningManager * mLightningManager
Lightning manager.
Definition: VClouds.h:564
SkyX::VClouds::VClouds::mVolCloudsMaterial
Ogre::MaterialPtr mVolCloudsMaterial
Vol. clouds material.
Definition: VClouds.h:572
SkyX::VClouds::VClouds::mVisible
bool mVisible
Is VClouds visible?
Definition: VClouds.h:557
SkyX::VClouds::VClouds::mLightResponse
Ogre::Vector4 mLightResponse
Light response: x - Sun light power y - Sun beta multiplier z - Ambient color multiplier w - Distance...
Definition: VClouds.h:542
SkyX::VClouds::VClouds::setSunColor
void setSunColor(const Ogre::Vector3 &SunColor)
Set sun color.
Definition: VClouds.cpp:257
SkyX::VClouds::LightningManager::_setVisible
void _setVisible(const bool &v)
Set visible.
Definition: LightningManager.cpp:306
SkyX::VClouds::VClouds::GeometrySettings::Nb
int Nb
Definition: VClouds.h:73
SkyX::VClouds::VClouds
Definition: VClouds.h:35
SkyX::VClouds::GeometryManager::remove
void remove()
Remove.
Definition: GeometryManager.cpp:72
SkyX::VClouds::VClouds::GeometrySettings::Na
int Na
Number of slices per geometry zone.
Definition: VClouds.h:73
SkyX::VClouds::VClouds::setAmbientFactors
void setAmbientFactors(const Ogre::Vector4 &AmbientFactors)
Set ambient factors.
Definition: VClouds.cpp:302
SkyX::VClouds::VClouds::RenderQueueGroups::vcloudsLightningsOver
Ogre::uint8 vcloudsLightningsOver
VClouds lightnings render queue group (when the camera is over the cloud field)
Definition: VClouds.h:57
SkyX::VClouds::VClouds::setWheater
void setWheater(const float &Humidity, const float &AverageCloudsSize, const bool &DelayedResponse)
Set wheater parameters Use this funtion to update the cloud field parameters, you'll get a smart and ...
Definition: VClouds.cpp:317
SkyX::VClouds::DataManager::setWheater
void setWheater(const float &Humidity, const float &AverageCloudsSize, const bool &delayedResponse=true)
Set wheater parameters Use this funtion to update the cloud field parameters, you'll get a smart and ...
Definition: DataManager.cpp:295
SkyXLOG
#define SkyXLOG(msg)
Include external headers.
Definition: Prerequisites.h:34
Ogre
Definition: ExtinguishableFireAffector.cpp:35
SkyX::VClouds::VClouds::mCamera
Ogre::Camera * mCamera
Current rendering camera.
Definition: VClouds.h:569
SkyX::VClouds::LightningManager::updateMaterial
void updateMaterial()
Update material.
Definition: LightningManager.cpp:230
SkyX::VClouds::VClouds::mDataManager
DataManager * mDataManager
Data manager.
Definition: VClouds.h:560
SkyX::VClouds::GeometryManager::updateGeometry
void updateGeometry(Ogre::Camera *c, const Ogre::Real &timeSinceLastCameraFrame)
Update geoemtry.
Definition: GeometryManager.cpp:104
SkyX::VClouds::VClouds::mSunDirection
Ogre::Vector3 mSunDirection
Sun direction.
Definition: VClouds.h:529
SkyX::VClouds::LightningManager::remove
void remove()
Remove.
Definition: LightningManager.cpp:80
SkyX::VClouds::VClouds::RenderQueueGroups::vcloudsLightningsUnder
Ogre::uint8 vcloudsLightningsUnder
VClouds lightnings render queue group (when the camera is under the cloud field)
Definition: VClouds.h:55
SkyX::VClouds::VClouds::remove
void remove()
Remove.
Definition: VClouds.cpp:127
SkyX::VClouds::VClouds::setAmbientColor
void setAmbientColor(const Ogre::Vector3 &AmbientColor)
Set ambient color.
Definition: VClouds.cpp:272
SkyX::VClouds::VClouds::GeometrySettings::Nc
int Nc
Definition: VClouds.h:73
SkyX::VClouds::LightningManager::create
void create()
Create.
Definition: LightningManager.cpp:53
SkyX::VClouds::DataManager::update
void update(const Ogre::Real &timeSinceLastFrame)
Update.
Definition: DataManager.cpp:78