2
// C++ Implementation: CaelumEnvironment
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2006
9
// This program is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 2 of the License, or
12
// (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
27
#include "CaelumEnvironment.h"
28
#include "caelum/include/Caelum.h"
29
#include "CaelumSky.h"
30
#include "CaelumSun.h"
32
#include "SimpleWater.h"
33
#include "HydraxWater.h"
34
#include "framework/Tokeniser.h"
35
//#include "caelum/include/CaelumSystem.h"
37
#include "services/time/TimeService.h"
42
namespace Environment {
46
CaelumEnvironment::CaelumEnvironment(Ogre::SceneManager *sceneMgr, Ogre::RenderWindow* window, Ogre::Camera& camera)
48
SetCaelumTime("set_caelumtime",this, "Sets the caelum time. parameters: <hour> <minute>")
57
//,mLensFlare(camera, sceneMgr)
59
// sceneMgr->setAmbientLight(Ogre::ColourValue(0.3f, 0.3f, 0.3f));
60
sceneMgr->setAmbientLight(Ogre::ColourValue(0.6, 0.6, 0.6));
61
// setupCaelum(root, sceneMgr, window , camera);
62
/* mLensFlare.setNode(mCaelumSystem->getSun()- } catch (const Ogre::Exception& ex) {
63
S_LOG_FAILURE("Could not load caelum. Message: " << ex.getFullDescription());
66
mLensFlare.initialize();*/
67
// mLensFlare.setVisible(false);
68
//Ogre::::Root::getSingleton().addFrameListener(this);
71
CaelumEnvironment::~CaelumEnvironment()
76
mWindow->removeListener(mCaelumSystem);
78
mCaelumSystem->shutdown(true);
79
// delete mCaelumSystem; //calling shutdown() will delete the instance, so no need to do it again
83
void CaelumEnvironment::createEnvironment()
86
setupCaelum( Ogre::Root::getSingletonPtr(), mSceneMgr, mWindow, mCamera);
87
} catch (const Ogre::Exception& ex) {
88
S_LOG_FAILURE("Could not load caelum. Message: " << ex.getFullDescription());
95
void CaelumEnvironment::setupWater()
98
//mWater = new Water(mCamera, mSceneMgr);
99
// mWater = new HydraxWater(mCamera, *mSceneMgr);
100
///NOTE: we default to simple water for now since there are a couple of performance problems with hydrax
101
mWater = new SimpleWater(mCamera, *mSceneMgr);
102
if (mWater->isSupported()) {
103
mWater->initialize();
106
mWater = new SimpleWater(mCamera, *mSceneMgr);
107
mWater->initialize();
114
void CaelumEnvironment::setupCaelum(::Ogre::Root *root, ::Ogre::SceneManager *sceneMgr, ::Ogre::RenderWindow* window, ::Ogre::Camera& camera)
116
/// Pick components to use
117
///We'll skip the ground fog for now...
118
caelum::CaelumSystem::CaelumComponent componentMask =
119
static_cast<caelum::CaelumSystem::CaelumComponent> (
120
caelum::CaelumSystem::CAELUM_COMPONENT_SKY_COLOUR_MODEL |
121
caelum::CaelumSystem::CAELUM_COMPONENT_SUN |
122
caelum::CaelumSystem::CAELUM_COMPONENT_SOLAR_SYSTEM_MODEL |
123
caelum::CaelumSystem::CAELUM_COMPONENT_SKY_DOME |
124
caelum::CaelumSystem::CAELUM_COMPONENT_POINT_STARFIELD |
125
caelum::CaelumSystem::CAELUM_COMPONENT_CLOUDS |
126
caelum::CaelumSystem::CAELUM_COMPONENT_MOON |
127
// caelum::CaelumSystem::CAELUM_COMPONENT_GROUND_FOG |
130
caelum::CaelumSystem::CaelumComponent componentMaskFallback =
131
static_cast<caelum::CaelumSystem::CaelumComponent> (
132
caelum::CaelumSystem::CAELUM_COMPONENT_SKY_COLOUR_MODEL |
133
caelum::CaelumSystem::CAELUM_COMPONENT_SUN |
134
caelum::CaelumSystem::CAELUM_COMPONENT_SOLAR_SYSTEM_MODEL |
135
caelum::CaelumSystem::CAELUM_COMPONENT_SKY_DOME |
136
caelum::CaelumSystem::CAELUM_COMPONENT_IMAGE_STARFIELD | // Point starfield require shaders
137
caelum::CaelumSystem::CAELUM_COMPONENT_CLOUDS |
138
// caelum::CaelumSystem::CAELUM_COMPONENT_MOON | // Moon would be ugly without shaders
142
mCaelumSystem = new caelum::CaelumSystem (root, sceneMgr, componentMask, false);
143
} catch (const Ogre::Exception& ex) {
144
S_LOG_FAILURE("Could not load main caelum technique, will try fallback. Message: " << ex.getFullDescription());
146
sceneMgr->getRootSceneNode()->removeAndDestroyChild("CaelumRoot");
147
mCaelumSystem = new caelum::CaelumSystem (root, sceneMgr, componentMaskFallback, false);
151
mCaelumSystem->setManageSceneFog (true);
152
mCaelumSystem->setManageAmbientLight(true);
153
mCaelumSystem->setGlobalFogDensityMultiplier (0.005);
154
mCaelumSystem->setMinimumAmbientLight(Ogre::ColourValue(0.2, 0.2, 0.2, 1.0));
156
///Get the sky dome for Create a sky dome CaelumSky
157
mDome = mCaelumSystem->getSkyDome();
159
/// Set up some star field options
160
// mCaelumSystem->getPointStarfield ()->setInclination (::Ogre::Degree (13));
162
/// Setup sun options
163
if (mCaelumSystem->getSun ()) {
164
mSun = new CaelumSun(*this, mCaelumSystem->getSun());
166
///use a simple sun object
169
mCaelumSystem->setEnsureSingleShadowSource(true); ///we want to use only one shadow caster source, for now at least
170
mCaelumSystem->setEnsureSingleLightSource(true); ///We want to only use the brightest light source only, even if another is closer. This is to make sure the main light is taken from the sun instead of the moon (which will result in a dark landscape).
172
mSky = new CaelumSky(*this);
175
/// Register all to the render window
176
window->addListener (mCaelumSystem);
179
/// Set time acceleration to fit with real world time
180
mCaelumSystem->getUniversalClock ()->setTimeScale (1);
182
int year, month, day, hour, minute, second;
183
bool usingServerTime = Ember::EmberServices::getSingleton().getTimeService()->getServerTime(year, month, day, hour, minute, second);
185
if (!usingServerTime) {
186
S_LOG_WARNING("Could not get server time, using local time for environment.");
189
///little hack here. We of course want to use the server time, but currently when you log in when it's dark, you won't see much, so in order to show the world in it's full glory we'll try to set the time to day time
193
} else if (hour > 16) {
199
mCaelumSystem->getUniversalClock ()->setGregorianDateTime (year, month, day, hour, minute, second);
201
mCaelumSystem->getUniversalClock()->setUpdateRate( 1 / (24 * 60)); //update every minute
204
///advance it one second to force it to do initial updating, since other subsystems such as the terrain rendering depends on the sun postions etc.
206
ev.timeSinceLastEvent = 1;
207
ev.timeSinceLastFrame = 1;
208
mCaelumSystem->updateSubcomponents(1000);
210
Ogre::Root::getSingleton().addFrameListener(mCaelumSystem);
213
ISun* CaelumEnvironment::getSun()
218
ISky* CaelumEnvironment::getSky()
223
IFog* CaelumEnvironment::getFog()
229
IWater* CaelumEnvironment::getWater()
234
void CaelumEnvironment::setTime(int hour, int minute, int second)
236
int year, month, day, _hour, _minute, _second;
237
Ember::EmberServices::getSingleton().getTimeService()->getServerTime(year, month, day, _hour, _minute, _second);
239
mCaelumSystem->getUniversalClock ()->setGregorianDateTime(year, month, day, hour, minute, second);
242
void CaelumEnvironment::setTime(int seconds)
244
int year, month, day, _hour, _minute, _second;
245
Ember::EmberServices::getSingleton().getTimeService()->getServerTime(year, month, day, _hour, _minute, _second);
247
mCaelumSystem->getUniversalClock ()->setGregorianDateTime(year, month, day, 0, 0, seconds);
251
void CaelumEnvironment::setWorldPosition(float longitudeDegrees, float latitudeDegrees)
254
mCaelumSystem->getSolarSystemModel ()->setObserverLatitude (Ogre::Degree(latitudeDegrees));
255
mCaelumSystem->getSolarSystemModel ()->setObserverLongitude(Ogre::Degree(longitudeDegrees));
259
void CaelumEnvironment::runCommand(const std::string &command, const std::string &args)
261
if (SetCaelumTime == command) {
262
Ember::Tokeniser tokeniser;
263
tokeniser.initTokens(args);
264
std::string hourString = tokeniser.nextToken();
265
std::string minuteString = tokeniser.nextToken();
267
int hour = ::Ogre::StringConverter::parseInt( hourString);
268
int minute = ::Ogre::StringConverter::parseInt( minuteString);
269
setTime(hour, minute);