~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/environment/CaelumEnvironment.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: CaelumEnvironment
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2006
 
8
//
 
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.
 
13
// 
 
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.
 
18
// 
 
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.//
 
22
//
 
23
#ifdef HAVE_CONFIG_H
 
24
#include "config.h"
 
25
#endif
 
26
 
 
27
#include "CaelumEnvironment.h"
 
28
#include "caelum/include/Caelum.h"
 
29
#include "CaelumSky.h"
 
30
#include "CaelumSun.h"
 
31
#include "Water.h"
 
32
#include "SimpleWater.h"
 
33
#include "HydraxWater.h"
 
34
#include "framework/Tokeniser.h"
 
35
//#include "caelum/include/CaelumSystem.h"
 
36
 
 
37
#include "services/time/TimeService.h"
 
38
 
 
39
 
 
40
namespace EmberOgre {
 
41
 
 
42
namespace Environment {
 
43
 
 
44
 
 
45
 
 
46
CaelumEnvironment::CaelumEnvironment(Ogre::SceneManager *sceneMgr, Ogre::RenderWindow* window, Ogre::Camera& camera)
 
47
 
48
 SetCaelumTime("set_caelumtime",this, "Sets the caelum time. parameters: <hour> <minute>")
 
49
, mCaelumSystem(0)
 
50
, mSceneMgr(sceneMgr)
 
51
, mWindow(window)
 
52
, mCamera(camera)
 
53
, mSky(0)
 
54
, mSun(0)
 
55
, mWater(0)
 
56
 
 
57
//,mLensFlare(camera, sceneMgr)
 
58
{
 
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());
 
64
        }
 
65
>getNode());
 
66
                mLensFlare.initialize();*/
 
67
//              mLensFlare.setVisible(false);
 
68
                //Ogre::::Root::getSingleton().addFrameListener(this);
 
69
}
 
70
 
 
71
CaelumEnvironment::~CaelumEnvironment()
 
72
{
 
73
        delete mSky;
 
74
        delete mSun;
 
75
        delete mWater;
 
76
        mWindow->removeListener(mCaelumSystem);
 
77
        if (mCaelumSystem) {
 
78
                mCaelumSystem->shutdown(true);
 
79
//              delete mCaelumSystem; //calling shutdown() will delete the instance, so no need to do it again
 
80
        }
 
81
}
 
82
 
 
83
void CaelumEnvironment::createEnvironment()
 
84
{
 
85
        try {
 
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());
 
89
                throw;
 
90
        }
 
91
        setupWater();
 
92
        
 
93
}
 
94
 
 
95
void CaelumEnvironment::setupWater()
 
96
{
 
97
 
 
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();
 
104
        } else {
 
105
                delete mWater;
 
106
                mWater = new SimpleWater(mCamera, *mSceneMgr);
 
107
                mWater->initialize();
 
108
        }
 
109
 
 
110
 
 
111
}
 
112
 
 
113
 
 
114
void CaelumEnvironment::setupCaelum(::Ogre::Root *root, ::Ogre::SceneManager *sceneMgr, ::Ogre::RenderWindow* window, ::Ogre::Camera& camera)
 
115
{
 
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 |
 
128
                        0);
 
129
 
 
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
 
139
                        0);
 
140
 
 
141
        try {
 
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());
 
145
 
 
146
                sceneMgr->getRootSceneNode()->removeAndDestroyChild("CaelumRoot");
 
147
                mCaelumSystem = new caelum::CaelumSystem (root, sceneMgr, componentMaskFallback, false);
 
148
        }
 
149
 
 
150
 
 
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));
 
155
 
 
156
        ///Get the sky dome for  Create a sky dome CaelumSky
 
157
        mDome = mCaelumSystem->getSkyDome();
 
158
        
 
159
        /// Set up some star field options
 
160
//      mCaelumSystem->getPointStarfield ()->setInclination (::Ogre::Degree (13));
 
161
 
 
162
        /// Setup sun options
 
163
        if (mCaelumSystem->getSun ()) {
 
164
                mSun = new CaelumSun(*this, mCaelumSystem->getSun());
 
165
        } else {
 
166
                ///use a simple sun object
 
167
        }
 
168
        
 
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).
 
171
 
 
172
        mSky = new CaelumSky(*this);
 
173
 
 
174
 
 
175
        /// Register all to the render window
 
176
        window->addListener (mCaelumSystem);
 
177
        
 
178
        
 
179
        /// Set time acceleration to fit with real world time
 
180
        mCaelumSystem->getUniversalClock ()->setTimeScale (1);
 
181
 
 
182
        int year, month, day, hour, minute, second;
 
183
        bool usingServerTime = Ember::EmberServices::getSingleton().getTimeService()->getServerTime(year, month, day, hour, minute, second);
 
184
        
 
185
        if (!usingServerTime) {
 
186
                S_LOG_WARNING("Could not get server time, using local time for environment.");
 
187
        }
 
188
        
 
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
 
190
        /*
 
191
        if (hour < 6) {
 
192
                hour = 6;
 
193
        } else if (hour > 16) {
 
194
                hour = 15;
 
195
        }
 
196
        */
 
197
        
 
198
        
 
199
        mCaelumSystem->getUniversalClock ()->setGregorianDateTime (year, month, day, hour, minute, second);
 
200
        
 
201
        mCaelumSystem->getUniversalClock()->setUpdateRate( 1 / (24 * 60)); //update every minute
 
202
        
 
203
        
 
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.
 
205
        Ogre::FrameEvent ev;
 
206
        ev.timeSinceLastEvent = 1;
 
207
        ev.timeSinceLastFrame = 1;
 
208
        mCaelumSystem->updateSubcomponents(1000);
 
209
        
 
210
        Ogre::Root::getSingleton().addFrameListener(mCaelumSystem);
 
211
}
 
212
        
 
213
ISun* CaelumEnvironment::getSun()
 
214
{
 
215
        return mSun;
 
216
}
 
217
 
 
218
ISky* CaelumEnvironment::getSky()
 
219
{
 
220
        return mSky;
 
221
}
 
222
 
 
223
IFog* CaelumEnvironment::getFog()
 
224
{
 
225
        return mSky;
 
226
        //return mFog;
 
227
}
 
228
 
 
229
IWater* CaelumEnvironment::getWater()
 
230
{
 
231
        return mWater;
 
232
}
 
233
 
 
234
void CaelumEnvironment::setTime(int hour, int minute, int second)
 
235
{
 
236
        int year, month, day, _hour, _minute, _second;
 
237
        Ember::EmberServices::getSingleton().getTimeService()->getServerTime(year, month, day, _hour, _minute, _second);
 
238
        
 
239
        mCaelumSystem->getUniversalClock ()->setGregorianDateTime(year, month, day, hour, minute, second);
 
240
}
 
241
 
 
242
void CaelumEnvironment::setTime(int seconds)
 
243
{
 
244
        int year, month, day, _hour, _minute, _second;
 
245
        Ember::EmberServices::getSingleton().getTimeService()->getServerTime(year, month, day, _hour, _minute, _second);
 
246
        
 
247
        mCaelumSystem->getUniversalClock ()->setGregorianDateTime(year, month, day, 0, 0, seconds);
 
248
}
 
249
 
 
250
 
 
251
void CaelumEnvironment::setWorldPosition(float longitudeDegrees, float latitudeDegrees)
 
252
{
 
253
        if (mCaelumSystem) {
 
254
                mCaelumSystem->getSolarSystemModel ()->setObserverLatitude (Ogre::Degree(latitudeDegrees));
 
255
                mCaelumSystem->getSolarSystemModel ()->setObserverLongitude(Ogre::Degree(longitudeDegrees));
 
256
        }
 
257
}
 
258
 
 
259
void CaelumEnvironment::runCommand(const std::string &command, const std::string &args)
 
260
{
 
261
        if (SetCaelumTime == command) {
 
262
                Ember::Tokeniser tokeniser;
 
263
                tokeniser.initTokens(args);
 
264
                std::string hourString = tokeniser.nextToken();
 
265
                std::string minuteString = tokeniser.nextToken();
 
266
                
 
267
                int hour = ::Ogre::StringConverter::parseInt( hourString);
 
268
                int minute = ::Ogre::StringConverter::parseInt( minuteString);
 
269
                setTime(hour, minute);
 
270
        }
 
271
}
 
272
 
 
273
 
 
274
}
 
275
 
 
276
}