Browse Source

Transition OpenGL loader to glad.

Former-commit-id: fe6520dab52910cc7e571d2458e0d9b9d9f28fa5
Jérémie Dumas 7 years ago
parent
commit
0448a3e3b6

+ 6 - 25
include/igl/opengl/gl.h

@@ -1,9 +1,9 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2013, 2017 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #ifndef IGL_OPENGL_GL_H
 #define IGL_OPENGL_GL_H
@@ -16,29 +16,10 @@
 //     #include "gl.h"
 // Instead of:
 //     #include <OpenGL/gl3.h>
-// or 
+// or
 //     #include <GL/gl.h>
 //
 
-// For now this includes glu, glew and glext (perhaps these should be
-// separated)
-#ifdef _WIN32
-#    define NOMINMAX
-#    include <Windows.h>
-#    undef DrawText
-#    undef NOMINMAX
-#endif
-
-#ifndef __APPLE__
-#  define GLEW_STATIC
-#  include <GL/glew.h>
-#endif
-
-#ifdef __APPLE__
-#  include <OpenGL/gl3.h>
-#  define __gl_h_ /* Prevent inclusion of the old gl.h */
-#else
-#  include <GL/gl.h>
-#endif
+#include <glad/glad.h>
 
 #endif

+ 13 - 41
include/igl/opengl/glfw/Viewer.cpp

@@ -10,36 +10,12 @@
 #define IGL_VIEWER_VIEWER_CPP
 #include "Viewer.h"
 
-#ifdef _WIN32
-#  include <windows.h>
-#  undef max
-#  undef min
-#endif
-
 #include <chrono>
 #include <thread>
 
-#ifndef __APPLE__
-#  define GLEW_STATIC
-#  include <GL/glew.h>
-#endif
-
-#ifdef __APPLE__
-#   include <OpenGL/gl3.h>
-#   define __gl_h_ /* Prevent inclusion of the old gl.h */
-#else
-#   include <GL/gl.h>
-#endif
-
 #include <Eigen/LU>
 
-//#define GLFW_INCLUDE_GLU
-#if defined(__APPLE__)
-#define GLFW_INCLUDE_GLCOREARB
-#else
-#define GL_GLEXT_PROTOTYPES
-#endif
-
+#include "../gl.h"
 #include <GLFW/glfw3.h>
 
 #include <cmath>
@@ -242,23 +218,19 @@ namespace glfw
       return EXIT_FAILURE;
     }
     glfwMakeContextCurrent(window);
-    #ifndef __APPLE__
-      glewExperimental = true;
-      GLenum err = glewInit();
-      if(GLEW_OK != err)
-      {
-        /* Problem: glewInit failed, something is seriously wrong. */
-       fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
-      }
-      glGetError(); // pull and savely ignonre unhandled errors like GL_INVALID_ENUM
-      fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
-    #endif
+    // Load OpenGL and its extensions
+    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
+    {
+      printf("Failed to load OpenGL and its extensions");
+      return(-1);
+    }
+    printf("OpenGL Version %d.%d loaded", GLVersion.major, GLVersion.minor);
     #if defined(DEBUG) || defined(_DEBUG)
       int major, minor, rev;
       major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
       minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
       rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
-      printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
+      printf("OpenGL version received: %d.%d.%d\n", major, minor, rev);
       printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
       printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
     #endif
@@ -624,9 +596,9 @@ namespace glfw
       Eigen::MatrixXd UV_V;
       Eigen::MatrixXi UV_F;
 
-      return igl::writeOBJ(mesh_file_name_string, 
+      return igl::writeOBJ(mesh_file_name_string,
           data().V,
-          data().F, 
+          data().F,
           corner_normals, fNormIndices, UV_V, UV_F);
     }
     else
@@ -709,7 +681,7 @@ namespace glfw
       case '<':
       case '>':
       {
-        selected_data_index = 
+        selected_data_index =
           (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
         return true;
       }
@@ -1086,7 +1058,7 @@ namespace glfw
   {
     assert(!data_list.empty() && "data_list should never be empty");
     assert(
-      (selected_data_index >= 0 && selected_data_index < data_list.size()) && 
+      (selected_data_index >= 0 && selected_data_index < data_list.size()) &&
       "selected_data_index should be in bounds");
     return data_list[selected_data_index];
   }

+ 4 - 5
include/igl/opengl/glfw/Viewer.h

@@ -12,7 +12,6 @@
 #define IGL_OPENGL_4
 #endif
 
-
 #include "../../igl_inline.h"
 #include "../MeshGL.h"
 #include "../ViewerCore.h"
@@ -88,7 +87,7 @@ namespace glfw
     // the data_list and opengl_state_list.
     //
     // Returns number of meshes (always >= 1)
-    // 
+    //
     // Side Effects:
     //   selected_data_index is set this newly created, last entry (i.e.,
     //   #meshes-1)
@@ -99,7 +98,7 @@ namespace glfw
     // Inputs:
     //   index  index of mesh to erase
     // Returns whether erasure was successful <=> cannot erase last mesh
-    // 
+    //
     // Side Effects:
     //   If selected_data_index is greater than or equal to index then it is
     //   decremented
@@ -110,12 +109,12 @@ namespace glfw
     //   viewer.data().clear();
     //
     IGL_INLINE bool erase_mesh(const size_t index);
-  
+
     // Alec: I call this data_list instead of just data to avoid confusion with
     // old "data" variable.
     // Stores all the data that should be visualized
     std::vector<ViewerData> data_list;
-  
+
     size_t selected_data_index;
     GLFWwindow* window;
     // Stores all the viewing options

+ 5 - 10
include/igl/opengl/glfw/background_window.cpp

@@ -17,16 +17,11 @@ IGL_INLINE bool igl::opengl::glfw::background_window(GLFWwindow* & window)
   window = glfwCreateWindow(1, 1,"", NULL, NULL);
   if(!window) return false;
   glfwMakeContextCurrent(window);
-  #ifndef __APPLE__
-    glewExperimental = true;
-    GLenum err = glewInit();
-    if(GLEW_OK != err)
-    {
-      /* Problem: glewInit failed, something is seriously wrong. */
-     fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
-    }
-    glGetError(); // pull and safely ignore unhandled errors like GL_INVALID_ENUM
-  #endif
+  if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
+  {
+    printf("Failed to load OpenGL and its extensions");
+  }
+  glGetError(); // pull and safely ignore unhandled errors like GL_INVALID_ENUM
   return true;
 }
 

+ 2 - 3
include/igl/opengl/glfw/background_window.h

@@ -1,9 +1,8 @@
 #ifndef IGL_OPENGL_GLFW_BACKGROUND_WINDOW_H
 #define IGL_OPENGL_GLFW_BACKGROUND_WINDOW_H
 #include "../../igl_inline.h"
-#include "../gl.h"
 
-#define GLFW_INCLUDE_GLU
+#include "../gl.h"
 #include <GLFW/glfw3.h>
 
 namespace igl
@@ -13,7 +12,7 @@ namespace igl
     namespace glfw
     {
       // Create a background window with a valid core profile opengl context
-      // set to current. 
+      // set to current.
       //
       // After you're finished with this window you may call
       // `glfwDestroyWindow(window)`

+ 12 - 9
include/igl/opengl/glfw/map_texture.cpp

@@ -1,14 +1,15 @@
+#ifdef IGL_OPENGL_4
+
 #include "map_texture.h"
 #include "background_window.h"
 #include "../create_shader_program.h"
-#include "../gl.h"
 
-#define GLFW_INCLUDE_GLU
+#include "../gl.h"
 #include <GLFW/glfw3.h>
 
 #include <iostream>
 #include <string>
-  
+
 template <typename DerivedV, typename DerivedF, typename DerivedU>
 IGL_INLINE bool igl::opengl::glfw::map_texture(
   const Eigen::MatrixBase<DerivedV> & _V,
@@ -26,7 +27,7 @@ IGL_INLINE bool igl::opengl::glfw::map_texture(
   return map_texture(_V,_F,_U,in_data,w,h,nc,out_data,out_w,out_h,out_nc);
 }
 
-  
+
 template <typename DerivedV, typename DerivedF, typename DerivedU>
 IGL_INLINE bool igl::opengl::glfw::map_texture(
   const Eigen::MatrixBase<DerivedV> & _V,
@@ -47,7 +48,7 @@ IGL_INLINE bool igl::opengl::glfw::map_texture(
     glfwTerminate();
     return false;
   };
-  // Force inputs to be RowMajor at the cost of a copy 
+  // Force inputs to be RowMajor at the cost of a copy
   Eigen::Matrix<
     double,
     DerivedV::RowsAtCompileTime,
@@ -71,7 +72,7 @@ IGL_INLINE bool igl::opengl::glfw::map_texture(
   }
 
   // Compile each shader
-  std::string vertex_shader = dim == 2 ? 
+  std::string vertex_shader = dim == 2 ?
     R"(
 #version 330 core
 layout(location = 0) in vec2 position;
@@ -106,7 +107,7 @@ void main()
   color = texture(tex,tex_coord_f).rgb;
 }
 )";
-  GLuint prog_id = 
+  GLuint prog_id =
     igl::opengl::create_shader_program(vertex_shader,fragment_shader,{});
   glUniform1i(glGetUniformLocation(prog_id, "tex"),0);
   // Generate and attach buffers to vertex array
@@ -129,7 +130,7 @@ void main()
   glBufferData(GL_ARRAY_BUFFER, sizeof(double)*V.size(), V.data(), GL_STATIC_DRAW);
   glVertexAttribLPointer(1, V.cols(), GL_DOUBLE, V.cols() * sizeof(GLdouble), (GLvoid*)0);
   glBindVertexArray(0);
-  glBindBuffer(GL_ARRAY_BUFFER, 0); 
+  glBindBuffer(GL_ARRAY_BUFFER, 0);
   glBindVertexArray(0);
   // Prepare texture
   GLuint in_tex;
@@ -162,7 +163,7 @@ void main()
     GLenum bufs[1] = {GL_COLOR_ATTACHMENT0};
     glDrawBuffers(1, bufs);
   }
-  if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
+  if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
   {
     fail("framebuffer setup failed.");
   }
@@ -206,3 +207,5 @@ void main()
 // generated by autoexplicit.sh
 template bool igl::opengl::glfw::map_texture<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, -1, 1, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, unsigned char const*, int, int, int, std::vector<unsigned char, std::allocator<unsigned char> >&);
 #endif
+
+#endif // IGL_OPENGL_4

+ 4 - 0
include/igl/opengl/glfw/map_texture.h

@@ -1,6 +1,8 @@
 #ifndef IGL_OPENGL_GLFW_MAP_TEXTURE_H
 #define IGL_OPENGL_GLFW_MAP_TEXTURE_H
 
+#ifdef IGL_OPENGL_4
+
 #include "../../igl_inline.h"
 #include <Eigen/Core>
 #include <vector>
@@ -56,4 +58,6 @@ namespace igl
 #  include "map_texture.cpp"
 #endif
 
+#endif // IGL_OPENGL_4
+
 #endif

+ 6 - 25
include/igl/opengl2/gl.h

@@ -1,9 +1,9 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #ifndef IGL_OPENGL2_GL_H
 #define IGL_OPENGL2_GL_H
@@ -16,29 +16,10 @@
 //     #include "gl.h"
 // Instead of:
 //     #include <OpenGL/gl.h>
-// or 
+// or
 //     #include <GL/gl.h>
 //
 
-// For now this includes glu, glew and glext (perhaps these should be
-// separated)
-#ifdef _WIN32
-#    define NOMINMAX
-#    include <Windows.h>
-#    undef DrawText
-#    undef NOMINMAX
-#endif
-
-#ifndef __APPLE__
-#  define GLEW_STATIC
-#  include <GL/glew.h>
-#endif
-
-#ifdef __APPLE__
-#  include <OpenGL/gl.h>
-#  define __gl_h_ /* Prevent inclusion of the old gl.h */
-#else
-#  include <GL/gl.h>
-#endif
+#include <glad/glad.h>
 
 #endif

+ 12 - 36
shared/cmake/libigl.cmake

@@ -10,7 +10,6 @@ option(LIBIGL_WITH_EMBREE           "Use Embree"         OFF)
 option(LIBIGL_WITH_LIM              "Use LIM"            ON)
 option(LIBIGL_WITH_MATLAB           "Use Matlab"         ON)
 option(LIBIGL_WITH_MOSEK            "Use MOSEK"          ON)
-option(LIBIGL_WITH_NANOGUI          "Use Nanogui menu"   OFF)
 option(LIBIGL_WITH_OPENGL           "Use OpenGL"         ON)
 option(LIBIGL_WITH_OPENGL_GLFW      "Use GLFW"           ON)
 option(LIBIGL_WITH_PNG              "Use PNG"            ON)
@@ -26,9 +25,6 @@ set(LIBIGL_ROOT "${CMAKE_CURRENT_LIST_DIR}/../..")
 set(LIBIGL_SOURCE_DIR "${LIBIGL_ROOT}/include")
 set(LIBIGL_EXTERNAL "${LIBIGL_ROOT}/external")
 
-### Multiple dependencies are buried in Nanogui
-set(NANOGUI_DIR "${LIBIGL_EXTERNAL}/nanogui")
-
 # Dependencies are linked as INTERFACE targets unless libigl is compiled as a static library
 if(LIBIGL_USE_STATIC_LIBRARY)
   set(IGL_SCOPE PUBLIC)
@@ -69,7 +65,7 @@ if(TARGET Eigen3::Eigen)
   # If an imported target already exists, use it
   target_link_libraries(igl_common INTERFACE Eigen3::Eigen)
 else()
-  target_include_directories(igl_common SYSTEM INTERFACE ${NANOGUI_DIR}/ext/eigen)
+  target_include_directories(igl_common SYSTEM INTERFACE ${LIBIGL_EXTERNAL}/eigen)
 endif()
 
 # C++11 Thread library
@@ -82,7 +78,7 @@ function(compile_igl_module module_dir)
   string(REPLACE "/" "_" module_name "${module_dir}")
   if(LIBIGL_USE_STATIC_LIBRARY)
     file(GLOB SOURCES_IGL_${module_name}
-      "${LIBIGL_SOURCE_DIR}/igl/${module_dir}/*.cpp" 
+      "${LIBIGL_SOURCE_DIR}/igl/${module_dir}/*.cpp"
       "${LIBIGL_SOURCE_DIR}/igl/copyleft/${module_dir}/*.cpp")
     add_library(igl_${module_name} STATIC ${SOURCES_IGL_${module_name}} ${ARGN})
     if(MSVC)
@@ -272,40 +268,21 @@ endif()
 ### Compile the opengl parts ###
 
 if(LIBIGL_WITH_OPENGL)
-  # OpenGL modules
+  # OpenGL module
   find_package(OpenGL REQUIRED)
   compile_igl_module("opengl")
-  compile_igl_module("opengl2")
+  # compile_igl_module("opengl2")
   target_link_libraries(igl_opengl ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
-  target_link_libraries(igl_opengl2 ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
+  # target_link_libraries(igl_opengl2 ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
   target_include_directories(igl_opengl SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
-  target_include_directories(igl_opengl2 SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
-
-  # GLEW for linux and windows
-  if(NOT TARGET glew)
-    add_library(glew STATIC ${NANOGUI_DIR}/ext/glew/src/glew.c)
-    target_include_directories(glew SYSTEM PUBLIC ${NANOGUI_DIR}/ext/glew/include)
-    target_compile_definitions(glew PUBLIC -DGLEW_BUILD -DGLEW_NO_GLU)
-  endif()
-  target_link_libraries(igl_opengl ${IGL_SCOPE} glew)
-  target_link_libraries(igl_opengl2 ${IGL_SCOPE} glew)
+  # target_include_directories(igl_opengl2 SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
 
-  # Nanogui
-  if(LIBIGL_WITH_NANOGUI)
-    if(LIBIGL_WITH_PYTHON)
-      set(NANOGUI_BUILD_PYTHON ON CACHE BOOL " " FORCE)
-    else()
-      set(NANOGUI_BUILD_PYTHON OFF CACHE BOOL " " FORCE)
-    endif()
-    set(NANOGUI_BUILD_EXAMPLE OFF CACHE BOOL " " FORCE)
-    set(NANOGUI_BUILD_SHARED  OFF CACHE BOOL " " FORCE)
-    add_subdirectory(${NANOGUI_DIR} nanogui)
-    target_include_directories(nanogui PUBLIC
-      "${NANOGUI_DIR}/include"
-      "${NANOGUI_DIR}/ext/nanovg/src")
-    target_compile_definitions(nanogui ${IGL_SCOPE} -DIGL_VIEWER_WITH_NANOGUI)
-    
+  # glad module
+  if(NOT TARGET glad)
+    add_subdirectory(${LIBIGL_EXTERNAL}/glad glad)
   endif()
+  target_link_libraries(igl_opengl ${IGL_SCOPE} glad)
+  # target_link_libraries(igl_opengl2 ${IGL_SCOPE} glad)
 
   # GLFW module
   if(LIBIGL_WITH_OPENGL_GLFW)
@@ -315,9 +292,8 @@ if(LIBIGL_WITH_OPENGL)
       set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
       set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
       set(GLFW_INSTALL OFF CACHE BOOL " " FORCE)
-      add_subdirectory(${NANOGUI_DIR}/ext/glfw glfw)
+      add_subdirectory(${LIBIGL_EXTERNAL}/glfw glfw)
     endif()
-    target_include_directories(glfw ${IGL_SCOPE} ${NANOGUI_DIR}/ext/glfw/include)
     target_link_libraries(igl_opengl_glfw ${IGL_SCOPE} igl_opengl glfw)
   endif()