#include <GL/glew.h>
#include "frameBuffer.h"

namespace render{

frameBuffer::frameBuffer()
{
    _obj = 0;
}

frameBuffer::~frameBuffer()
{
    free();
}

void frameBuffer::gen()
{
    if(glIsFramebuffer(_obj))
        return;
    _alloc();
    glGenFramebuffers(1, &_obj);
}

void frameBuffer::_alloc()
{
    _addToList();
}

void frameBuffer::free()
{
    if(_obj)
    {
        unbind();
        glDeleteFramebuffers(1, &_obj);
        _obj = 0;
    }
    _removeFromList();
}

void frameBuffer::bind(target t)
{
    glBindFramebuffer((uint)t, _obj);
    _t = t;
}

void frameBuffer::unbind()
{
    glBindFramebuffer((uint)_t, 0);
}

void frameBuffer::attachTexture(attach type, texture &tex)
{
    bind();
    glFramebufferTexture2D(GL_FRAMEBUFFER, (GLenum)type, tex.getMode(), tex.getGLTexture(), 0);
}

}
