diff --git a/apps/OpenSpace/main.cpp b/apps/OpenSpace/main.cpp index a800cdfa9c..684cbcc1bc 100644 --- a/apps/OpenSpace/main.cpp +++ b/apps/OpenSpace/main.cpp @@ -49,7 +49,6 @@ sgct::SGCTWindow* FirstOpenVRWindow = nullptr; std::pair supportedOpenGLVersion() { // Just create a window in order to retrieve the available OpenGL version before we // create the real window - glfwInit(); // On OS X we need to explicitly set the version and specify that we are using CORE @@ -77,26 +76,20 @@ std::pair supportedOpenGLVersion() { // And get rid of the window again glfwDestroyWindow(offscreen); glfwWindowHint(GLFW_VISIBLE, GL_TRUE); + return { major, minor }; } void mainInitFunc() { LTRACE("main::mainInitFunc(begin)"); - LDEBUG("Initializing OpenSpace Engine"); - // @CLEANUP: The return value should be replaced with an exception ---abock - bool success = OsEng.initialize(); + LDEBUG("Initializing OpenSpace Engine started"); + OsEng.initialize(); + LDEBUG("Initializing OpenSpace Engine finished"); - LDEBUG("Initializing OpenGL in OpenSpace Engine"); - if (success) { - success = OsEng.initializeGL(); - } - - if (!success) { - LFATAL("Initializing OpenSpaceEngine failed"); - LogMgr.flushLogs(); - exit(EXIT_FAILURE); - } + LDEBUG("Initializing OpenGL in OpenSpace Engine started"); + OsEng.initializeGL(); + LDEBUG("Initializing OpenGL in OpenSpace Engine finished"); // Find if we have at least one OpenVR window // Save reference to first OpenVR window, which is the one we will copy to the HMD. @@ -160,21 +153,17 @@ void mainPostSyncPreDrawFunc() { void mainRenderFunc() { LTRACE("main::mainRenderFunc(begin)"); - using glm::mat4; - using glm::translate; - //not the most efficient, but for clarity @JK - mat4 userMatrix = translate(mat4(1.f), SgctEngine->getDefaultUserPtr()->getPos()); - mat4 sceneMatrix = SgctEngine->getModelMatrix(); - mat4 viewMatrix = SgctEngine->getCurrentViewMatrix() * userMatrix; - - //dont shift nav-direction on master, makes it very tricky to navigate @JK - if (!SgctEngine->isMaster()) { - viewMatrix = viewMatrix * sceneMatrix; - } - - mat4 projectionMatrix = SgctEngine->getCurrentProjectionMatrix(); + glm::mat4 viewMatrix = + SgctEngine->getCurrentViewMatrix() * + // User matrix + glm::translate( + glm::mat4(1.f), + SgctEngine->getDefaultUserPtr()->getPos() + ) + ; + glm::mat4 projectionMatrix = SgctEngine->getCurrentProjectionMatrix(); #ifdef OPENVR_SUPPORT bool currentWindowIsHMD = FirstOpenVRWindow == _sgctEngine->getCurrentWindowPtr(); if (sgct::SGCTOpenVR::isHMDActive() && currentWindowIsHMD) { @@ -184,9 +173,13 @@ void mainRenderFunc() { } #endif - // @CLEANUP: Pass the scene matrix to the render function as well and let the - // OpenSpaceEngine decide whether it wants to apply it or not - OsEng.render(projectionMatrix, viewMatrix); + if (SgctEngine->isMaster()) { + OsEng.render(viewMatrix, projectionMatrix); + } + else { + glm::mat4 sceneMatrix = SgctEngine->getModelMatrix(); + OsEng.render(viewMatrix * sceneMatrix, projectionMatrix); + } LTRACE("main::mainRenderFunc(end)"); } @@ -195,7 +188,7 @@ void mainPostDrawFunc() { #ifdef OPENVR_SUPPORT if (FirstOpenVRWindow) { - //Copy the first OpenVR window to the HMD + // Copy the first OpenVR window to the HMD sgct::SGCTOpenVR::copyWindowToHMD(FirstOpenVRWindow); } #endif @@ -242,9 +235,11 @@ void mainMousePosCallback(double x, double y) { } void mainMouseScrollCallback(double posX, double posY) { + LTRACE("main::mainMouseScrollCallback(begin"); if (SgctEngine->isMaster()) { OsEng.mouseScrollWheelCallback(posY); } + LTRACE("main::mainMouseScrollCallback(end)"); } void mainCharCallback(unsigned int codepoint, int mods) { @@ -273,28 +268,30 @@ void mainLogCallback(const char* msg) { return; } // Remove the trailing \n that is passed along - LINFOC("SGCT", message.substr(0, std::max(message.size() - 1, 0))); + LINFOC("SGCT", message.substr(0, message.size() - 1)); } int main_main(int argc, char** argv) { std::pair glVersion = supportedOpenGLVersion(); - // create the OpenSpace engine and get arguments for the sgct engine + // Create the OpenSpace engine and get arguments for the SGCT engine // @CLEANUP: Replace the return valua with throwing an exception --abock std::vector sgctArguments; - bool success = openspace::OpenSpaceEngine::create( + bool requestQuit = false; + openspace::OpenSpaceEngine::create( argc, argv, std::make_unique(), - sgctArguments + sgctArguments, + requestQuit ); - if (!success) { - return EXIT_FAILURE; + if (requestQuit) { + return EXIT_SUCCESS; } LINFO("Detected OpenGL version: " << glVersion.first << "." << glVersion.second); - // create sgct engine c arguments + // Create sgct engine c arguments int newArgc = static_cast(sgctArguments.size()); char** newArgv = new char*[newArgc]; @@ -315,7 +312,7 @@ int main_main(int argc, char** argv) { LDEBUG("Creating SGCT Engine"); SgctEngine = new sgct::Engine(newArgc, newArgv); - // deallocate sgct c arguments + // Deallocate sgct c arguments delete[] newArgv; // Bind functions @@ -336,12 +333,12 @@ int main_main(int argc, char** argv) { sgct::MessageHandler::instance()->setNotifyLevel(sgct::MessageHandler::NOTIFY_ALL); - // set encode and decode functions + // Set encode and decode functions // NOTE: starts synchronizing before init functions sgct::SharedData::instance()->setEncodeFunction(mainEncodeFun); sgct::SharedData::instance()->setDecodeFunction(mainDecodeFun); - // try to open a window + // Try to open a window LDEBUG("Initialize SGCT Engine"); std::map, sgct::Engine::RunMode> versionMapping = { { { 3, 3 }, sgct::Engine::RunMode::OpenGL_3_3_Core_Profile }, @@ -360,14 +357,13 @@ int main_main(int argc, char** argv) { auto cleanup = [&](){ OsEng.deinitialize(); - // clear function bindings to avoid crash after destroying the OpenSpace Engine + // Clear function bindings to avoid crash after destroying the OpenSpace Engine sgct::MessageHandler::instance()->setLogToCallback(false); sgct::MessageHandler::instance()->setLogCallback(nullptr); LDEBUG("Destroying OpenSpaceEngine"); openspace::OpenSpaceEngine::destroy(); - // Clean up (deallocate) LDEBUG("Destroying SGCT Engine"); delete SgctEngine; }; @@ -405,21 +401,19 @@ int main(int argc, char** argv) { return main_main(argc, argv); } catch (const ghoul::RuntimeError& e) { - // Write out all of the information about the exception, flush the logs, and throw + // Write out all of the information about the exception and flush the logs LFATALC(e.component, e.message); LogMgr.flushLogs(); - throw; + return EXIT_FAILURE; } catch (const std::exception& e) { - // Write out all of the information about the exception, flush the logs, and throw LFATALC("Exception", e.what()); LogMgr.flushLogs(); - throw; + return EXIT_FAILURE; } catch (...) { - // Write out all of the information about the exception, flush the logs, and throw LFATALC("Exception", "Unknown exception"); LogMgr.flushLogs(); - throw; + return EXIT_FAILURE; } } diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index e7b539c195..12e17d6a03 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -36,39 +36,43 @@ #include namespace ghoul { + class Dictionary; + namespace cmdparser { class CommandlineParser; } namespace fontrendering { class FontManager; } -} + +} // namespace ghoul namespace openspace { class ConfigurationManager; class DownloadManager; -class LuaConsole; -class NetworkEngine; class GUI; -class RenderEngine; +class LuaConsole; class ModuleEngine; -class WindowWrapper; -class SettingsEngine; -class TimeManager; -class SyncEngine; +class NetworkEngine; class ParallelConnection; +class RenderEngine; +class SettingsEngine; +class SyncEngine; +class TimeManager; +class WindowWrapper; namespace interaction { class InteractionHandler; } namespace gui { class GUI; } -//namespace scripting { class ScriptEngine; } namespace properties { class PropertyOwner; } -namespace scripting { struct LuaLibrary; } -namespace scripting { class ScriptScheduler; } -namespace scripting { class ScriptEngine; } - +namespace scripting { + struct LuaLibrary; + class ScriptEngine; + class ScriptScheduler; +} // namespace scripting + class OpenSpaceEngine { public: - static bool create(int argc, char** argv, + static void create(int argc, char** argv, std::unique_ptr windowWrapper, - std::vector& sgctArguments); + std::vector& sgctArguments, bool& requestClose); static void destroy(); static bool isInitialized(); static OpenSpaceEngine& ref(); @@ -76,30 +80,13 @@ public: double runTime(); void setRunTime(double t); - // Guaranteed to return a valid pointer - ConfigurationManager& configurationManager(); - interaction::InteractionHandler& interactionHandler(); - RenderEngine& renderEngine(); - scripting::ScriptEngine& scriptEngine(); - scripting::ScriptScheduler& scriptScheduler(); - NetworkEngine& networkEngine(); - LuaConsole& console(); - ModuleEngine& moduleEngine(); - ParallelConnection& parallelConnection(); - properties::PropertyOwner& globalPropertyOwner(); - WindowWrapper& windowWrapper(); - ghoul::fontrendering::FontManager& fontManager(); - DownloadManager& downloadManager(); - TimeManager& timeManager(); - SettingsEngine& settingsEngine(); - - // SGCT callbacks - bool initialize(); - bool initializeGL(); + // callbacks + void initialize(); + void initializeGL(); void deinitialize(); void preSynchronization(); void postSynchronizationPreDraw(); - void render(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix); + void render(const glm::mat4& viewMatrix, const glm::mat4& projectionMatrix); void postDraw(); void keyboardCallback(Key key, KeyModifier mod, KeyAction action); void charCallback(unsigned int codepoint, KeyModifier mod); @@ -109,22 +96,35 @@ public: void externalControlCallback(const char* receivedChars, int size, int clientId); void encode(); void decode(); - + void enableBarrier(); void disableBarrier(); - + void writeDocumentation(); void toggleShutdownMode(); - bool useBusyWaitForDecode(); - bool logSGCTOutOfOrderErrors(); - void runPostInitializationScripts(const std::string& sceneDescription); - + + + // Guaranteed to return a valid pointer + ConfigurationManager& configurationManager(); + LuaConsole& console(); + DownloadManager& downloadManager(); + ModuleEngine& moduleEngine(); + NetworkEngine& networkEngine(); + ParallelConnection& parallelConnection(); + RenderEngine& renderEngine(); + SettingsEngine& settingsEngine(); + TimeManager& timeManager(); + WindowWrapper& windowWrapper(); + ghoul::fontrendering::FontManager& fontManager(); + interaction::InteractionHandler& interactionHandler(); + properties::PropertyOwner& globalPropertyOwner(); + scripting::ScriptEngine& scriptEngine(); + scripting::ScriptScheduler& scriptScheduler(); // This method is only to be called from Modules - enum class CallbackOption { Initialize = 0, // Callback for the end of the initialization Deinitialize, // Callback for the end of the deinitialization @@ -186,39 +186,38 @@ public: void registerModuleMouseScrollWheelCallback(std::function function); /** - * Returns the Lua library that contains all Lua functions available to affect the - * application. - */ + * Returns the Lua library that contains all Lua functions available to affect the + * application. + */ static scripting::LuaLibrary luaLibrary(); private: OpenSpaceEngine(std::string programName, std::unique_ptr windowWrapper); - ~OpenSpaceEngine(); + ~OpenSpaceEngine() = default; void clearAllWindows(); void gatherCommandlineArguments(); void loadFonts(); - void runScripts(const ghoul::Dictionary& scripts); void runPreInitializationScripts(const std::string& sceneDescription); void configureLogging(); // Components std::unique_ptr _configurationManager; - std::unique_ptr _interactionHandler; - std::unique_ptr _renderEngine; - std::unique_ptr _scriptEngine; - std::unique_ptr _scriptScheduler; - std::unique_ptr _networkEngine; - std::unique_ptr _syncEngine; - std::unique_ptr _commandlineParser; + std::unique_ptr _downloadManager; std::unique_ptr _console; std::unique_ptr _moduleEngine; - std::unique_ptr _settingsEngine; - std::unique_ptr _timeManager; - std::unique_ptr _downloadManager; + std::unique_ptr _networkEngine; std::unique_ptr _parallelConnection; + std::unique_ptr _renderEngine; + std::unique_ptr _settingsEngine; + std::unique_ptr _syncEngine; + std::unique_ptr _timeManager; std::unique_ptr _windowWrapper; + std::unique_ptr _commandlineParser; std::unique_ptr _fontManager; + std::unique_ptr _interactionHandler; + std::unique_ptr _scriptEngine; + std::unique_ptr _scriptScheduler; // Others std::unique_ptr _globalPropertyNamespace; @@ -241,24 +240,24 @@ private: std::vector> mouseButton; std::vector> mousePosition; std::vector> mouseScrollWheel; - - - - } _moduleCallbacks; double _runTime; - // Whether the application is currently in shutdown mode (i.e. counting down the timer - // and closing it at '0' - bool _isInShutdownMode; - // The total amount of time the application will wait before actually shutting down - float _shutdownWait; - // The current state of the countdown; if it reaches '0', the application will close - float _shutdownCountdown; + // Structure that is responsible for the delayed shutdown of the application + struct { + // Whether the application is currently in shutdown mode (i.e. counting down the + // timer and closing it at '0' + bool inShutdown; + // Total amount of time the application will wait before actually shutting down + float waitTime; + // Current state of the countdown; if it reaches '0', the application will + // close + float timer; + } _shutdown; // The first frame might take some more time in the update loop, so we need to know to - // disable the synchronization; otherwise a hardware sync will kill us after 1 sec + // disable the synchronization; otherwise a hardware sync will kill us after 1 minute bool _isFirstRenderingFirstFrame; static OpenSpaceEngine* _engine; diff --git a/include/openspace/rendering/renderengine.h b/include/openspace/rendering/renderengine.h index a9644fd41c..e67abec3d0 100644 --- a/include/openspace/rendering/renderengine.h +++ b/include/openspace/rendering/renderengine.h @@ -100,7 +100,7 @@ public: void updateFade(); void updateRenderer(); void updateScreenSpaceRenderables(); - void render(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix); + void render(const glm::mat4& viewMatrix, const glm::mat4& projectionMatrix); void renderScreenLog(); void renderShutdownInformation(float timer, float fullTime); diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 49fc73e75e..9751553fe8 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -69,6 +69,7 @@ #include #include #include +#include #include #include @@ -91,14 +92,13 @@ using namespace ghoul::logging; using namespace ghoul::cmdparser; namespace { - const std::string _loggerCat = "OpenSpaceEngine"; - const std::string _sgctDefaultConfigFile = "${SGCT}/single.xml"; - const std::string _defaultCacheLocation = "${BASE_PATH}/cache"; + const char* _loggerCat = "OpenSpaceEngine"; + const char* SgctDefaultConfigFile = "${SGCT}/single.xml"; - const std::string _sgctConfigArgumentCommand = "-config"; + const char* SgctConfigArgumentCommand = "-config"; - const std::string PreInitializeFunction = "preInitialization"; - const std::string PostInitializationFunction = "postInitialization"; + const char* PreInitializeFunction = "preInitialization"; + const char* PostInitializationFunction = "postInitialization"; const int CacheVersion = 1; const int DownloadVersion = 1; @@ -140,9 +140,7 @@ OpenSpaceEngine::OpenSpaceEngine(std::string programName, , _windowWrapper(std::move(windowWrapper)) , _globalPropertyNamespace(new properties::PropertyOwner) , _runTime(0.0) - , _isInShutdownMode(false) - , _shutdownCountdown(0.f) - , _shutdownWait(0.f) + , _shutdown({false, 0.f, 0.f}) , _isFirstRenderingFirstFrame(true) { _interactionHandler->setPropertyOwner(_globalPropertyNamespace.get()); @@ -173,34 +171,36 @@ OpenSpaceEngine::OpenSpaceEngine(std::string programName, TransformationManager::initialize(); } -OpenSpaceEngine::~OpenSpaceEngine() { - _globalPropertyNamespace = nullptr; - _windowWrapper = nullptr; - _parallelConnection = nullptr; - _configurationManager = nullptr; - _interactionHandler = nullptr; - _renderEngine = nullptr; - _scriptEngine = nullptr; - _networkEngine = nullptr; - _syncEngine = nullptr; - _commandlineParser = nullptr; - _console = nullptr; - _moduleEngine = nullptr; - _settingsEngine = nullptr; -} +//OpenSpaceEngine::~OpenSpaceEngine() { +// _globalPropertyNamespace = nullptr; +// _windowWrapper = nullptr; +// _parallelConnection = nullptr; +// _configurationManager = nullptr; +// _interactionHandler = nullptr; +// _renderEngine = nullptr; +// _scriptEngine = nullptr; +// _networkEngine = nullptr; +// _syncEngine = nullptr; +// _commandlineParser = nullptr; +// _console = nullptr; +// _moduleEngine = nullptr; +// _settingsEngine = nullptr; +//} OpenSpaceEngine& OpenSpaceEngine::ref() { ghoul_assert(_engine, "OpenSpaceEngine not created"); return *_engine; } -bool OpenSpaceEngine::create(int argc, char** argv, +void OpenSpaceEngine::create(int argc, char** argv, std::unique_ptr windowWrapper, - std::vector& sgctArguments) + std::vector& sgctArguments, bool& requestClose) { ghoul_assert(!_engine, "OpenSpaceEngine was already created"); ghoul_assert(windowWrapper != nullptr, "No Window Wrapper was provided"); + requestClose = false; + ghoul::initialize(); // Initialize the LogManager and add the console log as this will be used every time @@ -225,8 +225,10 @@ bool OpenSpaceEngine::create(int argc, char** argv, // Sanity check of values if (argc < 1 || argv == nullptr) { - LFATAL("No arguments were passed to this function"); - return false; + throw ghoul::RuntimeError( + "No arguments were passed to this function", + "OpenSpaceEngine" + ); } // Create other objects @@ -244,7 +246,8 @@ bool OpenSpaceEngine::create(int argc, char** argv, bool showHelp = _engine->_commandlineParser->execute(); if (showHelp) { _engine->_commandlineParser->displayHelp(); - return false; + requestClose = true; + return; } sgctArguments = *arguments; @@ -252,13 +255,8 @@ bool OpenSpaceEngine::create(int argc, char** argv, std::string configurationFilePath = commandlineArgumentPlaceholders.configurationName; if (configurationFilePath.empty()) { LDEBUG("Finding configuration"); - try { - configurationFilePath = - ConfigurationManager::findConfiguration(configurationFilePath); - } - catch (const ghoul::RuntimeError& e) { - LFATALC(e.component, e.message); - } + configurationFilePath = + ConfigurationManager::findConfiguration(configurationFilePath); } configurationFilePath = absPath(configurationFilePath); LINFO("Configuration Path: '" << configurationFilePath << "'"); @@ -276,22 +274,21 @@ bool OpenSpaceEngine::create(int argc, char** argv, for (const documentation::TestResult::Warning& w : e.result.warnings) { LWARNINGC(w.offender, std::to_string(w.reason)); } - return false; - + throw; } catch (const ghoul::RuntimeError& e) { LFATAL("Loading of configuration file '" << configurationFilePath << "' failed"); - LFATALC(e.component, e.message); - return false; + throw; } - bool hasCacheCommandline = !commandlineArgumentPlaceholders.cacheFolder.empty(); - bool hasCacheConfiguration = _engine->configurationManager().hasKeyAndValue( + const bool hasCacheCommandline = !commandlineArgumentPlaceholders.cacheFolder.empty(); + const bool hasCacheConfiguration = _engine->configurationManager().hasKeyAndValue( ConfigurationManager::KeyPerSceneCache ); std::string cacheFolder = absPath("${CACHE}"); if (hasCacheCommandline) { cacheFolder = commandlineArgumentPlaceholders.cacheFolder; + // @CLEANUP: Why is this commented out? ---abock //FileSys.registerPathToken( // "${CACHE}", // commandlineArgumentPlaceholders.cacheFolder, @@ -321,7 +318,9 @@ bool OpenSpaceEngine::create(int argc, char** argv, LINFOC("OpenSpace Version", OPENSPACE_VERSION_MAJOR << "." << OPENSPACE_VERSION_MINOR << "." << - OPENSPACE_VERSION_PATCH << " (" << OPENSPACE_VERSION_STRING << ")"); + OPENSPACE_VERSION_PATCH << + " (" << OPENSPACE_VERSION_STRING << ")" + ); // Create directories that doesn't exist auto tokens = FileSys.tokens(); @@ -356,7 +355,7 @@ bool OpenSpaceEngine::create(int argc, char** argv, // Determining SGCT configuration file LDEBUG("Determining SGCT configuration file"); - std::string sgctConfigurationPath = _sgctDefaultConfigFile; + std::string sgctConfigurationPath = SgctDefaultConfigFile; _engine->configurationManager().getValue( ConfigurationManager::KeyConfigSgct, sgctConfigurationPath); @@ -369,13 +368,12 @@ bool OpenSpaceEngine::create(int argc, char** argv, // 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() + 1, SgctConfigArgumentCommand); sgctArguments.insert(sgctArguments.begin() + 2, absPath(sgctConfigurationPath)); - - return true; } void OpenSpaceEngine::destroy() { + LTRACE("OpenSpaceEngine::destroy(begin)"); for (const auto& func : _engine->_moduleCallbacks.deinitializeGL) { func(); } @@ -398,9 +396,11 @@ void OpenSpaceEngine::destroy() { LogManager::deinitialize(); ghoul::deinitialize(); + LTRACE("OpenSpaceEngine::destroy(end)"); } -bool OpenSpaceEngine::initialize() { +void OpenSpaceEngine::initialize() { + LTRACE("OpenSpaceEngine::initialize(begin)"); // clear the screen so the user don't have to see old buffer contents from the // graphics card LDEBUG("Clearing all Windows"); @@ -420,19 +420,22 @@ bool OpenSpaceEngine::initialize() { using Verbosity = ghoul::systemcapabilities::SystemCapabilitiesComponent::Verbosity; Verbosity verbosity = Verbosity::Default; - if (configurationManager().hasKeyAndValue( - ConfigurationManager::KeyCapabilitiesVerbosity)) - { - std::map verbosityMap = { + if (configurationManager().hasKey(ConfigurationManager::KeyCapabilitiesVerbosity)) { + static const std::map VerbosityMap = { { "None", Verbosity::None }, { "Minimal", Verbosity::Minimal }, { "Default", Verbosity::Default }, { "Full", Verbosity::Full } }; - std::string v = configurationManager().value(ConfigurationManager::KeyCapabilitiesVerbosity); - if (verbosityMap.find(v) != verbosityMap.end()) - verbosity = verbosityMap[v]; + std::string v = configurationManager().value( + ConfigurationManager::KeyCapabilitiesVerbosity + ); + ghoul_assert( + VerbosityMap.find(v) != VerbosityMap.end(), + "Missing check for syscaps verbosity in openspace.cfg documentation" + ); + verbosity = VerbosityMap.find(v)->second; } SysCap.logCapabilities(verbosity); @@ -442,14 +445,21 @@ bool OpenSpaceEngine::initialize() { LINFO("Required OpenGL version: " << version.toString()); if (OpenGLCap.openGLVersion() < version) { - LFATAL("Module required higher OpenGL version than is supported"); - return false; + throw ghoul::RuntimeError( + "Module required higher OpenGL version than is supported", + "OpenSpaceEngine" + ); } - std::string requestURL = ""; - bool success = configurationManager().getValue(ConfigurationManager::KeyDownloadRequestURL, requestURL); - if (success) { - _downloadManager = std::make_unique(requestURL, DownloadVersion); + if (configurationManager().hasKey(ConfigurationManager::KeyDownloadRequestURL)) { + const std::string requestUrl = configurationManager().value( + ConfigurationManager::KeyDownloadRequestURL + ); + + _downloadManager = std::make_unique( + requestUrl, + DownloadVersion + ); } // Register Lua script functions @@ -465,19 +475,25 @@ bool OpenSpaceEngine::initialize() { writeDocumentation(); - bool disableMasterRendering = false; - configurationManager().getValue( - ConfigurationManager::KeyDisableMasterRendering, disableMasterRendering); - _renderEngine->setDisableRenderingOnMaster(disableMasterRendering); + if (configurationManager().hasKey(ConfigurationManager::KeyDisableMasterRendering)) { + const bool disableMasterRendering = configurationManager().value( + ConfigurationManager::KeyDisableMasterRendering + ); + _renderEngine->setDisableRenderingOnMaster(disableMasterRendering); + } - configurationManager().getValue( - ConfigurationManager::KeyShutdownCountdown, _shutdownWait - ); + if (configurationManager().hasKey(ConfigurationManager::KeyShutdownCountdown)) { + _shutdown.waitTime = configurationManager().value( + ConfigurationManager::KeyShutdownCountdown + ); + } - if (!commandlineArgumentPlaceholders.sceneName.empty()) + if (!commandlineArgumentPlaceholders.sceneName.empty()) { configurationManager().setValue( ConfigurationManager::KeyConfigScene, - commandlineArgumentPlaceholders.sceneName); + commandlineArgumentPlaceholders.sceneName + ); + } // Initialize the SettingsEngine _settingsEngine->initialize(); @@ -490,6 +506,8 @@ bool OpenSpaceEngine::initialize() { loadFonts(); // Initialize the Scene + // @CLEANUP: This should become a unique_ptr that is either created inside the + // renderengine or moved into it ---abock Scene* sceneGraph = new Scene; sceneGraph->initialize(); @@ -508,24 +526,23 @@ bool OpenSpaceEngine::initialize() { } // Run start up scripts - try { - runPreInitializationScripts(scenePath); - } - catch (const ghoul::RuntimeError& e) { - LFATALC(e.component, e.message); - } + runPreInitializationScripts(scenePath); _syncEngine->addSyncables(Time::ref().getSyncables()); _syncEngine->addSyncables(_renderEngine->getSyncables()); _syncEngine->addSyncable(_scriptEngine.get()); LINFO("Finished initializing"); - return true; + LTRACE("OpenSpaceEngine::initialize(end)"); } void OpenSpaceEngine::deinitialize() { + LTRACE("OpenSpaceEngine::deinitialize(begin)"); + _interactionHandler->deinitialize(); _renderEngine->deinitialize(); + + LTRACE("OpenSpaceEngine::deinitialize(end)"); } void OpenSpaceEngine::writeDocumentation() { @@ -538,13 +555,17 @@ void OpenSpaceEngine::writeDocumentation() { const bool hasLuaDocType = configurationManager().hasKey(LuaDocumentationType); const bool hasLuaDocFile = configurationManager().hasKey(LuaDocumentationFile); if (hasLuaDocType && hasLuaDocFile) { - std::string luaDocumentationType; - configurationManager().getValue(LuaDocumentationType, luaDocumentationType); - std::string luaDocumentationFile; - configurationManager().getValue(LuaDocumentationFile, luaDocumentationFile); + std::string luaDocumentationType = configurationManager().value( + LuaDocumentationType + ); + std::string luaDocumentationFile = configurationManager().value( + LuaDocumentationFile + ); - luaDocumentationFile = absPath(luaDocumentationFile); - _scriptEngine->writeDocumentation(luaDocumentationFile, luaDocumentationType); + _scriptEngine->writeDocumentation( + absPath(luaDocumentationFile), + luaDocumentationType + ); } // If a general documentation was specified, generate it now @@ -556,24 +577,40 @@ void OpenSpaceEngine::writeDocumentation() { const bool hasDocumentationType = configurationManager().hasKey(DocumentationType); const bool hasDocumentationFile = configurationManager().hasKey(DocumentationFile); if (hasDocumentationType && hasDocumentationFile) { - std::string documentationType; - configurationManager().getValue(DocumentationType, documentationType); - std::string documentationFile; - configurationManager().getValue(DocumentationFile, documentationFile); - documentationFile = absPath(documentationFile); - DocEng.writeDocumentation(documentationFile, documentationType); + std::string documentationType = configurationManager().value( + DocumentationType + ); + std::string documentationFile = configurationManager().value( + DocumentationFile + ); + + DocEng.writeDocumentation( + absPath(documentationFile), + documentationType + ); } const std::string FactoryDocumentationType = - ConfigurationManager::KeyFactoryDocumentation + '.' + ConfigurationManager::PartType; + ConfigurationManager::KeyFactoryDocumentation + '.' + + ConfigurationManager::PartType; const std::string FactoryDocumentationFile = - ConfigurationManager::KeyFactoryDocumentation + '.' + ConfigurationManager::PartFile; - bool hasFactoryDocumentationType = configurationManager().hasKey(FactoryDocumentationType); - bool hasFactoryDocumentationFile = configurationManager().hasKey(FactoryDocumentationFile); + ConfigurationManager::KeyFactoryDocumentation + '.' + + ConfigurationManager::PartFile; + + bool hasFactoryDocumentationType = configurationManager().hasKey( + FactoryDocumentationType + ); + bool hasFactoryDocumentationFile = configurationManager().hasKey( + FactoryDocumentationFile + ); if (hasFactoryDocumentationType && hasFactoryDocumentationFile) { - std::string type = configurationManager().value(FactoryDocumentationType); - std::string file = configurationManager().value(FactoryDocumentationFile); + std::string type = configurationManager().value( + FactoryDocumentationType + ); + std::string file = configurationManager().value( + FactoryDocumentationFile + ); FactoryManager::ref().writeDocumentation(absPath(file), type); } @@ -615,27 +652,8 @@ void OpenSpaceEngine::gatherCommandlineArguments() { )); } -void OpenSpaceEngine::runScripts(const ghoul::Dictionary& scripts) { - for (size_t i = 1; i <= scripts.size(); ++i) { - std::stringstream stream; - stream << i; - const std::string& key = stream.str(); - const bool hasKey = scripts.hasKeyAndValue(key); - if (!hasKey) { - LERROR("The startup scripts have to be declared in a simple array format." - " Startup scripts did not contain the key '" << key << "'"); - break; - } - - std::string scriptPath; - scripts.getValue(key, scriptPath); - std::string&& absoluteScriptPath = absPath(scriptPath); - _engine->scriptEngine().runScriptFile(absoluteScriptPath); - } -} - - void OpenSpaceEngine::runPreInitializationScripts(const std::string& sceneDescription) { + // @CLEANUP: Move this into the scene loading? ---abock LINFO("Running Initialization scripts"); ghoul::lua::LuaState state; @@ -645,24 +663,29 @@ void OpenSpaceEngine::runPreInitializationScripts(const std::string& sceneDescri ghoul::lua::runScriptFile(state, absPath(sceneDescription)); // Get the preinitialize function - lua_getglobal(state, PreInitializeFunction.c_str()); + lua_getglobal(state, PreInitializeFunction); bool isFunction = lua_isfunction(state, -1); if (!isFunction) { - LERROR("Error executing startup script '" << sceneDescription << "'. Scene '" << - sceneDescription << "' does not have a function '" << - PreInitializeFunction << "'"); + LERROR( + "Error executing startup script '" << sceneDescription << "'. Scene '" << + sceneDescription << "' does not have a function '" << + PreInitializeFunction << "'" + ); return; } // And execute the preinitialize function int success = lua_pcall(state, 0, 0, 0); if (success != 0) { - LERROR("Error executing '" << PreInitializeFunction << "': " << - lua_tostring(state, -1)); + LERROR( + "Error executing '" << PreInitializeFunction << "': " << + lua_tostring(state, -1) + ); } } void OpenSpaceEngine::runPostInitializationScripts(const std::string& sceneDescription) { + // @CLEANUP: Move this into the scene loading? ---abock LINFO("Running Setup scripts"); ghoul::lua::LuaState state; OsEng.scriptEngine().initializeLuaState(state); @@ -671,20 +694,23 @@ void OpenSpaceEngine::runPostInitializationScripts(const std::string& sceneDescr ghoul::lua::runScriptFile(state, absPath(sceneDescription)); // Get the preinitialize function - lua_getglobal(state, PostInitializationFunction.c_str()); + lua_getglobal(state, PostInitializationFunction); bool isFunction = lua_isfunction(state, -1); if (!isFunction) { - LERROR("Error executing startup script '" << sceneDescription << "'. Scene '" << - sceneDescription << "' does not have a function '" << - PostInitializationFunction << "'"); + LERROR( + "Error executing startup script '" << sceneDescription << "'. Scene '" << + sceneDescription << "' does not have a function '" << + PostInitializationFunction << "'" + ); return; } // And execute the preinitialize function int success = lua_pcall(state, 0, 0, 0); if (success != 0) { - LERROR("Error executing '" << PostInitializationFunction << "': " << - lua_tostring(state, -1) + LERROR( + "Error executing '" << PostInitializationFunction << "': " << + lua_tostring(state, -1) ); } } @@ -693,12 +719,11 @@ void OpenSpaceEngine::loadFonts() { ghoul::Dictionary fonts; configurationManager().getValue(ConfigurationManager::KeyFonts, fonts); - const glm::ivec3 fontAtlasSize{1024, 1024, 1}; + glm::ivec3 fontAtlasSize{1024, 1024, 1}; _fontManager = std::make_unique(fontAtlasSize); for (const std::string& key : fonts.keys()) { - std::string font; - fonts.getValue(key, font); + std::string font = fonts.value(key); font = absPath(font); if (!FileSys.fileExists(font)) { @@ -736,7 +761,7 @@ void OpenSpaceEngine::configureLogging() { ConfigurationManager::KeyLogging + '.' + ConfigurationManager::PartLogs; if (configurationManager().hasKeyAndValue(KeyLogLevel)) { - std::string logLevel; + std::string logLevel = "Info"; configurationManager().getValue(KeyLogLevel, logLevel); bool immediateFlush = false; @@ -749,16 +774,15 @@ void OpenSpaceEngine::configureLogging() { level, immediateFlush ? ImmediateFlush::Yes : ImmediateFlush::No ); + LogMgr.addLog(std::make_unique()); } if (configurationManager().hasKeyAndValue(KeyLogs)) { - ghoul::Dictionary logs; - configurationManager().getValue(KeyLogs, logs); + ghoul::Dictionary logs = configurationManager().value(KeyLogs); for (size_t i = 1; i <= logs.size(); ++i) { - ghoul::Dictionary logInfo; - logs.getValue(std::to_string(i), logInfo); + ghoul::Dictionary logInfo = logs.value(std::to_string(i)); try { LogMgr.addLog(createLog(logInfo)); @@ -776,19 +800,24 @@ void OpenSpaceEngine::configureLogging() { #endif // WIN32 #ifndef GHOUL_LOGGING_ENABLE_TRACE - std::string logLevel; + std::string logLevel = "Info"; configurationManager().getValue(KeyLogLevel, logLevel); LogLevel level = ghoul::logging::levelFromString(logLevel); if (level == ghoul::logging::LogLevel::Trace) { - LWARNING("Desired logging level is set to 'Trace' but application was " << - "compiled without Trace support"); + LWARNING( + "Desired logging level is set to 'Trace' but application was " << + "compiled without Trace support" + ); } #endif // GHOUL_LOGGING_ENABLE_TRACE } -bool OpenSpaceEngine::initializeGL() { +void OpenSpaceEngine::initializeGL() { + LTRACE("OpenSpaceEngine::initializeGL(begin)"); + LINFO("Initializing Rendering Engine"); + // @CLEANUP: Remove the return statement and replace with exceptions ---abock bool success = _renderEngine->initializeGL(); for (const auto& func : _moduleCallbacks.initializeGL) { @@ -797,10 +826,12 @@ bool OpenSpaceEngine::initializeGL() { LINFO("Finished initializing OpenGL"); - // If using swapgroups, - LINFO("_windowWrapper->isUsingSwapGroups(): " << _windowWrapper->isUsingSwapGroups()); - LINFO("_windowWrapper->isSwapGroupMaster(): " << _windowWrapper->isSwapGroupMaster()); - return success; + // If using swapgroups, + + LINFO("IsUsingSwapGroups: " << _windowWrapper->isUsingSwapGroups()); + LINFO("IsSwapGroupMaster: " << _windowWrapper->isSwapGroupMaster()); + + LTRACE("OpenSpaceEngine::initializeGL(end)"); } double OpenSpaceEngine::runTime() { @@ -812,6 +843,7 @@ void OpenSpaceEngine::setRunTime(double d) { } void OpenSpaceEngine::preSynchronization() { + LTRACE("OpenSpaceEngine::preSynchronization(begin)"); FileSys.triggerFilesystemEvents(); if (_isFirstRenderingFirstFrame) { @@ -845,17 +877,20 @@ void OpenSpaceEngine::preSynchronization() { for (const auto& func : _moduleCallbacks.preSync) { func(); } + LTRACE("OpenSpaceEngine::preSynchronization(end)"); } void OpenSpaceEngine::postSynchronizationPreDraw() { + LTRACE("OpenSpaceEngine::postSynchronizationPreDraw(begin)"); + bool master = _windowWrapper->isMaster(); _syncEngine->postsync(master); - if (_isInShutdownMode) { - if (_shutdownCountdown <= 0.f) { + if (_shutdown.inShutdown) { + if (_shutdown.timer <= 0.f) { _windowWrapper->terminate(); } - _shutdownCountdown -= _windowWrapper->averageDeltaTime(); + _shutdown.timer -= _windowWrapper->averageDeltaTime(); } _renderEngine->updateSceneGraph(); @@ -893,11 +928,15 @@ void OpenSpaceEngine::postSynchronizationPreDraw() { } LogMgr.resetMessageCounters(); + + LTRACE("OpenSpaceEngine::postSynchronizationPreDraw(end)"); } -void OpenSpaceEngine::render(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix) { +void OpenSpaceEngine::render(const glm::mat4& viewMatrix, + const glm::mat4& projectionMatrix) +{ LTRACE("OpenSpaceEngine::render(begin)"); - _renderEngine->render(projectionMatrix, viewMatrix); + _renderEngine->render(viewMatrix, projectionMatrix); for (const auto& func : _moduleCallbacks.render) { func(); @@ -907,12 +946,16 @@ void OpenSpaceEngine::render(const glm::mat4& projectionMatrix, const glm::mat4& } void OpenSpaceEngine::postDraw() { + LTRACE("OpenSpaceEngine::postDraw(begin)"); + _renderEngine->postDraw(); for (const auto& func : _moduleCallbacks.postDraw) { func(); } + // @CLEANUP: Replace the two windows by a single call to whether a gui should be + // rendered ---abock bool showGui = _windowWrapper->hasGuiWindow() ? _windowWrapper->isGuiWindow() : true; if (showGui) { _renderEngine->renderScreenLog(); @@ -920,8 +963,8 @@ void OpenSpaceEngine::postDraw() { _console->render(); } - if (_isInShutdownMode) { - _renderEngine->renderShutdownInformation(_shutdownCountdown, _shutdownWait); + if (_shutdown.inShutdown) { + _renderEngine->renderShutdownInformation(_shutdown.timer, _shutdown.waitTime); } if (_isFirstRenderingFirstFrame) { @@ -929,6 +972,7 @@ void OpenSpaceEngine::postDraw() { _isFirstRenderingFirstFrame = false; } + LTRACE("OpenSpaceEngine::postDraw(end)"); } void OpenSpaceEngine::keyboardCallback(Key key, KeyModifier mod, KeyAction action) { @@ -939,11 +983,15 @@ void OpenSpaceEngine::keyboardCallback(Key key, KeyModifier mod, KeyAction actio } } + // @CLEANUP: Remove the commandInputButton and replace with a method just based + // on Lua by binding a key to the Lua script toggling the console ---abock if (key == _console->commandInputButton()) { if (action == KeyAction::Press) { _console->toggleMode(); } } else if (!_console->isVisible()) { + // @CLEANUP: Make the interaction handler return whether a key has been consumed + // and then pass it on to the console ---abock _interactionHandler->keyboardCallback(key, mod, action); } else { _console->keyboardCallback(key, mod, action); @@ -1015,16 +1063,16 @@ void OpenSpaceEngine::externalControlCallback(const char* receivedChars, int siz } void OpenSpaceEngine::toggleShutdownMode() { - if (_isInShutdownMode) { - // If we are already in shutdown mode, we want to disable it instead + if (_shutdown.inShutdown) { + // If we are already in shutdown mode, we want to disable it LINFO("Disabled shutdown mode"); - _isInShutdownMode = false; + _shutdown.inShutdown = false; } else { - // Else, we hav eto enable it + // Else, we have to enable it LINFO("Shutting down OpenSpace"); - _shutdownCountdown = _shutdownWait; - _isInShutdownMode = true; + _shutdown.timer = _shutdown.waitTime; + _shutdown.inShutdown = true; } } @@ -1049,14 +1097,6 @@ scripting::LuaLibrary OpenSpaceEngine::luaLibrary() { }; } -bool OpenSpaceEngine::useBusyWaitForDecode() { - return _settingsEngine->busyWaitForDecode(); -} - -bool OpenSpaceEngine::logSGCTOutOfOrderErrors() { - return _settingsEngine->logSGCTOutOfOrderErrors(); -} - void OpenSpaceEngine::enableBarrier() { _windowWrapper->setBarrier(true); } @@ -1096,6 +1136,21 @@ void OpenSpaceEngine::registerModuleMouseScrollWheelCallback( _moduleCallbacks.mouseScrollWheel.push_back(std::move(function)); } +ConfigurationManager& OpenSpaceEngine::configurationManager() { + ghoul_assert(_configurationManager, "ConfigurationManager must not be nullptr"); + return *_configurationManager; +} + +LuaConsole& OpenSpaceEngine::console() { + ghoul_assert(_console, "LuaConsole must not be nullptr"); + return *_console; +} + +DownloadManager& OpenSpaceEngine::downloadManager() { + ghoul_assert(_downloadManager, "Download Manager must not be nullptr"); + return *_downloadManager; +} + NetworkEngine& OpenSpaceEngine::networkEngine() { ghoul_assert(_networkEngine, "NetworkEngine must not be nullptr"); return *_networkEngine; @@ -1106,9 +1161,34 @@ ModuleEngine& OpenSpaceEngine::moduleEngine() { return *_moduleEngine; } -ConfigurationManager& OpenSpaceEngine::configurationManager() { - ghoul_assert(_configurationManager, "ConfigurationManager must not be nullptr"); - return *_configurationManager; +ParallelConnection& OpenSpaceEngine::parallelConnection() { + ghoul_assert(_parallelConnection, "ParallelConnection must not be nullptr"); + return *_parallelConnection; +} + +RenderEngine& OpenSpaceEngine::renderEngine() { + ghoul_assert(_renderEngine, "RenderEngine must not be nullptr"); + return *_renderEngine; +} + +SettingsEngine& OpenSpaceEngine::settingsEngine() { + ghoul_assert(_settingsEngine, "Settings Engine must not be nullptr"); + return *_settingsEngine; +} + +TimeManager& OpenSpaceEngine::timeManager() { + ghoul_assert(_timeManager, "Download Manager must not be nullptr"); + return *_timeManager; +} + +WindowWrapper& OpenSpaceEngine::windowWrapper() { + ghoul_assert(_windowWrapper, "Window Wrapper must not be nullptr"); + return *_windowWrapper; +} + +ghoul::fontrendering::FontManager& OpenSpaceEngine::fontManager() { + ghoul_assert(_fontManager, "Font Manager must not be nullptr"); + return *_fontManager; } interaction::InteractionHandler& OpenSpaceEngine::interactionHandler() { @@ -1116,31 +1196,6 @@ interaction::InteractionHandler& OpenSpaceEngine::interactionHandler() { return *_interactionHandler; } -RenderEngine& OpenSpaceEngine::renderEngine() { - ghoul_assert(_renderEngine, "RenderEngine must not be nullptr"); - return *_renderEngine; -} - -ScriptEngine& OpenSpaceEngine::scriptEngine() { - ghoul_assert(_scriptEngine, "ScriptEngine must not be nullptr"); - return *_scriptEngine; -} - -ScriptScheduler& OpenSpaceEngine::scriptScheduler(){ - ghoul_assert(_scriptScheduler, "ScriptScheduler must not be nullptr"); - return *_scriptScheduler; -} - -LuaConsole& OpenSpaceEngine::console() { - ghoul_assert(_console, "LuaConsole must not be nullptr"); - return *_console; -} - -ParallelConnection& OpenSpaceEngine::parallelConnection() { - ghoul_assert(_parallelConnection, "ParallelConnection must not be nullptr"); - return *_parallelConnection; -} - properties::PropertyOwner& OpenSpaceEngine::globalPropertyOwner() { ghoul_assert( _globalPropertyNamespace, @@ -1149,29 +1204,14 @@ properties::PropertyOwner& OpenSpaceEngine::globalPropertyOwner() { return *_globalPropertyNamespace; } -WindowWrapper& OpenSpaceEngine::windowWrapper() { - ghoul_assert(_windowWrapper, "Window Wrapper must not be nullptr"); - return *_windowWrapper; -} - -ghoul::fontrendering::FontManager& OpenSpaceEngine::fontManager() { - ghoul_assert(_fontManager, "Font Manager must not be nullptr"); - return *_fontManager; +ScriptEngine& OpenSpaceEngine::scriptEngine() { + ghoul_assert(_scriptEngine, "ScriptEngine must not be nullptr"); + return *_scriptEngine; } -DownloadManager& OpenSpaceEngine::downloadManager() { - ghoul_assert(_downloadManager, "Download Manager must not be nullptr"); - return *_downloadManager; -} - -TimeManager& OpenSpaceEngine::timeManager() { - ghoul_assert(_timeManager, "Download Manager must not be nullptr"); - return *_timeManager; -} - -SettingsEngine& OpenSpaceEngine::settingsEngine() { - ghoul_assert(_settingsEngine, "Settings Engine must not be nullptr"); - return *_settingsEngine; +ScriptScheduler& OpenSpaceEngine::scriptScheduler() { + ghoul_assert(_scriptScheduler, "ScriptScheduler must not be nullptr"); + return *_scriptScheduler; } } // namespace openspace diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 5a9b5082ff..b19a2cfcff 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -468,7 +468,8 @@ void RenderEngine::updateFade() { } } -void RenderEngine::render(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix){ +void RenderEngine::render(const glm::mat4& viewMatrix, const glm::mat4& projectionMatrix) +{ LTRACE("RenderEngine::render(begin)"); _mainCamera->sgctInternal.setViewMatrix(viewMatrix); _mainCamera->sgctInternal.setProjectionMatrix(projectionMatrix); diff --git a/tests/main.cpp b/tests/main.cpp index df340ba31b..354b19dca7 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -83,7 +83,8 @@ namespace { int main(int argc, char** argv) { std::vector args; - openspace::OpenSpaceEngine::create(argc, argv, std::make_unique(), args); + bool close; + openspace::OpenSpaceEngine::create(argc, argv, std::make_unique(), args, close); testing::InitGoogleTest(&argc, argv);