From fc3faab5f1f1593c25ca72ee0831f95f0f43e222 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 28 May 2015 20:33:22 +0200 Subject: [PATCH] Replace commandline argument with automatically detecting the supported OpenGL version --- include/openspace/engine/openspaceengine.h | 2 +- src/engine/openspaceengine.cpp | 15 +------- src/main.cpp | 44 ++++++++++++++-------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index 11f1eee190..08a25948d9 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -59,7 +59,7 @@ namespace scripting { class OpenSpaceEngine { public: - static bool create(int argc, char** argv, std::vector& sgctArguments, std::string& openGlVersion); + static bool create(int argc, char** argv, std::vector& sgctArguments); static void destroy(); static OpenSpaceEngine& ref(); diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 7197d9249d..d38555a20e 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -96,7 +96,6 @@ namespace { struct { std::string configurationName; std::string sgctConfigurationName; - std::string openGlVersion; } commandlineArgumentPlaceholders; } @@ -148,8 +147,7 @@ OpenSpaceEngine& OpenSpaceEngine::ref() { bool OpenSpaceEngine::create( int argc, char** argv, - std::vector& sgctArguments, - std::string& openGlVersion) + std::vector& sgctArguments) { ghoul::initialize(); @@ -260,10 +258,6 @@ bool OpenSpaceEngine::create( sgctConfigurationPath = commandlineArgumentPlaceholders.sgctConfigurationName; } - openGlVersion = commandlineArgumentPlaceholders.openGlVersion; - if (openGlVersion != DefaultOpenGlVersion) - LINFO("Using OpenGL version " << openGlVersion); - // 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]); @@ -401,13 +395,6 @@ bool OpenSpaceEngine::gatherCommandlineArguments() { "the OpenSpace configuration file"); _commandlineParser->addCommand(sgctConfigFileCommand); - commandlineArgumentPlaceholders.openGlVersion = DefaultOpenGlVersion; - CommandlineCommand* openGlVersionCommand = new SingleCommand( - &commandlineArgumentPlaceholders.openGlVersion, - "-ogl", "-o", - "Sets the OpenGL version that is to be used; valid values are '4.2' and '4.3'"); - _commandlineParser->addCommand(openGlVersionCommand); - return true; } diff --git a/src/main.cpp b/src/main.cpp index 7adc2126a2..117e7d435f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,21 @@ void mainDecodeFun(); void mainExternalControlCallback(const char * receivedChars, int size); void mainLogCallback(const char* msg); +std::pair supportedOpenGLVersion () { + glfwInit(); + glfwWindowHint(GLFW_VISIBLE, GL_FALSE); + GLFWwindow* offscreen = glfwCreateWindow(128, 128, "", nullptr, nullptr); + glfwMakeContextCurrent(offscreen); + + int major, minor; + glGetIntegerv(GL_MAJOR_VERSION, &major); + glGetIntegerv(GL_MINOR_VERSION, &minor); + + glfwDestroyWindow(offscreen); + glfwWindowHint(GLFW_VISIBLE, GL_TRUE); + return { major, minor }; +} + //temporary post-FX functions, TODO make a more permanent solution to this @JK void postFXPass(); void setupPostFX(); @@ -60,13 +75,13 @@ namespace { } int main(int argc, char** argv) { + auto glVersion = supportedOpenGLVersion(); + // create the OpenSpace engine and get arguments for the sgct engine std::vector sgctArguments; - std::string openGlVersion = ""; const bool success = openspace::OpenSpaceEngine::create( argc, argv, - sgctArguments, - openGlVersion + sgctArguments ); if (!success) return EXIT_FAILURE; @@ -115,21 +130,20 @@ int main(int argc, char** argv) { // try to open a window LDEBUG("Initialize SGCT Engine"); -#ifdef __APPLE__ - sgct::Engine::RunMode rm = sgct::Engine::RunMode::OpenGL_4_1_Core_Profile; -#else - std::map versionMapping = { - { "4.2", sgct::Engine::RunMode::OpenGL_4_2_Core_Profile }, - { "4.3", sgct::Engine::RunMode::OpenGL_4_3_Core_Profile }, - { "4.4", sgct::Engine::RunMode::OpenGL_4_4_Core_Profile }, - { "4.5", sgct::Engine::RunMode::OpenGL_4_5_Core_Profile } + std::map, sgct::Engine::RunMode> versionMapping = { + { { 3, 3 }, sgct::Engine::RunMode::OpenGL_3_3_Core_Profile }, + { { 4, 0 }, sgct::Engine::RunMode::OpenGL_4_0_Core_Profile }, + { { 4, 1 }, sgct::Engine::RunMode::OpenGL_4_1_Core_Profile }, + { { 4, 2 }, sgct::Engine::RunMode::OpenGL_4_2_Core_Profile }, + { { 4, 3 }, sgct::Engine::RunMode::OpenGL_4_3_Core_Profile }, + { { 4, 4 }, sgct::Engine::RunMode::OpenGL_4_4_Core_Profile }, + { { 4, 5 }, sgct::Engine::RunMode::OpenGL_4_5_Core_Profile } }; - if (versionMapping.find(openGlVersion) == versionMapping.end()) { - LFATAL("Requested OpenGL version " << openGlVersion << " not supported"); + if (versionMapping.find(glVersion) == versionMapping.end()) { + LFATAL("Requested OpenGL version " << glVersion.first << "." << glVersion.second << " not supported"); return EXIT_FAILURE; } - sgct::Engine::RunMode rm = versionMapping[openGlVersion]; -#endif + sgct::Engine::RunMode rm = versionMapping[glVersion]; const bool initSuccess = _sgctEngine->init(rm); if (!initSuccess) { LFATAL("Initializing failed");