1 #ifndef _ShaderProgram_h_
2 #define _ShaderProgram_h_
3 
4 #include <GG/Base.h>
5 
6 #include <boost/filesystem/operations.hpp>
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 bool ReadFile(const boost::filesystem::path& path, std::string& file_contents);
13 
14 class ShaderProgram {
15 private:
16     ShaderProgram();    // default ctor forbidden, makes no sense
17 
18 public:
19     // Use shaderProgramFactory() to construct.
20     ShaderProgram(const std::string& vertex_shader, const std::string& fragment_shader);
21 
22     // shader factory -- will return nullptr if OpenGL version is too low
23     static std::unique_ptr<ShaderProgram> shaderProgramFactory(
24         const std::string& vertex_shader, const std::string& fragment_shader);
25 
26     ~ShaderProgram();
27 
28     GLuint              ProgramID() const;
29     bool                LinkSucceeded() const;
30     const std::string&  ProgramInfoLog() const;
31     const std::string&  ProgramValidityInfoLog() const;
32     const std::string&  VertexShaderInfoLog() const;
33     const std::string&  FragmentShaderInfoLog() const;
34 
35     // These bind the value of the uniform float(s) referred called \a name in
36     // the program.  Multi-float overloads are for when \a name is a vec.
37     void                Bind(const std::string& name, float f);
38     void                Bind(const std::string& name, float f0, float f1);
39     void                Bind(const std::string& name, float f0, float f1, float f2);
40     void                Bind(const std::string& name, float f0, float f1, float f2, float f3);
41 
42     // Binds the given array of values to the int array \a name in the
43     // program.  \a element_size indicates the number of
44     // ints in each element of the array, and must be 1, 2, 3, or 4
45     // (corresponding to GLSL types float, vec2, vec3, and vec4).
46     void                Bind(const std::string& name, std::size_t element_size, const std::vector<float> &floats);
47 
48     void                Bind(const std::string& name, GLuint texture_id);
49 
50     // These bind the value of the uniform int(s) referred called \a name in
51     // the program.  Multi-int overloads are for when \a name is an ivec.
52     void                BindInt(const std::string& name, int i);
53     void                BindInts(const std::string& name, int i0, int i1);
54     void                BindInts(const std::string& name, int i0, int i1, int i2);
55     void                BindInts(const std::string& name, int i0, int i1, int i2, int i3);
56 
57     // Binds the given array of values to the int array \a name in the
58     // program.  \a element_size indicates the number of
59     // ints in each element of the array, and must be 1, 2, 3, or 4
60     // (corresponding to GLSL types int, ivec2, ivec3, and ivec4).
61     void                BindInts(const std::string& name, std::size_t element_size, const std::vector<GLint> &ints);
62 
63     bool                AllValuesBound();
64 
65     void                Use();
66     void                stopUse();
67 
68 private:
69     GLuint      m_program_id;
70     GLuint      m_vertex_shader_id;
71     GLuint      m_fragment_shader_id;
72     bool        m_link_succeeded;
73     std::string m_program_log;
74     std::string m_program_validity_log;
75     std::string m_vertex_shader_log;
76     std::string m_fragment_shader_log;
77 };
78 
79 #endif
80