21 #include "hx3d/graphics/framebuffer.hpp" 23 #include "hx3d/core/core.hpp" 25 #include "hx3d/window/application.hpp" 26 #include "hx3d/window/game.hpp" 27 #include "hx3d/graphics/texture.hpp" 29 #include "hx3d/utils/log.hpp" 34 GLint Framebuffer::_defaultID = 0;
47 Framebuffer::~Framebuffer() {
48 if (glIsFramebuffer(_id) == GL_TRUE)
49 glDeleteFramebuffers(1, &_id);
50 if (glIsRenderbuffer(_depthBuffer) == GL_TRUE)
51 glDeleteRenderbuffers(1, &_depthBuffer);
64 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_defaultID);
67 void Framebuffer::create() {
68 if (glIsFramebuffer(_id) == GL_TRUE)
69 glDeleteFramebuffers(1, &_id);
72 glGenFramebuffers(1, &_id);
74 glBindFramebuffer(GL_FRAMEBUFFER, _id);
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);
81 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
83 glDeleteFramebuffers(1, &_id);
84 glDeleteRenderbuffers(1, &_depthBuffer);
89 Log.
Info(
format(
"FBO created (size: %dx%d).", _width, _height));
92 glBindFramebuffer(GL_FRAMEBUFFER, _defaultID);
96 glBindFramebuffer(GL_FRAMEBUFFER, buffer._id);
97 glViewport(0, 0, buffer._width, buffer._height);
101 glBindFramebuffer(GL_FRAMEBUFFER, _defaultID);
106 glm::vec4 colorFloat = color.
toFloat();
108 glClearColor(colorFloat.r, colorFloat.g, colorFloat.b, colorFloat.a);
109 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
116 void Framebuffer::createRenderBuffer(GLuint&
id, GLenum
format) {
118 if(glIsRenderbuffer(
id) == GL_TRUE)
119 glDeleteRenderbuffers(1, &
id);
121 glGenRenderbuffers(1, &
id);
123 glBindRenderbuffer(GL_RENDERBUFFER,
id);
125 glRenderbufferStorage(GL_RENDERBUFFER, format, _width, _height);
127 glBindRenderbuffer(GL_RENDERBUFFER, 0);
glm::vec4 toFloat()
Convert the color to a float format (between 0 and 1).
std::string format(const std::string fmt,...)
Format a string using printf notation.
Ptr< Texture > getColorBuffer()
Get the framebuffer color buffer.
Four [0..255] components defined color.
static void fetchDefaultFramebuffer()
Fetch the default framebuffer of the application.
static window::Application * App()
Get the application instance.
void resize(unsigned int width, unsigned int height)
Resize the framebuffer.
void Error(const std::string fmt,...)
Write an error message.
static hx3d::LogImpl Log
Current log implementation.
Centralized framework management.
static Ptr< Texture > createColorBuffer(unsigned int width, unsigned int height)
Create a color buffer (used in framebuffers).
Render-to-texture buffer.
Framebuffer()
Create a framebuffer at screen size.
static void useDefault()
Use the default framebuffer.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
static void use(Framebuffer &buf)
Use the framebuffer as current framebuffer.
static void clear(Color color)
Clear the framebuffer.
void Info(const std::string fmt,...)
Write an info message.