Skip to content

Instantly share code, notes, and snippets.

@donkaban
Created July 8, 2014 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donkaban/c7fed0f3eb922cab3736 to your computer and use it in GitHub Desktop.
Save donkaban/c7fed0f3eb922cab3736 to your computer and use it in GitHub Desktop.
simple Framebuffer
class FrameBuffer
{
private:
uint bufferID = 0;
uint depthID = 0;
uint textureID = 0;
bool depth = false;
uint width = 0;
uint height = 0;
public:
FrameBuffer(int w, int h, bool depth = false);
~FrameBuffer();
void setCurrent();
uint getTextureID() {return textureID;}
};
FrameBuffer::FrameBuffer(int w, int h, bool depth) :
depth(depth),
width(w),
height(h)
{
GLint maxRBSize;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxRBSize);
if(maxRBSize <= w || maxRBSize <= h)
FATAL_ERROR("bad framebuffer size : %dx%d ", w,h);
glGenFramebuffers(1, &bufferID);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindFramebuffer(GL_FRAMEBUFFER, bufferID);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, textureID, 0);
if(depth)
{
glGenRenderbuffers(1, &depthID);
glBindRenderbuffer(GL_RENDERBUFFER, depthID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,w,h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER, depthID);
}
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
FATAL_ERROR("FBO %dx%d depth: %d id: %d not complete!", w,h,depth,bufferID);
}
FrameBuffer::~FrameBuffer()
{
glDeleteTextures(1, &textureID);
if(depth)
glDeleteRenderbuffers(1, &depthID);
glDeleteFramebuffers(1, &bufferID);
}
void FrameBuffer::setCurrent()
{
glViewport(0,0,width,height);
glBindFramebuffer(GL_FRAMEBUFFER,bufferID);
if(depth) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment