hx3d  1
2D/3D Simple Game Framework
framebuffer.cpp
1 /*
2  Framebuffer.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/framebuffer.hpp"
22 
23 #include "hx3d/core/core.hpp"
24 
25 #include "hx3d/window/application.hpp"
26 #include "hx3d/window/game.hpp"
27 #include "hx3d/graphics/texture.hpp"
28 
29 #include "hx3d/utils/log.hpp"
30 
31 namespace hx3d {
32 namespace graphics {
33 
34 GLint Framebuffer::_defaultID = 0;
35 
37  Framebuffer(Core::App()->getWidth(), Core::App()->getHeight())
38 {}
39 
40 Framebuffer::Framebuffer(unsigned int width, unsigned int height):
41  _width(width),
42  _height(height)
43 {
44  create();
45 }
46 
47 Framebuffer::~Framebuffer() {
48  if (glIsFramebuffer(_id) == GL_TRUE)
49  glDeleteFramebuffers(1, &_id);
50  if (glIsRenderbuffer(_depthBuffer) == GL_TRUE)
51  glDeleteRenderbuffers(1, &_depthBuffer);
52 
53  Log.Info("FBO deleted");
54 }
55 
56 void Framebuffer::resize(unsigned int width, unsigned int height) {
57  _width = width;
58  _height = height;
59 
60  create();
61 }
62 
64  glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_defaultID);
65 }
66 
67 void Framebuffer::create() {
68  if (glIsFramebuffer(_id) == GL_TRUE)
69  glDeleteFramebuffers(1, &_id);
70 
71  // Génération d'un id
72  glGenFramebuffers(1, &_id);
73 
74  glBindFramebuffer(GL_FRAMEBUFFER, _id);
75 
76  _colorBuffer = Texture::createColorBuffer(_width, _height);
77  createRenderBuffer(_depthBuffer, GL_DEPTH_COMPONENT16);
78  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _colorBuffer->getID(), 0);
79  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthBuffer);
80 
81  if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
82  {
83  glDeleteFramebuffers(1, &_id);
84  glDeleteRenderbuffers(1, &_depthBuffer);
85 
86  Log.Error("FBO Error.");
87  }
88  else {
89  Log.Info(format("FBO created (size: %dx%d).", _width, _height));
90  }
91 
92  glBindFramebuffer(GL_FRAMEBUFFER, _defaultID);
93 }
94 
96  glBindFramebuffer(GL_FRAMEBUFFER, buffer._id);
97  glViewport(0, 0, buffer._width, buffer._height);
98 }
99 
101  glBindFramebuffer(GL_FRAMEBUFFER, _defaultID);
102  glViewport(0, 0, Core::App()->getWidth(), Core::App()->getHeight());
103 }
104 
106  glm::vec4 colorFloat = color.toFloat();
107 
108  glClearColor(colorFloat.r, colorFloat.g, colorFloat.b, colorFloat.a);
109  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110 }
111 
113  return _colorBuffer;
114 }
115 
116 void Framebuffer::createRenderBuffer(GLuint& id, GLenum format) {
117 
118  if(glIsRenderbuffer(id) == GL_TRUE)
119  glDeleteRenderbuffers(1, &id);
120 
121  glGenRenderbuffers(1, &id);
122 
123  glBindRenderbuffer(GL_RENDERBUFFER, id);
124 
125  glRenderbufferStorage(GL_RENDERBUFFER, format, _width, _height);
126 
127  glBindRenderbuffer(GL_RENDERBUFFER, 0);
128 }
129 
130 } /* graphics */
131 } /* hx3d */
glm::vec4 toFloat()
Convert the color to a float format (between 0 and 1).
Definition: color.cpp:42
std::string format(const std::string fmt,...)
Format a string using printf notation.
Definition: string.cpp:27
Ptr< Texture > getColorBuffer()
Get the framebuffer color buffer.
Four [0..255] components defined color.
Definition: color.hpp:32
static void fetchDefaultFramebuffer()
Fetch the default framebuffer of the application.
Definition: framebuffer.cpp:63
static window::Application * App()
Get the application instance.
Definition: core.cpp:78
hx3d framework namespace
Definition: audio.hpp:26
void resize(unsigned int width, unsigned int height)
Resize the framebuffer.
Definition: framebuffer.cpp:56
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
Centralized framework management.
Definition: core.hpp:50
static Ptr< Texture > createColorBuffer(unsigned int width, unsigned int height)
Create a color buffer (used in framebuffers).
Definition: texture.cpp:140
Render-to-texture buffer.
Definition: framebuffer.hpp:37
Framebuffer()
Create a framebuffer at screen size.
Definition: framebuffer.cpp:36
static void useDefault()
Use the default framebuffer.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
static void use(Framebuffer &buf)
Use the framebuffer as current framebuffer.
Definition: framebuffer.cpp:95
static void clear(Color color)
Clear the framebuffer.
void Info(const std::string fmt,...)
Write an info message.
Definition: log.cpp:58