OpenGL_shader.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_OPENGL_SHADER_H
  9. #define IGL_OPENGL_SHADER_H
  10. #include <igl/igl_inline.h>
  11. #include <string>
  12. #include <Eigen/Core>
  13. #ifdef __APPLE__
  14. # include <OpenGL/gl3.h>
  15. # define __gl_h_ /* Prevent inclusion of the old gl.h */
  16. #else
  17. # ifdef _WIN32
  18. # include <windows.h>
  19. # endif
  20. # include <GL/gl.h>
  21. #endif
  22. namespace igl
  23. {
  24. // This class wraps an OpenGL program composed of three shaders
  25. // TODO: write documentation
  26. class OpenGL_shader
  27. {
  28. public:
  29. typedef unsigned int GLuint;
  30. typedef int GLint;
  31. GLuint vertex_shader;
  32. GLuint fragment_shader;
  33. GLuint geometry_shader;
  34. GLuint program_shader;
  35. IGL_INLINE OpenGL_shader() : vertex_shader(0), fragment_shader(0),
  36. geometry_shader(0), program_shader(0) { }
  37. // Create a new shader from the specified source strings
  38. IGL_INLINE bool init(const std::string &vertex_shader_string,
  39. const std::string &fragment_shader_string,
  40. const std::string &fragment_data_name,
  41. const std::string &geometry_shader_string = "",
  42. int geometry_shader_max_vertices = 3);
  43. // Create a new shader from the specified files on disk
  44. IGL_INLINE bool init_from_files(const std::string &vertex_shader_filename,
  45. const std::string &fragment_shader_filename,
  46. const std::string &fragment_data_name,
  47. const std::string &geometry_shader_filename = "",
  48. int geometry_shader_max_vertices = 3);
  49. // Select this shader for subsequent draw calls
  50. IGL_INLINE void bind();
  51. // Release all OpenGL objects
  52. IGL_INLINE void free();
  53. // Return the OpenGL handle of a named shader attribute (-1 if it does not exist)
  54. IGL_INLINE GLint attrib(const std::string &name) const;
  55. // Return the OpenGL handle of a uniform attribute (-1 if it does not exist)
  56. IGL_INLINE GLint uniform(const std::string &name) const;
  57. // Bind a per-vertex array attribute and refresh its contents from an Eigen amtrix
  58. IGL_INLINE GLint bindVertexAttribArray(const std::string &name, GLuint bufferID,
  59. const Eigen::MatrixXf &M, bool refresh) const;
  60. IGL_INLINE GLuint create_shader_helper(GLint type, const std::string &shader_string);
  61. };
  62. }
  63. #ifndef IGL_STATIC_LIBRARY
  64. # include "OpenGL_shader.cpp"
  65. #endif
  66. #endif