changeset 613:412a2d01a189

automatic initialization of glew
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Apr 2019 16:44:23 +0200
parents ba72bffceb7d
children 4ec32d4d0867
files Applications/Sdl/SdlOpenGLWindow.cpp Framework/StoneInitialization.cpp Resources/CMake/GlewConfiguration.cmake Resources/CMake/OrthancStoneConfiguration.cmake Resources/CMake/OrthancStoneParameters.cmake Resources/CMake/SdlConfiguration.cmake Samples/Sdl/BasicScene.cpp
diffstat 7 files changed, 109 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/Sdl/SdlOpenGLWindow.cpp	Tue Apr 30 16:18:46 2019 +0200
+++ b/Applications/Sdl/SdlOpenGLWindow.cpp	Tue Apr 30 16:44:23 2019 +0200
@@ -23,10 +23,21 @@
 
 #if ORTHANC_ENABLE_SDL == 1
 
+#if !defined(ORTHANC_ENABLE_GLEW)
+#  error Macro ORTHANC_ENABLE_GLEW must be defined
+#endif
+
+#if ORTHANC_ENABLE_GLEW == 1
+#  include <GL/glew.h>
+#endif
+
 #include <Core/OrthancException.h>
 
 namespace OrthancStone
 {
+  static boost::mutex  globalMutex_;
+  static bool          globalIsGlewInitialized_ = false;
+  
   SdlOpenGLWindow::SdlOpenGLWindow(const char* title,
                                    unsigned int width,
                                    unsigned int height) :
@@ -39,6 +50,29 @@
       throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
                                       "Cannot initialize OpenGL");
     }
+
+#if ORTHANC_ENABLE_GLEW == 1
+    // The initialization function of glew (i.e. "glewInit()") can
+    // only be called once an OpenGL is setup.
+    // https://stackoverflow.com/a/45033669/881731
+    {
+      boost::mutex::scoped_lock lock(globalMutex_);
+
+      if (!globalIsGlewInitialized_)
+      {
+        LOG(INFO) << "Initializing glew";
+        
+        GLenum err = glewInit();
+        if (GLEW_OK != err)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
+                                          "Cannot initialize glew");
+        }
+
+        globalIsGlewInitialized_ = true;
+      }
+    }    
+#endif
   }
 
   
--- a/Framework/StoneInitialization.cpp	Tue Apr 30 16:18:46 2019 +0200
+++ b/Framework/StoneInitialization.cpp	Tue Apr 30 16:44:23 2019 +0200
@@ -28,37 +28,16 @@
 #  error Macro ORTHANC_ENABLE_SDL must be defined
 #endif
 
-
 #if ORTHANC_ENABLE_SDL == 1
 #  include "../Applications/Sdl/SdlWindow.h"
 #endif
 
-#if ORTHANC_ENABLE_OPENGL == 1
-#  include "GL/glew.h"
-#endif
-
 namespace OrthancStone
 {
   void StoneInitialize()
   {
     Orthanc::Logging::Initialize();
 
-#if 0
-#if ORTHANC_ENABLE_OPENGL == 1
-    glEnable(GL_DEBUG_OUTPUT);
-    GLenum err = glewInit();
-    if (GLEW_OK != err)
-    {
-      const char* error =
-        reinterpret_cast<const char*>(glewGetErrorString(err));
-      std::stringstream message;
-      message << "Using GLEW version " << reinterpret_cast<const char*>(
-        glewGetString(GLEW_VERSION));
-      LOG(INFO) << "Using GLEW version " << message.str();
-    }
-#endif
-#endif
-
 #if ORTHANC_ENABLE_SDL == 1
     OrthancStone::SdlWindow::GlobalInitialize();
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/CMake/GlewConfiguration.cmake	Tue Apr 30 16:44:23 2019 +0200
@@ -0,0 +1,44 @@
+# Stone of Orthanc
+# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+# Department, University Hospital of Liege, Belgium
+# Copyright (C) 2017-2019 Osimis S.A., Belgium
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public License
+# as published by the Free Software Foundation, either version 3 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+if (STATIC_BUILD OR NOT USE_SYSTEM_GLEW)
+  SET(GLEW_SOURCES_DIR ${CMAKE_BINARY_DIR}/glew-2.1.0)
+  SET(GLEW_URL "http://orthanc.osimis.io/ThirdPartyDownloads/glew-2.1.0.tgz")
+  SET(GLEW_MD5 "b2ab12331033ddfaa50dc39345343980")
+  DownloadPackage(${GLEW_MD5} ${GLEW_URL} "${GLEW_SOURCES_DIR}")
+
+  set(GLEW_SOURCES
+    ${GLEW_SOURCES_DIR}/src/glew.c
+    )
+
+  include_directories(${GLEW_SOURCES_DIR}/include)
+
+  add_definitions(
+    -DGLEW_STATIC=1
+    )
+
+else()
+  include(FindGLEW)
+  if (NOT GLEW_FOUND)
+    message(FATAL_ERROR "Please install the libglew-dev package")
+  endif()
+  
+  include_directories(${GLEW_INCLUDE_DIRS})
+  link_libraries(${GLEW_LIBRARIES})
+endif()
--- a/Resources/CMake/OrthancStoneConfiguration.cmake	Tue Apr 30 16:18:46 2019 +0200
+++ b/Resources/CMake/OrthancStoneConfiguration.cmake	Tue Apr 30 16:44:23 2019 +0200
@@ -88,21 +88,39 @@
 elseif(ENABLE_SDL)
   message("SDL is enabled")
   include(${CMAKE_CURRENT_LIST_DIR}/SdlConfiguration.cmake)
-  add_definitions(-DORTHANC_ENABLE_NATIVE=1)
-  add_definitions(-DORTHANC_ENABLE_QT=0)
-  add_definitions(-DORTHANC_ENABLE_SDL=1)
+  add_definitions(
+    -DORTHANC_ENABLE_NATIVE=1
+    -DORTHANC_ENABLE_QT=0
+    -DORTHANC_ENABLE_SDL=1
+    )
 elseif(ENABLE_QT)
   message("QT is enabled")
   include(${CMAKE_CURRENT_LIST_DIR}/QtConfiguration.cmake)
-  add_definitions(-DORTHANC_ENABLE_NATIVE=1)
-  add_definitions(-DORTHANC_ENABLE_QT=1)
-  add_definitions(-DORTHANC_ENABLE_SDL=0)
+  add_definitions(
+    -DORTHANC_ENABLE_NATIVE=1
+    -DORTHANC_ENABLE_QT=1
+    -DORTHANC_ENABLE_SDL=0
+    )
 else()
   message("SDL and QT are both disabled")
   unset(USE_SYSTEM_SDL CACHE)
-  add_definitions(-DORTHANC_ENABLE_SDL=0)
-  add_definitions(-DORTHANC_ENABLE_QT=0)
-  add_definitions(-DORTHANC_ENABLE_NATIVE=0)
+  add_definitions(
+    -DORTHANC_ENABLE_SDL=0
+    -DORTHANC_ENABLE_QT=0
+    -DORTHANC_ENABLE_NATIVE=0
+    )
+endif()
+
+
+if (ENABLE_OPENGL AND CMAKE_SYSTEM_NAME STREQUAL "Windows")
+  include(${CMAKE_CURRENT_LIST_DIR}/GlewConfiguration.cmake)
+  add_definitions(
+    -DORTHANC_ENABLE_GLEW=1
+    )
+else()
+  add_definitions(
+    -DORTHANC_ENABLE_GLEW=0
+    )
 endif()
 
 
@@ -400,6 +418,7 @@
   ${SDL_SOURCES}
   ${QT_SOURCES}
   ${BOOST_EXTENDED_SOURCES}
+  ${GLEW_SOURCES}
   )
 
 
--- a/Resources/CMake/OrthancStoneParameters.cmake	Tue Apr 30 16:18:46 2019 +0200
+++ b/Resources/CMake/OrthancStoneParameters.cmake	Tue Apr 30 16:44:23 2019 +0200
@@ -42,6 +42,7 @@
 # Advanced parameters to fine-tune linking against system libraries
 set(USE_SYSTEM_CAIRO ON CACHE BOOL "Use the system version of Cairo")
 set(USE_SYSTEM_FREETYPE ON CACHE BOOL "Use the system version of Freetype")
+set(USE_SYSTEM_GLEW ON CACHE BOOL "Use the system version of glew (for Windows only)")
 set(USE_SYSTEM_PIXMAN ON CACHE BOOL "Use the system version of Pixman")
 set(USE_SYSTEM_SDL ON CACHE BOOL "Use the system version of SDL2")
 
--- a/Resources/CMake/SdlConfiguration.cmake	Tue Apr 30 16:18:46 2019 +0200
+++ b/Resources/CMake/SdlConfiguration.cmake	Tue Apr 30 16:44:23 2019 +0200
@@ -23,14 +23,6 @@
   SET(SDL_MD5 "44fc4a023349933e7f5d7a582f7b886e")
   DownloadPackage(${SDL_MD5} ${SDL_URL} "${SDL_SOURCES_DIR}")
 
-  if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
-    SET(GLEW_SOURCES_DIR ${CMAKE_BINARY_DIR}/glew-2.1.0)
-    SET(GLEW_URL "http://orthanc.osimis.io/ThirdPartyDownloads/glew-2.1.0.tgz")
-    SET(GLEW_MD5 "b2ab12331033ddfaa50dc39345343980")
-    DownloadPackage(${GLEW_MD5} ${GLEW_URL} "${GLEW_SOURCES_DIR}")
-  endif()
-  
-
   include_directories(${SDL_SOURCES_DIR}/include)
 
   set(TMP "${SDL_SOURCES_DIR}/include/SDL_config_premake.h")
@@ -157,12 +149,8 @@
       ${SDL_SOURCES_DIR}/src/timer/windows/*.c
       ${SDL_SOURCES_DIR}/src/video/windows/*.c
       ${SDL_SOURCES_DIR}/src/windows/dlopen/*.c
-
-      ${GLEW_SOURCES_DIR}/src/glew.c
       )
 
-    include_directories(${GLEW_SOURCES_DIR}/include)
-
     list(APPEND SDL_SOURCES ${TMP})
 
     # NB: OpenGL ES headers are not available in MinGW-W64
@@ -177,8 +165,6 @@
       -DSDL_VIDEO_RENDER_D3D=1
       -DSDL_VIDEO_RENDER_OGL=1
       -DSDL_VIDEO_DRIVER_WINDOWS=1
-
-      -DGLEW_STATIC=1
       )
 
     if (MSVC)
--- a/Samples/Sdl/BasicScene.cpp	Tue Apr 30 16:18:46 2019 +0200
+++ b/Samples/Sdl/BasicScene.cpp	Tue Apr 30 16:44:23 2019 +0200
@@ -274,20 +274,12 @@
 
 void Run(OrthancStone::Scene2D& scene)
 {
-
   OrthancStone::SdlOpenGLWindow window("Hello", 1024, 768);
 
-  GLenum err = glewInit();
-  if (GLEW_OK != err)
-  {
-    throw std::runtime_error("GLEW_OK != err");
-  }
-
   scene.FitContent(window.GetCanvasWidth(), window.GetCanvasHeight());
-
   
   glEnable(GL_DEBUG_OUTPUT);
-  glDebugMessageCallback(OpenGLMessageCallback, 0 );
+  glDebugMessageCallback(OpenGLMessageCallback, 0);
 
   OrthancStone::OpenGLCompositor compositor(window, scene);
   compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, 
@@ -368,6 +360,7 @@
 int main(int argc, char* argv[])
 {
   OrthancStone::StoneInitialize();
+  Orthanc::Logging::EnableInfoLevel(true);
 
   try
   {