SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLFbo.cpp
Go to the documentation of this file.
1 /**
2  * \file SLGLFbo.cpp
3  * \brief Wraps an OpenGL framebuffer object
4  * \date September 2018
5  * \authors Stefan Thoeni
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 <SLGLState.h>
12 #include <SLGLFbo.h>
13 
14 //-----------------------------------------------------------------------------
16  SLuint h,
17  SLenum magFilter,
18  SLenum minFilter,
19  SLint internalFormat,
20  SLint format,
21  SLint wrap) : 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 }
76 //-----------------------------------------------------------------------------
78 {
79  glDeleteTextures(1, &texID);
80  glDeleteFramebuffers(1, &fboID);
81 }
82 //-----------------------------------------------------------------------------
83 void SLGLFbo::activateAsTexture(const int progId,
84  const SLstring& glSamplerName,
85  const int textureUnit)
86 {
87  glActiveTexture(GL_TEXTURE0 + textureUnit);
88  glBindTexture(GL_TEXTURE_2D, texID);
89  glUniform1i(glGetUniformLocation(progId,
90  glSamplerName.c_str()),
91  textureUnit);
92 }
93 //-----------------------------------------------------------------------------
unsigned int SLenum
Definition: SL.h:176
unsigned int SLuint
Definition: SL.h:171
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
Wraps an OpenGL framebuffer object.
Singleton class for global render state.
SLuint texID
Definition: SLGLFbo.h:38
void activateAsTexture(int progId, const SLstring &samplerName, int textureUnit=0)
Definition: SLGLFbo.cpp:83
~SLGLFbo()
Definition: SLGLFbo.cpp:77
SLuint fboID
Definition: SLGLFbo.h:37
SLuint rboID
Definition: SLGLFbo.h:39
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)
Definition: SLGLFbo.cpp:15