Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
FramebufferObject.cpp
Go to the documentation of this file.
1 #include "FramebufferObject.h"
2 
3 using namespace vis2;
4 
5 FramebufferObject::FramebufferObject(const unsigned int _vp_width, const unsigned int _vp_height, const bool _color_only)
6  : color_only(_color_only),
7  fbo_handle(0),
8  fbo_setup_done(false),
9  fbo_status(VIS2_FBO_SETUP_FAIL),
10  fbo_width(_vp_width), fbo_height(_vp_height),
11  fbo_renderbuffer_color_handle(0),
12  fbo_renderbuffer_depth_handle(0)
13 {
14 
15  // ------------------------- CREATE AND BIND FBO -------------------------------- //
16 
17  // create and bind a FrameBuffer Object
18  glGenFramebuffers(1, &fbo_handle);
19  glBindFramebuffer(GL_FRAMEBUFFER, fbo_handle);
20 
21  // create and bind a renderable object - a RenderBuffer
22  // // glGenRenderbuffers(1, &fbo_renderbuffer_color_handle);
23  // // glBindRenderbuffer(GL_RENDERBUFFER, fbo_renderbuffer_color_handle);
24 
25  // // if (!color_only)
26  // // {
27  // // glGenRenderbuffers(1, &fbo_renderbuffer_depth_handle);
28  // // glBindRenderbuffer(GL_RENDERBUFFER, fbo_renderbuffer_depth_handle);
29  // // }
30 
31  // create storage for the render buffer and attach it to the FBO
32  // // glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB, _vp_width, _vp_height);
33  // // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fbo_renderbuffer_color_handle);
34 
35  // // if (!color_only)
36  // // {
37  // // glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, _vp_width, _vp_height);
38  // // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo_renderbuffer_depth_handle);
39  // // }
40 
41  // create and bind a texture
42  this->fbo_color_texture = new Texture(_vp_width, _vp_height, VIS2_TEX_TYPE_RGBA);
43  this->fbo_color_texture->textureBind2FBO(0); // to color attachment 0
44 
45  if (!color_only)
46  {
47  this->fbo_depth_texture = new Texture(_vp_width, _vp_height, VIS2_TEX_TYPE_DEPTH);
49  }
50  // // --------------------- NOTE: for depth-only FBOs !!! ------------------------ //
51  // // glDrawBuffer(GL_NONE);
52  // // glReadBuffer(GL_NONE);
53  // // --------------------- NOTE: for depth-only FBOs !!! ------------------------ //
54 
55  // check if the setup worked:
56  GLenum fbo_gl_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
57  if (fbo_gl_status != GL_FRAMEBUFFER_COMPLETE)
58  {
59  // ------------------------- DEACTIVATE ALL BUFFERS ------------------------- //
60  glBindFramebuffer(GL_FRAMEBUFFER, 0); // deactivate fbo
61  // glBindRenderbuffer(GL_RENDERBUFFER, 0); /// deactivate the renderbuffer
62 
63  std::cout << "Could not complete FBO setup successfully :( !" << std::endl;
65  }
66  else
67  {
68  // ------------------------- DEACTIVATE ALL BUFFERS ------------------------- //
69  glBindFramebuffer(GL_FRAMEBUFFER, 0); // deactivate fbo
70  // glBindRenderbuffer(GL_RENDERBUFFER, 0); /// deactivate the renderbuffer
71 
72  // std::cout << "FBO setup completed successfully :) !" << std::endl;
73  fbo_setup_done = true;
75  }
76 }
77 
79 {
80  glDeleteFramebuffers(1,&fbo_handle);
81  glDeleteRenderbuffers(1,&fbo_renderbuffer_color_handle);
82  glDeleteRenderbuffers(1,&fbo_renderbuffer_depth_handle);
83 
84  delete fbo_color_texture;
85  if (!color_only)
86  delete fbo_depth_texture;
87 }
88 
89 void FramebufferObject::bindFramebufferObject(const float _r, const float _g, const float _b, const float _a)
90 {
92  return;
93 
94  // bind the fbo
95  glBindFramebuffer(GL_FRAMEBUFFER, fbo_handle);
96 
97  // declare the viewport
98  glClearColor(_r, _g, _b, _a);
99  glViewport(0,0,fbo_width,fbo_height);
100 
101  // clear the color and depth buffers
102  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103 
104 }
105 
107 {
108  // unbind the FBO
109  GLenum fbo_gl_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
110  if (fbo_gl_status == GL_FRAMEBUFFER_UNSUPPORTED)
111  std::cout << "GL_FRAMEBUFFER_UNSUPPORTED" << std::endl;
112  if (fbo_gl_status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
113  std::cout << "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT" << std::endl;
114  if (fbo_gl_status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT)
115  std::cout << "FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT" << std::endl;
116  if (fbo_gl_status == GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER)
117  std::cout << "FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER" << std::endl;
118  if (fbo_gl_status == GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER)
119  std::cout << "FRAMEBUFFER_INCOMPLETE_READ_BUFFER" << std::endl;
120 
121  glBindFramebuffer(GL_FRAMEBUFFER, 0);
122 }