diff --git a/ext/ghoul b/ext/ghoul index 260c7e0d3d..8418f394f4 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit 260c7e0d3d72355df7fa36b4c81a30fc77728f7f +Subproject commit 8418f394f472457db9ca7684d27e910063cfd898 diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index a0b38eb422..1bdfbaf05c 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -80,7 +80,7 @@ public: private: OpenSpaceEngine(std::string programName); - ~OpenSpaceEngine(); + ~OpenSpaceEngine() = default; void clearAllWindows(); bool gatherCommandlineArguments(); diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 4c2b20e998..305124ba63 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -84,25 +84,207 @@ OpenSpaceEngine::OpenSpaceEngine(std::string programName) : _commandlineParser(programName, true) , _syncBuffer(nullptr) { - // initialize OpenSpace helpers SpiceManager::initialize(); Time::initialize(); FactoryManager::initialize(); ghoul::systemcapabilities::SystemCapabilities::initialize(); } -OpenSpaceEngine::~OpenSpaceEngine() { - ghoul::systemcapabilities::SystemCapabilities::deinitialize(); - FactoryManager::deinitialize(); - Time::deinitialize(); - SpiceManager::deinitialize(); -} - OpenSpaceEngine& OpenSpaceEngine::ref() { assert(_engine); return *_engine; } - + +bool OpenSpaceEngine::create(int argc, char** argv, + std::vector& sgctArguments) +{ + assert(_engine == nullptr); + + // Initialize the LogManager and add the console log as this will be used every time + // and we need a fall back if something goes wrong between here and when we add the + // logs from the configuration file. If the user requested as specific loglevel in the + // configuration file, we will deinitialize this LogManager and reinitialize it later + // with the correct LogLevel + LogManager::initialize(LogManager::LogLevel::Debug, true); + LogMgr.addLog(new ConsoleLog); + + LDEBUG("Initialize FileSystem"); + ghoul::filesystem::FileSystem::initialize(); + + // Sanity check of values + if (argc < 1 || argv == nullptr) { + LFATAL("No arguments were passed to this function"); + return false; + } + + // Create other objects + LDEBUG("Creating OpenSpaceEngine"); + _engine = new OpenSpaceEngine(std::string(argv[0])); + + // Query modules for commandline arguments + const bool gatherSuccess = _engine->gatherCommandlineArguments(); + if (!gatherSuccess) + return false; + + // Parse commandline arguments + std::vector remainingArguments; + _engine->_commandlineParser.setCommandLine(argc, argv, &sgctArguments); + const bool executeSuccess = _engine->_commandlineParser.execute(); + if (!executeSuccess) + return false; + + // Find configuration + std::string configurationFilePath = commandlineArgumentPlaceholders.configurationName; + if (configurationFilePath.empty()) { + LDEBUG("Finding configuration"); + const bool findConfigurationSuccess = + OpenSpaceEngine::findConfiguration(configurationFilePath); + if (!findConfigurationSuccess) { + LFATAL("Could not find OpenSpace configuration file!"); + return false; + } + } + configurationFilePath = absPath(configurationFilePath); + LINFO("Configuration Path: '" << configurationFilePath << "'"); + + // Loading configuration from disk + LDEBUG("Loading configuration from disk"); + const bool configLoadSuccess = _engine->configurationManager().loadFromFile( + configurationFilePath); + if (!configLoadSuccess) { + LFATAL("Loading of configuration file '" << configurationFilePath << "' failed"); + return false; + } + + // Initialize the requested logs from the configuration file + _engine->configureLogging(); + + // Create directories that doesn't exist + auto tokens = FileSys.tokens(); + for (auto token : tokens) { + if (!FileSys.directoryExists(token)) { + std::string p = absPath(token); + LDEBUG("Directory '" << p << "' does not exist, creating."); + if (!FileSys.createDirectory(p, true)) + LERROR("Directory '" << p << "' could not be created"); + } + } + + // Create the cachemanager + FileSys.createCacheManager(absPath("${" + constants::configurationmanager::keyCache + "}")); + _engine->_console.loadHistory(); + + _engine->_syncBuffer = new SyncBuffer(1024); + + // Determining SGCT configuration file + LDEBUG("Determining SGCT configuration file"); + std::string sgctConfigurationPath = _sgctDefaultConfigFile; + _engine->configurationManager().getValue( + constants::configurationmanager::keyConfigSgct, sgctConfigurationPath); + + if (!commandlineArgumentPlaceholders.sgctConfigurationName.empty()) { + LDEBUG("Overwriting SGCT configuration file with commandline argument: " << + commandlineArgumentPlaceholders.sgctConfigurationName); + sgctConfigurationPath = commandlineArgumentPlaceholders.sgctConfigurationName; + } + + // Prepend the outgoing sgctArguments with the program name + // as well as the configuration file that sgct is supposed to use + sgctArguments.insert(sgctArguments.begin(), argv[0]); + sgctArguments.insert(sgctArguments.begin() + 1, _sgctConfigArgumentCommand); + sgctArguments.insert(sgctArguments.begin() + 2, absPath(sgctConfigurationPath)); + + return true; +} + +void OpenSpaceEngine::destroy() { + delete _engine; + ghoul::systemcapabilities::SystemCapabilities::deinitialize(); + FactoryManager::deinitialize(); + Time::deinitialize(); + SpiceManager::deinitialize(); + + FileSystem::deinitialize(); + LogManager::deinitialize(); +} + +bool OpenSpaceEngine::initialize() { + // clear the screen so the user don't have to see old buffer contents from the + // graphics card + clearAllWindows(); + + // Detect and log OpenCL and OpenGL versions and available devices + SysCap.addComponent(new ghoul::systemcapabilities::CPUCapabilitiesComponent); + SysCap.addComponent(new ghoul::systemcapabilities::OpenGLCapabilitiesComponent); + SysCap.detectCapabilities(); + SysCap.logCapabilities(); + + // Load SPICE time kernel + bool success = loadSpiceKernels(); + if (!success) + return false; + + // Register Lua script functions + LDEBUG("Registering Lua libraries"); + _scriptEngine.addLibrary(RenderEngine::luaLibrary()); + _scriptEngine.addLibrary(SceneGraph::luaLibrary()); + _scriptEngine.addLibrary(Time::luaLibrary()); + _scriptEngine.addLibrary(interaction::InteractionHandler::luaLibrary()); + _scriptEngine.addLibrary(LuaConsole::luaLibrary()); + _scriptEngine.addLibrary(GUI::luaLibrary()); + + // TODO: Maybe move all scenegraph and renderengine stuff to initializeGL + scriptEngine().initialize(); + + // If a LuaDocumentationFile was specified, generate it now + using constants::configurationmanager::keyLuaDocumentationType; + using constants::configurationmanager::keyLuaDocumentationFile; + const bool hasType = configurationManager().hasKey(keyLuaDocumentationType); + const bool hasFile = configurationManager().hasKey(keyLuaDocumentationFile); + if (hasType && hasFile) { + std::string luaDocumentationType; + configurationManager().getValue(keyLuaDocumentationType, luaDocumentationType); + std::string luaDocumentationFile; + configurationManager().getValue(keyLuaDocumentationFile, luaDocumentationFile); + + luaDocumentationFile = absPath(luaDocumentationFile); + _scriptEngine.writeDocumentation(luaDocumentationFile, luaDocumentationType); + } + + + // Load scenegraph + SceneGraph* sceneGraph = new SceneGraph; + _renderEngine.setSceneGraph(sceneGraph); + + // initialize the RenderEngine + _renderEngine.initialize(); + sceneGraph->initialize(); + + std::string sceneDescriptionPath; + success = configurationManager().getValue( + constants::configurationmanager::keyConfigScene, sceneDescriptionPath); + if (success) + sceneGraph->scheduleLoadSceneFile(sceneDescriptionPath); + + _interactionHandler.setKeyboardController(new interaction::KeyboardControllerFixed); + //_interactionHandler.setKeyboardController(new interaction::KeyboardControllerLua); + _interactionHandler.setMouseController(new interaction::TrackballMouseController); + + // Run start up scripts + runStartupScripts(); + + // Load a light and a monospaced font + loadFonts(); + + _gui.initialize(); + + return true; +} + +bool OpenSpaceEngine::isInitialized() { + return _engine != nullptr; +} + void OpenSpaceEngine::clearAllWindows() { size_t n = sgct::Engine::instance()->getNumberOfWindows(); for (size_t i = 0; i < n; ++i) { @@ -266,190 +448,6 @@ void OpenSpaceEngine::configureLogging() { } } -bool OpenSpaceEngine::create(int argc, char** argv, - std::vector& sgctArguments) -{ - // TODO custom assert (ticket #5) - assert(_engine == nullptr); - - // Initialize the LogManager and add the console log as this will be used every time - // and we need a fall back if something goes wrong between here and when we add the - // logs from the configuration file. If the user requested as specific loglevel in the - // configuration file, we will deinitialize this LogManager and reinitialize it later - // with the correct LogLevel - LogManager::initialize(LogManager::LogLevel::Debug, true); - LogMgr.addLog(new ConsoleLog); - ghoul::filesystem::FileSystem::initialize(); - - // Sanity check of values - if (argc < 1) { - LFATAL("No arguments were passed to the function"); - return false; - } - - // create other objects - LDEBUG("Creating OpenSpaceEngine"); - _engine = new OpenSpaceEngine(std::string(argv[0])); - - // Query modules for commandline arguments - const bool gatherSuccess = _engine->gatherCommandlineArguments(); - if (!gatherSuccess) - return false; - - // Parse commandline arguments - std::vector remainingArguments; - _engine->_commandlineParser.setCommandLine(argc, argv, &sgctArguments); - const bool executeSuccess = _engine->_commandlineParser.execute(); - if (!executeSuccess) - return false; - - // Find configuration - std::string configurationFilePath = commandlineArgumentPlaceholders.configurationName; - if (configurationFilePath.empty()) { - LDEBUG("Finding configuration"); - const bool findConfigurationSuccess = - OpenSpaceEngine::findConfiguration(configurationFilePath); - if (!findConfigurationSuccess) { - LFATAL("Could not find OpenSpace configuration file!"); - return false; - } - } - configurationFilePath = absPath(configurationFilePath); - LINFO("Configuration Path: '" << configurationFilePath << "'"); - - // Loading configuration from disk - LDEBUG("Loading configuration from disk"); - const bool configLoadSuccess = _engine->configurationManager().loadFromFile( - configurationFilePath); - if (!configLoadSuccess) { - LFATAL("Loading of configuration file '" << configurationFilePath << "' failed"); - return false; - } - - // Initialize the requested logs from the configuration file - _engine->configureLogging(); - - // Create directories that doesn't exist - auto tokens = FileSys.tokens(); - for (auto token : tokens) { - if (!FileSys.directoryExists(token)) { - std::string p = absPath(token); - LDEBUG("Directory '" << p <<"' does not exist, creating."); - if(!FileSys.createDirectory(p, true)) - LERROR("Directory '" << p <<"' could not be created"); - } - } - - // Create the cachemanager - FileSys.createCacheManager(absPath("${" + constants::configurationmanager::keyCache + "}")); - _engine->_console.loadHistory(); - - _engine->_syncBuffer = new SyncBuffer(1024); - - // Determining SGCT configuration file - LDEBUG("Determining SGCT configuration file"); - std::string sgctConfigurationPath = _sgctDefaultConfigFile; - _engine->configurationManager().getValue( - constants::configurationmanager::keyConfigSgct, sgctConfigurationPath); - - if (!commandlineArgumentPlaceholders.sgctConfigurationName.empty()) { - LDEBUG("Overwriting SGCT configuration file with commandline argument: " << - commandlineArgumentPlaceholders.sgctConfigurationName); - sgctConfigurationPath = commandlineArgumentPlaceholders.sgctConfigurationName; - } - - // Prepend the outgoing sgctArguments with the program name - // as well as the configuration file that sgct is supposed to use - sgctArguments.insert(sgctArguments.begin(), argv[0]); - sgctArguments.insert(sgctArguments.begin() + 1, _sgctConfigArgumentCommand); - sgctArguments.insert(sgctArguments.begin() + 2, absPath(sgctConfigurationPath)); - - return true; -} - -void OpenSpaceEngine::destroy() { - delete _engine; - FileSystem::deinitialize(); - LogManager::deinitialize(); -} - -bool OpenSpaceEngine::isInitialized() { - return _engine != nullptr; -} - -bool OpenSpaceEngine::initialize() { - // clear the screen so the user don't have to see old buffer contents from the - // graphics card - clearAllWindows(); - - // Detect and log OpenCL and OpenGL versions and available devices - SysCap.addComponent(new ghoul::systemcapabilities::CPUCapabilitiesComponent); - SysCap.addComponent(new ghoul::systemcapabilities::OpenGLCapabilitiesComponent); - SysCap.detectCapabilities(); - SysCap.logCapabilities(); - - // Load SPICE time kernel - bool success = loadSpiceKernels(); - if (!success) - return false; - - // Register Lua script functions - LDEBUG("Registering Lua libraries"); - _scriptEngine.addLibrary(RenderEngine::luaLibrary()); - _scriptEngine.addLibrary(SceneGraph::luaLibrary()); - _scriptEngine.addLibrary(Time::luaLibrary()); - _scriptEngine.addLibrary(interaction::InteractionHandler::luaLibrary()); - _scriptEngine.addLibrary(LuaConsole::luaLibrary()); - _scriptEngine.addLibrary(GUI::luaLibrary()); - - // TODO: Maybe move all scenegraph and renderengine stuff to initializeGL - scriptEngine().initialize(); - - // If a LuaDocumentationFile was specified, generate it now - using constants::configurationmanager::keyLuaDocumentationType; - using constants::configurationmanager::keyLuaDocumentationFile; - const bool hasType = configurationManager().hasKey(keyLuaDocumentationType); - const bool hasFile = configurationManager().hasKey(keyLuaDocumentationFile); - if (hasType && hasFile) { - std::string luaDocumentationType; - configurationManager().getValue(keyLuaDocumentationType, luaDocumentationType); - std::string luaDocumentationFile; - configurationManager().getValue(keyLuaDocumentationFile, luaDocumentationFile); - - luaDocumentationFile = absPath(luaDocumentationFile); - _scriptEngine.writeDocumentation(luaDocumentationFile, luaDocumentationType); - } - - - // Load scenegraph - SceneGraph* sceneGraph = new SceneGraph; - _renderEngine.setSceneGraph(sceneGraph); - - // initialize the RenderEngine - _renderEngine.initialize(); - sceneGraph->initialize(); - - std::string sceneDescriptionPath; - success = configurationManager().getValue( - constants::configurationmanager::keyConfigScene, sceneDescriptionPath); - if (success) - sceneGraph->scheduleLoadSceneFile(sceneDescriptionPath); - - _interactionHandler.setKeyboardController(new interaction::KeyboardControllerFixed); - //_interactionHandler.setKeyboardController(new interaction::KeyboardControllerLua); - _interactionHandler.setMouseController(new interaction::TrackballMouseController); - - // Run start up scripts - runStartupScripts(); - - // Load a light and a monospaced font - loadFonts(); - - _gui.initialize(); - - return true; -} - ConfigurationManager& OpenSpaceEngine::configurationManager() { return _configurationManager; } @@ -476,9 +474,7 @@ GUI& OpenSpaceEngine::gui() { bool OpenSpaceEngine::initializeGL() { bool success = _renderEngine.initializeGL(); - if (_gui.isEnabled()) - _gui.initializeGL(); - + _gui.initializeGL(); return success; }