Kinetic Visualization
 All Classes Functions Variables Pages
MyShader.hpp
1 #ifndef _SHADER_HPP_
2 #define _SHADER_HPP_
3 
4 #include "common.hpp"
5 
6 // This is a very simple shader class
7 
8 class MyShader
9 {
10 public:
11 
12  // Loads shaders from files and compiles them.
13  // When path is "hello", the files "hello.frag" & "hello.vert"
14  // will be loaded.
15  MyShader(const string &path);
16  ~MyShader();
17 
18  // Bind the shader to the OGL state-machine
19  void bind() const
20  {
21  glUseProgram(_program);
22  }
23 
24  // Unbind the shader
25  void unbind() const
26  {
27  glUseProgram(0);
28  }
29 
30  // Query the location of a vertex attribute inside the shader.
31  GLint get_attrib_location(const std::string &name) const
32  {
33  return glGetAttribLocation(_program, name.c_str());
34  }
35 
36  // Query the location of a uniform variable inside the shader.
37  GLint get_uniform_location(const std::string &name) const
38  {
39  return glGetUniformLocation(_program, name.c_str());
40  }
41 
42  // Define the name of the variable inside the shader which represents the final color for each fragment.
43  void bind_frag_data_location(const std::string &name)
44  {
45  if(_program > 0)
46  {
47  glBindFragDataLocation(_program, 0, name.c_str() );
48  link();
49  }
50  }
51 
52  // Define the name of the variable inside the shader which represents the final color for each fragment.
53  void bind_frag_data_location(const std::string &name, const std::string &name2)
54  {
55  if(_program > 0)
56  {
57  glBindFragDataLocation(_program, 0, name.c_str() );
58  glBindFragDataLocation(_program, 1, name2.c_str() );
59  link();
60  }
61  }
62 
63  // A little cast helper.
64  // With this you can simply do "if (shader) {...}" to test if a
65  // shader has been compiled successfully.
66  operator bool ()
67  {
68  return _success;
69  }
70 
71 public:
72  std::string shadername;
73 
74 private:
75 
76  bool _success;
77 
78  GLuint _vertex_shader;
79  GLuint _geometry_shader;
80  GLuint _fragment_shader;
81  GLuint _program;
82 
83  GLuint compile(GLenum type, const string &source);
84  void link (void);
85 
86  void shader_log(GLuint shader);
87  void program_log(GLuint program);
88 
89  bool m_geometry;
90 };
91 
92 #endif //#ifndef _SHADER_HPP_
93