RigsofRods
Soft-body Physics Simulation
CloudsManager.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 "CloudsManager.h"
25 
26 #include "SkyX.h"
27 
28 namespace SkyX
29 {
32  : mSkyX(s)
33  , mOptions(Options())
34  , mCloudLayerPass(0)
35  {
37  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.95f, 1.0f));
38  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.7,0.7,0.65), 0.625f));
39  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.55,0.4), 0.5625f));
40  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.45,0.3)*0.4, 0.5f));
41  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.25,0.25)*0.1, 0.45f));
42  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.2,0.2,0.3)*0.1, 0.35f));
43  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.2,0.2,0.5)*0.15, 0));
44 
46  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.95f, 1.0f));
47  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.8, 0.75f));
48  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.8,0.75,0.55)*1.3, 0.5625f));
49  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.5,0.2)*0.75, 0.5f));
50  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.5,0.2)*0.35, 0.4725f));
51  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.5,0.5)*0.15, 0.45f));
52  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.2,0.2,0.25)*0.5, 0.3f));
53  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.5,0.5)*0.35, 0.0f));
54  }
55 
57  : mSkyX(s)
58  , mOptions(o)
59  , mCloudLayerPass(0)
60  {
62  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.95f, 1.0f));
63  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.7,0.7,0.65), 0.625f));
64  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.55,0.4), 0.5625f));
65  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.45,0.3)*0.4, 0.5f));
66  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.25,0.25)*0.1, 0.45f));
67  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.2,0.2,0.3)*0.1, 0.35f));
68  mAmbientGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.2,0.2,0.5)*0.15, 0));
69 
71  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.95f, 1.0f));
72  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(1,1,1)*0.8, 0.75f));
73  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.8,0.75,0.55)*1.3, 0.5625f));
74  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.5,0.2)*0.75, 0.5f));
75  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.6,0.5,0.2)*0.35, 0.4725f));
76  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.5,0.5)*0.15, 0.45f));
77  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.2,0.2,0.25)*0.5, 0.3f));
78  mSunGradient.addCFrame(ColorGradient::ColorFrame(Ogre::Vector3(0.5,0.5,0.5)*0.35, 0.0f));
79  }
80 
82  {
83  _unregister();
84  }
85 
86  void CloudLayer::_registerCloudLayer(Ogre::Pass* CloudLayerPass)
87  {
88  _unregister();
89 
90  CloudLayerPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
91  CloudLayerPass->setCullingMode(Ogre::CULL_NONE);
92  CloudLayerPass->setLightingEnabled(false);
93  CloudLayerPass->setDepthWriteEnabled(false);
94 
95  CloudLayerPass->setVertexProgram("SkyX_Clouds_VP");
97  {
98  CloudLayerPass->setFragmentProgram("SkyX_Clouds_LDR_FP");
99  }
100  else
101  {
102  CloudLayerPass->setFragmentProgram("SkyX_Clouds_HDR_FP");
103  }
104 
105  // TODO
106  CloudLayerPass->createTextureUnitState("Clouds.png")->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP);
107  CloudLayerPass->createTextureUnitState("CloudsNormal.png")->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP);
108  CloudLayerPass->createTextureUnitState("CloudsTile.png")->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP);
109 
110  mCloudLayerPass = CloudLayerPass;
111 
114  }
115 
117  {
118  if (mCloudLayerPass)
119  {
120  mCloudLayerPass->getParent()->removePass(mCloudLayerPass->getIndex());
121  mCloudLayerPass = static_cast<Ogre::Pass*>(NULL);
122  }
123  }
124 
126  {
127  if (!mCloudLayerPass)
128  {
129  return;
130  }
131 
132  mCloudLayerPass->getFragmentProgramParameters()
133  ->setNamedConstant("uScale", mOptions.Scale);
134  mCloudLayerPass->getFragmentProgramParameters()
135  ->setNamedConstant("uHeight", mOptions.Height);
136 
137  float WindDirection_[2] = {mOptions.WindDirection.x, mOptions.WindDirection.y};
138 
139  mCloudLayerPass->getFragmentProgramParameters()
140  ->setNamedConstant("uWindDirection", WindDirection_, 1, 2);
141 
142  mCloudLayerPass->getFragmentProgramParameters()
143  ->setNamedConstant("uCloudLayerHeightVolume", mOptions.HeightVolume);
144  mCloudLayerPass->getFragmentProgramParameters()
145  ->setNamedConstant("uCloudLayerVolumetricDisplacement", mOptions.VolumetricDisplacement);
146  mCloudLayerPass->getFragmentProgramParameters()
147  ->setNamedConstant("uDetailAttenuation", mOptions.DetailAttenuation);
148  mCloudLayerPass->getFragmentProgramParameters()
149  ->setNamedConstant("uDistanceAttenuation", mOptions.DistanceAttenuation);
150  }
151 
153  {
154  if (!mCloudLayerPass)
155  {
156  return;
157  }
158 
159  mCloudLayerPass->getFragmentProgramParameters()
160  ->setNamedConstant("uExposure", mSkyX->getAtmosphereManager()->getOptions().Exposure);
161  mCloudLayerPass->getFragmentProgramParameters()
162  ->setNamedConstant("uTime", mSkyX->_getTimeOffset()*mOptions.TimeMultiplier);
163  /*
164  mCloudLayerPass->getFragmentProgramParameters()
165  ->setNamedConstant("uSunPosition", -mSkyX->getAtmosphereManager()->getSunDirection()*mSkyX->getMeshManager()->getSkydomeRadius());
166 
167  Ogre::Vector3 AmbientColor = Ogre::Vector3::ZERO;
168 
169  Ogre::Vector3 SunDir = -mSkyX->getAtmosphereManager()->getSunDirection();
170  Ogre::Real Ang = 0;
171  for (int k = 0; k < 3; k++)
172  {
173  Ogre::Vector2 Coords = Ogre::Vector2(Ogre::Math::Cos(Ang), Ogre::Math::Sin(Ang));
174  Ang += 2*Ogre::Math::PI/3;
175  AmbientColor += mSkyX->getAtmosphereManager()->getColorAt(Ogre::Vector3(Coords.x, mOptions.Height/mSkyX->getMeshManager()->getSkydomeRadius(), Coords.y).normalisedCopy());
176  }
177 
178  AmbientColor /= 3;
179 
180  mCloudLayerPass->getFragmentProgramParameters()
181  ->setNamedConstant("uAmbientLuminosity", AmbientColor*0.75f);
182 
183  float Mult = 1.5f;
184 
185  mCloudLayerPass->getFragmentProgramParameters()
186  ->setNamedConstant("uSunColor", Ogre::Vector3(
187  Ogre::Math::Clamp<Ogre::Real>(AmbientColor.x*Mult, 0, 1),
188  Ogre::Math::Clamp<Ogre::Real>(AmbientColor.y*Mult, 0, 1),
189  Ogre::Math::Clamp<Ogre::Real>(AmbientColor.z*Mult, 0, 1)));
190  */
191 
192  //Ogre::Vector3 SunDir = mSkyX->getAtmosphereManager()->getSunDirection();
193  //if (SunDir.y > 0.15f)
194  //{
195  // SunDir = -SunDir;
196  //}
197 
198  // mCloudLayerPass->getFragmentProgramParameters()
199  // ->setNamedConstant("uSunPosition", -SunDir*mSkyX->getMeshManager()->getSkydomeRadius());
200 
201  float point = (mSkyX->getController()->getSunDirection().y + 1.0f) / 2.0f;
202 
203  mCloudLayerPass->getFragmentProgramParameters()
204  ->setNamedConstant("uSunColor", mSunGradient.getColor(point));
205  mCloudLayerPass->getFragmentProgramParameters()
206  ->setNamedConstant("uAmbientLuminosity", mAmbientGradient.getColor(point));
207  }
208 
211  : mSkyX(s)
212  {
213  }
214 
216  {
217  removeAll();
218  }
219 
221  {
222  CloudLayer *NewCloudLayer = new CloudLayer(mSkyX, o);
223 
224  // TODO
225  NewCloudLayer->_registerCloudLayer(static_cast<Ogre::MaterialPtr>(
226  Ogre::MaterialManager::getSingleton().getByName(mSkyX->getGPUManager()->getSkydomeMaterialName()))
227  ->getTechnique(0)->createPass());
228 
229  mCloudLayers.push_back(NewCloudLayer);
230 
231  bool changeOrder = false;
232 
233  // Short layers by height
234  for (unsigned int k = 0; k < mCloudLayers.size(); k++)
235  {
236  if (k+1 < mCloudLayers.size())
237  {
238  if (mCloudLayers.at(k)->getOptions().Height < mCloudLayers.at(k+1)->getOptions().Height)
239  {
240  // Swap
241  CloudLayer* cl = mCloudLayers.at(k);
242  mCloudLayers.at(k) = mCloudLayers.at(k+1);
243  mCloudLayers.at(k+1) = cl;
244 
245  changeOrder = true;
246  k = 0;
247  }
248  }
249  }
250 
251  if (changeOrder)
252  {
253  unregisterAll();
254  registerAll();
255  }
256 
257  return NewCloudLayer;
258  }
259 
261  {
263  {
264  if((*CloudLayersIt) == cl)
265  {
266  delete (*CloudLayersIt);
268  return;
269  }
270  }
271  }
272 
274  {
276  {
277  delete (*CloudLayersIt);
278  }
279 
280  mCloudLayers.clear();
281  }
282 
284  {
285  for(unsigned int k = 0; k < mCloudLayers.size(); k++)
286  {
287  mCloudLayers.at(k)->_registerCloudLayer(static_cast<Ogre::MaterialPtr>(
288  Ogre::MaterialManager::getSingleton().getByName(mSkyX->getGPUManager()->getSkydomeMaterialName()))
289  ->getTechnique(0)->createPass());
290  }
291  }
292 
294  {
296  {
297  if((*CloudLayersIt) == cl)
298  {
299  (*CloudLayersIt)->_unregister();
300  }
301  }
302  }
303 
305  {
307  {
308  (*CloudLayersIt)->_unregister();
309  }
310  }
311 
313  {
315  {
316  (*CloudLayersIt)->_updateInternalPassParameters();
317  }
318  }
319 }
SkyX::ColorGradient
Definition: ColorGradient.h:31
SkyX::CloudLayer
Definition: CloudsManager.h:35
SkyX::CloudLayer::Options::Height
Ogre::Real Height
Cloud layer height.
Definition: CloudsManager.h:43
SkyX::AtmosphereManager::getOptions
const Options & getOptions() const
Get current options.
Definition: AtmosphereManager.h:139
SkyX::CloudsManager::registerAll
void registerAll()
Register all.
Definition: CloudsManager.cpp:283
SkyX::GPUManager::getSkydomeMaterialName
const Ogre::String getSkydomeMaterialName() const
Get skydome material name.
Definition: GPUManager.cpp:341
SkyX::ColorGradient::getColor
const Ogre::Vector3 getColor(const Ogre::Real &p) const
Get color value.
Definition: ColorGradient.cpp:37
SkyX::SkyX::getLightingMode
const LightingMode & getLightingMode() const
Get lighting mode.
Definition: SkyX.h:275
SkyX::SkyX::getController
Controller * getController() const
Get current controller.
Definition: SkyX.h:244
SkyX::CloudLayer::mSunGradient
ColorGradient mSunGradient
Definition: CloudsManager.h:215
SkyX::CloudLayer::Options::DistanceAttenuation
Ogre::Real DistanceAttenuation
Distance attenuation.
Definition: CloudsManager.h:52
SkyX::CloudLayer::Options
Cloud layer options.
Definition: CloudsManager.h:40
SkyX::SkyX::LM_LDR
@ LM_LDR
Low dynamic range.
Definition: SkyX.h:99
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::CloudLayer::CloudLayer
CloudLayer(SkyX *s)
Default onstructor.
Definition: CloudsManager.cpp:31
SkyX::CloudLayer::Options::VolumetricDisplacement
Ogre::Real VolumetricDisplacement
Volumetric displacement(For volumetric effects on the gpu)
Definition: CloudsManager.h:58
SkyX::CloudLayer::Options::Scale
Ogre::Real Scale
Cloud layer scale.
Definition: CloudsManager.h:45
SkyX::CloudsManager::mSkyX
SkyX * mSkyX
SkyX parent pointer.
Definition: CloudsManager.h:282
SkyX::SkyX::_getTimeOffset
const Ogre::Real & _getTimeOffset() const
Get time offset.
Definition: SkyX.h:342
SkyX::CloudLayer::Options::TimeMultiplier
Ogre::Real TimeMultiplier
Time multiplier.
Definition: CloudsManager.h:49
SkyX::CloudLayer::~CloudLayer
~CloudLayer()
Destructor.
Definition: CloudsManager.cpp:81
SkyX.h
SkyX::SkyX::getGPUManager
GPUManager * getGPUManager()
Get GPU manager.
Definition: SkyX.h:199
SkyX::CloudsManager::remove
void remove(CloudLayer *cl)
Remove the specified cloud layer.
Definition: CloudsManager.cpp:260
CloudsManager.h
SkyX::CloudLayer::_unregister
void _unregister()
Unregister cloud pass.
Definition: CloudsManager.cpp:116
SkyX::CloudLayer::mCloudLayerPass
Ogre::Pass * mCloudLayerPass
Cloud layer pass.
Definition: CloudsManager.h:218
SkyX::SkyX::getAtmosphereManager
AtmosphereManager * getAtmosphereManager()
Get atmosphere manager.
Definition: SkyX.h:191
SkyX::CloudsManager::~CloudsManager
~CloudsManager()
Destructor.
Definition: CloudsManager.cpp:215
SkyX::CloudsManager::removeAll
void removeAll()
Remove all cloud layers.
Definition: CloudsManager.cpp:273
SkyX::CloudsManager::CloudLayersIt
std::vector< CloudLayer * >::iterator CloudLayersIt
Cloud layers iterator.
Definition: CloudsManager.h:279
SkyX::CloudLayer::Options::DetailAttenuation
Ogre::Real DetailAttenuation
Detail attenuation.
Definition: CloudsManager.h:54
SkyX::CloudsManager::add
CloudLayer * add(const CloudLayer::Options &o)
Add a cloud layer.
Definition: CloudsManager.cpp:220
SkyX::ColorGradient::addCFrame
void addCFrame(const ColorFrame &CFrame)
Add color frame.
Definition: ColorGradient.h:51
SkyX::CloudsManager::mCloudLayers
std::vector< CloudLayer * > mCloudLayers
Cloud layers std::vector.
Definition: CloudsManager.h:277
SkyX::CloudLayer::mOptions
Options mOptions
Cloud layer options.
Definition: CloudsManager.h:211
SkyX::CloudLayer::Options::WindDirection
Ogre::Vector2 WindDirection
Wind direction.
Definition: CloudsManager.h:47
SkyX::CloudsManager::unregisterAll
void unregisterAll()
Unregister all cloud layers.
Definition: CloudsManager.cpp:304
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
SkyX::CloudsManager::CloudsManager
CloudsManager(SkyX *h)
Constructor.
Definition: CloudsManager.cpp:210
SkyX::CloudsManager::unregister
void unregister(CloudLayer *cl)
Unregister cloud layer.
Definition: CloudsManager.cpp:293
SkyX::CloudsManager::update
void update()
Update cloud layers.
Definition: CloudsManager.cpp:312
SkyX::CloudLayer::mAmbientGradient
ColorGradient mAmbientGradient
Ambient and Sun color gradients.
Definition: CloudsManager.h:214
SkyX::CloudLayer::mSkyX
SkyX * mSkyX
SkyX parent pointer.
Definition: CloudsManager.h:221
SkyX::CloudLayer::_registerCloudLayer
void _registerCloudLayer(Ogre::Pass *CloudLayerPass)
Register layer.
Definition: CloudsManager.cpp:86
SkyX::CloudLayer::Options::HeightVolume
Ogre::Real HeightVolume
Cloud layer height volume(For volumetric effects on the gpu)
Definition: CloudsManager.h:56
SkyX::Controller::getSunDirection
virtual Ogre::Vector3 getSunDirection()=0
Get sun direction.
SkyX::CloudLayer::_updateInternalPassParameters
void _updateInternalPassParameters()
Update internal cloud pass parameters.
Definition: CloudsManager.cpp:152
SkyX::CloudLayer::_updatePassParameters
void _updatePassParameters()
Update cloud pass parameters.
Definition: CloudsManager.cpp:125