1 #include "framebuffer.h"
2 #define GL_GLEXT_PROTOTYPES
3 #include <SDL_opengl.h>
8 void FrameBuffer::Create(int w, int h)
16 glGenTextures(1, &m_render_texture);
17 glGenFramebuffers(1, &m_render_fbo);
19 glBindTexture(GL_TEXTURE_2D, m_render_texture);
20 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
22 glBindFramebuffer(GL_FRAMEBUFFER, m_render_fbo);
24 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_render_texture, 0);
26 glBindFramebuffer(GL_FRAMEBUFFER, 0);
27 glBindTexture(GL_TEXTURE_2D, 0);
30 void FrameBuffer::Destroy()
32 if (!m_render_texture) return;
33 glDeleteFramebuffers(1, &m_render_fbo);
34 glDeleteTextures(1, &m_render_texture);
37 void FrameBuffer::Bind()
39 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_render_fbo);
42 void FrameBuffer::UnBind()
44 glBindFramebuffer(GL_FRAMEBUFFER, 0);
47 void FrameBuffer::Blit()
49 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_render_fbo);
50 glBlitFramebuffer(0, 0, m_width, m_height, 0, 0, m_width, m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
53 void FrameBuffer::Clear(float r, float g, float b, float a)
56 glClearColor(r,g,b,a);
57 glClear(GL_COLOR_BUFFER_BIT);