Skip to content
Snippets Groups Projects
FrameBuffer.cpp 1.61 KiB
Newer Older
  • Learn to ignore specific revisions
  • BATON Theau's avatar
    BATON Theau committed
    #include "FrameBuffer.hpp"
    
    
    #include <stdexcept>
    
    BATON Theau's avatar
    BATON Theau committed
    
    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);
    
    BATON Theau's avatar
    BATON Theau committed
            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);
    
    
    BATON Theau's avatar
    BATON Theau committed
            if(!this->usable()) {
    
                throw std::runtime_error("Incomplete FrameBuffer.");
    
    BATON Theau's avatar
    BATON Theau committed
            }
        }
    
        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);
        }
    }