GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
framebuffer.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 #include <gls/objects/renderbuffer.hpp>
11 #include <type_traits>
12 #include <unordered_map>
13 #include <cassert>
14 
15 namespace gls {
16 
17 namespace priv {
18  void gen_framebuffers( GLsizei size, GLuint* name ) { glGenFramebuffers( size, name ); }
19  void delete_framebuffers( GLsizei size, const GLuint* name ) { glDeleteFramebuffers( size, name ); }
20 }
21 
26 class framebuffer {
27 public:
34  GLuint name() const {
35  return m_object.name();
36  }
37 
48  void bind() {
49  check_gl_error( glBindFramebuffer( GL_FRAMEBUFFER, m_object.name() ) );
50  }
51 
62  static void unbind() {
63  check_gl_error( glBindFramebuffer( GL_FRAMEBUFFER, 0 ) );
64  }
65 
75  void attach_texture( GLenum attachment, GLenum textarget, const texture<GL_TEXTURE_CUBE_MAP>& the_texture, GLint level ) {
76  bind();
77  check_gl_error( glFramebufferTexture2D( GL_FRAMEBUFFER, attachment, textarget, the_texture.name(), level ) );
78  unbind();
79  }
80 
89  template<GLenum Target>
90  void attach_texture( GLenum attachment, const texture<Target>& the_texture, GLint level ) {
91  static_assert( Target != GL_TEXTURE_CUBE_MAP, "glFramebufferTexture cannot take non-array cubemaps" );
92 
93  bind();
94  check_gl_error( glFramebufferTexture( GL_FRAMEBUFFER, attachment, the_texture.name(), level ) );
95  unbind();
96  }
97 
107  template<GLenum Target>
108  void attach_texture_layer( GLenum attachment, const texture<Target>& the_texture, GLint level, GLint layer ) {
109  static_assert( Target != GL_TEXTURE_CUBE_MAP, "glFramebufferTextureLayer cannot take non-array cubemaps" );
110 
111  bind();
112  check_gl_error( glFramebufferTextureLayer( GL_FRAMEBUFFER, attachment, the_texture.name(), level, layer ) );
113  unbind();
114  }
115 
122  void detach_texture( GLenum attachment ) {
123  bind();
124  check_gl_error( glFramebufferTexture( GL_FRAMEBUFFER, attachment, 0, 0 ) );
125  unbind();
126  }
127 
140  void add_renderbuffer( GLenum attachment, renderbuffer&& the_renderbuffer ) {
141  bind();
142  check_gl_error( glFramebufferRenderbuffer( GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, the_renderbuffer.name() ) );
143  m_renderbuffers.emplace( attachment, std::forward<renderbuffer>( the_renderbuffer ) );
144  unbind();
145  }
146 
153  void remove_renderbuffer( GLenum attachment ) {
154  bind();
155  check_gl_error( glFramebufferRenderbuffer( GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, 0 ) );
156  m_renderbuffers.erase( attachment );
157  unbind();
158  }
159 
166  GLenum status() {
167  bind();
168  auto status_value = check_gl_error( glCheckFramebufferStatus( GL_FRAMEBUFFER ) );
169  unbind();
170 
171  return status_value;
172  }
173 
180  bool complete() {
181  return status() == GL_FRAMEBUFFER_COMPLETE;
182  }
183 
184 private:
185  std::unordered_map<GLenum, renderbuffer> m_renderbuffers;
187 };
188 
189 }
190 
GLuint name() const
Retrieve the OpenGL name of this texture.
Definition: texture.hpp:47
bool complete()
Check if the framebuffer is complete.
Definition: framebuffer.hpp:180
void attach_texture(GLenum attachment, GLenum textarget, const texture< GL_TEXTURE_CUBE_MAP > &the_texture, GLint level)
Attach a non-array cubemap to the given attachment.
Definition: framebuffer.hpp:75
GLenum status()
Check the status of the framebuffer.
Definition: framebuffer.hpp:166
Class encapsulating an OpenGL framebuffer object.
Definition: framebuffer.hpp:26
void remove_renderbuffer(GLenum attachment)
Clear a renderbuffer attachment.
Definition: framebuffer.hpp:153
void bind()
Bind this framebuffer to the GL_FRAMEBUFFER target.
Definition: framebuffer.hpp:48
void attach_texture_layer(GLenum attachment, const texture< Target > &the_texture, GLint level, GLint layer)
Attach a texture layer to the given attachment.
Definition: framebuffer.hpp:108
GLuint name() const
Retrieve the OpenGL name of this framebuffer.
Definition: framebuffer.hpp:34
void detach_texture(GLenum attachment)
Clear a texture attachment.
Definition: framebuffer.hpp:122
void add_renderbuffer(GLenum attachment, renderbuffer &&the_renderbuffer)
Attach a renderbuffer to the given attachment.
Definition: framebuffer.hpp:140
Class encapsulating an OpenGL renderbuffer object.
Definition: renderbuffer.hpp:23
Definition: buffer.hpp:12
Class encapsulating an OpenGL texture object.
Definition: texture.hpp:26
static void unbind()
Unbind the framebuffer currently bound to the GL_FRAMEBUFFER target.
Definition: framebuffer.hpp:62
void attach_texture(GLenum attachment, const texture< Target > &the_texture, GLint level)
Attach a texture to the given attachment.
Definition: framebuffer.hpp:90