zMol
A viewer for molecular data using OpenGL and ambient occlusion
glsl.hpp
1 /****************************************************************************
2 
3 Copyright (c) 2012 Carlos Rafael Giani ( email: dv xxx AT pseudoterminal xxx DOT xxx org , remove the xxx )
4 
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24 ****************************************************************************/
25 
26 
27 
28 #ifndef ZMOL_GLSL_HPP
29 #define ZMOL_GLSL_HPP
30 
31 #include <string>
32 #include <set>
33 #include <unordered_map>
34 #include <memory>
35 #include <functional>
36 #include "opengl3.hpp"
37 #include "noncopyable.hpp"
38 
39 
40 namespace zmol
41 {
42 
43 
56 class shader:
57  private noncopyable
58 {
59 public:
60  typedef std::shared_ptr < shader > sharedptr;
61 
69  explicit shader(GLenum const p_type);
73  ~shader();
74 
78  inline GLuint get_name() const
79  {
80  return m_name;
81  }
82 
95  bool compile_and_store(std::string const &p_code);
96 
106  std::string get_info_log() const;
107 
108 protected:
109  GLuint m_name;
110 };
111 
112 
113 
114 
126 class program:
127  private noncopyable
128 {
129 public:
133  program();
137  ~program();
138 
142  inline GLuint get_name() const
143  {
144  return m_name;
145  }
146 
155  void attach_shader(shader::sharedptr const &p_shader);
156 
165  void detach_shader(shader::sharedptr const &p_shader);
166 
174  bool link();
175 
179  void bind();
183  void unbind();
184 
190  GLint get_attribute_location(std::string const &p_attribute_name) const;
196  GLint get_uniform_location(std::string const &p_uniform_name) const;
202  GLuint get_uniform_block_index(std::string const &p_block_name) const;
203 
213  std::string get_info_log() const;
214 
215 protected:
216  typedef std::unordered_map < std::string, GLint > locations_type;
217  typedef std::unordered_map < std::string, GLuint > indices_type;
218  typedef std::set < shader::sharedptr > glsl_shader_set_type;
219 
220  GLuint m_name;
221  glsl_shader_set_type m_attached_shaders;
222  mutable locations_type m_attribute_locations, m_uniform_locations;
223  mutable indices_type m_uniform_block_indices;
224 };
225 
226 
227 // glUniformBlockBinding
228 void bind_uniform_block_to_location(program &p_prog, GLuint const p_block_index, GLuint const p_location);
229 
230 
231 
232 
233 
234 namespace detail
235 {
236 
237 
238 typedef std::function < void(std::string const &p_errormsg) > errormsg_callback;
239 
240 
241 class build_program_impl
242 {
243 public:
244  explicit build_program_impl(program &p_prog, errormsg_callback const &p_errormsg_callback);
245  build_program_impl& operator()(GLenum const p_type, std::string const &p_identifier, std::string const &p_code);
246  build_program_impl& operator()(shader::sharedptr const &p_shaderptr);
247  void link();
248 private:
249  program &m_prog;
250  errormsg_callback m_errormsg_callback;
251 };
252 
253 
254 }
255 
256 
276 detail::build_program_impl build_program(program &p_prog, detail::errormsg_callback const &p_errormsg_callback = detail::errormsg_callback());
277 
278 
279 
280 }
281 
282 
283 #endif
284