Răsfoiți Sursa

* extracted OpenGL_shader from the viewer

Former-commit-id: 9290e11f4856c5f49f28e02fba9d2bc38ca0f73f
Daniele Panozzo 11 ani în urmă
părinte
comite
1b6b21a941

+ 175 - 0
include/igl/viewer/OpenGL_shader.cpp

@@ -0,0 +1,175 @@
+#include "OpenGL_shader.h"
+
+#ifdef __APPLE__
+#   include <OpenGL/gl3.h>
+#   define __gl_h_ /* Prevent inclusion of the old gl.h */
+#else
+#   ifdef _WIN32
+#       include <windows.h>
+#   endif
+#   include <GL/gl.h>
+#endif
+
+#include <iostream>
+#include <fstream>
+
+IGL_INLINE bool igl::OpenGL_shader::init_from_files(
+  const std::string &vertex_shader_filename,
+  const std::string &fragment_shader_filename,
+  const std::string &fragment_data_name,
+  const std::string &geometry_shader_filename,
+  int geometry_shader_max_vertices)
+{
+  auto file_to_string = [](const std::string &filename)
+  {
+    std::ifstream t(filename);
+    return std::string((std::istreambuf_iterator<char>(t)),
+                        std::istreambuf_iterator<char>());
+  };
+
+  return init(
+    file_to_string(vertex_shader_filename),
+    file_to_string(fragment_shader_filename),
+    fragment_data_name,
+    file_to_string(geometry_shader_filename),
+    geometry_shader_max_vertices
+ );
+}
+
+IGL_INLINE bool igl::OpenGL_shader::init(
+  const std::string &vertex_shader_string,
+  const std::string &fragment_shader_string,
+  const std::string &fragment_data_name,
+  const std::string &geometry_shader_string,
+  int geometry_shader_max_vertices)
+{
+  using namespace std;
+  vertex_shader = create_shader_helper(GL_VERTEX_SHADER, vertex_shader_string);
+  geometry_shader = create_shader_helper(GL_GEOMETRY_SHADER, geometry_shader_string);
+  fragment_shader = create_shader_helper(GL_FRAGMENT_SHADER, fragment_shader_string);
+
+  if (!vertex_shader || !fragment_shader)
+    return false;
+
+  program_shader = glCreateProgram();
+
+  glAttachShader(program_shader, vertex_shader);
+  glAttachShader(program_shader, fragment_shader);
+
+  if (geometry_shader)
+  {
+    glAttachShader(program_shader, geometry_shader);
+
+    /* This covers only basic cases and may need to be modified */
+    glProgramParameteri(program_shader, GL_GEOMETRY_INPUT_TYPE, GL_TRIANGLES);
+    glProgramParameteri(program_shader, GL_GEOMETRY_OUTPUT_TYPE, GL_TRIANGLES);
+    glProgramParameteri(program_shader, GL_GEOMETRY_VERTICES_OUT, geometry_shader_max_vertices);
+  }
+
+  glBindFragDataLocation(program_shader, 0, fragment_data_name.c_str());
+  glLinkProgram(program_shader);
+
+  GLint status;
+  glGetProgramiv(program_shader, GL_LINK_STATUS, &status);
+
+  if (status != GL_TRUE)
+  {
+    char buffer[512];
+    glGetProgramInfoLog(program_shader, 512, NULL, buffer);
+    cerr << "Linker error: " << endl << buffer << endl;
+    program_shader = 0;
+    return false;
+  }
+
+  return true;
+}
+
+IGL_INLINE void igl::OpenGL_shader::bind()
+{
+  glUseProgram(program_shader);
+}
+
+IGL_INLINE GLint igl::OpenGL_shader::attrib(const std::string &name) const
+{
+  return glGetAttribLocation(program_shader, name.c_str());
+}
+
+IGL_INLINE GLint igl::OpenGL_shader::uniform(const std::string &name) const
+{
+  return glGetUniformLocation(program_shader, name.c_str());
+}
+
+IGL_INLINE GLint igl::OpenGL_shader::bindVertexAttribArray(
+  const std::string &name, GLuint bufferID, const Eigen::MatrixXf &M, bool refresh) const
+{
+  GLint id = attrib(name);
+  if (id < 0)
+    return id;
+  if (M.size() == 0)
+  {
+    glDisableVertexAttribArray(id);
+    return id;
+  }
+  glBindBuffer(GL_ARRAY_BUFFER, bufferID);
+  if (refresh)
+    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*M.size(), M.data(), GL_DYNAMIC_DRAW);
+  glVertexAttribPointer(id, M.rows(), GL_FLOAT, GL_FALSE, 0, 0);
+  glEnableVertexAttribArray(id);
+  return id;
+}
+
+IGL_INLINE void igl::OpenGL_shader::free()
+{
+  if (program_shader)
+  {
+    glDeleteProgram(program_shader);
+    program_shader = 0;
+  }
+  if (vertex_shader)
+  {
+    glDeleteShader(vertex_shader);
+    vertex_shader = 0;
+  }
+  if (fragment_shader)
+  {
+    glDeleteShader(fragment_shader);
+    fragment_shader = 0;
+  }
+  if (geometry_shader)
+  {
+    glDeleteShader(geometry_shader);
+    geometry_shader = 0;
+  }
+}
+
+IGL_INLINE GLuint igl::OpenGL_shader::create_shader_helper(GLint type, const std::string &shader_string)
+{
+  using namespace std;
+  if (shader_string.empty())
+    return (GLuint) 0;
+
+  GLuint id = glCreateShader(type);
+  const char *shader_string_const = shader_string.c_str();
+  glShaderSource(id, 1, &shader_string_const, NULL);
+  glCompileShader(id);
+
+  GLint status;
+  glGetShaderiv(id, GL_COMPILE_STATUS, &status);
+
+  if (status != GL_TRUE)
+  {
+    char buffer[512];
+    if (type == GL_VERTEX_SHADER)
+      cerr << "Vertex shader:" << endl;
+    else if (type == GL_FRAGMENT_SHADER)
+      cerr << "Fragment shader:" << endl;
+    else if (type == GL_GEOMETRY_SHADER)
+      cerr << "Geometry shader:" << endl;
+    cerr << shader_string << endl << endl;
+    glGetShaderInfoLog(id, 512, NULL, buffer);
+    cerr << "Error: " << endl << buffer << endl;
+    return (GLuint) 0;
+  }
+
+  return id;
+}

+ 63 - 0
include/igl/viewer/OpenGL_shader.h

@@ -0,0 +1,63 @@
+#ifndef IGL_OPENGL_SHADER_H
+#define IGL_OPENGL_SHADER_H
+
+#include <igl/igl_inline.h>
+
+namespace igl
+{
+
+class OpenGL_shader
+{
+public:
+  typedef unsigned int GLuint;
+  typedef int GLint;
+
+  GLuint vertex_shader;
+  GLuint fragment_shader;
+  GLuint geometry_shader;
+  GLuint program_shader;
+
+  IGL_INLINE OpenGL_shader() : vertex_shader(0), fragment_shader(0),
+    geometry_shader(0), program_shader(0) { }
+
+  // Create a new shader from the specified source strings
+  IGL_INLINE bool init(const std::string &vertex_shader_string,
+    const std::string &fragment_shader_string,
+    const std::string &fragment_data_name,
+    const std::string &geometry_shader_string = "",
+    int geometry_shader_max_vertices = 3);
+
+  // Create a new shader from the specified files on disk
+  IGL_INLINE bool init_from_files(const std::string &vertex_shader_filename,
+    const std::string &fragment_shader_filename,
+    const std::string &fragment_data_name,
+    const std::string &geometry_shader_filename = "",
+    int geometry_shader_max_vertices = 3);
+
+  // Select this shader for subsequent draw calls
+  IGL_INLINE void bind();
+
+  // Release all OpenGL objects
+  IGL_INLINE void free();
+
+  // Return the OpenGL handle of a named shader attribute (-1 if it does not exist)
+  IGL_INLINE GLint attrib(const std::string &name) const;
+
+  // Return the OpenGL handle of a uniform attribute (-1 if it does not exist)
+  IGL_INLINE GLint uniform(const std::string &name) const;
+
+  // Bind a per-vertex array attribute and refresh its contents from an Eigen amtrix
+  IGL_INLINE GLint bindVertexAttribArray(const std::string &name, GLuint bufferID,
+    const Eigen::MatrixXf &M, bool refresh) const;
+
+  IGL_INLINE GLuint create_shader_helper(GLint type, const std::string &shader_string);
+
+};
+
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "OpenGL_shader.cpp"
+#endif
+
+#endif

+ 1 - 0
include/igl/viewer/TODOs.txt

@@ -1,3 +1,4 @@
+- depth test for overlays cannot be disabled
 - data.lines, data.points should not concatenate colors with coordinates
 - snap to canonical recenters origin but trackball does not
 - rewrite in libigl style

+ 2 - 2
include/igl/viewer/TextRenderer.h

@@ -5,7 +5,7 @@
 #define IGL_TEXT_RENDERER_H
 
 #include <igl/igl_inline.h>
-
+#include <igl/viewer/OpenGL_shader.h>
 
 namespace igl
 {
@@ -26,7 +26,7 @@ public:
   IGL_INLINE void DrawText(Eigen::Vector3d pos, Eigen::Vector3d normal, const std::string &text);
 
 protected:
-  igl::Viewer::OpenGL_shader m_shader;
+  igl::OpenGL_shader m_shader;
   std::map<std::string, void *> m_textObjects;
   GLuint m_shaderHandleBackup;
   GLuint m_TriTexUniLocationDepth;

+ 0 - 129
include/igl/viewer/Viewer.cpp

@@ -1020,135 +1020,6 @@ namespace igl
     return id;
   }
 
-  bool Viewer::OpenGL_shader::init_from_files(
-    const std::string &vertex_shader_filename,
-    const std::string &fragment_shader_filename,
-    const std::string &fragment_data_name,
-    const std::string &geometry_shader_filename,
-    int geometry_shader_max_vertices)
-  {
-    auto file_to_string = [](const std::string &filename)
-    {
-      std::ifstream t(filename);
-      return std::string((std::istreambuf_iterator<char>(t)),
-                          std::istreambuf_iterator<char>());
-    };
-
-    return init(
-      file_to_string(vertex_shader_filename),
-      file_to_string(fragment_shader_filename),
-      fragment_data_name,
-      file_to_string(geometry_shader_filename),
-      geometry_shader_max_vertices
-   );
-  }
-
-  bool Viewer::OpenGL_shader::init(
-    const std::string &vertex_shader_string,
-    const std::string &fragment_shader_string,
-    const std::string &fragment_data_name,
-    const std::string &geometry_shader_string,
-    int geometry_shader_max_vertices)
-  {
-    using namespace std;
-    vertex_shader = create_shader_helper(GL_VERTEX_SHADER, vertex_shader_string);
-    geometry_shader = create_shader_helper(GL_GEOMETRY_SHADER, geometry_shader_string);
-    fragment_shader = create_shader_helper(GL_FRAGMENT_SHADER, fragment_shader_string);
-
-    if (!vertex_shader || !fragment_shader)
-      return false;
-
-    program_shader = glCreateProgram();
-
-    glAttachShader(program_shader, vertex_shader);
-    glAttachShader(program_shader, fragment_shader);
-
-    if (geometry_shader)
-    {
-      glAttachShader(program_shader, geometry_shader);
-
-      /* This covers only basic cases and may need to be modified */
-      glProgramParameteri(program_shader, GL_GEOMETRY_INPUT_TYPE, GL_TRIANGLES);
-      glProgramParameteri(program_shader, GL_GEOMETRY_OUTPUT_TYPE, GL_TRIANGLES);
-      glProgramParameteri(program_shader, GL_GEOMETRY_VERTICES_OUT, geometry_shader_max_vertices);
-    }
-
-    glBindFragDataLocation(program_shader, 0, fragment_data_name.c_str());
-    glLinkProgram(program_shader);
-
-    GLint status;
-    glGetProgramiv(program_shader, GL_LINK_STATUS, &status);
-
-    if (status != GL_TRUE)
-    {
-      char buffer[512];
-      glGetProgramInfoLog(program_shader, 512, NULL, buffer);
-      cerr << "Linker error: " << endl << buffer << endl;
-      program_shader = 0;
-      return false;
-    }
-
-    return true;
-  }
-
-  void Viewer::OpenGL_shader::bind()
-  {
-    glUseProgram(program_shader);
-  }
-
-  GLint Viewer::OpenGL_shader::attrib(const std::string &name) const
-  {
-    return glGetAttribLocation(program_shader, name.c_str());
-  }
-
-  GLint Viewer::OpenGL_shader::uniform(const std::string &name) const
-  {
-    return glGetUniformLocation(program_shader, name.c_str());
-  }
-
-  GLint Viewer::OpenGL_shader::bindVertexAttribArray(
-    const std::string &name, GLuint bufferID, const Eigen::MatrixXf &M, bool refresh) const
-  {
-    GLint id = attrib(name);
-    if (id < 0)
-      return id;
-    if (M.size() == 0)
-    {
-      glDisableVertexAttribArray(id);
-      return id;
-    }
-    glBindBuffer(GL_ARRAY_BUFFER, bufferID);
-    if (refresh)
-      glBufferData(GL_ARRAY_BUFFER, sizeof(float)*M.size(), M.data(), GL_DYNAMIC_DRAW);
-    glVertexAttribPointer(id, M.rows(), GL_FLOAT, GL_FALSE, 0, 0);
-    glEnableVertexAttribArray(id);
-    return id;
-  }
-
-  void Viewer::OpenGL_shader::free()
-  {
-    if (program_shader)
-    {
-      glDeleteProgram(program_shader);
-      program_shader = 0;
-    }
-    if (vertex_shader)
-    {
-      glDeleteShader(vertex_shader);
-      vertex_shader = 0;
-    }
-    if (fragment_shader)
-    {
-      glDeleteShader(fragment_shader);
-      fragment_shader = 0;
-    }
-    if (geometry_shader)
-    {
-      glDeleteShader(geometry_shader);
-      geometry_shader = 0;
-    }
-  }
-
   void Viewer::OpenGL_state::init()
   {
     // Mesh: Vertex Array Object & Buffer objects

+ 2 - 47
include/igl/viewer/Viewer.h

@@ -20,13 +20,13 @@
 #endif
 
 #include <Eigen/Core>
-
+#include <igl/viewer/OpenGL_shader.h>
 
 namespace igl
 {
   // Forward declaration of the viewer_plugin class
   class Viewer_plugin;
-  
+
   class Viewer
   {
   public:
@@ -184,51 +184,6 @@ namespace igl
       /*********************************/
     };
 
-    class OpenGL_shader
-    {
-    public:
-      typedef unsigned int GLuint;
-      typedef int GLint;
-
-      GLuint vertex_shader;
-      GLuint fragment_shader;
-      GLuint geometry_shader;
-      GLuint program_shader;
-
-      OpenGL_shader() : vertex_shader(0), fragment_shader(0),
-        geometry_shader(0), program_shader(0) { }
-
-      // Create a new shader from the specified source strings
-      bool init(const std::string &vertex_shader_string,
-        const std::string &fragment_shader_string,
-        const std::string &fragment_data_name,
-        const std::string &geometry_shader_string = "",
-        int geometry_shader_max_vertices = 3);
-
-      // Create a new shader from the specified files on disk
-      bool init_from_files(const std::string &vertex_shader_filename,
-        const std::string &fragment_shader_filename,
-        const std::string &fragment_data_name,
-        const std::string &geometry_shader_filename = "",
-        int geometry_shader_max_vertices = 3);
-
-      // Select this shader for subsequent draw calls
-      void bind();
-
-      // Release all OpenGL objects
-      void free();
-
-      // Return the OpenGL handle of a named shader attribute (-1 if it does not exist)
-      GLint attrib(const std::string &name) const;
-
-      // Return the OpenGL handle of a uniform attribute (-1 if it does not exist)
-      GLint uniform(const std::string &name) const;
-
-      // Bind a per-vertex array attribute and refresh its contents from an Eigen amtrix
-      GLint bindVertexAttribArray(const std::string &name, GLuint bufferID,
-        const Eigen::MatrixXf &M, bool refresh) const;
-    };
-
     class OpenGL_state
     {
     public: