Project

General

Profile

SO3Engine
SO3Environment.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
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
40
41namespace SO3
42{
43
44SEnvironment::SEnvironment(SScene* parent, const std::string& environmentName) : SData(environmentName)
45{
46 // Init ptr
47 scene = parent;
48 water = 0;
49 sky = 0;
50 sun = 0;
51 moon = 0;
52 universalClock = 0;
53
54 // Init vars
55 paused = false;
56 enabled = false;
57 skyEnabled = false;
58 waterEnabled = false;
59
60 // Init position
61 observerLatitude = 45.0f;
62 observerLongitude = 0.0f;
63
64 // Init wind & humidity
65 windSpeed = 1.0f;
66 windDirection = Ogre::Vector2::UNIT_X;
67 humidityFactor = 25.0f;
68
69 // Time/date calculation
70 universalClock = new SUniversalClock();
71 universalClock->SetGregorianDateTime(2000, 1, 1, 12, 0, 0); // Mid-day
72 // universalClock->SetGregorianDateTime(2000, 1, 1, 0, 0, 0); // Following tests are for E0 N45 coordinates.
73 // universalClock->SetGregorianDateTime(2000, 1, 1, 7, 38, 0); // Testing sunrise
74 // universalClock->SetGregorianDateTime(2000, 1, 1, 12, 4, 0); // Testing sun transit
75 // universalClock->SetGregorianDateTime(2000, 1, 1, 16, 29, 0); // Testing sunset
76 // universalClock->SetGregorianDateTime(2000, 1, 1, 3, 0, 0); // Testing moonrise (approx)
77 // universalClock->SetGregorianDateTime(2000, 1, 1, 0, 16, 0); // Testing moon transit
78 // universalClock->SetGregorianDateTime(2000, 1, 1, 8, 13, 0); // Testing moonset
79 // universalClock->SetGregorianDateTime(2000, 1, 1, 2, 10, 0);
80
81 // Sun/moon position & lighting
82 sun = new SSun(this, "SO3/Internal/Environment/"+ name +"/Sun");
83 moon = new SMoon(this, "SO3/Internal/Environment/"+ name +"/Moon");
84
85 sun->UpdateShadowFarDistance(scene->GetOgreScenePointer()->getShadowFarDistance());
86 sun->UpdateShadowFarClipDistance(scene->GetOgreScenePointer()->getShadowDirectionalLightExtrusionDistance());
87
88 moon->UpdateShadowFarDistance(scene->GetOgreScenePointer()->getShadowFarDistance());
89 moon->UpdateShadowFarClipDistance(scene->GetOgreScenePointer()->getShadowDirectionalLightExtrusionDistance());
90
91 // Sky object
92 sky = new SSky(this, "SO3/Internal/Environment/"+ name +"/Sky");
93
94 // Water object
95 water = new SWater(this, "SO3/Internal/Environment/"+ name +"/Water");
96
97 // Add frame listener
98 Ogre::Root::getSingleton().addFrameListener(this);
99
100 //SetTimeSpeedFactor(3600); // 1 sec = 1 hour
101 //SetTimeSpeedFactor(60); // 1 sec = 1 minute
102 SetTimeSpeedFactor(1); // realtime
103}
104
105SEnvironment::SEnvironment() : SData("")
106{
107 // Forbiden (private)
108}
109
111{
112 // Removing frame listener
113 Ogre::Root::getSingleton().removeFrameListener(this);
114
116 SO3_SAFE_DELETE(water);
117 SO3_SAFE_DELETE(sky);
118 SO3_SAFE_DELETE(moon);
119 SO3_SAFE_DELETE(sun);
120 SO3_SAFE_DELETE(universalClock);
121}
122
124{
125 return scene;
126}
127
129{
130 ViewportList::iterator iSearchedViewport = viewportList.find(targetViewport);
131 if(iSearchedViewport == viewportList.end())
132 {
133 // Add to the viewports list
134 viewportList.insert(targetViewport);
135
136 // Register viewport in sub-components.
137 if(sky)
138 sky->RegisterViewport(targetViewport);
139 if(water)
140 water->RegisterViewport(targetViewport);
141
142 // Check if the viewport's parent render target is allready registered.
143 Ogre::RenderTarget* parentRenderTarger = targetViewport->GetOgreViewPortPointer()->getTarget();
144 RenderTargetList::iterator searchedRenderTarget = mRenderTargetList.find(parentRenderTarger);
145 if(searchedRenderTarget == mRenderTargetList.end())
146 {
147 // Not found, register SEnvironment as a listener on it, and add it to the list.
148 parentRenderTarger->addListener(this);
149 mRenderTargetList.insert(RenderTargetList::value_type(parentRenderTarger, 1));
150 }
151 else
152 {
153 // Found, increase the reference count.
154 mRenderTargetList[parentRenderTarger] += 1;
155 }
156 }
157 else
158 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "The viewport already exist in the list of associated viewports!", "SEnvironment::RegisterViewport");
159}
160
162{
163 ViewportList::iterator iSearchedViewport = viewportList.find(targetViewport);
164 if(iSearchedViewport != viewportList.end())
165 {
166 // Check if we need to unregister SkyX from the viewport's parent rendertarget.
167 Ogre::RenderTarget* parentRenderTarger = targetViewport->GetOgreViewPortPointer()->getTarget();
168 mRenderTargetList[parentRenderTarger] -= 1;
169 if(mRenderTargetList[parentRenderTarger] == 0)
170 {
171 parentRenderTarger->removeListener(this);
172 mRenderTargetList.erase(parentRenderTarger);
173 }
174
175 // Unregister viewport from sub-components.
176 if(water)
177 water->UnregisterViewport(targetViewport);
178 if(sky)
179 sky->UnregisterViewport(targetViewport);
180
181 // Remove from the viewport list
182 viewportList.erase(targetViewport);
183 }
184
185 //$BB do not send an exception, now all viewports are unregistered by scene
186 //else
187 // OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Cannot find the viewport in the list of associated viewports!", "SEnvironment::UnregisterViewport");
188}
189
190void SEnvironment::UnregisterCamera(Ogre::Camera* cam)
191{
192 // Unregister viewport from sub-components.
193 if(water)
194 water->UnregisterCamera(cam);
195 if(sky)
196 sky->UnregisterCamera(cam);
197}
198
200{
201 ViewportList::iterator iSearchedViewport = viewportList.find(targetViewport);
202 if(iSearchedViewport != viewportList.end())
203 return true;
204 else
205 return false;
206}
207
208bool SEnvironment::IsRegisteredViewport(Ogre::Viewport* targetViewport)
209{
210 ViewportList::iterator iViewportList = viewportList.begin();
211 while(iViewportList != viewportList.end())
212 {
213 if ((*iViewportList)->GetOgreViewPortPointer() == targetViewport)
214 return true;
215
216 iViewportList++;
217 }
218 return false;
219}
220
222{
223 ViewportList viewportListCopy = viewportList;
224 ViewportList::iterator iViewportList = viewportListCopy.begin();
225 while(iViewportList != viewportListCopy.end())
226 {
227 UnregisterViewport(*iViewportList);
228 iViewportList++;
229 }
230}
231
236
238{
239 return universalClock;
240}
241
243{
244 return water;
245}
246
248{
249 return sky;
250}
251
253{
254 return sun;
255}
256
258{
259 return moon;
260}
261
263{
264 return enabled;
265}
266
267void SEnvironment::SetEnable(const bool& enableEnvironment)
268{
269 enabled = enableEnvironment;
270 if(enabled)
271 {
272 if(water)
273 water->SetEnable(waterEnabled);
274 if(sky)
275 sky->SetEnable(skyEnabled);
276 if(sun)
277 sun->SetEnable(true);
278 if(moon)
279 moon->SetEnable(true);
280 }
281 else
282 {
283 if(water)
284 water->SetEnable(false);
285 if(sky)
286 sky->SetEnable(false);
287 if(sun)
288 sun->SetEnable(false);
289 if(moon)
290 moon->SetEnable(false);
291 }
292}
293
295{
296 return waterEnabled;
297}
298
299void SEnvironment::SetWaterEnable(const bool& enableWater)
300{
301 waterEnabled = enableWater;
302 if(enabled)
303 if(water)
304 water->SetEnable(waterEnabled);
305}
306
308{
309 return skyEnabled;
310}
311
312void SEnvironment::SetSkyEnable(const bool& enableSky)
313{
314 skyEnabled = enableSky;
315 if(enabled)
316 if(sky)
317 sky->SetEnable(skyEnabled);
318}
319
321{
322 return windSpeed;
323}
324
325void SEnvironment::SetWindSpeed(const float& newWindSpeed)
326{
327 windSpeed = newWindSpeed;
328}
329
331{
332 return windDirection;
333}
334
335void SEnvironment::SetWindDirection(const Ogre::Vector2& newWindDirection)
336{
337 windDirection = newWindDirection.normalisedCopy();
338}
339
341{
342 return humidityFactor;
343}
344
345void SEnvironment::SetHumidity(const float& newHumidity)
346{
347 // Check values
348 humidityFactor = newHumidity;
349 if(newHumidity < 0.0f)
350 humidityFactor = 0.0f;
351 if(newHumidity > 100.0f)
352 humidityFactor = 100.0f;
353}
354
356{
357 if(universalClock)
358 return universalClock->GetTimeScale();
359 else
360 return 0.0f;
361}
362
363void SEnvironment::SetTimeSpeedFactor(const float& newTimeSpeedFactor)
364{
365 if(universalClock)
366 universalClock->SetTimeScale(newTimeSpeedFactor);
367}
368
370{
371 return paused;
372}
373
374void SEnvironment::SetPaused(const bool& pauseSimulation)
375{
376 paused = pauseSimulation;
377}
378
380{
381 SDateTime tempDateTime;
382 double second = 0.0;
383
384 if(universalClock)
385 universalClock->GetGregorianDateTime(tempDateTime.year, tempDateTime.month, tempDateTime.day, tempDateTime.hour, tempDateTime.minute, second);
386
387 tempDateTime.second = static_cast<int> (second);
388 return tempDateTime;
389}
390
392{
393 double second = static_cast<double> (dateTime.second);
394 if(universalClock)
395 universalClock->SetGregorianDateTime(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute, second);
396}
397
399{
400 return observerLongitude;
401}
402
403void SEnvironment::SetLongitude(const Ogre::Degree& newLongitude)
404{
405 observerLongitude = newLongitude;
406}
407
409{
410 return observerLatitude;
411}
412
413void SEnvironment::SetLatitude(const Ogre::Degree& newLatitude)
414{
415 observerLatitude = newLatitude;
416}
417
418bool SEnvironment::frameStarted(const Ogre::FrameEvent& evt)
419{
420 if(!paused)
421 {
422 Ogre::Real updateTime = 0.0f;
423 if(universalClock)
424 if(universalClock->GetTimeScale() != 0.0f)
425 universalClock->Update(evt.timeSinceLastFrame);
426 }
427
428 // Update sun
429 if(sun)
430 sun->Update();
431
432 // Update moon
433 if(moon)
434 moon->Update();
435
436 // Update water
437 if(water)
438 water->Update(evt.timeSinceLastFrame);
439
440 // Update sky
441 if(sky)
442 sky->Update(evt.timeSinceLastFrame);
443
444 return true;
445}
446
448{
449 // Check if the viewport's parent render target is allready registered.
450 Ogre::RenderTarget* parentRenderTarger = targetViewport->getTarget();
451 RenderTargetList::iterator searchedRenderTarget = mRenderTargetList.find(parentRenderTarger);
452 if(searchedRenderTarget != mRenderTargetList.end())
453 {
454 // Found, drop from the list and then push back.
455 parentRenderTarger->removeListener(this);
456 parentRenderTarger->addListener(this);
457 }
458}
459
461{
462 // Check if the viewport's parent render target is allready registered.
463 Ogre::RenderTarget* parentRenderTarger = targetViewport->getTarget();
464 RenderTargetList::iterator searchedRenderTarget = mRenderTargetList.find(parentRenderTarger);
465 if(searchedRenderTarget != mRenderTargetList.end())
466 {
467 // Found, drop from the list
468 parentRenderTarger->removeListener(this);
469 }
470}
471
473{
474 if(water && water->GetEnable())
475 {
476 bool hasStereo = viewport->IsStereoViewportRegistered();
477 if (hasStereo)
478 viewport->UnregisterStereoViewport();
479
480 water->UpdateCamera(viewport);
481
482 if (hasStereo)
483 viewport->RegisterStereoViewport();
484 }
485
486 if(sky)
487 sky->UpdateCamera(viewport);
488}
489}
std::string name
Definition SO3DataScol.h:44
void RegisterViewport(SViewPort *viewport)
void UnregisterCamera(Ogre::Camera *cam)
void UnregisterViewport(SViewPort *viewport)
void SetEnable(const bool &enableComponent)
ViewportList GetRegisteredViewports()
SUniversalClock * GetUniversalClock()
Ogre::Degree GetLatitude()
virtual bool frameStarted(const Ogre::FrameEvent &evt)
void SetEnable(const bool &enableEnvironment)
void UnregisterViewport(SViewPort *targetViewport)
void _NotifyEnvironmentComponentUnRegisterRenderTarget(Ogre::Viewport *targetViewport)
void SetPaused(const bool &pauseSimulation)
Ogre::Degree GetLongitude()
std::set< SViewPort * > ViewportList
void SetWaterEnable(const bool &enableWater)
void RegisterViewport(SViewPort *targetViewport)
void SetWindSpeed(const float &newWindSpeed)
void SetSkyEnable(const bool &enableSky)
void UnregisterCamera(Ogre::Camera *cam)
void _NotifyEnvironmentComponentRegisterRenderTarget(Ogre::Viewport *targetViewport)
void SetDateTime(const SDateTime &newDateTime)
Ogre::Vector2 GetWindDirection()
void SetHumidity(const float &newHumidity)
void SetTimeSpeedFactor(const float &newTimeSpeedFactor)
void UpdateCamera(SViewPort *viewport)
bool IsRegisteredViewport(SViewPort *targetViewport)
void SetLongitude(const Ogre::Degree &newLongitude)
void SetWindDirection(const Ogre::Vector2 &newWindDirection)
void SetLatitude(const Ogre::Degree &newLatitude)
Ogre::SceneManager * GetOgreScenePointer()
Definition SO3Scene.cpp:449
virtual void Update(const Ogre::Real &updateTime)
Definition SO3Sky.cpp:239
virtual void UpdateCamera(SViewPort *viewport)
Definition SO3Sky.cpp:235
void UpdateShadowFarDistance(const float &distance)
void UpdateShadowFarClipDistance(const float &distance)
void SetEnable(const bool &enable)
void SetTimeScale(const float &scale)
void GetGregorianDateTime(int &year, int &month, int &day, int &hour, int &minute, double &second)
void Update(const float &time)
void SetGregorianDateTime(const int &year, const int &month, const int &day, const int &hour, const int &minute, const double &second)
void RegisterStereoViewport()
void UnregisterStereoViewport()
bool IsStereoViewportRegistered()
Ogre::Viewport * GetOgreViewPortPointer()
virtual void UpdateCamera(SViewPort *viewport)
virtual void Update(const Ogre::Real &updateTime)