Newer
Older
namespace megu {
FrameBuffer::FrameBuffer(GLuint width, GLuint height)
: _id(0), _rbo_id(0), _texture() {
glGenFramebuffers(1, &this->_id);
glBindFramebuffer(GL_FRAMEBUFFER, this->_id);
this->_texture.store(width, height, NULL, Texture::Format::RGB);
this->_texture.setSmoothing(false);
this->_texture.setWraping(Texture::Wraping::CLAMP_TO_EDGE, Texture::Axis::S | Texture::Axis::T);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->_texture.identifier(), 0);
glGenRenderbuffers(1, &this->_rbo_id);
glBindRenderbuffer(GL_RENDERBUFFER, this->_rbo_id);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, this->_rbo_id);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
throw std::runtime_error("Incomplete FrameBuffer.");
}
}
FrameBuffer::~FrameBuffer() {
glDeleteRenderbuffers(1, &this->_rbo_id);
glDeleteFramebuffers(1, &this->_id);
}
bool FrameBuffer::usable() const {
this->bind();
return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
}
void FrameBuffer::bind() const {
glBindFramebuffer(GL_FRAMEBUFFER, this->_id);
}
void FrameBuffer::unbind() const {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void FrameBuffer::BindDefaultFrameBuffer() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
}