Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
CloudsManager.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of SkyX.
4Visit http://www.paradise-studios.net/products/skyx/
5
6Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt.
21--------------------------------------------------------------------------------
22*/
23
24#include "CloudsManager.h"
25
26#include "SkyX.h"
27
28namespace 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
85
86 void CloudLayer::_registerCloudLayer(Ogre::Pass* CloudLayerPass)
87 {
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
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 {
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}
const Options & getOptions() const
Get current options.
ColorGradient mSunGradient
void _unregister()
Unregister cloud pass.
SkyX * mSkyX
SkyX parent pointer.
Options mOptions
Cloud layer options.
Ogre::Pass * mCloudLayerPass
Cloud layer pass.
~CloudLayer()
Destructor.
CloudLayer(SkyX *s)
Default onstructor.
void _updateInternalPassParameters()
Update internal cloud pass parameters.
ColorGradient mAmbientGradient
Ambient and Sun color gradients.
void _registerCloudLayer(Ogre::Pass *CloudLayerPass)
Register layer.
void _updatePassParameters()
Update cloud pass parameters.
~CloudsManager()
Destructor.
void unregister(CloudLayer *cl)
Unregister cloud layer.
void unregisterAll()
Unregister all cloud layers.
void update()
Update cloud layers.
CloudLayer * add(const CloudLayer::Options &o)
Add a cloud layer.
void registerAll()
Register all.
CloudsManager(SkyX *h)
Constructor.
std::vector< CloudLayer * >::iterator CloudLayersIt
Cloud layers iterator.
SkyX * mSkyX
SkyX parent pointer.
std::vector< CloudLayer * > mCloudLayers
Cloud layers std::vector.
void removeAll()
Remove all cloud layers.
void remove(CloudLayer *cl)
Remove the specified cloud layer.
const Ogre::Vector3 getColor(const Ogre::Real &p) const
Get color value.
std::pair< Ogre::Vector3, Ogre::Real > ColorFrame
Color frame type definition ColorFrame.first: Colour value ColorFrame.second: Position in the gradien...
void addCFrame(const ColorFrame &CFrame)
Add color frame.
virtual Ogre::Vector3 getSunDirection()=0
Get sun direction.
const Ogre::String getSkydomeMaterialName() const
Get skydome material name.
AtmosphereManager * getAtmosphereManager()
Get atmosphere manager.
Definition SkyX.h:191
@ LM_LDR
Low dynamic range.
Definition SkyX.h:99
const Ogre::Real & _getTimeOffset() const
Get time offset.
Definition SkyX.h:342
const LightingMode & getLightingMode() const
Get lighting mode.
Definition SkyX.h:275
GPUManager * getGPUManager()
Get GPU manager.
Definition SkyX.h:199
Controller * getController() const
Get current controller.
Definition SkyX.h:244
Cloud layer options.
Ogre::Vector2 WindDirection
Wind direction.
Ogre::Real Height
Cloud layer height.
Ogre::Real VolumetricDisplacement
Volumetric displacement(For volumetric effects on the gpu)
Ogre::Real DistanceAttenuation
Distance attenuation.
Ogre::Real DetailAttenuation
Detail attenuation.
Ogre::Real TimeMultiplier
Time multiplier.
Ogre::Real Scale
Cloud layer scale.
Ogre::Real HeightVolume
Cloud layer height volume(For volumetric effects on the gpu)