SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLFbo Class Reference

Wrapper around an OpenGL framebuffer object for offscreen rendering. More...

#include <SLGLFbo.h>

Public Member Functions

 SLGLFbo (SLuint w, SLuint h, SLenum magFilter=GL_NEAREST, SLenum minFilter=GL_NEAREST, SLint internalFormat=GL_RGB16F, SLint format=GL_FLOAT, SLint wrap=GL_REPEAT)
 Creates the framebuffer, its colour texture and its depth renderbuffer. More...
 
 ~SLGLFbo ()
 Deletes the texture, the renderbuffer and the framebuffer. More...
 
void activateAsTexture (int progId, const SLstring &samplerName, int textureUnit=0)
 Binds the colour texture to a texture unit and sets the sampler uniform. More...
 

Public Attributes

SLuint width
 Width of the framebuffer in pixels. More...
 
SLuint height
 Height of the framebuffer in pixels. More...
 
SLuint attachment
 
SLuint fboID
 GL id of the framebuffer object. More...
 
SLuint texID
 GL id of the colour attachment texture. More...
 
SLuint rboID
 GL id of the depth renderbuffer. More...
 

Detailed Description

Wrapper around an OpenGL framebuffer object for offscreen rendering.

An SLGLFbo owns three GL objects: a framebuffer, a 2D colour texture bound to GL_COLOR_ATTACHMENT0, and a renderbuffer holding a 24 bit depth buffer. Rendering into it and then binding the result as a texture is the usual way to implement a post-processing or reflection pass.

Warning
The constructor issues OpenGL calls, so an instance may only be created on the main thread once a GL context exists — unlike the mesh classes, it cannot be built during asynchronous asset loading.

Definition at line 26 of file SLGLFbo.h.

Constructor & Destructor Documentation

◆ SLGLFbo()

SLGLFbo::SLGLFbo ( SLuint  w,
SLuint  h,
SLenum  magFilter = GL_NEAREST,
SLenum  minFilter = GL_NEAREST,
SLint  internalFormat = GL_RGB16F,
SLint  format = GL_FLOAT,
SLint  wrap = GL_REPEAT 
)

Creates the framebuffer, its colour texture and its depth renderbuffer.

Parameters
wWidth in pixels
hHeight in pixels
magFilterGL magnification filter of the colour texture
minFilterGL minification filter of the colour texture
internalFormatGL internal format of the colour texture
formatGL data type of the colour texture pixels
wrapGL wrap mode applied to both S and T The previously bound framebuffer is restored before returning. A completeness failure is reported on stderr, not thrown.

Definition at line 15 of file SLGLFbo.cpp.

21  : width(w), height(h)
22 {
23  GLint previousFrameBuffer;
24  glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previousFrameBuffer);
25 
26  // Init framebuffer.
27  glGenFramebuffers(1, &fboID);
28  glBindFramebuffer(GL_FRAMEBUFFER, fboID);
29 
30  glGenTextures(1, &texID);
31  glBindTexture(GL_TEXTURE_2D, texID);
32 
33  // Texture parameters.
34  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
35  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
36  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
37  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
38 
39  glTexImage2D(GL_TEXTURE_2D,
40  0,
41  internalFormat,
42  w,
43  h,
44  0,
45  GL_RGBA,
46  format,
47  nullptr);
48 
49  glFramebufferTexture2D(GL_FRAMEBUFFER,
50  GL_COLOR_ATTACHMENT0,
51  GL_TEXTURE_2D,
52  texID,
53  0);
54 
55  glGenRenderbuffers(1, &rboID);
56  glBindRenderbuffer(GL_RENDERBUFFER, rboID);
57 
58  // Use a single rbo for both depth and stencil buffer
59  glRenderbufferStorage(GL_RENDERBUFFER,
60  GL_DEPTH_COMPONENT24,
61  w,
62  h);
63 
64  glFramebufferRenderbuffer(GL_FRAMEBUFFER,
65  GL_DEPTH_ATTACHMENT,
66  GL_RENDERBUFFER,
67  rboID);
68 
69  glBindRenderbuffer(GL_RENDERBUFFER, 0);
70  glBindFramebuffer(GL_FRAMEBUFFER,
71  previousFrameBuffer);
72 
73  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
74  std::cerr << "FBO failed to initialize correctly." << std::endl;
75 }
SLuint texID
GL id of the colour attachment texture.
Definition: SLGLFbo.h:62
SLuint height
Height of the framebuffer in pixels.
Definition: SLGLFbo.h:59
SLuint fboID
GL id of the framebuffer object.
Definition: SLGLFbo.h:61
SLuint rboID
GL id of the depth renderbuffer.
Definition: SLGLFbo.h:63
SLuint width
Width of the framebuffer in pixels.
Definition: SLGLFbo.h:58

◆ ~SLGLFbo()

SLGLFbo::~SLGLFbo ( )

Deletes the texture, the renderbuffer and the framebuffer.

Definition at line 77 of file SLGLFbo.cpp.

78 {
79  glDeleteTextures(1, &texID);
80  glDeleteFramebuffers(1, &fboID);
81 }

Member Function Documentation

◆ activateAsTexture()

void SLGLFbo::activateAsTexture ( int  progId,
const SLstring samplerName,
int  textureUnit = 0 
)

Binds the colour texture to a texture unit and sets the sampler uniform.

Parameters
progIdGL id of the shader program holding the sampler
samplerNameName of the sampler uniform in that program
textureUnitTexture unit to bind to, added to GL_TEXTURE0

Definition at line 83 of file SLGLFbo.cpp.

86 {
87  glActiveTexture(GL_TEXTURE0 + textureUnit);
88  glBindTexture(GL_TEXTURE_2D, texID);
89  glUniform1i(glGetUniformLocation(progId,
90  glSamplerName.c_str()),
91  textureUnit);
92 }

Member Data Documentation

◆ attachment

SLuint SLGLFbo::attachment
Deprecated:
Never assigned or read; left uninitialised

Definition at line 60 of file SLGLFbo.h.

◆ fboID

SLuint SLGLFbo::fboID

GL id of the framebuffer object.

Definition at line 61 of file SLGLFbo.h.

◆ height

SLuint SLGLFbo::height

Height of the framebuffer in pixels.

Definition at line 59 of file SLGLFbo.h.

◆ rboID

SLuint SLGLFbo::rboID

GL id of the depth renderbuffer.

Definition at line 63 of file SLGLFbo.h.

◆ texID

SLuint SLGLFbo::texID

GL id of the colour attachment texture.

Definition at line 62 of file SLGLFbo.h.

◆ width

SLuint SLGLFbo::width

Width of the framebuffer in pixels.

Definition at line 58 of file SLGLFbo.h.


The documentation for this class was generated from the following files: