Эх сурвалжийг харах

background window

Former-commit-id: 1a733a849245f75e4537b83e28fd3b357f823782
Alec Jacobson 8 жил өмнө
parent
commit
7a96a0d556

+ 25 - 0
include/igl/opengl/glfw/background_window.cpp

@@ -0,0 +1,25 @@
+#include "background_window.h"
+
+#include <iostream>
+
+IGL_INLINE bool igl::opengl::glfw::background_window(GLFWwindow* & window)
+{
+  if(!glfwInit()) return false;
+  glfwSetErrorCallback([](int id,const char* m){std::cerr<<m<<std::endl;});
+  glfwWindowHint(GLFW_SAMPLES, 4);
+  // Use 3.2 core profile
+  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
+  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
+  // Use background window
+  glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
+  window = glfwCreateWindow(1, 1,"", NULL, NULL);
+  if(!window) return false;
+  glfwMakeContextCurrent(window);
+  return true;
+}
+
+#ifdef IGL_STATIC_LIBRARY
+// Explicit template instantiation
+#endif

+ 35 - 0
include/igl/opengl/glfw/background_window.h

@@ -0,0 +1,35 @@
+#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 <GLFW/glfw3.h>
+
+namespace igl
+{
+  namespace opengl
+  {
+    namespace glfw
+    {
+      // Create a background window with a valid core profile opengl context
+      // set to current. 
+      //
+      // After you're finished with this window you may call
+      // `glfwDestroyWindow(window)`
+      //
+      // After you're finished with glfw you should call `glfwTerminate()`
+      //
+      // Outputs:
+      //    window  pointer to glfw window
+      // Returns true iff success
+      IGL_INLINE bool background_window(GLFWwindow* & window);
+    }
+  }
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "background_window.cpp"
+#endif
+
+#endif

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

@@ -1,4 +1,5 @@
 #include "map_texture.h"
 #include "map_texture.h"
+#include "background_window.h"
 #include "../create_shader_program.h"
 #include "../create_shader_program.h"
 #include "../gl.h"
 #include "../gl.h"
 
 
@@ -63,18 +64,12 @@ IGL_INLINE bool igl::opengl::glfw::map_texture(
     DerivedF::ColsAtCompileTime,
     DerivedF::ColsAtCompileTime,
     Eigen::RowMajor> F = _F.template cast<int>();
     Eigen::RowMajor> F = _F.template cast<int>();
   const int dim = U.cols();
   const int dim = U.cols();
-  // Set up glfw
-  if(!glfwInit()) fail("Could not initialize glfw");
-  glfwSetErrorCallback([](int id,const char* m){std::cerr<<m<<std::endl;});
-  glfwWindowHint(GLFW_SAMPLES, 4);
-  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
-  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
-  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
-  glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
-  GLFWwindow* window = glfwCreateWindow(1, 1,"", NULL, NULL);
-  if(!window) fail("Could not create glfw window");
-  glfwMakeContextCurrent(window);
+  GLFWwindow * window;
+  if(!background_window(window))
+  {
+    fail("Could not initialize glfw window");
+  }
+
   // Compile each shader
   // Compile each shader
   std::string vertex_shader = dim == 2 ? 
   std::string vertex_shader = dim == 2 ? 
     R"(
     R"(