GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
shader.hpp
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include <gls/headercheck.hpp>
8 #include <gls/errorcheck.hpp>
9 #include <gls/objects/object.hpp>
10 
11 #if !defined( NDEBUG )
12  #if !defined( GLS_ERROR_STREAM )
13  #include <iostream>
14  #define GLS_ERROR_STREAM std::cerr
15  #endif
16 #endif
17 
18 namespace gls {
19 
20 namespace priv {
21  template<GLenum Type>
22  void create_shader( GLsizei, GLuint* name ) { *name = glCreateShader( Type ); }
23  void delete_shader( GLsizei, const GLuint* name ) { glDeleteShader( *name ); }
24 }
25 
32 template<GLenum Type>
33 class shader {
34 public:
41  GLuint name() const {
42  return m_object.name();
43  }
44 
59  bool compile( const std::string& source ) {
60  auto c_str = source.c_str();
61 
62  check_gl_error( glShaderSource( name(), 1, reinterpret_cast<const GLchar**>( &c_str ), nullptr ) );
63  check_gl_error( glCompileShader( name() ) );
64 
65  auto compile_status = GL_FALSE;
66  check_gl_error( glGetShaderiv( name(), GL_COMPILE_STATUS, &compile_status ) );
67 
68 #if !defined( NDEBUG )
69  auto info_log = get_info_log();
70 
71  if( !info_log.empty() ) {
72  GLS_ERROR_STREAM << std::move( info_log );
73  }
74 #endif
75 
76  return compile_status == GL_TRUE;
77  }
78 
90  std::string get_info_log() const {
91  auto info_log_length = GLint();
92  check_gl_error( glGetShaderiv( name(), GL_INFO_LOG_LENGTH, &info_log_length ) );
93 
94  if( !info_log_length ) {
95  return std::string();
96  }
97 
98  auto info_log = std::vector<GLchar>( static_cast<std::size_t>( info_log_length ), 0 );
99  check_gl_error( glGetShaderInfoLog( name(), info_log_length, nullptr, info_log.data() ) );
100 
101  return std::string( static_cast<const char*>( info_log.data() ) );
102  }
103 
104 private:
105  object<priv::create_shader<Type>, priv::delete_shader> m_object;
106 };
107 
108 }
109 
Class encapsulating an OpenGL shader object.
Definition: shader.hpp:33
bool compile(const std::string &source)
Compile this shader from the given source.
Definition: shader.hpp:59
std::string get_info_log() const
Get the link information log.
Definition: shader.hpp:90
GLuint name() const
Retrieve the OpenGL name of this shader.
Definition: shader.hpp:41
A RAII wrapper around any OpenGL object.
Definition: object.hpp:21
Definition: buffer.hpp:12