SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLFrameBuffer.cpp
Go to the documentation of this file.
1 /**
2  * \file SLGLFrameBuffer.cpp
3  * \brief Wrapper class around OpenGL Frame Buffer Objects (FBO)
4  * \date April 2018
5  * \authors Carlos Arauz, Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #include <SLGLFrameBuffer.h>
12 
13 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 //! Constructor
19  SLsizei rboHeight)
20 {
21  _fboId = 0;
22  _rboId = 0;
23  _prevFboId = 0;
26 }
27 //-----------------------------------------------------------------------------
28 //! clear delete buffers and respectively adjust the stats variables
30 {
31  deleteGL();
34 }
35 //-----------------------------------------------------------------------------
36 //! calls the delete functions only if the buffers exist
38 {
39  unbind();
40 
41  if (_fboId)
42  {
43  glDeleteBuffers(1, &_fboId);
44  _fboId = 0;
45  }
46 
47  if (_rboId)
48  {
49  glDeleteBuffers(1, &_rboId);
50  _rboId = 0;
51  }
52 }
53 //-----------------------------------------------------------------------------
54 //! generate the frame buffer and the render buffer if wanted
56 {
57  if (_fboId == 0)
58  {
59  glGenFramebuffers(1, &_fboId);
60  glGenRenderbuffers(1, &_rboId);
62  glFramebufferRenderbuffer(GL_FRAMEBUFFER,
63  GL_DEPTH_ATTACHMENT,
64  GL_RENDERBUFFER,
65  _rboId);
66 
67  // test if the generated fbo is valid
68  if ((glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE)
69  SL_EXIT_MSG("Frame buffer creation failed!");
70 
71  unbind();
73 
75  }
76 }
77 //-----------------------------------------------------------------------------
79 {
80  assert(_fboId && "No framebuffer generated");
81 
82  // Keep the previous FB ID for later unbinding
83  SLint prevFboId;
84  glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevFboId);
85  if (prevFboId != _fboId)
86  _prevFboId = prevFboId;
87 
88  glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
89 
90  assert(_rboId && "No renderbuffer generated");
91  glBindRenderbuffer(GL_RENDERBUFFER, _rboId);
92 }
93 //-----------------------------------------------------------------------------
95 {
96  // iOS does not allow binding to 0. That's why we keep the previous FB ID
97  glBindFramebuffer(GL_FRAMEBUFFER, _prevFboId);
98 
99  glBindRenderbuffer(GL_RENDERBUFFER, 0);
100 }
101 //-----------------------------------------------------------------------------
102 //! change the render buffer size at will
104  SLsizei height)
105 {
106  bind();
107  _rboWidth = width;
108  _rboHeight = height;
109  glRenderbufferStorage(GL_RENDERBUFFER,
110  GL_DEPTH_COMPONENT24,
111  width,
112  height);
113 }
114 //-----------------------------------------------------------------------------
115 //! attach one 2D texture to the frame buffer
117  SLenum target,
118  SLGLTexture* texture,
119  SLint level)
120 {
121  assert(_fboId && _rboId);
122  glFramebufferTexture2D(GL_FRAMEBUFFER,
123  attachment,
124  target,
125  texture->texID(),
126  level);
127 }
128 //-----------------------------------------------------------------------------
129 /*
130 SLfloat* SLGLFrameBuffer::readPixels() const
131 {
132  SLfloat* depth = new SLfloat[_dimensions.y * _dimensions.x];
133  glReadPixels(0,
134  0,
135  _rboWidth,
136  _rboHeight,
137  GL_DEPTH_COMPONENT,
138  GL_FLOAT,
139  depth);
140  GET_GL_ERROR;
141  return depth;
142 }
143  */
144 //-----------------------------------------------------------------------------
unsigned int SLenum
Definition: SL.h:176
unsigned int SLuint
Definition: SL.h:171
#define SL_EXIT_MSG(message)
Definition: SL.h:240
int SLsizei
Definition: SL.h:172
int SLint
Definition: SL.h:170
Wrapper class around OpenGL Frame Buffer Objects (FBO)
#define GET_GL_ERROR
Definition: SLGLState.h:56
SLuint _prevFboId
previously active frame buffer identifier
SLsizei rboWidth()
void attachTexture2D(SLenum attachment, SLenum target, SLGLTexture *texture, SLint level=0)
Attaches texture image to framebuffer.
SLsizei _rboHeight
height of the render buffer
void clear()
Calls delete and clears data.
void bindAndSetBufferStorage(SLsizei width, SLsizei height)
Sets the size of the buffer storage.
static SLuint totalBufferCount
SLuint _fboId
static total size of all buffers in bytes
SLsizei _rboWidth
width of the render buffer
SLGLFrameBuffer(SLsizei rboWidth, SLsizei rboHeight)
Constructor.
void generate()
Generates the framebuffer.
static SLuint totalBufferSize
static total no. of buffers in use
void unbind()
Unbinds the framebuffer and renderbuffer.
SLuint _rboId
render buffer identifier
SLuint _sizeBytes
size in bytes of this buffer
void bind()
Binds the framebuffer and renderbuffer.
SLsizei rboHeight()
void deleteGL()
Deletes this buffers.
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
SLuint texID() const
Definition: SLGLTexture.h:227