BARE2D
FBORenderer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <glm/glm.hpp>
4 #include <memory>
5 
6 #include "Camera2D.hpp"
7 #include "Renderer.hpp"
8 
9 namespace BARE2D {
10 
11  /**
12  * @class FBORenderer
13  * @brief The FBORenderer represents and manages an entire FrameBufferObject. This allows one to draw entire scenes
14  * first to a texture, then perform some operations on that texture, then draw that texture to the screen. Shaders have
15  * uniforms projectionMatrix (mat4), colourTexture (sampler2D), and depthTexture (sampler2D)
16  */
17  class FBORenderer : public Renderer {
18  public:
19  FBORenderer(std::string& fragShader,
20  std::string& vertShader,
21  unsigned int windowWidth,
22  unsigned int windowHeight,
23  unsigned int numColourAttachments = 1);
24  virtual ~FBORenderer();
25 
26  virtual void init() override;
27  virtual void initUniforms() override;
28 
29  void setCamera(std::shared_ptr<Camera2D>& cam);
30  std::shared_ptr<Camera2D> getCamera();
31 
32  void disableAttachment(unsigned int index);
33  void enableAttachment(unsigned int index);
34  void disableAttachments();
35  void enableAttachments();
36 
37  virtual void render() override;
38 
39  virtual void begin() override;
40  virtual void end() override;
41  virtual void destroy() override;
42 
43  protected:
44  virtual void preRender() override;
45 
46  virtual void createRenderBatches() override;
47 
48  /**
49  * @brief Binds the FBO and appropriate texture attachments
50  */
51  virtual void bind();
52 
53  /**
54  * @brief Unbinds the FBO + textures
55  */
56  virtual void unbind();
57 
58  /**
59  * @brief Creates all of the OpenGL textures, and takes their handles into m_colourTextureID, and m_depthTextureID
60  */
61  virtual void createTextures();
62 
63  /**
64  * @brief Creates the OpenGL FBO instance and holds its handle in m_fboID;
65  */
66  virtual void createFBO();
67 
68  // The handle for the FBO object
69  GLuint m_fboID;
70  // The handles for the texture to draw colours, depths, etc. to. Accessed purely through the shader/s.
71  GLuint* m_textureIDs = nullptr;
72  // One for colour (normally), one for depth+stencil (8 bits of the 32 are dedicated to the stencil). Total number of
73  // attachments to the FBO
74  unsigned int m_numTextures;
75  // Please note that the number of colour attachments is going to be the number of textures - 1.
76 
77  // The perspective camera (it also holds the window size, FBO position, etc.)
78  std::shared_ptr<Camera2D> m_camera;
79 
80  // The fragment and vertex shader paths for the GLSL shader program
82 
83  // Some shaders won't touch the depth buffer.
84  bool m_shaderHasDepth = false;
85  };
86 
87 } // namespace BARE2D
BARE2D::FBORenderer::m_fboID
GLuint m_fboID
Definition: FBORenderer.hpp:69
BARE2D::FBORenderer::~FBORenderer
virtual ~FBORenderer()
Definition: FBORenderer.cpp:30
BARE2D::Renderer
The renderer class holds some shader program, manages some VBO, some render batch(es),...
Definition: Renderer.hpp:15
BARE2D::FBORenderer::m_vertexShaderPath
std::string m_vertexShaderPath
Definition: FBORenderer.hpp:81
BARE2D::FBORenderer
The FBORenderer represents and manages an entire FrameBufferObject. This allows one to draw entire sc...
Definition: FBORenderer.hpp:17
BARE2D
Definition: App.cpp:13
BARE2D::FBORenderer::createFBO
virtual void createFBO()
Creates the OpenGL FBO instance and holds its handle in m_fboID;.
Definition: FBORenderer.cpp:341
BARE2D::FBORenderer::createTextures
virtual void createTextures()
Creates all of the OpenGL textures, and takes their handles into m_colourTextureID,...
Definition: FBORenderer.cpp:267
BARE2D::FBORenderer::preRender
virtual void preRender() override
Does stuff inside of the render function, within the shader's use.
Definition: FBORenderer.cpp:172
BARE2D::FBORenderer::m_numTextures
unsigned int m_numTextures
Definition: FBORenderer.hpp:74
BARE2D::FBORenderer::unbind
virtual void unbind()
Unbinds the FBO + textures.
Definition: FBORenderer.cpp:375
Camera2D.hpp
BARE2D::FBORenderer::enableAttachment
void enableAttachment(unsigned int index)
Definition: FBORenderer.cpp:46
BARE2D::FBORenderer::disableAttachment
void disableAttachment(unsigned int index)
Definition: FBORenderer.cpp:42
Renderer.hpp
BARE2D::FBORenderer::init
virtual void init() override
Initializes all necessary bits of the renderer.
Definition: FBORenderer.cpp:62
BARE2D::FBORenderer::FBORenderer
FBORenderer(std::string &fragShader, std::string &vertShader, unsigned int windowWidth, unsigned int windowHeight, unsigned int numColourAttachments=1)
Definition: FBORenderer.cpp:15
BARE2D::FBORenderer::setCamera
void setCamera(std::shared_ptr< Camera2D > &cam)
Definition: FBORenderer.cpp:34
BARE2D::FBORenderer::createRenderBatches
virtual void createRenderBatches() override
Constructs all of the render batches from data given by, say, draw() calls.
Definition: FBORenderer.cpp:192
BARE2D::FBORenderer::initUniforms
virtual void initUniforms() override
Initializes all uniforms, such as colour attachments, depth attachments, etc.
Definition: FBORenderer.cpp:110
BARE2D::FBORenderer::m_fragmentShaderPath
std::string m_fragmentShaderPath
Definition: FBORenderer.hpp:81
BARE2D::FBORenderer::m_camera
std::shared_ptr< Camera2D > m_camera
Definition: FBORenderer.hpp:78
BARE2D::FBORenderer::end
virtual void end() override
Creates the renderbatches, does necessary stuff before render() call.
Definition: FBORenderer.cpp:165
BARE2D::FBORenderer::bind
virtual void bind()
Binds the FBO and appropriate texture attachments.
Definition: FBORenderer.cpp:363
BARE2D::FBORenderer::m_shaderHasDepth
bool m_shaderHasDepth
Definition: FBORenderer.hpp:84
BARE2D::FBORenderer::m_textureIDs
GLuint * m_textureIDs
Definition: FBORenderer.hpp:71
BARE2D::FBORenderer::enableAttachments
void enableAttachments()
Definition: FBORenderer.cpp:56
BARE2D::FBORenderer::render
virtual void render() override
Actually renders the contents to the screen!
Definition: FBORenderer.cpp:232
BARE2D::FBORenderer::getCamera
std::shared_ptr< Camera2D > getCamera()
Definition: FBORenderer.cpp:38
BARE2D::FBORenderer::disableAttachments
void disableAttachments()
Definition: FBORenderer.cpp:50
BARE2D::FBORenderer::begin
virtual void begin() override
Clears the necessary vectors, etc. to prepare for draw() calls, etc.
Definition: FBORenderer.cpp:134
BARE2D::FBORenderer::destroy
virtual void destroy() override
Frees all necessary memory.
Definition: FBORenderer.cpp:124