OpenGL_shader.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Wenzel Jacob <wenzel@inf.ethz.ch>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_VIEWER_OPENGL_SHADER_H
  9. #define IGL_VIEWER_OPENGL_SHADER_H
  10. #include <igl/igl_inline.h>
  11. #include <string>
  12. #include <Eigen/Core>
  13. #ifdef _WIN32
  14. # include <windows.h>
  15. # undef max
  16. # undef min
  17. # undef DrawText
  18. #endif
  19. #ifndef __APPLE__
  20. # define GLEW_STATIC
  21. # include <GL/glew.h>
  22. #endif
  23. #ifdef __APPLE__
  24. # include <OpenGL/gl3.h>
  25. # define __gl_h_ /* Prevent inclusion of the old gl.h */
  26. #else
  27. # ifdef _WIN32
  28. # include <windows.h>
  29. # endif
  30. # include <GL/gl.h>
  31. #endif
  32. namespace igl
  33. {
  34. namespace viewer
  35. {
  36. // This class wraps an OpenGL program composed of three shaders
  37. // TODO: write documentation
  38. class OpenGL_shader
  39. {
  40. public:
  41. typedef unsigned int GLuint;
  42. typedef int GLint;
  43. GLuint vertex_shader;
  44. GLuint fragment_shader;
  45. GLuint geometry_shader;
  46. GLuint program_shader;
  47. IGL_INLINE OpenGL_shader() : vertex_shader(0), fragment_shader(0),
  48. geometry_shader(0), program_shader(0) { }
  49. // Create a new shader from the specified source strings
  50. IGL_INLINE bool init(const std::string &vertex_shader_string,
  51. const std::string &fragment_shader_string,
  52. const std::string &fragment_data_name,
  53. const std::string &geometry_shader_string = "",
  54. int geometry_shader_max_vertices = 3);
  55. // Create a new shader from the specified files on disk
  56. IGL_INLINE bool init_from_files(const std::string &vertex_shader_filename,
  57. const std::string &fragment_shader_filename,
  58. const std::string &fragment_data_name,
  59. const std::string &geometry_shader_filename = "",
  60. int geometry_shader_max_vertices = 3);
  61. // Select this shader for subsequent draw calls
  62. IGL_INLINE void bind();
  63. // Release all OpenGL objects
  64. IGL_INLINE void free();
  65. // Return the OpenGL handle of a named shader attribute (-1 if it does not exist)
  66. IGL_INLINE GLint attrib(const std::string &name) const;
  67. // Return the OpenGL handle of a uniform attribute (-1 if it does not exist)
  68. IGL_INLINE GLint uniform(const std::string &name) const;
  69. // Bind a per-vertex array attribute and refresh its contents from an Eigen amtrix
  70. IGL_INLINE GLint bindVertexAttribArray(const std::string &name, GLuint bufferID,
  71. const Eigen::MatrixXf &M, bool refresh) const;
  72. IGL_INLINE GLuint create_shader_helper(GLint type, const std::string &shader_string);
  73. };
  74. }
  75. }
  76. #ifndef IGL_STATIC_LIBRARY
  77. # include "OpenGL_shader.cpp"
  78. #endif
  79. #endif