Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/GraphicsManager.cc @ 3329

Last change on this file since 3329 was 3327, checked in by rgrieder, 15 years ago

Merged all remaining revisions from core4 back to the trunk.

  • Property svn:eol-style set to native
File size: 14.7 KB
RevLine 
[612]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1293]3 *                    > www.orxonox.net <
[612]4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
[1349]11 *   of the License, or (at your option) any later version.
12 *
[612]13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[1535]23 *      Reto Grieder
[1755]24 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
[612]25 *   Co-authors:
[1755]26 *      Felix Schulthess
[612]27 *
28 */
[1035]29
[1755]30/**
31@file
32@brief
33    Implementation of an partial interface to Ogre.
34*/
[612]35
[2801]36#include "GraphicsManager.h"
[612]37
[2801]38#include <fstream>
39#include <boost/filesystem.hpp>
[3280]40#include <boost/shared_ptr.hpp>
[2801]41
42#include <OgreCompositorManager.h>
43#include <OgreConfigFile.h>
44#include <OgreFrameListener.h>
45#include <OgreRoot.h>
46#include <OgreLogManager.h>
47#include <OgreException.h>
[1755]48#include <OgreRenderWindow.h>
[2801]49#include <OgreRenderSystem.h>
50#include <OgreTextureManager.h>
51#include <OgreViewport.h>
52#include <OgreWindowEventUtilities.h>
[1538]53
[2801]54#include "SpecialConfig.h"
55#include "util/Exception.h"
[3280]56#include "util/StringUtils.h"
[2801]57#include "util/SubString.h"
[2842]58#include "core/Clock.h"
[2801]59#include "core/ConsoleCommand.h"
60#include "core/ConfigValueIncludes.h"
[1293]61#include "core/CoreIncludes.h"
[2801]62#include "core/Core.h"
[3091]63#include "core/Game.h"
[2848]64#include "core/GameMode.h"
[3327]65#include "core/WindowEventListener.h"
[1563]66#include "tools/ParticleInterface.h"
[1032]67
[3196]68// HACK!
69#include "overlays/map/Map.h"
70
[1625]71namespace orxonox
72{
[3280]73    using boost::shared_ptr;
74
[3327]75    class OgreWindowEventListener : public Ogre::WindowEventListener
[2801]76    {
[3327]77    public:
78        void windowResized     (Ogre::RenderWindow* rw)
79            { orxonox::WindowEventListener::resizeWindow(rw->getWidth(), rw->getHeight()); }
80        void windowFocusChange (Ogre::RenderWindow* rw)
81            { orxonox::WindowEventListener::changeWindowFocus(); }
82        void windowClosed      (Ogre::RenderWindow* rw)
83            { orxonox::Game::getInstance().stop(); }
84        void windowMoved       (Ogre::RenderWindow* rw)
85            { orxonox::WindowEventListener::moveWindow(); }
[2801]86    };
[1032]87
[2801]88    GraphicsManager* GraphicsManager::singletonRef_s = 0;
[1293]89
[1755]90    /**
91    @brief
[2801]92        Non-initialising constructor.
[1755]93    */
[2801]94    GraphicsManager::GraphicsManager()
95        : ogreRoot_(0)
96        , ogreLogger_(0)
97        , renderWindow_(0)
98        , viewport_(0)
[3280]99        , ogreWindowEventListener_(new OgreWindowEventListener())
[1024]100    {
[2801]101        RegisterObject(GraphicsManager);
102
103        assert(singletonRef_s == 0);
104        singletonRef_s = this;
105
106        this->setConfigValues();
[612]107
[2801]108        // Ogre setup procedure
109        setupOgre();
110
[3280]111        try
112        {
113            // load all the required plugins for Ogre
114            loadOgrePlugins();
115            // read resource declaration file
116            this->declareResources();
117            // Reads ogre config and creates the render window
118            this->loadRenderer();
[2801]119
[3280]120            // TODO: Spread this
121            this->initialiseResources();
[2801]122
[3280]123            // add console commands
124            FunctorMember<GraphicsManager>* functor1 = createFunctor(&GraphicsManager::printScreen);
125            functor1->setObject(this);
126            ccPrintScreen_ = createConsoleCommand(functor1, "printScreen");
127            CommandExecutor::addConsoleCommandShortcut(ccPrintScreen_);
128        }
129        catch (...)
130        {
131            // clean up
132            delete this->ogreRoot_;
133            delete this->ogreLogger_;
134            delete this->ogreWindowEventListener_;
135            throw;
136        }
[2801]137    }
138
[1755]139    /**
140    @brief
[2801]141        Destroys all the Ogre related objects
[1755]142    */
[2801]143    GraphicsManager::~GraphicsManager()
[1535]144    {
[3280]145/*
146        delete this->ccPrintScreen_;
147*/
[1502]148
[3280]149        // HACK! This fixes an exit crash
150        Map::hackDestroyMap();
151        // unload all compositors (this is only necessary because we don't yet destroy all resources!)
152        Ogre::CompositorManager::getSingleton().removeAll();
[1535]153
[3280]154        // Delete OGRE main control organ
155        delete this->ogreRoot_;
[3103]156
[3280]157        // delete the logManager (since we have created it in the first place).
158        delete this->ogreLogger_;
[3196]159
[3280]160        delete this->ogreWindowEventListener_;
[2662]161
[2801]162        assert(singletonRef_s);
163        singletonRef_s = 0;
[1535]164    }
165
[2801]166    void GraphicsManager::setConfigValues()
[1535]167    {
[2801]168        SetConfigValue(resourceFile_,    "resources.cfg")
169            .description("Location of the resources file in the data path.");
170        SetConfigValue(ogreConfigFile_,  "ogre.cfg")
171            .description("Location of the Ogre config file");
172        SetConfigValue(ogrePluginsFolder_, ORXONOX_OGRE_PLUGINS_FOLDER)
173            .description("Folder where the Ogre plugins are located.");
174        SetConfigValue(ogrePlugins_, ORXONOX_OGRE_PLUGINS)
175            .description("Comma separated list of all plugins to load.");
176        SetConfigValue(ogreLogFile_,     "ogre.log")
177            .description("Logfile for messages from Ogre. Use \"\" to suppress log file creation.");
178        SetConfigValue(ogreLogLevelTrivial_ , 5)
179            .description("Corresponding orxonox debug level for ogre Trivial");
180        SetConfigValue(ogreLogLevelNormal_  , 4)
181            .description("Corresponding orxonox debug level for ogre Normal");
182        SetConfigValue(ogreLogLevelCritical_, 2)
183            .description("Corresponding orxonox debug level for ogre Critical");
184        SetConfigValue(detailLevelParticle_, 2)
185            .description("O: off, 1: low, 2: normal, 3: high").callback(this, &GraphicsManager::detailLevelParticleChanged);
[1535]186    }
[612]187
[2801]188    void GraphicsManager::detailLevelParticleChanged()
[612]189    {
[1755]190        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
191            it->detailLevelChanged(this->detailLevelParticle_);
[612]192    }
193
[2801]194    void GraphicsManager::update(const Clock& time)
195    {
[3280]196        Ogre::FrameEvent evt;
197        evt.timeSinceLastFrame = time.getDeltaTime();
198        evt.timeSinceLastEvent = time.getDeltaTime(); // note: same time, but shouldn't matter anyway
[2801]199
[3280]200        // don't forget to call _fireFrameStarted to OGRE to make sure
201        // everything goes smoothly
202        ogreRoot_->_fireFrameStarted(evt);
[2801]203
[3280]204        // Pump messages in all registered RenderWindows
205        // This calls the WindowEventListener objects.
206        Ogre::WindowEventUtilities::messagePump();
207        // make sure the window stays active even when not focused
208        // (probably only necessary on windows)
209        this->renderWindow_->setActive(true);
[2801]210
[3280]211        // render
212        ogreRoot_->_updateAllRenderTargets();
[2801]213
[3280]214        // again, just to be sure OGRE works fine
215        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
[2801]216    }
217
[2850]218    void GraphicsManager::setCamera(Ogre::Camera* camera)
219    {
220        this->viewport_->setCamera(camera);
221    }
222
[1755]223    /**
224    @brief
[2801]225        Creates the Ogre Root object and sets up the ogre log.
[1755]226    */
[2801]227    void GraphicsManager::setupOgre()
[1538]228    {
[2801]229        COUT(3) << "Setting up Ogre..." << std::endl;
230
231        if (ogreConfigFile_ == "")
232        {
233            COUT(2) << "Warning: Ogre config file set to \"\". Defaulting to config.cfg" << std::endl;
234            ModifyConfigValue(ogreConfigFile_, tset, "config.cfg");
235        }
236        if (ogreLogFile_ == "")
237        {
238            COUT(2) << "Warning: Ogre log file set to \"\". Defaulting to ogre.log" << std::endl;
239            ModifyConfigValue(ogreLogFile_, tset, "ogre.log");
240        }
241
242        boost::filesystem::path ogreConfigFilepath(Core::getConfigPath() / this->ogreConfigFile_);
243        boost::filesystem::path ogreLogFilepath(Core::getLogPath() / this->ogreLogFile_);
244
245        // create a new logManager
246        // Ogre::Root will detect that we've already created a Log
[3280]247        std::auto_ptr<Ogre::LogManager> logger(new Ogre::LogManager());
[2801]248        COUT(4) << "Ogre LogManager created" << std::endl;
249
250        // create our own log that we can listen to
251        Ogre::Log *myLog;
[3280]252        myLog = logger->createLog(ogreLogFilepath.string(), true, false, false);
[2801]253        COUT(4) << "Ogre Log created" << std::endl;
254
255        myLog->setLogDetail(Ogre::LL_BOREME);
256        myLog->addListener(this);
257
258        COUT(4) << "Creating Ogre Root..." << std::endl;
259
260        // check for config file existence because Ogre displays (caught) exceptions if not
261        if (!boost::filesystem::exists(ogreConfigFilepath))
262        {
263            // create a zero sized file
264            std::ofstream creator;
265            creator.open(ogreConfigFilepath.string().c_str());
266            creator.close();
267        }
268
269        // Leave plugins file empty. We're going to do that part manually later
270        ogreRoot_ = new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string());
[3280]271        // In case that new Root failed the logger gets destroyed because of the std::auto_ptr
272        ogreLogger_ = logger.release();
[2801]273
274        COUT(3) << "Ogre set up done." << std::endl;
[1538]275    }
[2801]276
277    void GraphicsManager::loadOgrePlugins()
278    {
279        // just to make sure the next statement doesn't segfault
280        if (ogrePluginsFolder_ == "")
281            ogrePluginsFolder_ = ".";
282
283        boost::filesystem::path folder(ogrePluginsFolder_);
284        // Do some SubString magic to get the comma separated list of plugins
[3323]285        SubString plugins(ogrePlugins_, ",", " ", false, '\\', false, '"', false, '(', ')', false, '\0');
[2801]286        // Use backslash paths on Windows! file_string() already does that though.
287        for (unsigned int i = 0; i < plugins.size(); ++i)
288            ogreRoot_->loadPlugin((folder / plugins[i]).file_string());
289    }
290
291    void GraphicsManager::declareResources()
292    {
293        CCOUT(4) << "Declaring Resources" << std::endl;
294        //TODO: Specify layout of data file and maybe use xml-loader
295        //TODO: Work with ressource groups (should be generated by a special loader)
296
297        if (resourceFile_ == "")
298        {
299            COUT(2) << "Warning: Ogre resource file set to \"\". Defaulting to resources.cfg" << std::endl;
300            ModifyConfigValue(resourceFile_, tset, "resources.cfg");
301        }
302
303        // Load resource paths from data file using configfile ressource type
304        Ogre::ConfigFile cf;
305        try
306        {
307            cf.load((Core::getMediaPath() / resourceFile_).string());
308        }
309        catch (...)
310        {
311            //COUT(1) << ex.getFullDescription() << std::endl;
312            COUT(0) << "Have you forgotten to set the data path in orxnox.ini?" << std::endl;
313            throw;
314        }
315
316        // Go through all sections & settings in the file
317        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
318
319        std::string secName, typeName, archName;
320        while (seci.hasMoreElements())
321        {
322            try
323            {
324                secName = seci.peekNextKey();
325                Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
326                Ogre::ConfigFile::SettingsMultiMap::iterator i;
327                for (i = settings->begin(); i != settings->end(); ++i)
328                {
329                    typeName = i->first; // for instance "FileSystem" or "Zip"
330                    archName = i->second; // name (and location) of archive
331
332                    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
333                        (Core::getMediaPath() / archName).string(), typeName, secName);
334                }
335            }
336            catch (Ogre::Exception& ex)
337            {
338                COUT(1) << ex.getFullDescription() << std::endl;
339            }
340        }
341    }
342
343    void GraphicsManager::loadRenderer()
344    {
345        CCOUT(4) << "Configuring Renderer" << std::endl;
346
347        if (!ogreRoot_->restoreConfig())
348            if (!ogreRoot_->showConfigDialog())
[3280]349                ThrowException(InitialisationFailed, "OGRE graphics configuration dialogue failed.");
[2801]350
351        CCOUT(4) << "Creating render window" << std::endl;
352
353        this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox");
[3327]354        this->ogreWindowEventListener_->windowResized(renderWindow_);
[2801]355
356        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_);
357
358        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0);
359
360        // create a full screen default viewport
361        this->viewport_ = this->renderWindow_->addViewport(0, 0);
362    }
363
364    void GraphicsManager::initialiseResources()
365    {
366        CCOUT(4) << "Initialising resources" << std::endl;
367        //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
368        //try
369        //{
370            Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
371            /*Ogre::StringVector str = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
372            for (unsigned int i = 0; i < str.size(); i++)
373            {
374            Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(str[i]);
375            }*/
376        //}
377        //catch (...)
378        //{
379        //    CCOUT(2) << "Error: There was a serious error when initialising the resources." << std::endl;
380        //    throw;
381        //}
382    }
383
384    /**
385    @brief
386        Method called by the LogListener interface from Ogre.
387        We use it to capture Ogre log messages and handle it ourselves.
388    @param message
389        The message to be logged
390    @param lml
391        The message level the log is using
392    @param maskDebug
393        If we are printing to the console or not
394    @param logName
395        The name of this log (so you can have several listeners
396        for different logs, and identify them)
397    */
398    void GraphicsManager::messageLogged(const std::string& message,
399        Ogre::LogMessageLevel lml, bool maskDebug, const std::string& logName)
400    {
401        int orxonoxLevel;
402        switch (lml)
403        {
404        case Ogre::LML_TRIVIAL:
405            orxonoxLevel = this->ogreLogLevelTrivial_;
406            break;
407        case Ogre::LML_NORMAL:
408            orxonoxLevel = this->ogreLogLevelNormal_;
409            break;
410        case Ogre::LML_CRITICAL:
411            orxonoxLevel = this->ogreLogLevelCritical_;
412            break;
413        default:
414            orxonoxLevel = 0;
415        }
416        OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
417            << "Ogre: " << message << std::endl;
418    }
419
420    void GraphicsManager::printScreen()
421    {
422        assert(this->renderWindow_);
423       
424        this->renderWindow_->writeContentsToTimestampedFile(Core::getLogPathString() + "screenShot_", ".jpg");
425    }
[612]426}
Note: See TracBrowser for help on using the repository browser.