AnimEngine
Framebuffer.hpp
1 #pragma once
2 #ifndef KJY_OBJECT_ORIENTED_OPENGL_FRAMEBUFFER_HPP_
3 #define KJY_OBJECT_ORIENTED_OPENGL_FRAMEBUFFER_HPP_
4 
5 #include <glad/glad.h>
6 #include <vector>
7 #include <stack>
8 #include "../OOOGL_Core.hpp"
9 #include "fbo_interface.hpp"
10 #include "Texture.hpp"
11 #include "Renderbuffer.hpp"
12 #include "../Core_Utilities.hpp"
13 
14 namespace OOOGL{
15 
16 class Framebuffer : public OglObject {
17 public:
18  Framebuffer() : OglObject(_destroyFBO) {
19  glGenFramebuffers(1, getIdPtr());
20  glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttach);
21  color_attachments = OptionalIndexedBindingVector<FramebufferAttachable>(maxAttach);
22  }
23 
24 
25  void attachRenderbuffer(const Renderbuffer& renderbuffer, GLenum attachment, bool clobber = false);
26  void attach(const SimpleTexture2D& tex2d, GLenum attachment, int level = 0, bool clobber = false);
27  void attach(const TextureArray& texarray, GLenum attachment, int layer, int level = 0);
28  void dettach(GLenum attachment);
29 
30  void setDrawBuffers(const std::vector<GLenum>& bufs, bool clobber = false);
31  void setDrawBuffers(GLsizei n, const GLenum *bufs, bool clobber = false);
32 
33  void setSingleDrawMode(GLenum mode);
34  void setSingleReadMode(GLenum mode);
35 
36  void setDefaultWidth(GLint width);
37  GLint getDefaultWidth() const {return(default_width);}
38  void setDefaultHeight(GLint height);
39  GLint getDefaultHeight() const {return(default_height);}
40  void setDefaultLayers(GLint layers);
41  GLint getDefaultLayers() const {return(default_layers);}
42  void setDefaultSamples(GLint samples);
43  GLint getDefaultSamples() const {return(default_samples);}
44  void setDefaultFixedSampleLocations(GLint param);
45  GLint getDefaultFixedSampleLocations() const {return(default_fixed_sample_locations);}
46 
47  void bind() const {
48  CHECKED_GL_CALL(glBindFramebuffer(GL_FRAMEBUFFER, getNameDirect()));
49  }
50  void bindDraw() const {
51  CHECKED_GL_CALL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, getNameDirect()));
52  }
53  void bindRead() const {
54  CHECKED_GL_CALL(glBindFramebuffer(GL_READ_FRAMEBUFFER, getNameDirect()));
55  }
56 
57  GLenum getStatus(GLenum target = GL_FRAMEBUFFER) const {
58  return(glCheckNamedFramebufferStatus(getNameDirect(), GL_FRAMEBUFFER));
59  }
60 
61  bool isComplete() const {
62  return(getStatus() == GL_FRAMEBUFFER_COMPLETE);
63  }
64 
65  static size_t get_stashed_id_count(){return(stashed_read_draw_ids.size());}
66 
67  static void push_framebuffer_state();
68 
69  static void pop_framebuffer_state();
70 
71 protected:
72  void storeReference(GLenum attachment, const FramebufferAttachable& globj);
73 
75  public:
76  AttachmentIndexOutOfBoundsException(GLuint index) : _whatstr("Out of bounds attachment index '" + std::to_string(index) + "'") {}
77  virtual const char* what() const noexcept override{return(_whatstr.c_str());}
78  private:
79  const std::string _whatstr;
80  };
81 
82  /* OglObject instances to maintain reference counting on attached objects*/
83  GLint maxAttach = 0;
85 
86  // I don't actually know the defaults for these so -1 for now
87  GLint default_width = -1;
88  GLint default_height = -1;
89  GLint default_layers = -1;
90  GLint default_samples = -1;
91  GLint default_fixed_sample_locations = -1;
92 
93 private:
94  static void _destroyFBO(const GLuint* self){
95  glDeleteFramebuffers(1, self);
96  }
97  static std::stack<std::pair<GLint, GLint>> stashed_read_draw_ids;
98 
99  FramebufferAttachable _local_stencil_attachment = nullptr;
100  FramebufferAttachable _local_depth_attachment = nullptr;
101  FramebufferAttachable _local_depth_stencil_attachment = nullptr;
102 };
103 
104 }
105 
106 #endif
Definition: Texture.hpp:60
Definition: Texture.hpp:88
Definition: Framebuffer.hpp:16
Definition: OOOGL_Core.hpp:144
Definition: OOOGL_Core.hpp:105
Definition: Renderbuffer.hpp:16
Definition: Core_Utilities.hpp:299
Definition: fbo_interface.hpp:10
Definition: Core_Utilities.cpp:3