OpenGL_shader.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # include <GL/gl.h>
  28. #endif
  29. namespace igl
  30. {
  31. namespace viewer
  32. {
  33. // This class wraps an OpenGL program composed of three shaders
  34. // TODO: write documentation
  35. class OpenGL_shader
  36. {
  37. public:
  38. typedef unsigned int GLuint;
  39. typedef int GLint;
  40. GLuint vertex_shader;
  41. GLuint fragment_shader;
  42. GLuint geometry_shader;
  43. GLuint program_shader;
  44. IGL_INLINE OpenGL_shader() : vertex_shader(0), fragment_shader(0),
  45. geometry_shader(0), program_shader(0) { }
  46. // Create a new shader from the specified source strings
  47. IGL_INLINE bool init(const std::string &vertex_shader_string,
  48. const std::string &fragment_shader_string,
  49. const std::string &fragment_data_name,
  50. const std::string &geometry_shader_string = "",
  51. int geometry_shader_max_vertices = 3);
  52. // Create a new shader from the specified files on disk
  53. IGL_INLINE bool init_from_files(const std::string &vertex_shader_filename,
  54. const std::string &fragment_shader_filename,
  55. const std::string &fragment_data_name,
  56. const std::string &geometry_shader_filename = "",
  57. int geometry_shader_max_vertices = 3);
  58. // Select this shader for subsequent draw calls
  59. IGL_INLINE void bind();
  60. // Release all OpenGL objects
  61. IGL_INLINE void free();
  62. // Return the OpenGL handle of a named shader attribute (-1 if it does not exist)
  63. IGL_INLINE GLint attrib(const std::string &name) const;
  64. // Return the OpenGL handle of a uniform attribute (-1 if it does not exist)
  65. IGL_INLINE GLint uniform(const std::string &name) const;
  66. // Bind a per-vertex array attribute and refresh its contents from an Eigen amtrix
  67. IGL_INLINE GLint bindVertexAttribArray(const std::string &name, GLuint bufferID,
  68. const Eigen::MatrixXf &M, bool refresh) const;
  69. IGL_INLINE GLuint create_shader_helper(GLint type, const std::string &shader_string);
  70. };
  71. }
  72. }
  73. #ifndef IGL_STATIC_LIBRARY
  74. # include "OpenGL_shader.cpp"
  75. #endif
  76. #endif