1 #include "framebuffer.h"
6 void FrameBuffer::Create(int w, int h)
10 // FrameBuffer was already Created, destroy it before creating again
15 glGenTextures(1, &m_render_texture);
16 glGenFramebuffers(1, &m_render_fbo);
18 glBindTexture(GL_TEXTURE_2D, m_render_texture);
19 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
21 glBindFramebuffer(GL_FRAMEBUFFER, m_render_fbo);
23 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_render_texture, 0);
25 glBindFramebuffer(GL_FRAMEBUFFER, 0);
26 glBindTexture(GL_TEXTURE_2D, 0);
29 void FrameBuffer::Destroy()
31 if (!m_render_texture) return; // wasn't Created (or already Destroyed)
32 glDeleteFramebuffers(1, &m_render_fbo);
33 glDeleteTextures(1, &m_render_texture);
36 void FrameBuffer::Bind()
38 // will draw to this FrameBuffer
39 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_render_fbo);
42 void FrameBuffer::UnBind()
44 glBindFramebuffer(GL_FRAMEBUFFER, 0); // will both draw to and read pixels from screen
47 void FrameBuffer::Blit()
49 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_render_fbo); // read pixels from this FrameBuffer
50 glBlitFramebuffer(0, 0, m_width, m_height, 0, 0, m_width, m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST); // draw pixels
53 void FrameBuffer::Clear(float r, float g, float b, float a)
56 glClearColor(r,g,b,a);
57 glClear(GL_COLOR_BUFFER_BIT);